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

Output plots using downloaded raster from bounds2raster doesn't plot correctly #155

Closed
JackMcKew opened this issue May 29, 2020 · 4 comments · Fixed by #156
Closed

Output plots using downloaded raster from bounds2raster doesn't plot correctly #155

JackMcKew opened this issue May 29, 2020 · 4 comments · Fixed by #156

Comments

@JackMcKew
Copy link

I was recently trying to use a downloaded raster as the background map for some plots with geopandas. However noticed that some of the plots were just offset from the actual location. This problem only seems to occur when using downloaded rasters.

I initially thought this might be an extents problem, however both plots are showing the exact same extents.

I've included a reproducible example straight from the geopandas docs: https://geopandas.readthedocs.io/en/latest/gallery/plotting_basemap_background.html?highlight=basemap

import geopandas
import contextily

# Example from https://geopandas.readthedocs.io/en/latest/gallery/plotting_basemap_background.html?highlight=basemap

df = geopandas.read_file(geopandas.datasets.get_path('nybb'))

# Project to web mercator
df = df.to_crs(epsg=3857)

# Get boundaries of dataset
west, south, east, north = bbox = df.total_bounds

# Plot geodataframe
ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')

# Add basemap through contextily
contextily.add_basemap(ax,source=contextily.providers.CartoDB.Positron)

# Show limits are the same
print(ax.get_xlim(),ax.get_ylim())
(-8269187.064680938, -8201155.139990268) (4935220.954753395, 5002970.199586581)

contextily-bug_0_1

import geopandas
import contextily

# Example from https://geopandas.readthedocs.io/en/latest/gallery/plotting_basemap_background.html?highlight=basemap

df = geopandas.read_file(geopandas.datasets.get_path('nybb'))

# Project to web mercator
df = df.to_crs(epsg=3857)

# Get boundaries of dataset
west, south, east, north = bbox = df.total_bounds

# Get raster 
img, ext = contextily.bounds2raster(west,
                             south,
                             east,
                             north,
                             "test.tif",
                             source=contextily.providers.CartoDB.Positron)

# Plot geodataframe
ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')

# Add basemap from downloaded raster
contextily.add_basemap(ax,source='test.tif')

# Show limits are the same
print(ax.get_xlim(),ax.get_ylim())
(-8269187.064680938, -8201155.139990268) (4935220.954753395, 5002970.199586581)

contextily-bug_1_1

@darribas
Copy link
Collaborator

Thanks very much for the report!

Mmm... It's a strange one. I've played a bit and , if you set reset_extent=False, it aligns both correctly. Now in this setup, I think that should not matter, and in any case the behaviour you highlight should not happen anyway...

Will investigate!

@darribas
Copy link
Collaborator

OK I've done some exploration and I think I've narrowed it to this point:

https://github.com/geopandas/contextily/blob/master/contextily/plotting.py#L191

We set the extent for the rendering for the whole file whether you reset_extent or not. We need to catch it if reset_extent=True and adapt it to the extent of the window instead. PRs welcome or I'll be trying to issue a patch soon too.

@darribas
Copy link
Collaborator

OK, I've issued a PR to fix it over at #156

@JackMcKew
Copy link
Author

That was so fast! Thank you so much.

After running into this problem for a day or so, I finally realized that contextily caches the data anyway which is what I was trying to do by storing the local file! https://contextily.readthedocs.io/en/latest/intro_guide.html#Store-basemaps-locally

First, in a given session, each tile is only downloaded once. Tiles are stored in a temporary cache so the next time you need it, you don’t have go to the web. This results in important time savings and network footprint.

This package is awesome, thank you so much for your work! 👍

jorisvandenbossche added a commit that referenced this issue Oct 16, 2020
Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
jpn-- added a commit to jpn--/contextily that referenced this issue Jan 8, 2021
…iles

* commit '3c26d5a16a094e8d5ae05f8e5a41f05278816daf':
  DOC: update for latest pydata-sphinx-theme (fix sidebar + use CSS variables) (geopandas#168)
  RLS: v1.0.1
  Fix resetting of extent with local files (geopandas#155) (geopandas#156)
  Add random user ID for Nominatim and allow arbitrary geocoder (geopandas#164)
  Close and reopen memfile dataset before WarpedVRT (geopandas#165)
  Fix use of rasterio MemoryFile (geopandas#163)
  DOC: Use Cape Town consistently in description and variable names (geopandas#154)
  Add matplotlib Framework classifier (geopandas#152)
  Require Python 3.6 as minimum version (geopandas#150)
  Add earthengine to docker for binder
  Example interfacing GEE with contextily (geopandas#147)
  DOC: add notebook about working with local files (geopandas#139)

# Conflicts:
#	tests/test_ctx.py
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

Successfully merging a pull request may close this issue.

2 participants