generated from worldbank/template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
3,444 additions
and
3,235 deletions.
There are no files selected for viewing
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,298 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 30, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"The autoreload extension is already loaded. To reload it, use:\n", | ||
" %reload_ext autoreload\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import sys, os\n", | ||
"import rasterio\n", | ||
"\n", | ||
"import pandas as pd\n", | ||
"import geopandas as gpd\n", | ||
"import numpy as np\n", | ||
"\n", | ||
"from shapely.wkt import loads\n", | ||
"\n", | ||
"sys.path.insert(0, \"../src\")\n", | ||
"\n", | ||
"import GOSTrocks.rasterMisc as rMisc\n", | ||
"import GOSTrocks.dataMisc as dMisc\n", | ||
"\n", | ||
"%load_ext autoreload\n", | ||
"%autoreload 2" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"C:\\Users\\WB411133\\AppData\\Local\\Temp\\ipykernel_25180\\3039392467.py:14: FutureWarning: The geopandas.dataset module is deprecated and will be removed in GeoPandas 1.0. You can get the original 'naturalearth_lowres' data from https://www.naturalearthdata.com/downloads/110m-cultural-vectors/.\n", | ||
" world_filepath = gpd.datasets.get_path('naturalearth_lowres')\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# Local/input files\n", | ||
"iso3 = 'KHM'\n", | ||
"out_folder = \"c:/WBG/Work/KHM_Energy/data\"\n", | ||
"wsf_file = os.path.join(out_folder, \"WSF\", \"wsf.tif\")\n", | ||
"ghsl_file = os.path.join(out_folder, \"GHSL\", \"ghsl.tif\")\n", | ||
"overture_buildings = os.path.join(out_folder, \"overture\", \"overture_download_2024_03_29.csv\")\n", | ||
"overture_raster = os.path.join(out_folder, \"overture\", \"overture_download_2024_03_29.tif\")\n", | ||
"overture_raster_points = os.path.join(out_folder, \"overture\", \"overture_download_2024_03_29_points.tif\")\n", | ||
"for file in [wsf_file, ghsl_file]:\n", | ||
" if not os.path.exists(os.path.dirname(file)):\n", | ||
" os.makedirs(os.path.dirname(file))\n", | ||
"\n", | ||
"# get country extent from geopandas\n", | ||
"world_filepath = gpd.datasets.get_path('naturalearth_lowres')\n", | ||
"world = gpd.read_file(world_filepath)\n", | ||
"country = world[world.iso_a3 == iso3]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"FUBAR\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"\"\"\" Not working with World Bank Firewall\n", | ||
"# Download the WSF data\n", | ||
"if not os.path.exists(wsf_file):\n", | ||
" print(\"Downloading WSF data\")\n", | ||
" wsf_data, wsf_profile = dMisc.download_WSF(country, out_file = wsf_file)\n", | ||
"\n", | ||
"wsf_r = rasterio.open(wsf_file)\n", | ||
"\"\"\"\n", | ||
"print(\"FUBAR\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#Clip GHSL using local files\n", | ||
"local_version = r\"J:\\Data\\GLOBAL\\GHSL\\Built\\GHS_BUILT_S_E2020_GLOBE_R2023A_54009_100_V1_0.tif\"\n", | ||
"if not os.path.exists(ghsl_file):\n", | ||
" ghsl_raster = rasterio.open(local_version)\n", | ||
" data, profile = rMisc.clipRaster(ghsl_raster, country)\n", | ||
" with rasterio.open(ghsl_file, 'w', **profile) as dst:\n", | ||
" dst.write(data)\n", | ||
"ghsl_r = rasterio.open(ghsl_file)" | ||
] | ||
}, | ||
{ | ||
"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>class</th>\n", | ||
" <th>height</th>\n", | ||
" <th>wkt</th>\n", | ||
" <th>geometry</th>\n", | ||
" </tr>\n", | ||
" </thead>\n", | ||
" <tbody>\n", | ||
" <tr>\n", | ||
" <th>0</th>\n", | ||
" <td>NaN</td>\n", | ||
" <td>NaN</td>\n", | ||
" <td>POLYGON ((103.3991092 13.6076154, 103.3991148 ...</td>\n", | ||
" <td>POLYGON ((103.39911 13.60762, 103.39911 13.607...</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>1</th>\n", | ||
" <td>NaN</td>\n", | ||
" <td>NaN</td>\n", | ||
" <td>POLYGON ((103.819971 13.2832912, 103.8199974 1...</td>\n", | ||
" <td>POLYGON ((103.81997 13.28329, 103.82000 13.283...</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>2</th>\n", | ||
" <td>NaN</td>\n", | ||
" <td>NaN</td>\n", | ||
" <td>POLYGON ((103.8152555 13.2890315, 103.8151626 ...</td>\n", | ||
" <td>POLYGON ((103.81526 13.28903, 103.81516 13.289...</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>3</th>\n", | ||
" <td>NaN</td>\n", | ||
" <td>NaN</td>\n", | ||
" <td>POLYGON ((105.5873344 12.3655821, 105.5873972 ...</td>\n", | ||
" <td>POLYGON ((105.58733 12.36558, 105.58740 12.365...</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>4</th>\n", | ||
" <td>NaN</td>\n", | ||
" <td>NaN</td>\n", | ||
" <td>POLYGON ((103.0758412 13.2660819, 103.0758304 ...</td>\n", | ||
" <td>POLYGON ((103.07584 13.26608, 103.07583 13.266...</td>\n", | ||
" </tr>\n", | ||
" </tbody>\n", | ||
"</table>\n", | ||
"</div>" | ||
], | ||
"text/plain": [ | ||
" class height wkt \\\n", | ||
"0 NaN NaN POLYGON ((103.3991092 13.6076154, 103.3991148 ... \n", | ||
"1 NaN NaN POLYGON ((103.819971 13.2832912, 103.8199974 1... \n", | ||
"2 NaN NaN POLYGON ((103.8152555 13.2890315, 103.8151626 ... \n", | ||
"3 NaN NaN POLYGON ((105.5873344 12.3655821, 105.5873972 ... \n", | ||
"4 NaN NaN POLYGON ((103.0758412 13.2660819, 103.0758304 ... \n", | ||
"\n", | ||
" geometry \n", | ||
"0 POLYGON ((103.39911 13.60762, 103.39911 13.607... \n", | ||
"1 POLYGON ((103.81997 13.28329, 103.82000 13.283... \n", | ||
"2 POLYGON ((103.81526 13.28903, 103.81516 13.289... \n", | ||
"3 POLYGON ((105.58733 12.36558, 105.58740 12.365... \n", | ||
"4 POLYGON ((103.07584 13.26608, 103.07583 13.266... " | ||
] | ||
}, | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# read in and process Overture buildings\n", | ||
"ob = pd.read_csv(overture_buildings)\n", | ||
"ob_geoms = ob['wkt'].apply(loads)\n", | ||
"inB = gpd.GeoDataFrame(ob, geometry=ob_geoms, crs=4326)\n", | ||
"inB.head()\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# attempt to rasterrize the buildings as polygons\n", | ||
"if not os.path.exists(overture_raster):\n", | ||
" rasterized_buildings = rMisc.rasterizeDataFrame(inB, templateRaster=ghsl_file, mergeAlg=\"ADD\", re_proj=True, nodata=0.)\n", | ||
" with rasterio.open(overture_raster, 'w', **rasterized_buildings['meta']) as dst:\n", | ||
" dst.write_band(1, rasterized_buildings['vals'])\n", | ||
"overture_r = rasterio.open(overture_raster)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# attempt to rasterrize the buildings as points\n", | ||
"if not os.path.exists(overture_raster_points):\n", | ||
" inB_points = inB.copy()\n", | ||
" inB_points['geometry'] = inB_points['geometry'].centroid\n", | ||
" rasterized_buildings = rMisc.rasterizeDataFrame(inB_points, templateRaster=ghsl_file, mergeAlg=\"ADD\", re_proj=True, nodata=0.)\n", | ||
" with rasterio.open(overture_raster_points, 'w', **rasterized_buildings['meta']) as dst:\n", | ||
" dst.write_band(1, rasterized_buildings['vals'])\n", | ||
"overture_r_points = rasterio.open(overture_raster_points) " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 32, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Compare rasterized buildings with built area dataset\n", | ||
"### Open both datasets and threshold them to get built area data\n", | ||
"o_thresh = 1\n", | ||
"ghsl_thresh = 3000\n", | ||
"\n", | ||
"o_data = overture_r_points.read(1)\n", | ||
"o_data = (o_data > o_thresh).astype('uint8')\n", | ||
"\n", | ||
"ghsl_data = ghsl_r.read(1)\n", | ||
"ghsl_data = (ghsl_data > ghsl_thresh).astype('uint8') * 10\n", | ||
"\n", | ||
"combo_data = o_data + ghsl_data\n", | ||
"\n", | ||
"# Write out the combined data\n", | ||
"out_file = os.path.join(out_folder, \"overture_vs_ghsl.tif\")\n", | ||
"if not os.path.exists(out_file):\n", | ||
" meta = overture_r_points.meta.copy()\n", | ||
" meta.update(dtype=rasterio.uint8, nodata=0)\n", | ||
" with rasterio.open(out_file, 'w', **meta) as out_raster:\n", | ||
" out_raster.write_band(1, combo_data)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"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 | ||
} |
Oops, something went wrong.