Interconverting between pyproj and rasterio CRS objects


Nick Weir <nicholas.r.weir@...>
 

Hi there!

Now that geopandas is using pyproj CRS objects as the default for tracking CRSs in GeoDataFrames, I'm finding myself needing to figure out a way to frequently check identity and interconvert back and forth between pyproj.crs.CRS objects and rasterio.crs.CRS objects. However, I'm having a hard time figuring out the "ideal" way to do this. It's great that the following works:
import rasterio
import pyproj
pp_crs = pyproj.crs.CRS.from_epsg(4326)
rio_crs = rasterio.crs.CRS.from_epsg(4326)
assert pp_crs == rio_crs

I'm trying to figure out the best way to pass around CRSs within a python library (ideally in a single object type) and encountering issues. I don't want to wed myself to using EPSG codes to interconvert between objects, because I want to be able to accommodate CRSs/SRSs without codes. However, I'm finding problems using something else (like a WKT string) to convert between rasterio.crs.CRSs and pyproj.crs.CRSs. For example:
import rasterio
import pyproj
pp_crs = pyproj.crs.CRS.from_epsg(4326)
rio_crs = rasterio.crs.CRS.from_epsg(4326)
pp_to_rio_crs = rasterio.crs.CRS.from_wkt(pp_crs.to_wkt())
assert rio_crs == pp_to_rio_crs
Fails. Any thoughts? I could definitely be missing something here. Thanks! -N


Alan Snow
 

Here are instructions for rasterio to geopandas: https://gis.stackexchange.com/a/350231/144357

For geopandas to rasterio, you will need something like:

 
if LooseVersion(rasterio.__gdal_version__) > LooseVersion("3.0.0"):
 
crs = rasterio.crs.CRS.from_wkt(crs.to_wkt())
else: 
crs = rasterio.crs.CRS.from_wkt(crs.to_wkt("WKT1_GDAL"))


Apologies for the formatting ....


vincent.sarago@...
 

FYI: https://github.com/corteva/rioxarray/issues/92 and https://github.com/corteva/rioxarray/issues/92


Nick Weir <nicholas.r.weir@...>
 

Yeah, this of course works. I'm more thinking from a philosophical standpoint of "which object works better to pass around?"


It seems like the pyproj objects work a bit better. And that's what's natively used in geopandas and a bunch of other places. I guess the question becomes "what's the benefit of rasterio having its own CRS object type"...I guess the main argument against is that adding pyproj is adding an additional dependency?

See this gist for a bit of exploration of this.


Alan Snow
 

Add some history for reference: https://rasterio.groups.io/g/dev/topic/switch_from_proj_4_format_to/28991934


Sean Gillies
 

Hi Nick,

Rasterio has its own CRS class because pyproj 2 didn't exist at the time. 

On Thu, Mar 5, 2020 at 9:47 AM Nick Weir <nicholas.r.weir@...> wrote:
Yeah, this of course works. I'm more thinking from a philosophical standpoint of "which object works better to pass around?"


It seems like the pyproj objects work a bit better. And that's what's natively used in geopandas and a bunch of other places. I guess the question becomes "what's the benefit of rasterio having its own CRS object type"...I guess the main argument against is that adding pyproj is adding an additional dependency?

See this gist for a bit of exploration of this.



--
Sean Gillies


Nick Weir <nicholas.r.weir@...>
 

Got it. Forgot that there wasn’t a class there previously...thanks for the context!
--
Sent from my mobile.