Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to mediate between cartopy vs. rasterio CRS? #923

Open
choldgraf opened this issue Aug 9, 2017 · 8 comments
Open

How to mediate between cartopy vs. rasterio CRS? #923

choldgraf opened this issue Aug 9, 2017 · 8 comments

Comments

@choldgraf
Copy link

I'm trying to create a CRS from a geopandas CRS, and I'm getting a weird error:

As an example:

image

(I know it's using a string to create the CRS but it feels a bit like the globe error is a bug which is why I'm posting this, since IIRC that's an optional parameter)

@choldgraf
Copy link
Author

(or I guess more generally, what's the right way to create a cartopy CRS using a rasterio / geopandas CRS object?)

@choldgraf
Copy link
Author

ping

@choldgraf choldgraf changed the title Weird error in defining a CRS How to mediate between cartopy vs. rasterio CRS? Aug 14, 2017
@choldgraf
Copy link
Author

Changed the title because really that's the main goal here - I'm trying to read in TIFF files with either rasterio or geopandas (which reads in a CRS as a rasterio CRS), and I'd like to do plotting with cartopy if possible. However I can't for the life of me figure out how to turn the rasterio CRS into something I can use with cartopy...

@ajdawson
Copy link
Member

I don't know much about it really, but since your currently alone on this... the CRS class is a Cython class defined with cdef class CRS:. I'm not really sure these are designed to be instantiated directly in Python code, in fact the class is defined in _crs.pyx (note the leading underscore indicating private API) and only imported into crs.py so that subclasses can inherit from it. @pelson will be able to provide more useful information.

@pelson
Copy link
Member

pelson commented Jan 3, 2018

Hmm, interesting. Thanks for raising @choldgraf. The CRS object is indeed documented, and available at http://scitools.org.uk/cartopy/docs/v0.15/crs/index.html#cartopy.crs.CRS. Be aware though, you are constructing a CRS - not a projection. That means you aren't able to produce a map with it, and you will find a number of places when CRSes can't be used directly as the coordinate system of data (e.g. imshow, but plot & text are fine).

Given the above caveat, and the fact that I needed to make a code change to get it to work, I can't recommend creating CRSes directly in this way. We definitely want to be able to create projections from proj4 strings, but we aren't there yet. #153 relates.

Perhaps we need to add some detail to the docs to state that it is not recommended to create CRSes directly...


FWIW, the changes I needed to make were:

diff --git a/lib/cartopy/_crs.pxd b/lib/cartopy/_crs.pxd
index fab9ab4..1cf0ea8 100644
--- a/lib/cartopy/_crs.pxd
+++ b/lib/cartopy/_crs.pxd
@@ -29,5 +29,6 @@ cdef class CRS:
     cdef projPJ proj4
     cdef readonly proj4_init
     cdef proj4_params
+    cdef globe
 
     cpdef is_geodetic(self)

And I was then able to construct a CRS with:

# The equivalent of ccrs.PlateCarree()...? not quite
pc = ccrs.CRS({'a': 57.29577951308232, 'ellps': 'WGS84', 'lon_0': 0.0, 'proj': 'eqc'})

@pelson
Copy link
Member

pelson commented Feb 16, 2018

It is worth cross-referencing #153 here, as I think the question ultimately comes down to: "Can I make a cartopy CRS from a proj4 string?".

There has been some movement on this recently, with #813 being a really positive step in the right direction.

@djhoese
Copy link

djhoese commented Oct 10, 2018

I'd like to add on to this that this is something I'd like to make easier with the xarray library and other projection-heavy libraries (satpy, metpy, etc). I started pydata/xarray#2288 to hopefully find a way to easily convert between different libraries implementations.

I haven't had much time to work on it but I do have a geoxarray package that I started (https://github.com/geoxarray/geoxarray) that in the future will probably use the pycrs library: https://github.com/karimbahgat/PyCRS

I was hoping to add some conversion functions to pycrs for converting to/from rasterio and cartopy CRS objects. CC'ing @karimbahgat

@snowman2
Copy link
Contributor

snowman2 commented Jul 23, 2021

https://pyproj4.github.io/pyproj/latest/crs_compatibility.html

The latest versions of geopandas now use the pyproj.CRS object. With #1808, cartopy will extent the pyproj.CRS object. If the rasterio/geopandas/pyproj CRS has area of use information, in theory you could do:

import rasterio
import rasterio.crs
from cartopy.crs import Projection

with rasterio.Env(OSR_WKT_FORMAT="WKT2_2018"):
    rio_crs = rasterio.crs.CRS.from_epsg(32613)
    cart_crs = Projection(rio_crs)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants