-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added zonal statistics example and updated example mapping grid data …
…to vector data
- Loading branch information
Showing
25 changed files
with
1,750 additions
and
983 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
509 changes: 173 additions & 336 deletions
509
docs/html/_sources/examples/grid_to_vector_map.ipynb.txt
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,278 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Example - Zonal Statistics\n", | ||
"This is useful in the case where you want to get regional statistics for a raster." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import geopandas as gpd\n", | ||
"import numpy\n", | ||
"import rioxarray\n", | ||
"import xarray\n", | ||
"\n", | ||
"from geocube.api.core import make_geocube" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Load in the source raster data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#!wget https://prd-tnm.s3.amazonaws.com/StagedProducts/Elevation/13/IMG/USGS_NED_13_n42w091_IMG.zip" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"elevation = rioxarray.open_rasterio(\n", | ||
" \"zip://USGS_NED_13_n42w091_IMG.zip/USGS_NED_13_n42w091_IMG.img\"\n", | ||
").squeeze().drop(\"band\")\n", | ||
"elevation.name = \"elevation\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Create the data mask by rasterizing the unique ID of the vector data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"ssurgo_data = gpd.read_file(\"../../test/test_data/input/soil_data_group.geojson\")\n", | ||
"ssurgo_data = ssurgo_data.loc[ssurgo_data.hzdept_r==0]\n", | ||
"# convert the key to group to the vector data to an integer as that is one of the\n", | ||
"# best data types for this type of mapping. If your data is not integer,\n", | ||
"# then consider using a mapping of your data to an integer with something\n", | ||
"# like a categorical dtype.\n", | ||
"ssurgo_data[\"mukey\"] = ssurgo_data.mukey.astype(int)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"out_grid = make_geocube(\n", | ||
" vector_data=ssurgo_data,\n", | ||
" measurements=[\"mukey\"],\n", | ||
" like=elevation, # ensure the data are on the same grid\n", | ||
" fill=numpy.nan\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"<xarray.Dataset>\n", | ||
"Dimensions: (x: 10812, y: 10812)\n", | ||
"Coordinates:\n", | ||
" * y (y) float64 42.0 42.0 42.0 42.0 42.0 ... 41.0 41.0 41.0 41.0\n", | ||
" * x (x) float64 -91.0 -91.0 -91.0 -91.0 ... -90.0 -90.0 -90.0 -90.0\n", | ||
" spatial_ref int64 0\n", | ||
"Data variables:\n", | ||
" mukey (y, x) float64 nan nan nan nan nan nan ... nan nan nan nan nan\n", | ||
" elevation (y, x) float32 ...\n", | ||
"Attributes:\n", | ||
" grid_mapping: spatial_ref" | ||
] | ||
}, | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# merge the two together\n", | ||
"out_grid[\"elevation\"] = elevation\n", | ||
"out_grid" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Get the elevation statistics of each region using the mask" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"grouped_elevation = out_grid.drop(\"spatial_ref\").groupby(out_grid.mukey)\n", | ||
"grid_mean = grouped_elevation.mean().rename({\"elevation\": \"elevation_mean\"})\n", | ||
"grid_min = grouped_elevation.min().rename({\"elevation\": \"elevation_min\"})\n", | ||
"grid_max = grouped_elevation.max().rename({\"elevation\": \"elevation_max\"})\n", | ||
"grid_std = grouped_elevation.std().rename({\"elevation\": \"elevation_std\"})" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<div>\n", | ||
"<style scoped>\n", | ||
" .dataframe tbody tr th:only-of-type {\n", | ||
" vertical-align: middle;\n", | ||
" }\n", | ||
"\n", | ||
" .dataframe tbody tr th {\n", | ||
" vertical-align: top;\n", | ||
" }\n", | ||
"\n", | ||
" .dataframe thead th {\n", | ||
" text-align: right;\n", | ||
" }\n", | ||
"</style>\n", | ||
"<table border=\"1\" class=\"dataframe\">\n", | ||
" <thead>\n", | ||
" <tr style=\"text-align: right;\">\n", | ||
" <th></th>\n", | ||
" <th>elevation_mean</th>\n", | ||
" <th>elevation_min</th>\n", | ||
" <th>elevation_max</th>\n", | ||
" <th>elevation_std</th>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>mukey</th>\n", | ||
" <th></th>\n", | ||
" <th></th>\n", | ||
" <th></th>\n", | ||
" <th></th>\n", | ||
" </tr>\n", | ||
" </thead>\n", | ||
" <tbody>\n", | ||
" <tr>\n", | ||
" <td>198692.0</td>\n", | ||
" <td>173.130920</td>\n", | ||
" <td>169.394562</td>\n", | ||
" <td>188.442505</td>\n", | ||
" <td>3.307044</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <td>198714.0</td>\n", | ||
" <td>175.061554</td>\n", | ||
" <td>170.214157</td>\n", | ||
" <td>179.716675</td>\n", | ||
" <td>2.148920</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <td>198724.0</td>\n", | ||
" <td>179.933060</td>\n", | ||
" <td>178.237244</td>\n", | ||
" <td>181.490387</td>\n", | ||
" <td>0.628017</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <td>198750.0</td>\n", | ||
" <td>176.188461</td>\n", | ||
" <td>167.951233</td>\n", | ||
" <td>190.138763</td>\n", | ||
" <td>3.814724</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <td>198754.0</td>\n", | ||
" <td>171.632187</td>\n", | ||
" <td>167.610321</td>\n", | ||
" <td>181.611298</td>\n", | ||
" <td>2.996241</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <td>271425.0</td>\n", | ||
" <td>167.973709</td>\n", | ||
" <td>167.951233</td>\n", | ||
" <td>168.476776</td>\n", | ||
" <td>0.076759</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <td>271431.0</td>\n", | ||
" <td>176.718338</td>\n", | ||
" <td>170.258133</td>\n", | ||
" <td>180.460220</td>\n", | ||
" <td>2.732229</td>\n", | ||
" </tr>\n", | ||
" </tbody>\n", | ||
"</table>\n", | ||
"</div>" | ||
], | ||
"text/plain": [ | ||
" elevation_mean elevation_min elevation_max elevation_std\n", | ||
"mukey \n", | ||
"198692.0 173.130920 169.394562 188.442505 3.307044\n", | ||
"198714.0 175.061554 170.214157 179.716675 2.148920\n", | ||
"198724.0 179.933060 178.237244 181.490387 0.628017\n", | ||
"198750.0 176.188461 167.951233 190.138763 3.814724\n", | ||
"198754.0 171.632187 167.610321 181.611298 2.996241\n", | ||
"271425.0 167.973709 167.951233 168.476776 0.076759\n", | ||
"271431.0 176.718338 170.258133 180.460220 2.732229" | ||
] | ||
}, | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"zonal_stats = xarray.merge([grid_mean, grid_min, grid_max, grid_std])\n", | ||
"zonal_stats.to_dataframe()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.6.7" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.