geopandas / contextily

Context geo-tiles in Python
https://contextily.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
493 stars 81 forks source link

Question: Is there a way to clip to a polygon using add_basemap()? #194

Closed blu3r4d0n closed 2 years ago

blu3r4d0n commented 2 years ago

Hi, I've been trying at this for a while and I've read through the source code and didn't see any way to do this.

Essentially what I'd like to do is generate an image like below, but clipped to the areas within the polygon. I don't think is a feature of contextily but I wanted to ask to be sure, and if not I'd like to suggest it be added.

image

darribas commented 2 years ago

Hello @philipmnelson thanks for posting! If I understand your question correctly, you want to be able to only display the part of the basemap that is within the polygon, correct? If that's the case, you are right, we currently don't have specific support for it in contextily, but you could download the tile to disk (see the guide) and then load it with either rasterio or, probably a bit easier, rioxarray (which uses rasterio. This guide might be of use.

blu3r4d0n commented 2 years ago

@darribas thanks! I was able to do what I wanted with cartopy and shapely! But I'll keep in mind the solutions you have mentioned. Here's an example of the output I wanted to get! image