Skip to content

Commit

Permalink
🛰️
Browse files Browse the repository at this point in the history
  • Loading branch information
cboettig committed Oct 27, 2024
1 parent 8c257e9 commit ec3a881
Show file tree
Hide file tree
Showing 4 changed files with 459 additions and 139 deletions.
1 change: 1 addition & 0 deletions myst.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ project:
- file: reading/07-ibis-4.ipynb
- file: reading/spatial-1.ipynb
- file: reading/spatial-2.ipynb
- file: reading/spatial-3.ipynb

site:
template: book-theme
Expand Down
249 changes: 110 additions & 139 deletions reading/spatial-3.ipynb

Large diffs are not rendered by default.

281 changes: 281 additions & 0 deletions reading/spatial-4.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "2124260a-f39e-4f6d-9469-fbac6c72a864",
"metadata": {},
"source": [
"# Zonal Statistics"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "63c059eb-dc3b-47f9-a544-e0f9ff3b7b75",
"metadata": {},
"outputs": [],
"source": [
"from exactextract import exact_extract\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c21844bd-685b-4907-9396-30a8e44a85ea",
"metadata": {},
"outputs": [],
"source": [
"exact_extract?"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "5e46e88c-ab93-4c36-8a48-1a777668726f",
"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>label</th>\n",
" <th>grade</th>\n",
" <th>city</th>\n",
" <th>fill</th>\n",
" <th>mean</th>\n",
" <th>geometry</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>A1</td>\n",
" <td>A</td>\n",
" <td>New Haven</td>\n",
" <td>#76a865</td>\n",
" <td>0.709624</td>\n",
" <td>MULTIPOLYGON (((-72.90000 41.36885, -72.90325 ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>A2</td>\n",
" <td>A</td>\n",
" <td>New Haven</td>\n",
" <td>#76a865</td>\n",
" <td>0.761685</td>\n",
" <td>MULTIPOLYGON (((-72.89401 41.36331, -72.89256 ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>A3</td>\n",
" <td>A</td>\n",
" <td>New Haven</td>\n",
" <td>#76a865</td>\n",
" <td>0.665130</td>\n",
" <td>MULTIPOLYGON (((-72.90981 41.35949, -72.90284 ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>A4</td>\n",
" <td>A</td>\n",
" <td>New Haven</td>\n",
" <td>#76a865</td>\n",
" <td>0.751388</td>\n",
" <td>MULTIPOLYGON (((-72.90284 41.35795, -72.89990 ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>A5</td>\n",
" <td>A</td>\n",
" <td>New Haven</td>\n",
" <td>#76a865</td>\n",
" <td>0.742981</td>\n",
" <td>MULTIPOLYGON (((-72.89801 41.34930, -72.89883 ...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" label grade city fill mean \\\n",
"0 A1 A New Haven #76a865 0.709624 \n",
"1 A2 A New Haven #76a865 0.761685 \n",
"2 A3 A New Haven #76a865 0.665130 \n",
"3 A4 A New Haven #76a865 0.751388 \n",
"4 A5 A New Haven #76a865 0.742981 \n",
"\n",
" geometry \n",
"0 MULTIPOLYGON (((-72.90000 41.36885, -72.90325 ... \n",
"1 MULTIPOLYGON (((-72.89401 41.36331, -72.89256 ... \n",
"2 MULTIPOLYGON (((-72.90981 41.35949, -72.90284 ... \n",
"3 MULTIPOLYGON (((-72.90284 41.35795, -72.89990 ... \n",
"4 MULTIPOLYGON (((-72.89801 41.34930, -72.89883 ... "
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"city_stats = exact_extract(\"ndvi.tif\", \n",
" \"new_haven.parquet\", \n",
" [\"mean\"], \n",
" include_geom = True,\n",
" include_cols=[\"label\", \"grade\", \"city\", \"fill\"],\n",
" output=\"pandas\")\n",
"\n",
"city_stats.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e9d6361b-c9a9-48e2-9b3f-818e88951d0b",
"metadata": {},
"outputs": [],
"source": [
"city_stats.to_parquet(\"new_haven_stats.parquet\")"
]
},
{
"cell_type": "markdown",
"id": "e37afb42-dd1f-44ec-8180-99397dadd9ab",
"metadata": {},
"source": [
"We can bring our data back into ibis,"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "dc44e75d-c9f5-476b-8a09-b77833aafb23",
"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>grade</th>\n",
" <th>ndvi</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>C</td>\n",
" <td>0.547228</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>A</td>\n",
" <td>0.730811</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>D</td>\n",
" <td>0.486641</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>B</td>\n",
" <td>0.609324</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" grade ndvi\n",
"0 C 0.547228\n",
"1 A 0.730811\n",
"2 D 0.486641\n",
"3 B 0.609324"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import ibis\n",
"from ibis import _\n",
"con = ibis.duckdb.connect(extensions=[\"spatial\"])\n",
"\n",
"city = con.read_parquet(\"new_haven_stats.parquet\")"
]
},
{
"cell_type": "markdown",
"id": "5cce332f-65f4-4916-86e3-e9b7e82c9e1f",
"metadata": {},
"source": [
" and we are finally ready to summarize the data to determine if the spectre of redlining remains: on average, are A grades still greener than D grades? Construct the rest of the `ibis` code to compute the average NDVI by grade."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fea87402-e985-41b2-9999-e7749f28e6be",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
67 changes: 67 additions & 0 deletions reading/spatial-5.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "7ecc9fa6-a5ed-4bdb-9861-ca0f7ba6d990",
"metadata": {},
"source": [
"# Mapping and publishing\n",
"\n",
"In this section we take a look at adding our computed NDVI metrics to our map, along with methods for publishing our maps as interactive web applications."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ad08a14b-1abe-4955-b0d3-7688b7ca05eb",
"metadata": {},
"outputs": [],
"source": [
"from ibis import _\n",
"con = ibis.duckdb.connect(extensions=[\"spatial\"])\n",
"\n",
"city = (con\n",
" .read_geo(\"/vsicurl/https://dsl.richmond.edu/panorama/redlining/static/mappinginequality.gpkg\")\n",
" .filter(_.city == \"New Haven\")\n",
" .execute()\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "79338e3f-227b-4555-a58e-b6df628d1389",
"metadata": {},
"outputs": [],
"source": [
"import leafmap.maplibregl as leafmap\n",
"\n",
"m = leafmap.Map(style = \"voyager\")\n",
"m.add_cog_layer(\"https://github.com/espm-157/static-maps/raw/refs/heads/main/ndvi.tif\", colormap_name = \"greens\", nodata=1)\n",
"m.add_gdf(city, \"fill\", paint = { \"fill-color\": [\"get\", \"fill\"], \"fill-opacity\": 0.8})\n",
"m\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit ec3a881

Please sign in to comment.