Skip to content

Commit

Permalink
Merge pull request #31 from worldbank/LKA_Urb_CCDR
Browse files Browse the repository at this point in the history
Lka urb ccdr
  • Loading branch information
bpstewar authored May 25, 2024
2 parents ab9ce32 + 3577d7e commit 6050ebb
Show file tree
Hide file tree
Showing 10 changed files with 508 additions and 46 deletions.
1 change: 0 additions & 1 deletion docs/_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ parts:
chapters:
- file: notebooks/Tutorials/UrbanAreas_tutorials.ipynb
- file: notebooks/Tutorials/LEI_Example.ipynb

2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"sphinx_jupyterbook_latex",
"sphinx.ext.napoleon",
"sphinxcontrib.apidoc",
#"nbsphinx"
# "nbsphinx"
]
external_toc_exclude_missing = True
external_toc_path = "_toc.yml"
Expand Down
271 changes: 271 additions & 0 deletions notebooks/Implementations/MENA_Benchmarking/NTL_zonal_stats.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Benchmarking cities in MENA\n",
"\n",
"In support of an upcoming Urban flagship report, the MENA team is looking for a series of zonal statistics:\n",
"\n",
"- Nighttime Lights, Population, and built-area: \n",
" - Entire FUA \n",
" - Its associated urban center / “core” \n",
" - Associated “periphery” \n",
"\n",
"The unit of analysis is the Functional Urban Areas (FUAs) from the [UCDB Database](https://human-settlement.emergency.copernicus.eu/ghs_stat_ucdb2015mt_r2019a.php). For each FUA, we need to grab the associated urban periphary (lower threshold urban areas)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"import os\n",
"\n",
"import geopandas as gpd\n",
"\n",
"\n",
"sys.path.append(\"C:/WBG/Work/Code/GOSTrocks/src\")"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [],
"source": [
"data_folder = \"C:/WBG/Work/data\"\n",
"ucdb_file = os.path.join(\n",
" data_folder,\n",
" \"URBAN\",\n",
" \"GHS_STAT_UCDB2015MT_GLOBE_R2019A\",\n",
" \"GHS_STAT_UCDB2015MT_GLOBE_R2019A_V1_2.gpkg\",\n",
")\n",
"fua_file = os.path.join(\n",
" data_folder, \"URBAN\", \"GHS_FUA_UCDB2015_GLOBE_R2019A_54009_1K_V1_0.gpkg\"\n",
")\n",
"\n",
"out_folder = \"C:/WBG/Work/MENA_Urban/\"\n",
"urban_res_folder = os.path.join(out_folder, \"urban_data\")\n",
"\n",
"for out_folder in [urban_res_folder]:\n",
" if not os.path.exists(out_folder):\n",
" os.makedirs(out_folder)\n",
"\n",
"urban_periphary_file = os.path.join(urban_res_folder, \"urban_periphary.gpkg\")\n",
"urban_core_file = os.path.join(urban_res_folder, \"urban_core.gpkg\")"
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {},
"outputs": [],
"source": [
"inD = gpd.read_file(ucdb_file)\n",
"inF = gpd.read_file(fua_file)\n",
"inD = inD.to_crs(inF.crs)"
]
},
{
"cell_type": "code",
"execution_count": 85,
"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>eFUA_ID</th>\n",
" <th>UC_num</th>\n",
" <th>UC_IDs</th>\n",
" <th>eFUA_name</th>\n",
" <th>Commuting</th>\n",
" <th>Cntry_ISO</th>\n",
" <th>Cntry_name</th>\n",
" <th>FUA_area</th>\n",
" <th>UC_area</th>\n",
" <th>FUA_p_2015</th>\n",
" <th>UC_p_2015</th>\n",
" <th>Com_p_2015</th>\n",
" <th>geometry</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" <td>5959</td>\n",
" <td>Herat</td>\n",
" <td>1.0</td>\n",
" <td>AFG</td>\n",
" <td>Afghanistan</td>\n",
" <td>139.0</td>\n",
" <td>60.0</td>\n",
" <td>888811.175807</td>\n",
" <td>844574.564331</td>\n",
" <td>44236.611476</td>\n",
" <td>MULTIPOLYGON (((5529000.000 4155000.000, 55310...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>192.0</td>\n",
" <td>1.0</td>\n",
" <td>5964</td>\n",
" <td>Guzarah</td>\n",
" <td>1.0</td>\n",
" <td>AFG</td>\n",
" <td>Afghanistan</td>\n",
" <td>32.0</td>\n",
" <td>16.0</td>\n",
" <td>169489.573231</td>\n",
" <td>160204.260864</td>\n",
" <td>9285.312366</td>\n",
" <td>MULTIPOLYGON (((5541000.000 4139000.000, 55420...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>354.0</td>\n",
" <td>1.0</td>\n",
" <td>5968</td>\n",
" <td>Shindand</td>\n",
" <td>0.0</td>\n",
" <td>AFG</td>\n",
" <td>Afghanistan</td>\n",
" <td>12.0</td>\n",
" <td>12.0</td>\n",
" <td>147553.403870</td>\n",
" <td>147553.403870</td>\n",
" <td>0.000000</td>\n",
" <td>MULTIPOLYGON (((5573000.000 4030000.000, 55740...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>505.0</td>\n",
" <td>1.0</td>\n",
" <td>5970</td>\n",
" <td>Qala i Naw</td>\n",
" <td>0.0</td>\n",
" <td>AFG</td>\n",
" <td>Afghanistan</td>\n",
" <td>3.0</td>\n",
" <td>3.0</td>\n",
" <td>79809.722656</td>\n",
" <td>79809.722656</td>\n",
" <td>0.000000</td>\n",
" <td>MULTIPOLYGON (((5592000.000 4221000.000, 55900...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>648.0</td>\n",
" <td>1.0</td>\n",
" <td>5973</td>\n",
" <td>Farah</td>\n",
" <td>1.0</td>\n",
" <td>AFG</td>\n",
" <td>Afghanistan</td>\n",
" <td>32.0</td>\n",
" <td>16.0</td>\n",
" <td>131508.797060</td>\n",
" <td>122843.460327</td>\n",
" <td>8665.336733</td>\n",
" <td>MULTIPOLYGON (((5607000.000 3923000.000, 56080...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" eFUA_ID UC_num UC_IDs eFUA_name Commuting Cntry_ISO Cntry_name \\\n",
"0 1.0 1.0 5959 Herat 1.0 AFG Afghanistan \n",
"1 192.0 1.0 5964 Guzarah 1.0 AFG Afghanistan \n",
"2 354.0 1.0 5968 Shindand 0.0 AFG Afghanistan \n",
"3 505.0 1.0 5970 Qala i Naw 0.0 AFG Afghanistan \n",
"4 648.0 1.0 5973 Farah 1.0 AFG Afghanistan \n",
"\n",
" FUA_area UC_area FUA_p_2015 UC_p_2015 Com_p_2015 \\\n",
"0 139.0 60.0 888811.175807 844574.564331 44236.611476 \n",
"1 32.0 16.0 169489.573231 160204.260864 9285.312366 \n",
"2 12.0 12.0 147553.403870 147553.403870 0.000000 \n",
"3 3.0 3.0 79809.722656 79809.722656 0.000000 \n",
"4 32.0 16.0 131508.797060 122843.460327 8665.336733 \n",
"\n",
" geometry \n",
"0 MULTIPOLYGON (((5529000.000 4155000.000, 55310... \n",
"1 MULTIPOLYGON (((5541000.000 4139000.000, 55420... \n",
"2 MULTIPOLYGON (((5573000.000 4030000.000, 55740... \n",
"3 MULTIPOLYGON (((5592000.000 4221000.000, 55900... \n",
"4 MULTIPOLYGON (((5607000.000 3923000.000, 56080... "
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inF.head()"
]
},
{
"cell_type": "code",
"execution_count": 113,
"metadata": {},
"outputs": [],
"source": [
"fua_peripheries = inF.copy()\n",
"for idx, row in inF.iterrows():\n",
" # grab the related UCDBs\n",
" ucdb_ids = row[\"UC_IDs\"].split(\";\")\n",
" ucdb_ids = [int(x) for x in ucdb_ids]\n",
" sel_cores = inD.loc[inD[\"ID_HDC_G0\"].isin(ucdb_ids)]\n",
" periphery_geom = row[\"geometry\"].difference(sel_cores.unary_union)\n",
" fua_peripheries.loc[idx, \"geometry\"] = periphery_geom\n",
"\n",
"fua_peripheries.to_file(os.path.join(out_folder, \"FUA_peripheries.gpkg\"), driver=\"GPKG\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "urban_test",
"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.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 6050ebb

Please sign in to comment.