Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/xyz routes #88

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Prev Previous commit
x,y as query parameters
iacopoff committed Aug 25, 2021
commit e116f1984c66275c807f8df8669b9f1f39b2f82c
33 changes: 25 additions & 8 deletions xpublish/routers/xyz.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import xarray as xr
import cachey
from fastapi import APIRouter, Depends, Response
from fastapi import APIRouter, Depends, Response, Query, Path
from typing import Optional

from xpublish.utils.cache import CostTimer
@@ -21,15 +21,30 @@ class XYZRouter(APIRouter, LayerOptionsMixin):
xyz_router = XYZRouter()


def query_builder(time, xleft, xright, ybottom, ytop, xlab, ylab):
query = {}
query.update({xlab: slice(xleft, xright), ylab: slice(ytop, ybottom)})
if time:
query["time"] = time
return query


@xyz_router.get("/tiles/{var}/{z}/{x}/{y}")
@xyz_router.get("/tiles/{var}/{z}/{x}/{y}.{format}")
async def tiles(
var,
z,
x,
y,
format: str = "PNG",
time: Optional[str] = None,
var: str = Path(
..., description="Dataset's variable. It defines the map's data layer"
),
z: int = Path(..., description="Tiles' zoom level"),
x: int = Path(..., description="Tiles' column"),
y: int = Path(..., description="Tiles' row"),
format: str = Query("PNG", description="Image format. Default to PNG"),
time: str = Query(
None,
description="Filter by time in time-varying datasets. String time format should match dataset's time format",
),
xlab: str = Query("x", description="Dataset x coordinate label"),
ylab: str = Query("y", description="Dataset y coordinate label"),
cache: cachey.Cache = Depends(get_cache),
dataset: xr.Dataset = Depends(get_dataset),
):
@@ -41,6 +56,8 @@ async def tiles(

xleft, xright, ybottom, ytop = get_bounds(TMS, z, x, y)

query = query_builder(time, xleft, xright, ybottom, ytop, xlab, ylab)

cache_key = (
dataset.attrs.get(DATASET_ID_ATTR_KEY, "")
+ "/"
@@ -51,7 +68,7 @@ async def tiles(
if response is None:
with CostTimer() as ct:

tile = get_tiles(var, dataset, time, xleft, xright, ybottom, ytop)
tile = get_tiles(var, dataset, query)

byte_image = get_image_datashader(tile, datashader_settings, format)

10 changes: 4 additions & 6 deletions xpublish/utils/ows.py
Original file line number Diff line number Diff line change
@@ -42,14 +42,12 @@ def get_bounds(TMS, zoom, x, y):
return bbx.left, bbx.right, bbx.bottom, bbx.top


def get_tiles(var, dataset, time, xleft, xright, ybottom, ytop) -> xr.DataArray:
def get_tiles(var, dataset, query) -> xr.DataArray:

if time:
tile = dataset[var].sel(
time=time, x=slice(xleft, xright), y=slice(ytop, ybottom)
) # noqa
if query.get("time"):
tile = dataset[var].sel(query) # noqa
else:
tile = dataset[var].sel(x=slice(xleft, xright), y=slice(ytop, ybottom)) # noqa
tile = dataset[var].sel(query) # noqa

if 0 in tile.sizes.values():
raise HTTPException(status_code=406, detail=f"Map outside dataset domain")