Skip to content

Commit

Permalink
Metadata Improvements (#106)
Browse files Browse the repository at this point in the history
* add crs to GetMetadata requests

* check for coord in variables for additional_coords

* use default values in GetMap for additional coords not specified in request params

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
ndellicarpini and pre-commit-ci[bot] authored Dec 16, 2024
1 parent 1a089df commit 112696a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
5 changes: 5 additions & 0 deletions xpublish_wms/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class WMSGetMetadataQuery(WMSBaseQuery):
description="Bounding box to use for calculating min and max in the format 'minx,miny,maxx,maxy'",
),
)
crs: Literal["EPSG:4326", "EPSG:3857"] = Field(
"EPSG:4326",
description="Coordinate reference system to use for the query. EPSG:4326 and EPSG:3857 are supported for this request",
)
time: Optional[str] = (
Field(
None,
Expand Down Expand Up @@ -273,6 +277,7 @@ def wms_query(
day=day,
range=range,
bbox=bbox,
crs=crs if srs is None else srs,
time=time,
elevation=elevation,
)
Expand Down
38 changes: 26 additions & 12 deletions xpublish_wms/wms/get_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ def ensure_query_types(
self.autoscale = query.autoscale

available_selectors = ds.gridded.additional_coords(ds[self.parameter])
self.dim_selectors = {k: query_params[k] for k in available_selectors}
self.dim_selectors = {
k: query_params[k] if k in query_params else None
for k in available_selectors
}

def select_layer(self, ds: xr.Dataset) -> xr.DataArray:
"""
Expand Down Expand Up @@ -235,23 +238,34 @@ def select_elevation(self, ds: xr.Dataset, da: xr.DataArray) -> xr.DataArray:
def select_custom_dim(self, da: xr.DataArray) -> xr.DataArray:
"""
Select other dimension, ensuring a 2D array
If dimension is provided :
- use xarray to access custom coord
- method nearest to ensure at least one result
Otherwise:
- Get first value of coord
:param da:
:return:
"""
# Filter dimension from custom query, if any
for dim, value in self.dim_selectors.items():
if dim in da.coords:
dtype = da[dim].dtype
method = None
if "timedelta" in str(dtype):
value = pd.to_timedelta(value)
elif np.issubdtype(dtype, np.integer):
value = int(value)
method = "nearest"
elif np.issubdtype(dtype, np.floating):
value = float(value)
method = "nearest"
da = da.sel({dim: value}, method=method)
if value is None:
da = da.isel({dim: 0})
else:
dtype = da[dim].dtype
method = None
if "timedelta" in str(dtype):
value = pd.to_timedelta(value)
elif np.issubdtype(dtype, np.integer):
value = int(value)
method = "nearest"
elif np.issubdtype(dtype, np.floating):
value = float(value)
method = "nearest"
da = da.sel({dim: value}, method=method)

# Squeeze single value dimensions
da = da.squeeze()
Expand Down
7 changes: 5 additions & 2 deletions xpublish_wms/wms/get_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def get_minmax(
bbox=query.bbox if not entire_layer else "-180,-90,180,90",
width=1 if entire_layer else 512,
height=1 if entire_layer else 512,
crs="EPSG:4326",
crs=query.crs,
time=query.time,
elevation=query.elevation,
styles="raster/default",
Expand Down Expand Up @@ -139,7 +139,10 @@ def get_layer_details(ds: xr.Dataset, layer_name: str) -> dict:

additional_coords = ds.gridded.additional_coords(da)
additional_coord_values = {
coord: da.cf.coords[coord].values.tolist() for coord in additional_coords
coord: (
da.cf.coords[coord] if coord in da.cf.coords else da[coord]
).values.tolist()
for coord in additional_coords
}

return {
Expand Down

0 comments on commit 112696a

Please sign in to comment.