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

Support Mercator.GOOGLE and cartopy.crs.epsg() projections #77

Closed
sanzoghenzo opened this issue Apr 18, 2019 · 3 comments
Closed

Support Mercator.GOOGLE and cartopy.crs.epsg() projections #77

sanzoghenzo opened this issue Apr 18, 2019 · 3 comments

Comments

@sanzoghenzo
Copy link

I'm trying to add a basemap in the same fashion as #48.

I'm using GeoPandas data (gdf1 to gdfX) with local crs to compose a map and add a background map from web tiles.

    # ...
    import geoplot as gplt

    proj = gplt.crs.Mercator()
    gdf1 = gdf1.to_crs(epsg=4326)
    ax = gplt.pointplot(gdf1, projection=proj)
    lims = [ax.axis()]
    for gdf in gdf2:
        ax = gplt.choropleth(gdf.to_crs(epsg=4326), hue='vul_max', 
                             cmap='RdYlGn_r',  alpha=0.5, ax=ax, projection=proj)
        lims.append(ax.axis())
    # ... some other layers added ...
    ws, es, ss, ns = zip(*lims)
    ax.axis((min(ws), max(es), min(ss), max(ns)))
    ax = add_basemap(ax)

def add_basemap(ax, webmap='ST_TONER_LITE'):
    import contextily as ctx
    url = getattr(ctx.sources, webmap, ctx.sources.ST_TONER_LITE)
    w, e, s, n = ax.axis()
    img, ext = ctx.bounds2img(w, s, e, n, url=url)  # , ll=True)
    ax.imshow(img, extent=ext, interpolation='bilinear', zorder=-1)
    ax.axis((w, e, s, n))
    return ax

The problem is with Mercator() that centers the coordinate system to the center of the plot and messes up the boundaries (ax.axis returns "garbage").
Since we're talking about webtiles, that always use EPSG 3857, there should be a way to directly specify that projection.

Maybe you already know, but I found that cartopy has the Mercator.GOOGLE projection that could be used for this goal.

Using directly that class doesn't work because of the "load" method missing.
Manually passing the parameters works:

proj = gplt.crs.Mercator(central_longitude=0, 
                         min_latitude=-85.0511287798066,
                         max_latitude=85.0511287798066,
                         globe=Globe(ellipse=None,
                                     semimajor_axis=WGS84_SEMIMAJOR_AXIS,
                                     semiminor_axis=WGS84_SEMIMAJOR_AXIS,
                                     nadgrids='@null'))

It would be nice to have a convenience class for this directly in geoplot.

There's also a cartopy.crs.epsg() function to create the projection from an arbitrary EPSG (I'm thinking about outputting the maps in the original CRS), supporting it could open to many possibilities.

@ResidentMario
Copy link
Owner

Hmm. This library predates contextily, and I'm not sure what the best pattern for getting geoplot/geopandas/cartopy/contextily to play nice with one another is. There's even a PR perma-open against this library adding contextily support.

I can naively add support for the Google projection, probably. It irks me that cartopy doesn't expose this projection in their list in the documentation.

@ResidentMario
Copy link
Owner

Hi, closing this issue now because I am tracking these feature requests in #146 and #147. Thanks for reporting! I don't have a timeline yet for these things, but they're pretty high on the list of priorities for after this next geoplot@0.3.0 release.

@ResidentMario
Copy link
Owner

A special gcrs.WebMercator projection has now been added to geoplot (#169) that provides an EPSG:3857 projection, which addresses your original request. Support for arbitrary EPSG coordinate reference systems is still on the to-do list. If making webmaps is your goal, there is now a gplt.webmap plot type that does just that, using the WebMercator projection. 🎉

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

No branches or pull requests

2 participants