mplleaflet icon indicating copy to clipboard operation
mplleaflet copied to clipboard

Manage CRS from new geopandas release

Open PyMap opened this issue 5 years ago • 2 comments

Hi all! With new gepandas release, the pyproj.CRS object has been enriched and the old way (pyproj4) to specify projection as parameter (`crs') seems to have changed considerably.

When you try to plot a gd over a a layer with display method:

# 1. Plot data:
ax = gdf.plot()

# 2. Display dinamic map
mplleaflet.display(fig=ax.figure, crs=ax.crs)

The following error is returned:

TypeError: type object argument after ** must be a mapping, not CRS

This is seems to be explained because CRS with new geopandas release has a new obtect type.

Passing the crs with the .to_dict() this error doesn't appears again, but as I'm still getting some warnings like this one:

UserWarning: You will likely lose important projection information when converting to a PROJ string from another format. See: https://proj.org/faq.html#what-is-the-best-format-for-describing-coordinate-reference-systems
  proj_string = self.to_proj4()

I would like to know if this is the correct way to pass the gdf.crs as parameter in the display method.

Great work wit this library, much thanks!

PyMap avatar Apr 12 '20 19:04 PyMap

Not sure if this is helpful but when I ran into the same problem (only being able to plot things in the default EPSG4326 crs and not being able to pass in a different crs/epsg optional argument for mplleaflet.show), I came up with this workaround:

Besides downgrading pyproj to 1.9.6 (which means you wont be able to use geopandas >= 0.7.0), what you can also do is edit the leaflet_renderer.py script, when defining the LeafletRenderer class you can substitute:

old code which works with pyproj 1.9.6 or below - proj_in = pyproj.Proj(crs) proj_out = pyproj.Proj(crs_out) self.transformfunc = partial(pyproj.transform, proj_in, proj_out)

new code which works for pyproj >= 2.0.0 - proj_in = pyproj.CRS(crs) proj_out = pyproj.CRS(crs_out) transformer = pyproj.Transformer.from_crs(proj_in, proj_out, always_xy=True) self.transformfunc = partial(transformer.transform)

You should also return epsgstr rather than crs in the _crs_from_epsg(epsg) function at the bottom of leaflet_renderer.py to satisfy the new crs format used in pyproj >= 2.0.0.

Hope this helps someone :)

MochiYoshi avatar May 16 '20 21:05 MochiYoshi

Thanks for the workaround, I will try to take a look!

PyMap avatar May 16 '20 21:05 PyMap