-
Notifications
You must be signed in to change notification settings - Fork 235
Add zoom_adjust parameter to pygmt.datasets.load_tile_map and Figure.tilemap #2934
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
Changes from 11 commits
5ca369e
27669da
e48c08d
b428d12
ddb0351
5cbbccb
c36deff
f0c95d8
70a4c83
0b01664
32c04f8
9170ea0
90a4d4f
c141db5
dfd43b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,9 @@ | |
| Function to load raster tile maps from XYZ tile providers, and load as | ||
| :class:`xarray.DataArray`. | ||
| """ | ||
| from __future__ import annotations | ||
|
|
||
| from packaging.version import Version | ||
|
|
||
| try: | ||
| import contextily | ||
|
|
@@ -16,7 +19,15 @@ | |
| __doctest_requires__ = {("load_tile_map"): ["contextily"]} | ||
|
|
||
|
|
||
| def load_tile_map(region, zoom="auto", source=None, lonlat=True, wait=0, max_retries=2): | ||
| def load_tile_map( | ||
| region, | ||
| zoom="auto", | ||
| source=None, | ||
| lonlat=True, | ||
| wait=0, | ||
| max_retries=2, | ||
| zoom_adjust: int | None = None, | ||
| ): | ||
| """ | ||
| Load a georeferenced raster tile map from XYZ tile providers. | ||
|
|
||
|
|
@@ -40,9 +51,9 @@ def load_tile_map(region, zoom="auto", source=None, lonlat=True, wait=0, max_ret | |
| ``"auto"`` to automatically determine the zoom level based on the | ||
| bounding box region extent]. | ||
|
|
||
| **Note**: The maximum possible zoom level may be smaller than ``22``, | ||
| and depends on what is supported by the chosen web tile provider | ||
| source. | ||
| .. note:: | ||
| The maximum possible zoom level may be smaller than ``22``, and depends on | ||
| what is supported by the chosen web tile provider source. | ||
|
|
||
| source : xyzservices.TileProvider or str | ||
| Optional. The tile source: web tile provider or path to a local file. | ||
|
|
@@ -62,8 +73,8 @@ def load_tile_map(region, zoom="auto", source=None, lonlat=True, wait=0, max_ret | |
| basemap. See | ||
| :doc:`contextily:working_with_local_files`. | ||
|
|
||
| IMPORTANT: Tiles are assumed to be in the Spherical Mercator projection | ||
| (EPSG:3857). | ||
| .. important:: | ||
| Tiles are assumed to be in the Spherical Mercator projection (EPSG:3857). | ||
|
|
||
| lonlat : bool | ||
| Optional. If ``False``, coordinates in ``region`` are assumed to be | ||
|
|
@@ -79,6 +90,14 @@ def load_tile_map(region, zoom="auto", source=None, lonlat=True, wait=0, max_ret | |
| will stop trying to fetch more tiles from a rate-limited API [Default | ||
| is ``2``]. | ||
|
|
||
| zoom_adjust : int or None | ||
| Optional. The amount to adjust a chosen zoom level if it is chosen | ||
| automatically. Values outside of -1 to 1 are not recommended as they can lead to | ||
| slow execution. [Default is ``None``]. | ||
|
|
||
| .. note:: | ||
| The `zoom_adjust` parameter requires ``contextily>=1.5.0``. | ||
weiji14 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Returns | ||
| ------- | ||
| raster : xarray.DataArray | ||
|
|
@@ -117,6 +136,15 @@ def load_tile_map(region, zoom="auto", source=None, lonlat=True, wait=0, max_ret | |
| "to install the package." | ||
| ) | ||
|
|
||
| contextily_kwargs = {} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want to also include
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mm, not really necessary. I only made the |
||
| if zoom_adjust is not None: | ||
| contextily_kwargs["zoom_adjust"] = zoom_adjust | ||
| if Version(contextily.__version__) < Version("1.5.0"): | ||
| raise TypeError( | ||
| "The `zoom_adjust` parameter requires `contextily>=1.5.0` to work. " | ||
| "Please upgrade contextily, or manually set the `zoom` level instead." | ||
| ) | ||
|
|
||
| west, east, south, north = region | ||
| image, extent = contextily.bounds2img( | ||
| w=west, | ||
|
|
@@ -128,6 +156,7 @@ def load_tile_map(region, zoom="auto", source=None, lonlat=True, wait=0, max_ret | |
| ll=lonlat, | ||
| wait=wait, | ||
| max_retries=max_retries, | ||
| **contextily_kwargs, | ||
| ) | ||
|
|
||
| # Turn RGBA img from channel-last to channel-first and get 3-band RGB only | ||
|
|
@@ -139,7 +168,7 @@ def load_tile_map(region, zoom="auto", source=None, lonlat=True, wait=0, max_ret | |
| dataarray = xr.DataArray( | ||
| data=rgb_image, | ||
| coords={ | ||
| "band": np.uint8([0, 1, 2]), # Red, Green, Blue | ||
| "band": np.array(object=[0, 1, 2], dtype=np.uint8), # Red, Green, Blue | ||
| "y": np.linspace(start=top, stop=bottom, num=rgb_image.shape[1]), | ||
| "x": np.linspace(start=left, stop=right, num=rgb_image.shape[2]), | ||
| }, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.