lair.geo.clip#
- lair.geo.clip(data, bbox=None, extent=None, geom=None, crs='EPSG:4326', **kwargs)[source]#
Clip the data to the given bounds.
Note
A cell is kept when its center is inside the bounds or on them, with a
bbox, anextentor ageom, whichever way the coordinates run. A center exactly on an edge of the polygon (or of a hole in it) belongs to the polygon, on every side. Ageomkeeps the grid’s cells and sets those outside it to NaN; the grid is cut down to the window around the cells it keeps.- Parameters:
data (xr.DataArray | xr.Dataset) – The data to clip.
bbox (tuple[minx, miny, maxx, maxy]) – The bounding box to clip the data to.
extent (tuple[minx, maxx, miny, maxy]) – The extent to clip the data to.
geom (shapely.Polygon | Iterable[shapely.Polygon]) – The geometry, or a collection of geometries (list, array, GeoSeries), to clip the data to.
crs (Any, optional) – The CRS of the input geometries. Default is ‘EPSG:4326’.
kwargs (Any) – Additional keyword arguments for
bboxandextent, passed to the rioxarrayclip_boxmethod. None are taken withgeom.
- Returns:
xr.DataArray | xr.Dataset – The clipped data.
- Raises:
ValueError – Unless exactly one of
bbox,extentandgeomis given.TypeError – If
geomis given with keyword arguments.rioxarray.exceptions.NoDataInBounds – If no cell center is inside
geomor on it.
- Return type:
DataArray | Dataset
See also
BaseGrid.clipThe same for a grid or an inventory.
bbox2extentConverts between a bbox and an extent.
Examples
A box of 2 by 2 cells out of 4 by 4, by bbox or by extent:
>>> from lair.geo import clip, generate_regular_grid, write_rio_crs >>> grid = generate_regular_grid( ... -112.2, -111.8, 0.1, 40.6, 41.0, 0.1, x_label="lon", y_label="lat" ... ) >>> grid = write_rio_crs(grid.rio.set_spatial_dims(x_dim="lon", y_dim="lat"), 4326) >>> dict(clip(grid, bbox=(-112.1, 40.7, -111.9, 40.9)).sizes) {'lat': 2, 'lon': 2} >>> dict(clip(grid, extent=(-112.1, -111.9, 40.7, 40.9)).sizes) {'lat': 2, 'lon': 2}