Rasterio: get linear units

Created on 15 Feb 2019  路  10Comments  路  Source: mapbox/rasterio

Hello,
Is there a way to get the linear units of raster directly from the CRS object ?
In the example from the README.md the crs dictionary contains these information but re-doing the same manipulation I only get the EPSG code.

Thank you.

bug

All 10 comments

@appanacca right, rasterio made the choice to normalize inputs to an EPSG code when possible and this hides the units. We can add a property to the CRS class that will surface the units. Until then, the work around is to pick the units out of CRS.to_wkt, which is a bit gross.

For my point of view the issue can be closed.

Hello,
The GDAL's function GetLinearUnits() also returns the factor to convert the current unit to meters. Is there a way to get this factor ?
Thank you,

Link to GetLinearUnits() from GDAL's documentation : https://www.gdal.org/classOGRSpatialReference.html#a2c173500813894e627b0bc558a4ea9ff

@RomainLaporte this question has been on my mind, too. One solution would be to implement a lightweight LinearUnit class (using attrs, probably) with name and factor attributes. Another solution would be to support (optionally) the use of the pint package by providing definitions for geospatial units: https://pint.readthedocs.io/en/latest/defining.html#defining-units. @appanacca do you have any comments on these ideas?

Hi ! @RomainLaporte has a point, I think that using pint is a viable option but an overkill for the problem. Looking closely at the first implementation the conversion factor to meters is already available. So why not return a tuple like this :
`
@property
def linear_units(self):

      cdef char *units_c = NULL
      cdef double fmeter

      try:
          if is_projected():
              fmeter = OSRGetLinearUnits(self._osr, &units_c)
          else:
             fmeter = None
      except CPLE_BaseError as exc:
          raise CRSError("{}".format(exc))
      else:
          units_b = units_c
          return (units_b.decode('utf-8'), fmeter)

`

I have also put a condition to be sure that the crs is a projected one because if not the 'fmeter' conversion factor has no sense.
Could this work for you @sgillies @RomainLaporte ?

The solution presented by @appanacca seems ok for me.

@sgillies Any news ? If my solution seems ok to you I can implement it and send you a PR.

@appanacca a tuple is a pragmatic approach :+1: What would you think about naming the property linear_units_factor or linear_units_plus to avoid breaking deployed programs?

I have submitted a PR #1679.

Ok great !
Thanks to both of you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mangecoeur picture mangecoeur  路  3Comments

sgillies picture sgillies  路  4Comments

brendan-ward picture brendan-ward  路  4Comments

gfairchild picture gfairchild  路  5Comments

sgillies picture sgillies  路  5Comments