14
14
from collections .abc import Iterable , Sequence
15
15
from typing import Any , Literal
16
16
17
+ import numpy as np
17
18
import xarray as xr
18
19
from pygmt .encodings import charset
19
20
from pygmt .exceptions import GMTInvalidInput
@@ -195,11 +196,10 @@ def data_kind( # noqa: PLR0911
195
196
196
197
Recognized data kinds are:
197
198
198
- - ``"none"``: None and data is required. In this case, the data is usually given via
199
- a series of vectors (e.g., x/y/z)
200
- - ``"arg"``: bool, int, float, or None (only when ``required`` is False),
201
- representing an optional argument, mainly used for dealing with optional virtual
202
- files
199
+ - ``"none"``: data is ``None`` and is required. In this case, the input data is
200
+ usually given via a series of vectors (e.g., x/y/z)
201
+ - ``"arg"``: data is ``None`` and ``required=False``, or bool, int, float,
202
+ representing an optional argument, used for dealing with optional virtual files
203
203
- ``"file"``: a string or a :class:`pathlib.PurePath` object or a sequence of them,
204
204
representing a file name or a list of file names
205
205
- ``"geojson"``: a geo-like Python object that implements ``__geo_interface__``
@@ -253,14 +253,16 @@ def data_kind( # noqa: PLR0911
253
253
'vectors'
254
254
>>> data_kind(data={"x": [1, 2], "y": [3, 4]})
255
255
'vectors'
256
+ >>> data_kind(data=[[1, 2], [3, 4]])
257
+ 'vectors'
256
258
>>> data_kind(data=[1, 2, 3])
257
259
'vectors'
258
260
"""
259
261
# data is None and is required.
260
262
if data is None and required :
261
263
return "none"
262
264
263
- # A file or a list of files
265
+ # A file or a sequence of files
264
266
if isinstance (data , str | pathlib .PurePath ) or (
265
267
isinstance (data , list | tuple )
266
268
and all (isinstance (_file , str | pathlib .PurePath ) for _file in data )
@@ -277,11 +279,12 @@ def data_kind( # noqa: PLR0911
277
279
278
280
# Geo-like Python object that implements ``__geo_interface__`` (e.g.,
279
281
# geopandas.GeoDataFrame or shapely.geometry)
282
+ # Reference: https://gist.github.com/sgillies/2217756
280
283
if hasattr (data , "__geo_interface__" ):
281
284
return "geojson"
282
285
283
286
# A 2-D numpy.ndarray
284
- if hasattr (data , "__array_interface__" ) and data .ndim == 2 :
287
+ if isinstance (data , np . ndarray ) and data .ndim == 2 :
285
288
return "matrix"
286
289
287
290
# Fallback to "vectors" for anything else
0 commit comments