xarray.Dataset.xvec.query#
- Dataset.xvec.query(coord_name, geometry, predicate=None, distance=None, unique=False)#
Return a subset of a DataArray/Dataset filtered using a spatial query on
GeometryIndex
.Return the subset where the bounding box of each input geometry intersects the bounding box of a geometry in an
GeometryIndex
. If a predicate is provided, the tree geometries are first queried based on the bounding box of the input geometry and then are further filtered to those that meet the predicate when comparing the input geometry to the tree geometry: predicate(geometry, index_geometry)Bounding boxes are limited to two dimensions and are axis-aligned (equivalent to the bounds property of a geometry); any Z values present in input geometries are ignored when querying the tree.
- Parameters:
- coord_namestr
name of the coordinate axis backed by
GeometryIndex
- geometryshapely.Geometry | Sequence[shapely.Geometry]
Input geometries to query the
GeometryIndex
and filter results using the optional predicate.- predicatestr, optional
The predicate to use for testing geometries from the
GeometryIndex
that are within the input geometry’s bounding box, by default None. Any predicate supported byshapely.STRtree.query()
is a valid input.- distancefloat | Sequence[float], optional
Distances around each input geometry within which to query the tree for the
dwithin
predicate. If array_like, shape must be broadcastable to shape of geometry. Required ifpredicate=’dwithin’
, by default None- uniquebool, optional
Keep only unique geometries from the
GeometryIndex
in a result. If False, index geometries that match the query for multiple input geometries are duplicated for each match. If False, such geometries are returned only once. By default False
- Returns:
- filteredsame type as caller
A new object filtered according to the query
Examples
>>> da = ( ... xr.DataArray( ... np.random.rand(2), ... coords={"geom": [shapely.Point(1, 2), shapely.Point(3, 4)]}, ... dims="geom", ... ) ... .xvec.set_geom_indexes("geom", crs=4326) ... ) >>> da <xarray.DataArray (geom: 2)> array([0.76385513, 0.2312171 ]) Coordinates: * geom (geom) object POINT (1 2) POINT (3 4) Indexes: geom GeometryIndex (crs=EPSG:4326)
>>> da.xvec.query("geom", shapely.box(0, 0, 2.4, 2.2)) <xarray.DataArray (geom: 1)> array([0.76385513]) Coordinates: * geom (geom) object POINT (1 2) Indexes: geom GeometryIndex (crs=EPSG:4326)
>>> da.xvec.query( ... "geom", [shapely.box(0, 0, 2.4, 2.2), shapely.Point(2, 2).buffer(1)] ... ) <xarray.DataArray (geom: 2)> array([0.76385513, 0.76385513]) Coordinates: * geom (geom) object POINT (1 2) POINT (1 2) Indexes: geom GeometryIndex (crs=EPSG:4326)
>>> da.xvec.query( ... "geom", ... [shapely.box(0, 0, 2.4, 2.2), shapely.Point(2, 2).buffer(1)], ... unique=True, ... ) <xarray.DataArray (geom: 1)> array([0.76385513]) Coordinates: * geom (geom) object POINT (1 2) Indexes: geom GeometryIndex (crs=EPSG:4326)