Skip to content

Commit 6f83cf9

Browse files
committed
Merge branch 'main' into docstrings/output_type
2 parents 8857ce6 + 68bfe57 commit 6f83cf9

File tree

4 files changed

+71
-77
lines changed

4 files changed

+71
-77
lines changed

doc/api/index.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,18 +283,15 @@ the :meth:`~pygmt.clib.Session.call_module` method:
283283

284284
Passing memory blocks between Python data objects (e.g. :class:`numpy.ndarray`,
285285
:class:`pandas.Series`, :class:`xarray.DataArray`, etc) and GMT happens through
286-
*virtual files*. These methods are context managers that automate the
287-
conversion of Python variables to GMT virtual files:
286+
*virtual files*. These methods are context managers that automate the conversion of
287+
Python objects to and from GMT virtual files:
288288

289289
.. autosummary::
290290
:toctree: generated
291291

292-
clib.Session.virtualfile_from_matrix
293-
clib.Session.virtualfile_from_vectors
294-
clib.Session.virtualfile_from_grid
295292
clib.Session.virtualfile_in
296293
clib.Session.virtualfile_out
297-
clib.Session.return_dataset
294+
clib.Session.virtualfile_to_dataset
298295

299296
Low level access (these are mostly used by the :mod:`pygmt.clib` package):
300297

@@ -317,3 +314,7 @@ Low level access (these are mostly used by the :mod:`pygmt.clib` package):
317314
clib.Session.read_virtualfile
318315
clib.Session.extract_region
319316
clib.Session.get_libgmt_func
317+
clib.Session.virtualfile_from_data
318+
clib.Session.virtualfile_from_grid
319+
clib.Session.virtualfile_from_matrix
320+
clib.Session.virtualfile_from_vectors

pygmt/clib/session.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,14 +1738,14 @@ def read_virtualfile(
17381738
dtype = {"dataset": _GMT_DATASET, "grid": _GMT_GRID}[kind]
17391739
return ctp.cast(pointer, ctp.POINTER(dtype))
17401740

1741-
def return_dataset(
1741+
def virtualfile_to_dataset(
17421742
self,
17431743
output_type: Literal["pandas", "numpy", "file"],
1744-
vfile: str,
1744+
vfname: str,
17451745
column_names: list[str] | None = None,
17461746
) -> pd.DataFrame | np.ndarray | None:
17471747
"""
1748-
Output a dataset stored in a virtual file in different formats.
1748+
Output a tabular dataset stored in a virtual file to a different format.
17491749
17501750
The format of the dataset is determined by the ``output_type`` parameter.
17511751
@@ -1757,7 +1757,7 @@ def return_dataset(
17571757
- ``"pandas"`` will return a :class:`pandas.DataFrame` object.
17581758
- ``"numpy"`` will return a :class:`numpy.ndarray` object.
17591759
- ``"file"`` means the result was saved to a file and will return ``None``.
1760-
vfile
1760+
vfname
17611761
The virtual file name that stores the result data. Required for ``"pandas"``
17621762
and ``"numpy"`` output type.
17631763
column_names
@@ -1794,8 +1794,8 @@ def return_dataset(
17941794
... kind="dataset", fname=outtmp.name
17951795
... ) as vouttbl:
17961796
... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
1797-
... result = lib.return_dataset(
1798-
... output_type="file", vfile=vouttbl
1797+
... result = lib.virtualfile_to_dataset(
1798+
... output_type="file", vfname=vouttbl
17991799
... )
18001800
... assert result is None
18011801
... assert Path(outtmp.name).stat().st_size > 0
@@ -1804,23 +1804,27 @@ def return_dataset(
18041804
... with Session() as lib:
18051805
... with lib.virtualfile_out(kind="dataset") as vouttbl:
18061806
... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
1807-
... outnp = lib.return_dataset(output_type="numpy", vfile=vouttbl)
1807+
... outnp = lib.virtualfile_to_dataset(
1808+
... output_type="numpy", vfname=vouttbl
1809+
... )
18081810
... assert isinstance(outnp, np.ndarray)
18091811
...
18101812
... # pandas output
18111813
... with Session() as lib:
18121814
... with lib.virtualfile_out(kind="dataset") as vouttbl:
18131815
... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
1814-
... outpd = lib.return_dataset(output_type="pandas", vfile=vouttbl)
1816+
... outpd = lib.virtualfile_to_dataset(
1817+
... output_type="pandas", vfname=vouttbl
1818+
... )
18151819
... assert isinstance(outpd, pd.DataFrame)
18161820
...
18171821
... # pandas output with specified column names
18181822
... with Session() as lib:
18191823
... with lib.virtualfile_out(kind="dataset") as vouttbl:
18201824
... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
1821-
... outpd2 = lib.return_dataset(
1825+
... outpd2 = lib.virtualfile_to_dataset(
18221826
... output_type="pandas",
1823-
... vfile=vouttbl,
1827+
... vfname=vouttbl,
18241828
... column_names=["col1", "col2", "col3", "coltext"],
18251829
... )
18261830
... assert isinstance(outpd2, pd.DataFrame)
@@ -1846,7 +1850,7 @@ def return_dataset(
18461850
return None
18471851

18481852
# Read the virtual file as a GMT dataset and convert to pandas.DataFrame
1849-
result = self.read_virtualfile(vfile, kind="dataset").contents.to_dataframe()
1853+
result = self.read_virtualfile(vfname, kind="dataset").contents.to_dataframe()
18501854
if output_type == "numpy": # numpy.ndarray output
18511855
return result.to_numpy()
18521856

pygmt/datasets/tile_map.py

Lines changed: 47 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
:class:`xarray.DataArray`.
44
"""
55

6-
from __future__ import annotations
6+
from typing import Literal
77

88
from packaging.version import Version
99

1010
try:
1111
import contextily
12+
from xyzservices import TileProvider
1213

1314
_HAS_CONTEXTILY = True
1415
except ImportError:
16+
TileProvider = None
1517
_HAS_CONTEXTILY = False
1618

1719
import numpy as np
@@ -21,76 +23,63 @@
2123

2224

2325
def load_tile_map(
24-
region,
25-
zoom="auto",
26-
source=None,
27-
lonlat=True,
28-
wait=0,
29-
max_retries=2,
26+
region: list,
27+
zoom: int | Literal["auto"] = "auto",
28+
source: TileProvider | str | None = None,
29+
lonlat: bool = True,
30+
wait: int = 0,
31+
max_retries: int = 2,
3032
zoom_adjust: int | None = None,
31-
):
33+
) -> xr.DataArray:
3234
"""
3335
Load a georeferenced raster tile map from XYZ tile providers.
3436
3537
The tiles that compose the map are merged and georeferenced into an
36-
:class:`xarray.DataArray` image with 3 bands (RGB). Note that the returned
37-
image is in a Spherical Mercator (EPSG:3857) coordinate reference system.
38+
:class:`xarray.DataArray` image with 3 bands (RGB). Note that the returned image is
39+
in a Spherical Mercator (EPSG:3857) coordinate reference system.
3840
3941
Parameters
4042
----------
41-
region : list
42-
The bounding box of the map in the form of a list [*xmin*, *xmax*,
43-
*ymin*, *ymax*]. These coordinates should be in longitude/latitude if
44-
``lonlat=True`` or Spherical Mercator (EPSG:3857) if ``lonlat=False``.
45-
46-
zoom : int or str
47-
Optional. Level of detail. Higher levels (e.g. ``22``) mean a zoom
48-
level closer to the Earth's surface, with more tiles covering a smaller
49-
geographical area and thus more detail. Lower levels (e.g. ``0``) mean
50-
a zoom level further from the Earth's surface, with less tiles covering
51-
a larger geographical area and thus less detail [Default is
52-
``"auto"`` to automatically determine the zoom level based on the
53-
bounding box region extent].
43+
region
44+
The bounding box of the map in the form of a list [*xmin*, *xmax*, *ymin*,
45+
*ymax*]. These coordinates should be in longitude/latitude if ``lonlat=True`` or
46+
Spherical Mercator (EPSG:3857) if ``lonlat=False``.
47+
zoom
48+
Level of detail. Higher levels (e.g. ``22``) mean a zoom level closer to the
49+
Earth's surface, with more tiles covering a smaller geographical area and thus
50+
more detail. Lower levels (e.g. ``0``) mean a zoom level further from the
51+
Earth's surface, with less tiles covering a larger geographical area and thus
52+
less detail. Default is ``"auto"`` to automatically determine the zoom level
53+
based on the bounding box region extent.
5454
5555
.. note::
5656
The maximum possible zoom level may be smaller than ``22``, and depends on
5757
what is supported by the chosen web tile provider source.
58-
59-
source : xyzservices.TileProvider or str
60-
Optional. The tile source: web tile provider or path to a local file.
61-
Provide either:
62-
63-
- A web tile provider in the form of a
64-
:class:`xyzservices.TileProvider` object. See
65-
:doc:`Contextily providers <contextily:providers_deepdive>` for a
66-
list of tile providers [Default is
67-
``xyzservices.providers.OpenStreetMap.HOT``, i.e. OpenStreetMap
68-
Humanitarian web tiles].
69-
- A web tile provider in the form of a URL. The placeholders for the
70-
XYZ in the URL need to be {x}, {y}, {z}, respectively. E.g.
58+
source
59+
The tile source: web tile provider or path to a local file. Provide either:
60+
61+
- A web tile provider in the form of a :class:`xyzservices.TileProvider` object.
62+
See :doc:`Contextily providers <contextily:providers_deepdive>` for a list of
63+
tile providers. Default is ``xyzservices.providers.OpenStreetMap.HOT``, i.e.
64+
OpenStreetMap Humanitarian web tiles.
65+
- A web tile provider in the form of a URL. The placeholders for the XYZ in the
66+
URL need to be {x}, {y}, {z}, respectively. E.g.
7167
``https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png``.
72-
- A local file path. The file is read with
73-
:doc:`rasterio <rasterio:index>` and all bands are loaded into the
74-
basemap. See
68+
- A local file path. The file is read with :doc:`rasterio <rasterio:index>` and
69+
all bands are loaded into the basemap. See
7570
:doc:`contextily:working_with_local_files`.
7671
7772
.. important::
7873
Tiles are assumed to be in the Spherical Mercator projection (EPSG:3857).
79-
80-
lonlat : bool
81-
Optional. If ``False``, coordinates in ``region`` are assumed to be
82-
Spherical Mercator as opposed to longitude/latitude [Default is
83-
``True``].
84-
85-
wait : int
86-
Optional. If the tile API is rate-limited, the number of seconds to
87-
wait between a failed request and the next try [Default is ``0``].
88-
89-
max_retries : int
90-
Optional. Total number of rejected requests allowed before contextily
91-
will stop trying to fetch more tiles from a rate-limited API [Default
92-
is ``2``].
93-
74+
lonlat
75+
If ``False``, coordinates in ``region`` are assumed to be Spherical Mercator as
76+
opposed to longitude/latitude.
77+
wait
78+
If the tile API is rate-limited, the number of seconds to wait between a failed
79+
request and the next try.
80+
max_retries
81+
Total number of rejected requests allowed before contextily will stop trying to
82+
fetch more tiles from a rate-limited API.
9483
zoom_adjust
9584
The amount to adjust a chosen zoom level if it is chosen automatically. Values
9685
outside of -1 to 1 are not recommended as they can lead to slow execution.
@@ -100,15 +89,15 @@ def load_tile_map(
10089
10190
Returns
10291
-------
103-
raster : xarray.DataArray
92+
raster
10493
Georeferenced 3-D data array of RGB values.
10594
10695
Raises
10796
------
10897
ImportError
109-
If ``contextily`` is not installed or can't be imported. Follow
110-
:doc:`install instructions for contextily <contextily:index>`, (e.g.
111-
via ``python -m pip install contextily``) before using this function.
98+
If ``contextily`` is not installed or can't be imported. Follow the
99+
:doc:`install instructions for contextily <contextily:index>`, (e.g. via
100+
``python -m pip install contextily``) before using this function.
112101
113102
Examples
114103
--------

pygmt/src/grdview.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ def grdview(self, grid, **kwargs):
6363
The name of the color palette table to use.
6464
drapegrid : str or xarray.DataArray
6565
The file name or a DataArray of the image grid to be draped on top
66-
of the relief provided by grid. [Default determines colors from
67-
grid]. Note that ``zscale`` and ``plane`` always refers to the grid.
66+
of the relief provided by ``grid`` [Default determines colors from grid].
67+
Note that ``zscale`` and ``plane`` always refer to the grid.
6868
The drapegrid only provides the information pertaining to colors, which
6969
(if drapegrid is a grid) will be looked-up via the CPT (see ``cmap``).
7070
plane : float or str

0 commit comments

Comments
 (0)