Skip to content

Commit

Permalink
Normalize 4326 coordinates (#84)
Browse files Browse the repository at this point in the history
* Normalize regular grid projected coordinates

* Normalize coordinates

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

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

* Remove unused method

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
mpiannucci and pre-commit-ci[bot] authored Oct 16, 2024
1 parent fe69e9b commit cf00eeb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
7 changes: 5 additions & 2 deletions xpublish_wms/grids/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,12 @@ def sel_lat_lng(
parameters,
) -> tuple[xr.Dataset, list, list]:
"""Select the given dataset by the given lon/lat and optional elevation"""
subset = self.mask(subset[parameters])

subset = self.mask(subset)
subset = subset.cf.interp(longitude=lng, latitude=lat)
subset = subset.cf.interp(
longitude=lng,
latitude=lat,
)

x_axis = [strip_float(subset.cf["longitude"])]
y_axis = [strip_float(subset.cf["latitude"])]
Expand Down
22 changes: 22 additions & 0 deletions xpublish_wms/grids/regular.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def project(
if coord != da.cf["longitude"].name and coord != da.cf["latitude"].name:
coords[coord] = da.coords[coord]

# normalize longitude to be between -180 and 180
da = da.cf.assign_coords(
longitude=(((da.cf["longitude"] + 180) % 360) - 180),
).sortby(da.cf["longitude"].name)

# build new x coordinate
coords["x"] = ("x", da.cf["longitude"].values, da.cf["longitude"].attrs)
# build new y coordinate
Expand All @@ -61,3 +66,20 @@ def project(
da = da.unify_chunks()

return da, None, None

def sel_lat_lng(
self,
subset: xr.Dataset,
lng,
lat,
parameters,
) -> tuple[xr.Dataset, list, list]:
# normalize longitude to be between -180 and 180
subset = subset.cf.assign_coords(
longitude=(((subset.cf["longitude"] + 180) % 360) - 180),
).sortby(subset.cf["longitude"].name)

if lng > 180:
lng = (lng + 180) % 360 - 180

return super().sel_lat_lng(subset, lng, lat, parameters)
1 change: 1 addition & 0 deletions xpublish_wms/wms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def wms_handler(
method = query_params.get("request", "").lower()
logger.debug(f"Received wms request: {request.url}")
logger.info(f"WMS: {method}")

if method == "getcapabilities":
return get_capabilities(dataset, request, query_params)
elif method == "getmap":
Expand Down
3 changes: 3 additions & 0 deletions xpublish_wms/wms/get_feature_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ def get_feature_info(ds: xr.Dataset, query: dict) -> Response:
any_has_vertical_axis = True in has_vertical_axis

crs = query.get("crs", None) or query.get("srs")
if crs != "EPSG:4326":
raise HTTPException(501, "Only EPSG:4326 is supported")

bbox = [float(x) for x in query["bbox"].split(",")]
width = int(query["width"])
height = int(query["height"])
Expand Down

0 comments on commit cf00eeb

Please sign in to comment.