Skip to content

Integration with contextily to plot xyz basemaps #2115

Closed

Description

@weiji14

Description of the desired feature

To allow for a greater variety of basemaps from e.g. OpenStreetMap, Stamen, MapBox, etc, it would be nice to integrate with contextily (part of the geopandas organization). This is motivated by https://forum.generic-mapping-tools.org/t/pygmt-vs-plotly/3250/10.

image

On the implementation, here are 2 ideas on how to integrate contextily basemaps with pygmt.

  1. Have an additional option under fig.basemap which allows selection of the basemap (e.g. OpenStreetMap, Stamen, Mapbox, etc) (Add Figure.tilemap to plot XYZ tile maps #2394, to be called fig.tilemap)
  2. Create a new load_basemap function under pygmt/datasets that would load the image (np.ndarray) and georeference it (to an xarray.DataArray), and then the user plots it using fig.grdimage. (done at Add function to load raster tile maps using contextily #2125, called load_tile_map)
  3. Maybe there's another more Pythonic way? Comment down below!

Either way, these would require:

  1. Wrapping around the contextily.bounds2img function at https://contextily.readthedocs.io/en/latest/reference.html#contextily.bounds2img. This returns a numpy.ndarray and a geographic extent.
  2. The numpy.ndarray needs to be georeferenced into an xarray.DataArray. I have some recent code at https://zen3geo.readthedocs.io/en/v0.4.0/object-detection-boxes.html#georeference-image-using-rioxarray to do that
  3. The xarray.DataArray will need to be plotted on the PyGMT map

Are you willing to help implement and maintain this feature? Yes, but discuss about the implementation first

Activity

  1. added
    help wanted
    feature request
    on Sep 12, 2022
  2. seisman commented on Sep 19, 2022

    @seisman
    Member

    On the implementation, here are 2 ideas on how to integrate contextily basemaps with pygmt.

    1. Have an additional option under fig.basemap which allows selection of the basemap (e.g. OpenStreetMap, Stamen, Mapbox, etc)
    2. Create a new load_basemap function under pygmt/datasets that would load the image (np.ndarray) and georeference it (to an xarray.DataArray), and then the user plots it using fig.grdimage.
    3. Maybe there's another more Pythonic way? Comment down below!

    Can we do both 1 and 2? I.e., having the load_basemap (maybe load_map_tiles?) function in pygmt/datasets and having a new parameter in Figure.basemap to call this function.

  3. weiji14 commented on Sep 19, 2022

    @weiji14
    MemberAuthor

    On the implementation, here are 2 ideas on how to integrate contextily basemaps with pygmt.

    1. Have an additional option under fig.basemap which allows selection of the basemap (e.g. OpenStreetMap, Stamen, Mapbox, etc)
    2. Create a new load_basemap function under pygmt/datasets that would load the image (np.ndarray) and georeference it (to an xarray.DataArray), and then the user plots it using fig.grdimage.
    3. Maybe there's another more Pythonic way? Comment down below!

    Can we do both 1 and 2? I.e., having the load_basemap (maybe load_map_tiles?) function in pygmt/datasets and having a new parameter in Figure.basemap to call this function.

    That sounds like a neat idea. Start with (2) load_map_tiles first, and then do (1) new option in fig.basemap() (which will be a lot more complicated.

    Actually, we'll also need to let fig.grdimage work with RGB (3 bands) instead of just 1 band. It works if we save the RGB image to a GeoTIFF first and tell fig.grdimage to plot the .tif file, but would be nicer to plot a 3 band xr.DataArray directly, xref #1555. Or we can use a tempfile 😉

  4. weiji14 commented on Sep 19, 2022

    @weiji14
    MemberAuthor

    Started a Pull Request at #2125 for load_map_tiles load_tile_map (renamed based on #2125 (comment)).

    To jump ahead a little bit, this is the code I'm currently using to plot the 3-band xarray.DataArray. The raster can be obtained from running the snippet in #2125 (comment).

    import pygmt
    import rioxarray
    
    raster.rio.to_raster(raster_path="basemap.tif")
    
    fig = pygmt.Figure()
    fig.grdimage(grid="basemap.tif", frame=True)
    fig.savefig("Singapore_stamen_map.png")
    fig.show()

    produces this Stamen Terrain map over Singapore

    Singapore_stamen_map

  5. weiji14 commented on Mar 2, 2023

    @weiji14
    MemberAuthor
    1. Have an additional option under fig.basemap which allows selection of the basemap (e.g. OpenStreetMap, Stamen, Mapbox, etc)

    After a bit of thinking, and as mentioned in in #2125 (comment), I'm considering creating a new fig.tilemap method instead of complicating fig.basemap further. This fig.tilemap method would essentially look like:

    def tilemap(self, *, **kwargs):
        with GMTTempFile(suffix=".tif") as tmpfile:
            raster = pygmt.datasets.load_tile_map(...)
            raster.rio.to_raster(raster_path=tmpfile.name)
            fig.grdimage(grid=tmpfile.name, **kwargs)

    Any thoughts on this implementation? Note that projections will also need to be handled, something mentioned in #2125 (comment). Also, the .rio.to_raster requires adding rioxarray as another optional dependency.

    The tempfile is a bit of a hack to be honest, but is necessary until we can use grdimage to plot 3-band xarray.DataArrays directly, something to be done in #1555.

  6. self-assigned this
    on Mar 2, 2023
  7. seisman commented on Mar 2, 2023

    @seisman
    Member

    After a bit of thinking, and as mentioned in in #2125 (comment), I'm considering creating a new fig.tilemap method instead of complicating fig.basemap further.

    Yes, I aslo prefer fig.tilemap!

    The tempfile is a bit of a hack to be honest, but is necessary until we can use grdimage to plot 3-band xarray.DataArrays directly, something to be done in #1555.

    I agree.

  8. added this to the 0.9.0 milestone on Mar 4, 2023
  9. removed
    help wanted
    on Mar 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

feature request

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions