-
Notifications
You must be signed in to change notification settings - Fork 5
Open
Description
Nice to provide indexing as if you input mercator tiles into https://dudewheresmytile.mattnucc.com/?tab=tile then did .sel
on the bbox
returned from there.
e.g. with below code you can do:
ds.tile("314/547/10")
and it will sel the correct bbox for you.
from typing import overload
import mercantile
import xarray as xr
@xr.register_dataset_accessor("tile")
@xr.register_dataarray_accessor("tile")
class TileAccessor:
def __init__(self, xarray_obj):
self._ds = xarray_obj
def mbbox(self, bbox: mercantile.LngLatBbox) -> xr.Dataset:
return self._ds.sel(
{
ds.cf["longitude"].name: slice(bbox.west, bbox.east),
ds.cf["latitude"].name: slice(bbox.south, bbox.north),
}
)
@overload
def __call__(self, tile: str) -> xr.Dataset: ...
def __call__(self, X: int, Y: int = None, zoom: int = None) -> xr.Dataset:
"""
Slice the bbox associated with the tile.
Valid inputs:
"lat/lon/zoom"
"lat,lon,zoom"
lat, lon, zoom (as integers)
not sure how to do params properly for this overload.
"""
# no safety checks! be good plz :)
if isinstance(X, str):
bbox = mercantile.bounds(tuple(map(int, X.replace(",", "/").split("/"))))
else:
bbox = mercantile.bounds(X, Y, zoom)
return self.mbbox(bbox)
Metadata
Metadata
Assignees
Labels
No labels