Skip to content

Commit

Permalink
added zonal statistics example and updated example mapping grid data …
Browse files Browse the repository at this point in the history
…to vector data
  • Loading branch information
snowman2 committed Oct 28, 2019
1 parent 9192197 commit f87123f
Show file tree
Hide file tree
Showing 25 changed files with 1,750 additions and 983 deletions.
Binary file not shown.
Binary file not shown.
5 changes: 3 additions & 2 deletions docs/html/_sources/examples/examples.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ This page contains links to a collection of examples of how to use GeoCube.
:maxdepth: 1
:caption: Contents:

categorical.ipynb
resample_point_data.ipynb
categorical.ipynb
timestamp_missing_data.ipynb
grid_to_vector_map.ipynb
rasterize_point_data.ipynb
zonal_statistics.ipynb
509 changes: 173 additions & 336 deletions docs/html/_sources/examples/grid_to_vector_map.ipynb.txt

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/html/_sources/examples/resample_point_data.ipynb.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Example - Resampling Point Data"
"# Example - Filling in missing data & dealing with timestamps"
]
},
{
Expand Down Expand Up @@ -393,4 +393,4 @@
},
"nbformat": 4,
"nbformat_minor": 2
}
}
278 changes: 278 additions & 0 deletions docs/html/_sources/examples/zonal_statistics.ipynb.txt
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
}
13 changes: 7 additions & 6 deletions docs/html/examples/categorical.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Example - Resampling Point Data" href="resample_point_data.html" />
<link rel="prev" title="Usage Examples" href="examples.html" />
<link rel="next" title="Example - Rasterizing Point Data" href="timestamp_missing_data.html" />
<link rel="prev" title="Example - Filling in missing data &amp; dealing with timestamps" href="resample_point_data.html" />
</head>

<body class="wy-body-for-nav">
Expand Down Expand Up @@ -90,6 +90,7 @@
<li class="toctree-l1"><a class="reference internal" href="../readme.html">geocube README</a></li>
<li class="toctree-l1"><a class="reference internal" href="../installation.html">Installation</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="examples.html">Usage Examples</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="resample_point_data.html">Example - Filling in missing data &amp; dealing with timestamps</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Example - Categorical Data</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#Load-in-soil-data">Load in soil data</a></li>
<li class="toctree-l3"><a class="reference internal" href="#Generate-categories-for-categorical-data">Generate categories for categorical data</a></li>
Expand All @@ -100,9 +101,9 @@
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="resample_point_data.html">Example - Resampling Point Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="timestamp_missing_data.html">Example - Rasterizing Point Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="grid_to_vector_map.html">Example - Mapping Grid Data to Vector Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="rasterize_point_data.html">Example - Rasterizing Point Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="zonal_statistics.html">Example - Zonal Statistics</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../modules.html">API Documentation</a></li>
Expand Down Expand Up @@ -955,10 +956,10 @@ <h3>Make sure all categories are represented<a class="headerlink" href="#Make-su

<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">

<a href="resample_point_data.html" class="btn btn-neutral float-right" title="Example - Resampling Point Data" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="timestamp_missing_data.html" class="btn btn-neutral float-right" title="Example - Rasterizing Point Data" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>


<a href="examples.html" class="btn btn-neutral float-left" title="Usage Examples" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
<a href="resample_point_data.html" class="btn btn-neutral float-left" title="Example - Filling in missing data &amp; dealing with timestamps" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>

</div>

Expand Down
14 changes: 8 additions & 6 deletions docs/html/examples/examples.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Example - Categorical Data" href="categorical.html" />
<link rel="next" title="Example - Filling in missing data &amp; dealing with timestamps" href="resample_point_data.html" />
<link rel="prev" title="Installation" href="../installation.html" />
</head>

Expand Down Expand Up @@ -90,10 +90,11 @@
<li class="toctree-l1"><a class="reference internal" href="../readme.html">geocube README</a></li>
<li class="toctree-l1"><a class="reference internal" href="../installation.html">Installation</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Usage Examples</a><ul>
<li class="toctree-l2"><a class="reference internal" href="resample_point_data.html">Example - Filling in missing data &amp; dealing with timestamps</a></li>
<li class="toctree-l2"><a class="reference internal" href="categorical.html">Example - Categorical Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="resample_point_data.html">Example - Resampling Point Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="timestamp_missing_data.html">Example - Rasterizing Point Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="grid_to_vector_map.html">Example - Mapping Grid Data to Vector Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="rasterize_point_data.html">Example - Rasterizing Point Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="zonal_statistics.html">Example - Zonal Statistics</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../modules.html">API Documentation</a></li>
Expand Down Expand Up @@ -194,10 +195,11 @@
<div class="toctree-wrapper compound">
<p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="resample_point_data.html">Example - Filling in missing data &amp; dealing with timestamps</a></li>
<li class="toctree-l1"><a class="reference internal" href="categorical.html">Example - Categorical Data</a></li>
<li class="toctree-l1"><a class="reference internal" href="resample_point_data.html">Example - Resampling Point Data</a></li>
<li class="toctree-l1"><a class="reference internal" href="timestamp_missing_data.html">Example - Rasterizing Point Data</a></li>
<li class="toctree-l1"><a class="reference internal" href="grid_to_vector_map.html">Example - Mapping Grid Data to Vector Data</a></li>
<li class="toctree-l1"><a class="reference internal" href="rasterize_point_data.html">Example - Rasterizing Point Data</a></li>
<li class="toctree-l1"><a class="reference internal" href="zonal_statistics.html">Example - Zonal Statistics</a></li>
</ul>
</div>
</div>
Expand All @@ -210,7 +212,7 @@

<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">

<a href="categorical.html" class="btn btn-neutral float-right" title="Example - Categorical Data" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="resample_point_data.html" class="btn btn-neutral float-right" title="Example - Filling in missing data &amp; dealing with timestamps" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>


<a href="../installation.html" class="btn btn-neutral float-left" title="Installation" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
Expand Down
Loading

0 comments on commit f87123f

Please sign in to comment.