-
Notifications
You must be signed in to change notification settings - Fork 234
load_tile_map: Add the new parameter 'crs' to set the CRS of the returned dataarray #3554
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 3 commits
3304104
cfb69d2
00e2605
3265240
7660e2c
421c167
8346af7
4ed799c
6733e19
c1d55b0
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 |
|---|---|---|
|
|
@@ -3,25 +3,29 @@ | |
| :class:`xarray.DataArray`. | ||
| """ | ||
|
|
||
| import contextlib | ||
| from collections.abc import Sequence | ||
| from typing import Literal | ||
|
|
||
| from packaging.version import Version | ||
|
|
||
| try: | ||
| import contextily | ||
| from rasterio.crs import CRS | ||
| from xyzservices import TileProvider | ||
|
|
||
| _HAS_CONTEXTILY = True | ||
| except ImportError: | ||
| CRS = None | ||
|
Comment on lines
+13
to
+18
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. Maybe put the
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.
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. Rasterio is also a dependency of |
||
| TileProvider = None | ||
| _HAS_CONTEXTILY = False | ||
|
|
||
| with contextlib.suppress(ImportError): | ||
| # rioxarray is needed to register the rio accessor | ||
| try: | ||
| import rioxarray # noqa: F401 | ||
|
|
||
| _HAS_RIOXARRAY = True | ||
| except ImportError: | ||
| _HAS_RIOXARRAY = False | ||
|
|
||
| import numpy as np | ||
| import xarray as xr | ||
|
|
||
|
|
@@ -33,6 +37,7 @@ | |
| zoom: int | Literal["auto"] = "auto", | ||
| source: TileProvider | str | None = None, | ||
| lonlat: bool = True, | ||
| crs: str | CRS = "EPSG:3857", | ||
| wait: int = 0, | ||
| max_retries: int = 2, | ||
| zoom_adjust: int | None = None, | ||
|
|
@@ -42,7 +47,8 @@ | |
| The tiles that compose the map are merged and georeferenced into an | ||
| :class:`xarray.DataArray` image with 3 bands (RGB). Note that the returned image is | ||
| in a Spherical Mercator (EPSG:3857) coordinate reference system. | ||
| in a Spherical Mercator (EPSG:3857) coordinate reference system (CRS) by default, | ||
| but can be customized using the ``crs`` parameter. | ||
| Parameters | ||
| ---------- | ||
|
|
@@ -80,6 +86,10 @@ | |
| lonlat | ||
| If ``False``, coordinates in ``region`` are assumed to be Spherical Mercator as | ||
| opposed to longitude/latitude. | ||
| crs | ||
| Coordinate reference system (CRS) of the returned :class:`xarray.DataArray` | ||
| image. Default is ``"EPSG:3857"`` (i.e., Spherical Mercator). The CRS can be in | ||
| either string or :class:`rasterio.crs.CRS` format. | ||
| wait | ||
| If the tile API is rate-limited, the number of seconds to wait between a failed | ||
| request and the next try. | ||
|
|
@@ -128,6 +138,8 @@ | |
| ... raster.rio.crs.to_string() | ||
| 'EPSG:3857' | ||
| """ | ||
| _default_crs = "EPSG:3857" # The default CRS returned from contextily | ||
|
||
|
|
||
| if not _HAS_CONTEXTILY: | ||
| msg = ( | ||
| "Package `contextily` is required to be installed to use this function. " | ||
|
|
@@ -136,6 +148,14 @@ | |
| ) | ||
| raise ImportError(msg) | ||
|
|
||
| if crs != _default_crs and not _HAS_RIOXARRAY: | ||
| msg = ( | ||
| f"Package `rioxarray` is required if CRS is not '{_default_crs}'. " | ||
| "Please use `python -m pip install rioxarray` or " | ||
| "`mamba install -c conda-forge rioxarray` to install the package." | ||
| ) | ||
| raise ImportError(msg) | ||
|
|
||
| contextily_kwargs = {} | ||
| if zoom_adjust is not None: | ||
| contextily_kwargs["zoom_adjust"] = zoom_adjust | ||
|
|
@@ -176,8 +196,12 @@ | |
| dims=("band", "y", "x"), | ||
| ) | ||
|
|
||
| # If rioxarray is installed, set the coordinate reference system | ||
| # If rioxarray is installed, set the coordinate reference system. | ||
seisman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if hasattr(dataarray, "rio"): | ||
| dataarray = dataarray.rio.write_crs(input_crs="EPSG:3857") | ||
| dataarray = dataarray.rio.write_crs(input_crs=_default_crs) | ||
|
|
||
| # Reproject raster image from EPSG:3857 to specified CRS, using rioxarray. | ||
| if crs != _default_crs: | ||
| dataarray = dataarray.rio.reproject(dst_crs=crs) | ||
seisman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return dataarray | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,13 +9,9 @@ | |
| from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias | ||
|
|
||
| try: | ||
| import rioxarray # noqa: F401 | ||
| from xyzservices import TileProvider | ||
|
|
||
| _HAS_RIOXARRAY = True | ||
| except ImportError: | ||
| TileProvider = None | ||
| _HAS_RIOXARRAY = False | ||
|
|
||
|
|
||
| @fmt_docstring | ||
|
|
@@ -111,38 +107,21 @@ | |
| kwargs : dict | ||
| Extra keyword arguments to pass to :meth:`pygmt.Figure.grdimage`. | ||
| Raises | ||
| ------ | ||
| ImportError | ||
| If ``rioxarray`` is not installed. Follow | ||
| :doc:`install instructions for rioxarray <rioxarray:installation>`, (e.g. via | ||
| ``python -m pip install rioxarray``) before using this function. | ||
| """ | ||
| kwargs = self._preprocess(**kwargs) | ||
|
|
||
| if not _HAS_RIOXARRAY: | ||
|
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. The |
||
| raise ImportError( | ||
| "Package `rioxarray` is required to be installed to use this function. " | ||
| "Please use `python -m pip install rioxarray` or " | ||
| "`mamba install -c conda-forge rioxarray` to install the package." | ||
| ) | ||
|
|
||
| raster = load_tile_map( | ||
| region=region, | ||
| zoom=zoom, | ||
| source=source, | ||
| lonlat=lonlat, | ||
| crs="OGC:CRS84" if lonlat is True else "EPSG:3857", | ||
|
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. If |
||
| wait=wait, | ||
| max_retries=max_retries, | ||
| zoom_adjust=zoom_adjust, | ||
| ) | ||
|
|
||
| # Reproject raster from Spherical Mercator (EPSG:3857) to lonlat (OGC:CRS84) if | ||
| # bounding box region was provided in lonlat | ||
| if lonlat and raster.rio.crs == "EPSG:3857": | ||
| raster = raster.rio.reproject(dst_crs="OGC:CRS84") | ||
| raster.gmt.gtype = 1 # set to geographic type | ||
| if lonlat: | ||
| raster.gmt.gtype = 1 # Set to geographic type | ||
|
|
||
| # Only set region if no_clip is None or False, so that plot is clipped to exact | ||
| # bounding box region | ||
|
|
||

Uh oh!
There was an error while loading. Please reload this page.