Skip to content

Commit 58b140a

Browse files
Adds zoom_adjust to add_basemap (#228)
* adds zoom_adjust, needs to be tested * creates venv and test file * removes temporary files and adds to docstrings * removes temporary files * resolve diff * removes Stamen (deprecated) from providers test * add test to add_basemap zoom_adjust * better merge commit --------- Co-authored-by: Martin Fleischmann <[email protected]>
1 parent a8982d2 commit 58b140a

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

contextily/plotting.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def add_basemap(
2525
reset_extent=True,
2626
crs=None,
2727
resampling=Resampling.bilinear,
28+
zoom_adjust=None,
2829
**extra_imshow_args
2930
):
3031
"""
@@ -75,6 +76,10 @@ def add_basemap(
7576
[Optional. Default=Resampling.bilinear] Resampling
7677
method for executing warping, expressed as a
7778
`rasterio.enums.Resampling` method
79+
zoom_adjust : int or None
80+
[Optional. Default: None]
81+
The amount to adjust a chosen zoom level if it is chosen automatically.
82+
Values outside of -1 to 1 are not recommended as they can lead to slow execution.
7883
**extra_imshow_args :
7984
Other parameters to be passed to `imshow`.
8085
@@ -127,7 +132,7 @@ def add_basemap(
127132
)
128133
# Download image
129134
image, extent = bounds2img(
130-
left, bottom, right, top, zoom=zoom, source=source, ll=False
135+
left, bottom, right, top, zoom=zoom, source=source, ll=False, zoom_adjust=zoom_adjust
131136
)
132137
# Warping
133138
if crs is not None:

contextily/tile.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def bounds2raster(
171171

172172

173173
def bounds2img(
174-
w, s, e, n, zoom="auto", source=None, ll=False, wait=0, max_retries=2, n_connections=1, use_cache=True
174+
w, s, e, n, zoom="auto", source=None, ll=False, wait=0, max_retries=2, n_connections=1, use_cache=True, zoom_adjust=None
175175
):
176176
"""
177177
Take bounding box and zoom and return an image with all the tiles
@@ -220,6 +220,10 @@ def bounds2img(
220220
If False, caching of the downloaded tiles will be disabled. This can be useful in resource constrained
221221
environments, especially when using n_connections > 1, or when a tile provider's terms of use don't allow
222222
caching.
223+
zoom_adjust : int or None
224+
[Optional. Default: None]
225+
The amount to adjust a chosen zoom level if it is chosen automatically.
226+
Values outside of -1 to 1 are not recommended as they can lead to slow execution.
223227
224228
Returns
225229
-------
@@ -239,6 +243,8 @@ def bounds2img(
239243
auto_zoom = zoom == "auto"
240244
if auto_zoom:
241245
zoom = _calculate_zoom(w, s, e, n)
246+
if zoom_adjust:
247+
zoom += zoom_adjust
242248
zoom = _validate_zoom(zoom, provider, auto=auto_zoom)
243249
# create list of tiles to download
244250
tiles = list(mt.tiles(w, s, e, n, [zoom]))

tests/test_cx.py

+40-3
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,10 @@ def test_add_basemap_query():
354354
ax_extent = (x1, x2, y1, y2)
355355
assert ax.axis() == ax_extent
356356

357-
assert ax.images[0].get_array().sum() == 64717007
357+
assert ax.images[0].get_array().sum() == 64691220
358358
assert ax.images[0].get_array().shape == (256, 256, 4)
359-
assert_array_almost_equal(ax.images[0].get_array()[:, :, :3].mean(), 244.167719)
360-
assert_array_almost_equal(ax.images[0].get_array().mean(), 246.875789642)
359+
assert_array_almost_equal(ax.images[0].get_array()[:, :, :3].mean(), 244.03656)
360+
assert_array_almost_equal(ax.images[0].get_array().mean(), 246.77742)
361361

362362

363363
@pytest.mark.network
@@ -420,6 +420,43 @@ def test_add_basemap_auto_zoom():
420420
)
421421
assert_array_almost_equal(ax.images[0].get_array().mean(), 217.2718038, decimal=0)
422422

423+
@pytest.mark.network
424+
@pytest.mark.parametrize("zoom_adjust, expected_extent, expected_sum_1, expected_sum_2, expected_shape", [
425+
# zoom_adjust and expected values where zoom_adjust == 1
426+
(1, (
427+
-11740727.544603072,
428+
-11701591.786121061,
429+
4852834.051769271,
430+
4891969.810251278,
431+
), 648244877, 915631757, (1024, 1024, 4)),
432+
433+
# zoom_adjust and expected values where zoom_adjust == -1
434+
(-1, (
435+
-11740727.544603072,
436+
-11701591.786121061,
437+
4852834.051769271,
438+
4891969.810251278,
439+
), 40396582, 57108262, (256, 256, 4)),
440+
])
441+
def test_add_basemap_zoom_adjust(zoom_adjust, expected_extent, expected_sum_1, expected_sum_2, expected_shape):
442+
x1, x2, y1, y2 = [-11740727.544603072, -11701591.786121061, 4852834.0517692715, 4891969.810251278]
443+
444+
f, ax = matplotlib.pyplot.subplots(1)
445+
ax.set_xlim(x1, x2)
446+
ax.set_ylim(y1, y2)
447+
cx.add_basemap(ax, zoom="auto", zoom_adjust=zoom_adjust)
448+
449+
ax_extent = expected_extent
450+
assert_array_almost_equal(ax_extent, ax.images[0].get_extent())
451+
452+
assert ax.images[0].get_array()[:, :, :3].sum() == pytest.approx(expected_sum_1, rel=0.1)
453+
assert ax.images[0].get_array().sum() == pytest.approx(expected_sum_2, rel=0.1)
454+
assert ax.images[0].get_array().shape == expected_shape
455+
assert_array_almost_equal(
456+
ax.images[0].get_array()[:, :, :3].mean(), 204.695738, decimal=0
457+
)
458+
assert_array_almost_equal(ax.images[0].get_array().mean(), 217.2718038, decimal=0)
459+
423460

424461
@pytest.mark.network
425462
def test_add_basemap_warping():

tests/test_providers.py

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ def test_providers():
1616
)
1717
for provider in [
1818
cx.providers.OpenStreetMap.Mapnik,
19-
cx.providers.Stamen.Toner,
2019
cx.providers.NASAGIBS.ViirsEarthAtNight2012,
2120
]:
2221
cx.bounds2img(w, s, e, n, 4, source=provider, ll=True)

0 commit comments

Comments
 (0)