You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Zonal functions accept vector zones directly (#999)
* Accept vector zones in zonal functions (#998)
stats(), crosstab(), apply(), and crop() now accept GeoDataFrames or
list-of-(geometry, value) pairs as the zones argument. When vector
input is detected, rasterize() is called internally using the values
raster as the template. Adds column and rasterize_kw parameters.
* Add vector zones docs and notebook examples (#998)
Document column and rasterize_kw parameters in stats(), crosstab(),
apply(), and crop() docstrings. Add vector zones section to the
3_Zonal user guide notebook with GeoDataFrame, list-of-pairs,
crosstab, and rasterize_kw examples.
Copy file name to clipboardExpand all lines: examples/user_guide/3_Zonal.ipynb
+63-14Lines changed: 63 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -3,18 +3,7 @@
3
3
{
4
4
"cell_type": "markdown",
5
5
"metadata": {},
6
-
"source": [
7
-
"# Xarray-spatial\n",
8
-
"### User Guide: Zonal\n",
9
-
"-----\n",
10
-
"\n",
11
-
"Xarray-spatial's zonal functions provide an easy way to generate statistics for zones within a raster aggregate. It's set up with a default set of calculations, or you can input any set of custom calculations you'd like to perform.\n",
12
-
"\n",
13
-
"[Generate terrain](#Generate-Terrain-Data)\n",
14
-
"[Zonal Statistics](#Zonal-Statistics)\n",
15
-
"\n",
16
-
"-----------\n"
17
-
]
6
+
"source": "# Xarray-spatial\n### User Guide: Zonal\n-----\n\nXarray-spatial's zonal functions provide an easy way to generate statistics for zones within a raster aggregate. It's set up with a default set of calculations, or you can input any set of custom calculations you'd like to perform.\n\n[Generate terrain](#Generate-Terrain-Data)\n[Zonal Statistics](#Zonal-Statistics)\n[Zonal Crosstab](#Zonal-crosstab)\n[Vector Zones](#Vector-zones)\n\n-----------"
18
7
},
19
8
{
20
9
"cell_type": "markdown",
@@ -239,11 +228,71 @@
239
228
"source": "# Calculate crosstab: rows are trail segments, columns are tree species (1=Oak, 2=Pine, 3=Maple)\nresult = tree_species_agg.xrs.zonal_crosstab(zones_agg, zone_ids=[11, 12, 13, 14, 15, 16], cat_ids=[1, 2, 3])\nresult.columns = ['Trail Segment', 'Oak', 'Pine', 'Maple']\nresult"
240
229
},
241
230
{
242
-
"cell_type": "code",
231
+
"cell_type": "markdown",
243
232
"execution_count": null,
244
233
"metadata": {},
245
234
"outputs": [],
246
-
"source": []
235
+
"source": "## Vector zones\n\nZonal functions can also accept vector geometries directly -- a GeoDataFrame or a list of `(geometry, value)` pairs. The rasterization to match the values grid happens automatically inside the function call, so there's no separate `rasterize()` step.\n\nThis works with `zonal_stats`, `zonal_crosstab`, `zonal_apply`, and `crop`."
236
+
},
237
+
{
238
+
"cell_type": "markdown",
239
+
"source": "### GeoDataFrame zones\n\nLet's define three rectangular regions as polygons in a GeoDataFrame and compute elevation statistics directly.",
240
+
"metadata": {}
241
+
},
242
+
{
243
+
"cell_type": "code",
244
+
"source": "import geopandas as gpd\nfrom shapely.geometry import box\nfrom xrspatial.zonal import stats\n\n# Three rectangular regions across the terrain\nzones_gdf = gpd.GeoDataFrame(\n {'region_id': [1.0, 2.0, 3.0],\n 'name': ['West valley', 'Central ridge', 'East slope']},\n geometry=[\n box(-20, -20, -5, 20), # left third\n box(-5, -20, 5, 20), # middle third\n box(5, -20, 20, 20), # right third\n ],\n)\n\n# Pass the GeoDataFrame directly -- no manual rasterize() needed\nresult = stats(zones_gdf, terrain, column='region_id',\n stats_funcs=['mean', 'min', 'max', 'std', 'count'])\nresult",
245
+
"metadata": {},
246
+
"execution_count": null,
247
+
"outputs": []
248
+
},
249
+
{
250
+
"cell_type": "markdown",
251
+
"source": "The same thing works with the accessor. The values raster is used as the template for rasterization, so the zones automatically align to the grid.",
"source": "### List-of-pairs zones\n\nIf you don't have a GeoDataFrame, you can pass a list of `(geometry, zone_id)` tuples. No `column` argument needed here since the zone ID is the second element of each pair.",
"source": "### Crosstab with vector zones\n\nVector zones work with `zonal_crosstab` too. Here we'll use the tree species data from earlier with our GeoDataFrame regions.",
"source": "### Forwarding rasterize options\n\nYou can pass extra options to the internal `rasterize()` call via `rasterize_kw`. For example, `all_touched=True` will include pixels that touch the geometry boundary, not just those whose centre falls inside.",
0 commit comments