Skip to content

Commit

Permalink
Fix resetting of extent with local files (#155) (#156)
Browse files Browse the repository at this point in the history
Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
  • Loading branch information
darribas and jorisvandenbossche authored Oct 16, 2020
1 parent 27d60da commit 5feb757
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
8 changes: 5 additions & 3 deletions contextily/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,21 @@ def add_basemap(
}
]
image, img_transform = riomask(raster, window, crop=True)
extent = left, right, bottom, top
else:
# Read full
image = np.array([band for band in raster.read()])
img_transform = raster.transform
bb = raster.bounds
extent = bb.left, bb.right, bb.bottom, bb.top
# Warp
if (crs is not None) and (raster.crs != crs):
image, bounds, _ = _warper(
image, img_transform, raster.crs, crs, resampling
)
else:
bounds = raster.bounds
extent = bounds.left, bounds.right, bounds.bottom, bounds.top
image = image.transpose(1, 2, 0)
extent = bounds.left, bounds.right, bounds.bottom, bounds.top

# Plotting
if image.shape[2] == 1:
image = image[:, :, 0]
Expand Down
8 changes: 1 addition & 7 deletions tests/test_ctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,7 @@ def test_add_basemap():
loc = ctx.Place(SEARCH, path="./test2.tif", zoom_adjust=ADJUST)
ctx.add_basemap(ax, url="./test2.tif", reset_extent=True)

raster_extent = (
-11740803.981631357,
-11701668.223149346,
4852910.488797556,
4892046.247279563,
)
assert_array_almost_equal(raster_extent, ax.images[0].get_extent())
assert_array_almost_equal(subset, ax.images[0].get_extent())
assert ax.images[0].get_array().sum() == 8440966
assert ax.images[0].get_array().shape == (126, 126, 3)
assert_array_almost_equal(ax.images[0].get_array().mean(), 177.22696733014195)
Expand Down

0 comments on commit 5feb757

Please sign in to comment.