From a78a595c23e5ea61efa2bed77d0fe21b1b41666d Mon Sep 17 00:00:00 2001 From: Chris Rowe Date: Tue, 4 Jun 2024 12:54:49 -0400 Subject: [PATCH] Add max DSM metric --- city_metrix/layers/layer.py | 3 + city_metrix/metrics/__init__.py | 3 +- city_metrix/metrics/max_dsm.py | 14 + environment.yml | 1 + notebooks/tutorial/compute indicators.ipynb | 427 +++++++++++++++++++- 5 files changed, 443 insertions(+), 5 deletions(-) create mode 100644 city_metrix/metrics/max_dsm.py diff --git a/city_metrix/layers/layer.py b/city_metrix/layers/layer.py index 0b2da49..b2a1e17 100644 --- a/city_metrix/layers/layer.py +++ b/city_metrix/layers/layer.py @@ -65,6 +65,9 @@ def mean(self): def count(self): return self._zonal_stats("count") + + def max(self): + return self._zonal_stats("max") def _zonal_stats(self, stats_func): if box(*self.zones.total_bounds).area <= MAX_TILE_SIZE**2: diff --git a/city_metrix/metrics/__init__.py b/city_metrix/metrics/__init__.py index 10b559c..da9e79b 100644 --- a/city_metrix/metrics/__init__.py +++ b/city_metrix/metrics/__init__.py @@ -3,4 +3,5 @@ from .built_land_with_high_land_surface_temperature import built_land_with_high_land_surface_temperature from .mean_tree_cover import mean_tree_cover from .urban_open_space import urban_open_space -from .natural_areas import natural_areas \ No newline at end of file +from .natural_areas import natural_areas +from .max_dsm import max_dsm diff --git a/city_metrix/metrics/max_dsm.py b/city_metrix/metrics/max_dsm.py new file mode 100644 index 0000000..0e275c7 --- /dev/null +++ b/city_metrix/metrics/max_dsm.py @@ -0,0 +1,14 @@ +from geopandas import GeoDataFrame, GeoSeries + +from city_metrix.layers import AlosDSM +from exactextract import exact_extract + +def max_dsm(zones: GeoDataFrame) -> GeoSeries: + """ + Get max AlosDSM. + :param zones: GeoDataFrame with geometries to collect zonal stats on + :return: Pandas Series of hights + """ + max_height = exact_extract(AlosDSM(), zones, ["max"]) + + return max_height diff --git a/environment.yml b/environment.yml index 5848dbc..5fd2a23 100644 --- a/environment.yml +++ b/environment.yml @@ -21,3 +21,4 @@ dependencies: - pip=23.3.1 - pip: - cartoframes==1.2.5 + - git+https://github.com/isciences/exactextract diff --git a/notebooks/tutorial/compute indicators.ipynb b/notebooks/tutorial/compute indicators.ipynb index 99f03fd..f9d9132 100644 --- a/notebooks/tutorial/compute indicators.ipynb +++ b/notebooks/tutorial/compute indicators.ipynb @@ -1198,13 +1198,432 @@ "city_gdf_aoi" ] }, + { + "cell_type": "markdown", + "id": "02147478", + "metadata": {}, + "source": [ + "# Building hight" + ] + }, { "cell_type": "code", - "execution_count": null, - "id": "951151f1-e0f5-4a8a-99a7-19a6f7c84591", + "execution_count": 5, + "id": "fa88dd9c", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Authenticating to GEE with configured credentials file.\n" + ] + } + ], + "source": [ + "from city_metrix.metrics import max_dsm" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "0565257b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + " \n", + " " + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "buildings = gpd.read_file('./data/botanico/botanico-BuildingFootprints.geojson')\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "ceb48997", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + " \n", + " " + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Extracting layer ALOS DSM from Google Earth Engine:\n", + "[########################################] | 100% Completed | 209.59 ms\n" + ] + }, + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
idversionupdate_timesourcesnamesclasshas_partsheightmin_heightnum_floorsfacade_materialroof_materialroof_shapegeometrymax_dsm
008bc2e3118a31fff020028fa98d81cdd02024-04-06 07:00:00+00:00[ { \"property\": \"\", \"dataset\": \"Google Open Bu...NoneNoneFalseNaNNaNNaNNoneNoneNonePOLYGON ((-58.42594 -34.58705, -58.42598 -34.5...19.0
108bc2e3118a06fff0200e5245a02389f02024-04-06 07:00:00+00:00[ { \"property\": \"\", \"dataset\": \"Google Open Bu...NoneNoneFalseNaNNaNNaNNoneNoneNonePOLYGON ((-58.42593 -34.58685, -58.42597 -34.5...15.0
208bc2e3118a31fff0200626acc9f3edb02024-04-06 07:00:00+00:00[ { \"property\": \"\", \"dataset\": \"Google Open Bu...NoneNoneFalseNaNNaNNaNNoneNoneNonePOLYGON ((-58.42591 -34.58695, -58.42594 -34.5...30.0
308bc2e3118a06fff0200e65fed72e17202024-04-06 07:00:00+00:00[ { \"property\": \"\", \"dataset\": \"Google Open Bu...NoneNoneFalseNaNNaNNaNNoneNoneNonePOLYGON ((-58.42581 -34.58696, -58.42586 -34.5...30.0
408bc2e3118a06fff0200ea89dfadab5302024-04-06 07:00:00+00:00[ { \"property\": \"\", \"dataset\": \"Google Open Bu...NoneNoneFalseNaNNaNNaNNoneNoneNonePOLYGON ((-58.42591 -34.58687, -58.42599 -34.5...15.0
................................................
239508bc2e311bd72fff0200b4ae95fb717002024-04-06 07:00:00+00:00[ { \"property\": \"\", \"dataset\": \"Google Open Bu...NoneNoneFalseNaNNaNNaNNoneNoneNonePOLYGON ((-58.41270 -34.57662, -58.41287 -34.5...NaN
239608bc2e311bd72fff0200fe4f08158d9402024-04-06 07:00:00+00:00[ { \"property\": \"\", \"dataset\": \"Google Open Bu...NoneNoneFalseNaNNaNNaNNoneNoneNonePOLYGON ((-58.41265 -34.57667, -58.41269 -34.5...NaN
239708bc2e311bd09fff02007caeb9361c9202024-04-06 07:00:00+00:00[ { \"property\": \"\", \"dataset\": \"Google Open Bu...NoneNoneFalseNaNNaNNaNNoneNoneNonePOLYGON ((-58.41250 -34.57661, -58.41267 -34.5...NaN
239808bc2e311bd54fff02004c9d8a14aab502024-04-06 07:00:00+00:00[ { \"property\": \"\", \"dataset\": \"Google Open Bu...NoneNoneFalseNaNNaNNaNNoneNoneNonePOLYGON ((-58.41271 -34.57615, -58.41276 -34.5...NaN
239908bc2e311bc25fff02006ae8d2eb16f202024-04-06 07:00:00+00:00[ { \"property\": \"\", \"dataset\": \"Microsoft ML B...NoneNoneFalseNaNNaNNaNNoneNoneNonePOLYGON ((-58.41183 -34.57628, -58.41181 -34.5...NaN
\n", + "

2400 rows × 15 columns

\n", + "
" + ], + "text/plain": [ + " id version update_time \\\n", + "0 08bc2e3118a31fff020028fa98d81cdd 0 2024-04-06 07:00:00+00:00 \n", + "1 08bc2e3118a06fff0200e5245a02389f 0 2024-04-06 07:00:00+00:00 \n", + "2 08bc2e3118a31fff0200626acc9f3edb 0 2024-04-06 07:00:00+00:00 \n", + "3 08bc2e3118a06fff0200e65fed72e172 0 2024-04-06 07:00:00+00:00 \n", + "4 08bc2e3118a06fff0200ea89dfadab53 0 2024-04-06 07:00:00+00:00 \n", + "... ... ... ... \n", + "2395 08bc2e311bd72fff0200b4ae95fb7170 0 2024-04-06 07:00:00+00:00 \n", + "2396 08bc2e311bd72fff0200fe4f08158d94 0 2024-04-06 07:00:00+00:00 \n", + "2397 08bc2e311bd09fff02007caeb9361c92 0 2024-04-06 07:00:00+00:00 \n", + "2398 08bc2e311bd54fff02004c9d8a14aab5 0 2024-04-06 07:00:00+00:00 \n", + "2399 08bc2e311bc25fff02006ae8d2eb16f2 0 2024-04-06 07:00:00+00:00 \n", + "\n", + " sources names class \\\n", + "0 [ { \"property\": \"\", \"dataset\": \"Google Open Bu... None None \n", + "1 [ { \"property\": \"\", \"dataset\": \"Google Open Bu... None None \n", + "2 [ { \"property\": \"\", \"dataset\": \"Google Open Bu... None None \n", + "3 [ { \"property\": \"\", \"dataset\": \"Google Open Bu... None None \n", + "4 [ { \"property\": \"\", \"dataset\": \"Google Open Bu... None None \n", + "... ... ... ... \n", + "2395 [ { \"property\": \"\", \"dataset\": \"Google Open Bu... None None \n", + "2396 [ { \"property\": \"\", \"dataset\": \"Google Open Bu... None None \n", + "2397 [ { \"property\": \"\", \"dataset\": \"Google Open Bu... None None \n", + "2398 [ { \"property\": \"\", \"dataset\": \"Google Open Bu... None None \n", + "2399 [ { \"property\": \"\", \"dataset\": \"Microsoft ML B... None None \n", + "\n", + " has_parts height min_height num_floors facade_material roof_material \\\n", + "0 False NaN NaN NaN None None \n", + "1 False NaN NaN NaN None None \n", + "2 False NaN NaN NaN None None \n", + "3 False NaN NaN NaN None None \n", + "4 False NaN NaN NaN None None \n", + "... ... ... ... ... ... ... \n", + "2395 False NaN NaN NaN None None \n", + "2396 False NaN NaN NaN None None \n", + "2397 False NaN NaN NaN None None \n", + "2398 False NaN NaN NaN None None \n", + "2399 False NaN NaN NaN None None \n", + "\n", + " roof_shape geometry max_dsm \n", + "0 None POLYGON ((-58.42594 -34.58705, -58.42598 -34.5... 19.0 \n", + "1 None POLYGON ((-58.42593 -34.58685, -58.42597 -34.5... 15.0 \n", + "2 None POLYGON ((-58.42591 -34.58695, -58.42594 -34.5... 30.0 \n", + "3 None POLYGON ((-58.42581 -34.58696, -58.42586 -34.5... 30.0 \n", + "4 None POLYGON ((-58.42591 -34.58687, -58.42599 -34.5... 15.0 \n", + "... ... ... ... \n", + "2395 None POLYGON ((-58.41270 -34.57662, -58.41287 -34.5... NaN \n", + "2396 None POLYGON ((-58.41265 -34.57667, -58.41269 -34.5... NaN \n", + "2397 None POLYGON ((-58.41250 -34.57661, -58.41267 -34.5... NaN \n", + "2398 None POLYGON ((-58.41271 -34.57615, -58.41276 -34.5... NaN \n", + "2399 None POLYGON ((-58.41183 -34.57628, -58.41181 -34.5... NaN \n", + "\n", + "[2400 rows x 15 columns]" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "buildings[\"max_dsm\"] = max_dsm(buildings) \n", + "buildings" + ] } ], "metadata": {