We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 45e060b + 32941dc commit 57b44d6Copy full SHA for 57b44d6
CHANGES.md
@@ -8,6 +8,10 @@
8
9
* add point value query on right-click to map viewer (@hrodmn, https://github.com/developmentseed/titiler/pull/1100)
10
11
+### titiler.xarray
12
+
13
+* change `get_variable.drop_dim` parameter type from `str` to `List[str]` **breaking change**
14
15
## 0.21.1 (2025-01-29)
16
17
### titiler.core
src/titiler/xarray/tests/test_dependencies.py
@@ -44,3 +44,24 @@ def tiles(
44
response = client.get("/tiles/1/2/3", params={"variable": "yo"})
45
params = response.json()
46
assert params == {"variable": "yo"}
47
48
+ response = client.get("/tiles/1/2/3", params={"drop_dim": "yo=yo"})
49
+ params = response.json()
50
+ assert params == {"drop_dim": ["yo=yo"]}
51
52
+ response = client.get("/tiles/1/2/3", params={"drop_dim": "yo=1.0"})
53
54
+ assert params == {"drop_dim": ["yo=1.0"]}
55
56
+ response = client.get("/tiles/1/2/3", params={"drop_dim": ["yo=yo", "ye=ye"]})
57
58
+ assert params == {"drop_dim": ["yo=yo", "ye=ye"]}
59
60
+ response = client.get("/tiles/1/2/3", params={"drop_dim": "yo"})
61
+ assert response.status_code == 422
62
63
+ response = client.get("/tiles/1/2/3", params={"drop_dim": "=yo"})
64
65
66
+ response = client.get("/tiles/1/2/3", params={"drop_dim": "yo="})
67
src/titiler/xarray/tests/test_io_tools.py
@@ -87,7 +87,27 @@ def test_get_variable():
87
with pytest.raises(AssertionError):
88
get_variable(ds, "dataset")
89
90
- da = get_variable(ds, "dataset", drop_dim="universe=somewhere")
+ da = get_variable(ds, "dataset", drop_dim=["universe=somewhere"])
91
+ assert da.rio.crs
92
+ assert da.dims == ("z", "y", "x")
93
94
+ # 5D dataset - drop time dim
95
+ arr = numpy.arange(0, 33 * 35 * 2).reshape(2, 1, 1, 33, 35)
96
+ data = xarray.DataArray(
97
+ arr,
98
+ dims=("time", "universe", "z", "y", "x"),
99
+ coords={
100
+ "x": numpy.arange(-170, 180, 10),
101
+ "y": numpy.arange(-80, 85, 5),
102
+ "z": [0],
103
+ "universe": ["somewhere"],
104
+ "time": [datetime(2022, 1, 1), datetime(2023, 1, 1)],
105
+ },
106
+ )
107
108
+ da = get_variable(
109
+ ds, "dataset", drop_dim=["universe=somewhere", "time=2022-01-01T00:00:00"]
110
111
assert da.rio.crs
112
assert da.dims == ("z", "y", "x")
113
src/titiler/xarray/titiler/xarray/dependencies.py
@@ -1,10 +1,11 @@
1
"""titiler.xarray dependencies."""
2
3
from dataclasses import dataclass
4
-from typing import Optional, Union
+from typing import List, Optional, Union
5
6
import numpy
7
from fastapi import Query
+from pydantic.types import StringConstraints
from rio_tiler.types import RIOResampling, WarpResampling
from typing_extensions import Annotated
@@ -33,15 +34,20 @@ class XarrayIOParams(DefaultDependency):
33
34
# cache_client
35
36
37
+DropDimStr = Annotated[str, StringConstraints(pattern=r"^[^=]+=[^=]+$")]
38
39
40
@dataclass
41
class XarrayDsParams(DefaultDependency):
42
"""Xarray Dataset Options."""
43
variable: Annotated[str, Query(description="Xarray Variable name")]
drop_dim: Annotated[
- Optional[str],
- Query(description="Dimension to drop"),
+ Optional[List[DropDimStr]],
+ Query(
+ description="Dimensions to drop in form of `{dimension}={value}`",
+ ),
] = None
datetime: Annotated[
@@ -68,8 +74,10 @@ class CompatXarrayParams(XarrayIOParams):
68
74
variable: Annotated[Optional[str], Query(description="Xarray Variable name")] = None
69
75
70
76
71
72
77
78
79
80
73
81
82
83
src/titiler/xarray/titiler/xarray/io.py
@@ -139,15 +139,15 @@ def get_variable(
139
ds: xarray.Dataset,
140
variable: str,
141
datetime: Optional[str] = None,
142
- drop_dim: Optional[str] = None,
+ drop_dim: Optional[List[str]] = None,
143
) -> xarray.DataArray:
144
"""Get Xarray variable as DataArray.
145
146
Args:
147
ds (xarray.Dataset): Xarray Dataset.
148
variable (str): Variable to extract from the Dataset.
149
datetime (str, optional): datetime to select from the DataArray.
150
- drop_dim (str, optional): DataArray dimension to drop in form of `{dimension}={value}`.
+ drop_dim (list of str, optional): DataArray dimension to drop in form of `[{dimension}={value}],`.
151
152
Returns:
153
xarray.DataArray: 2D or 3D DataArray.
@@ -156,8 +156,9 @@ def get_variable(
156
da = ds[variable]
157
158
if drop_dim:
159
- dim_to_drop, dim_val = drop_dim.split("=")
160
- da = da.sel({dim_to_drop: dim_val}).drop_vars(dim_to_drop)
+ for dim in drop_dim:
+ dim_to_drop, dim_val = dim.split("=")
161
+ da = da.sel({dim_to_drop: dim_val}).drop_vars(dim_to_drop)
162
163
da = _arrange_dims(da)
164
0 commit comments