geopandas / geopandas

Python tools for geographic data
http://geopandas.org/
BSD 3-Clause "New" or "Revised" License
4.44k stars 923 forks source link

Converting between geopandas and cartopy CRS? #488

Open choldgraf opened 7 years ago

choldgraf commented 7 years ago

It seems like Cartopy has a focus in visualization / mapping, while geopandas focuses more on geodata representation, analysis, munging, etc. However I haven't been able to figure out how to convert the geopandas CRS into a cartopy CRS.

It seems like this is something that should be fairly common given that these are two of the major geo packages in Python. Does anybody have experience doing this, or can anyone point me to examples that show how these two packages work as complements to one another?

jorisvandenbossche commented 7 years ago

@choldgraf can you give a small toy example? Both are based on proj4, so they should certainly be convertible to each other somehow.

jorisvandenbossche commented 7 years ago

The cartopy CRS objects seem to have a proj4_params attribute, which could be used.

I don't know the exact application in which you want to do the conversion, but a small (non-sensical) toy one seems that they can be used inter-operatively using that attribute:

In [1]: import cartopy.crs

In [3]: crs = cartopy.crs.Mercator()

In [4]: crs
Out[4]: <cartopy.crs.Mercator at 0x7f9a640cb150>

In [6]: crs.proj4_init
Out[6]: '+ellps=WGS84 +proj=merc +lon_0=0.0 +lat_ts=0.0 +units=m +no_defs'

In [7]: crs.proj4_params
Out[7]: {'ellps': 'WGS84', 'lat_ts': 0.0, 'lon_0': 0.0, 'proj': 'merc', 'units': 'm'}

In [8]: import geopandas

In [11]: from shapely.geometry import Point

In [13]: s = geopandas.GeoSeries([Point(i, i) for i in range(5)])

In [14]: s
Out[14]: 
0    POINT (0 0)
1    POINT (1 1)
2    POINT (2 2)
3    POINT (3 3)
4    POINT (4 4)
dtype: object

In [15]: s.crs = crs.proj4_params

In [16]: s
Out[16]: 
0    POINT (0 0)
1    POINT (1 1)
2    POINT (2 2)
3    POINT (3 3)
4    POINT (4 4)
dtype: object

In [17]: s.to_crs(cartopy.crs.LambertConformal().proj4_params)
Out[17]: 
0    POINT (10841282.15360126 1702320.865941544)
1     POINT (10841281.6875621 1702322.543282503)
2    POINT (10841281.22152283 1702324.220623253)
3    POINT (10841280.75548344 1702325.897963792)
4    POINT (10841280.28944393 1702327.575304117)
dtype: object
choldgraf commented 7 years ago

ah this looks very useful, thanks! I may spend some time spinning this up into a little example if you're willing to have an extra dependency for your documentation. I think it'd make the geospatial analytics python world a lot stronger if packages gave more guidance on how to connect with one another.

jorisvandenbossche commented 7 years ago

Yes, that would certainly be useful! (eg how to plot a GeoDataFrame with cartopy and deal with crs) for the docs dependencies is not really a problem

choldgraf commented 7 years ago

ok cool - I am sorta working all of this out in my own head too right now, but as I figure it out I can put together a little guide. Would be great to see these packages used as complements to one another!

ResidentMario commented 7 years ago

+1 on this because I'd be interested in learning more as well.

choldgraf commented 7 years ago

whoah - I hadn't heard of geoplot before...gah I wish it were easier to discover all these libraries. @ResidentMario would you consider geoplot in a stable form right now?

ResidentMario commented 7 years ago

The geoplot API is stable, yes. It needs continuing work however, and isn't Windows-compatible due to a cartopy issue.

My long term plan is to introduce it as the geopandas viz component, just like pandas.plotting, but there's still work to do (both there and here) before that can happen.

There are some technical gotchas with how CRSs are implemented, my comments here might be interesting to look at.

choldgraf commented 7 years ago

ah shit, does cartopy not work in windows?!

ResidentMario commented 7 years ago

If I remember correctly, the TLDR is that you can't have a stable Windows environment with all of cartopy, shapely, and geopandas installed because the former former secretly depends on ancient internal GEOS bindings that really ought to be provided through shapely (shapely is in part a GEOS wrapper). At runtime one or the other library queries GEOS, gets an unexpected result, and crashes. DLL hell ensues. This is somewhat off-topic however...

choldgraf commented 7 years ago

haha sorry, I have been wrestling with geospatial python dependencies for a while now, so this kinda triggered my PTSD on the subject :-)

choldgraf commented 7 years ago

see #463 for an example that shows a bit of cartopy <-> gpd

gepcel commented 5 years ago

Hi, @choldgraf @ResidentMario, while I was searching around about crs conversion between gpd <-> cartopy, I found this issue.

Just to clarify, Cartopy definitely works under windows, I've been using cartopy under windows for years.