From d7f08cb10136e4d67f6908dd92f484775cfc8c42 Mon Sep 17 00:00:00 2001 From: Justin Terry Date: Wed, 14 Feb 2024 15:51:38 -0800 Subject: [PATCH 1/5] Initial commit --- city_metrix/layers/layer.py | 18 +- notebooks/tutorial/compute indicators.ipynb | 254 ++++++++++++-------- 2 files changed, 173 insertions(+), 99 deletions(-) diff --git a/city_metrix/layers/layer.py b/city_metrix/layers/layer.py index 4eac755..3aebbba 100644 --- a/city_metrix/layers/layer.py +++ b/city_metrix/layers/layer.py @@ -68,9 +68,12 @@ def count(self): def _zonal_stats(self, stats_func): if box(*self.zones.total_bounds).area <= MAX_TILE_SIZE**2: - return self._zonal_stats_tile(self.zones, [stats_func])[stats_func] + stats = self._zonal_stats_tile(self.zones, [stats_func])[stats_func] else: - return self._zonal_stats_fishnet(stats_func) + stats = self._zonal_stats_fishnet(stats_func) + + if self.layer is not None: + pass def _zonal_stats_fishnet(self, stats_func): # fishnet GeoDataFrame into smaller tiles @@ -91,6 +94,7 @@ def _zonal_stats_fishnet(self, stats_func): tile_funcs = get_stats_funcs(stats_func) # run zonal stats per data frame + print(f"Input covers too much area, splitting into {len(tile_gdfs)} tiles") tile_stats = pd.concat([ self._zonal_stats_tile(tile_gdf, tile_funcs) for tile_gdf in tile_gdfs @@ -104,18 +108,24 @@ def _zonal_stats_tile(self, tile_gdf, stats_func): bbox = tile_gdf.total_bounds aggregate_data = self.aggregate.get_data(bbox) mask_datum = [mask.get_data(bbox) for mask in self.masks] + layer_data = self.layer.get_data(bbox) if self.layer is not None else None # align to highest resolution raster, which should be the largest raster # since all are clipped to the extent - raster_data = [data for data in mask_datum + [aggregate_data] if isinstance(data, xr.DataArray)] + raster_data = [data for data in mask_datum + [aggregate_data] + layer_data if isinstance(data, xr.DataArray)] align_to = sorted(raster_data, key=lambda data: data.size, reverse=True).pop() aggregate_data = self._align(aggregate_data, align_to) mask_datum = [self._align(data, align_to) for data in mask_datum] + layer_data = self._align(layer_data, align_to) if layer_data is not None else None for mask in mask_datum: aggregate_data = aggregate_data.where(~np.isnan(mask)) zones = self._rasterize(tile_gdf, align_to) + + if layer_data is not None: + zones = zones + (layer_data.astype("uint32") << 16) + stats = zonal_stats(zones, aggregate_data, stats_funcs=stats_func) return stats @@ -228,7 +238,7 @@ def get_image_collection( ) with ProgressBar(): - print(f"Extracting layer {name} from Google Earth Engine:") + print(f"Extracting layer {name} from Google Earth Engine for bbox {bbox}:") data = ds.compute() # get in rioxarray format diff --git a/notebooks/tutorial/compute indicators.ipynb b/notebooks/tutorial/compute indicators.ipynb index 0ee439b..1bb532e 100644 --- a/notebooks/tutorial/compute indicators.ipynb +++ b/notebooks/tutorial/compute indicators.ipynb @@ -41,7 +41,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "id": "dcc7a7b9-cf15-442c-acfd-fef1806ad05e", "metadata": {}, "outputs": [], @@ -55,7 +55,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "id": "6bcc8215-0c19-45bd-8265-2be0c2a83c6b", "metadata": {}, "outputs": [ @@ -65,7 +65,7 @@ "'/Users/jt/dev/cities-cif'" ] }, - "execution_count": 2, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -100,7 +100,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 8, "id": "acbc5aaa-5f47-44ff-b9ab-b885c06d7424", "metadata": {}, "outputs": [ @@ -136,33 +136,33 @@ " \n", " \n", " 0\n", - " BRA-Salvador_ADM4-union_1\n", - " ADM4-union\n", - " BRA-Salvador\n", - " BRA-Salvador\n", - " 2022-08-03\n", - " MULTIPOLYGON (((-38.50135 -13.01134, -38.50140...\n", + " CHN-Chongqing_ADM-1_1\n", + " ADM-1\n", + " CHN-Chongqing\n", + " CHN-Chongqing\n", + " 2022-06-27\n", + " MULTIPOLYGON (((108.50540 32.20270, 108.49620 ...\n", " \n", " \n", "\n", "" ], "text/plain": [ - " geo_id geo_level geo_name geo_parent_name \\\n", - "0 BRA-Salvador_ADM4-union_1 ADM4-union BRA-Salvador BRA-Salvador \n", + " geo_id geo_level geo_name geo_parent_name \\\n", + "0 CHN-Chongqing_ADM-1_1 ADM-1 CHN-Chongqing CHN-Chongqing \n", "\n", " creation_date geometry \n", - "0 2022-08-03 MULTIPOLYGON (((-38.50135 -13.01134, -38.50140... " + "0 2022-06-27 MULTIPOLYGON (((108.50540 32.20270, 108.49620 ... " ] }, - "execution_count": 3, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# load boundary of a region of interest\n", - "boundary_path = 'https://cities-indicators.s3.eu-west-3.amazonaws.com/data/boundaries/boundary-BRA-Salvador-ADM4union.geojson'\n", + "boundary_path = 's3://cities-indicators/data/boundaries/boundary-CHN-Chongqing-ADM1.geojson'\n", "city_gdf_aoi = gpd.read_file(boundary_path, driver='GeoJSON')\n", "city_gdf_aoi.head()" ] @@ -205,67 +205,23 @@ " \n", " \n", " 0\n", - " BRA-Salvador_ADM4_1\n", - " ADM4\n", - " Pituaçu\n", - " BRA-Salvador\n", - " 2022-08-03\n", - " MULTIPOLYGON (((-38.40125 -12.96457, -38.40126...\n", - " \n", - " \n", - " 1\n", - " BRA-Salvador_ADM4_2\n", - " ADM4\n", - " Patamares\n", - " BRA-Salvador\n", - " 2022-08-03\n", - " MULTIPOLYGON (((-38.39898 -12.96269, -38.39898...\n", - " \n", - " \n", - " 2\n", - " BRA-Salvador_ADM4_3\n", - " ADM4\n", - " Piatã\n", - " BRA-Salvador\n", - " 2022-08-03\n", - " MULTIPOLYGON (((-38.37346 -12.93345, -38.37386...\n", - " \n", - " \n", - " 3\n", - " BRA-Salvador_ADM4_4\n", - " ADM4\n", - " Boca do Rio\n", - " BRA-Salvador\n", - " 2022-08-03\n", - " MULTIPOLYGON (((-38.41741 -12.97578, -38.41746...\n", - " \n", - " \n", - " 4\n", - " BRA-Salvador_ADM4_5\n", - " ADM4\n", - " Jardim Armação\n", - " BRA-Salvador\n", - " 2022-08-03\n", - " MULTIPOLYGON (((-38.43383 -12.98742, -38.43386...\n", + " CHN-Chongqing_ADM-1_1\n", + " ADM-1\n", + " CHN-Chongqing\n", + " CHN-Chongqing\n", + " 2022-06-27\n", + " MULTIPOLYGON (((108.50540 32.20270, 108.49620 ...\n", " \n", " \n", "\n", "" ], "text/plain": [ - " geo_id geo_level geo_name geo_parent_name \\\n", - "0 BRA-Salvador_ADM4_1 ADM4 Pituaçu BRA-Salvador \n", - "1 BRA-Salvador_ADM4_2 ADM4 Patamares BRA-Salvador \n", - "2 BRA-Salvador_ADM4_3 ADM4 Piatã BRA-Salvador \n", - "3 BRA-Salvador_ADM4_4 ADM4 Boca do Rio BRA-Salvador \n", - "4 BRA-Salvador_ADM4_5 ADM4 Jardim Armação BRA-Salvador \n", + " geo_id geo_level geo_name geo_parent_name \\\n", + "0 CHN-Chongqing_ADM-1_1 ADM-1 CHN-Chongqing CHN-Chongqing \n", "\n", " creation_date geometry \n", - "0 2022-08-03 MULTIPOLYGON (((-38.40125 -12.96457, -38.40126... \n", - "1 2022-08-03 MULTIPOLYGON (((-38.39898 -12.96269, -38.39898... \n", - "2 2022-08-03 MULTIPOLYGON (((-38.37346 -12.93345, -38.37386... \n", - "3 2022-08-03 MULTIPOLYGON (((-38.41741 -12.97578, -38.41746... \n", - "4 2022-08-03 MULTIPOLYGON (((-38.43383 -12.98742, -38.43386... " + "0 2022-06-27 MULTIPOLYGON (((108.50540 32.20270, 108.49620 ... " ] }, "execution_count": 4, @@ -292,25 +248,17 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 9, "id": "8bdaa746-b071-4e9f-8959-c8c57921c73c", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Authenticating to GEE with configured credentials file.\n" - ] - } - ], + "outputs": [], "source": [ "from city_metrix.metrics import mean_tree_cover" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 10, "id": "e69f2674-dc78-4098-b423-0686944eabe8", "metadata": {}, "outputs": [], @@ -328,7 +276,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 11, "id": "8b85131c", "metadata": {}, "outputs": [ @@ -336,9 +284,125 @@ "name": "stdout", "output_type": "stream", "text": [ - "Extracting layer tree cover from Google Earth Engine:\n", - "CPU times: user 6.65 s, sys: 1.12 s, total: 7.77 s\n", - "Wall time: 12.5 s\n" + "Input covers too much area, splitting into 55 tiles\n", + "Extracting layer tree cover from Google Earth Engine for bbox [106.2998057 28.4538074 106.7868306 28.661744 ]:\n", + "[########################################] | 100% Completed | 9.79 ss\n", + "Extracting layer tree cover from Google Earth Engine for bbox [106.7868306 28.5926236 106.86721119 28.661744 ]:\n", + "[########################################] | 100% Completed | 1.66 sms\n", + "Extracting layer tree cover from Google Earth Engine for bbox [108.37690815 28.1946548 108.7868306 28.661744 ]:\n", + "[########################################] | 100% Completed | 17.68 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [108.7868306 28.161744 109.2868306 28.661744 ]:\n", + "[########################################] | 100% Completed | 21.89 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [109.2868306 28.55722115 109.3171288 28.6316442 ]:\n", + "[########################################] | 100% Completed | 735.05 ms\n", + "Extracting layer tree cover from Google Earth Engine for bbox [105.722691 28.97837894 105.7868306 29.161744 ]:\n", + "[########################################] | 100% Completed | 1.85 sms\n", + "Extracting layer tree cover from Google Earth Engine for bbox [105.7868306 28.72981039 106.2868306 29.161744 ]:\n", + "[########################################] | 100% Completed | 40.28 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [106.2868306 28.661744 106.7868306 29.161744 ]:\n", + "[########################################] | 100% Completed | 22.10 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [106.7868306 28.661744 107.2868306 29.161744 ]:\n", + "[########################################] | 100% Completed | 20.55 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [107.2868306 28.8032582 107.7868306 29.161744 ]:\n", + "[########################################] | 100% Completed | 16.08 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [107.7868306 28.9628515 108.2868306 29.161744 ]:\n", + "[########################################] | 100% Completed | 8.98 ss\n", + "Extracting layer tree cover from Google Earth Engine for bbox [108.2868306 28.661744 108.7868306 29.161744 ]:\n", + "[########################################] | 100% Completed | 21.45 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [108.7868306 28.661744 109.2868306 29.161744 ]:\n", + "[########################################] | 100% Completed | 21.50 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [109.2868306 28.72059265 109.3156967 29.07002707]:\n", + "[########################################] | 100% Completed | 1.60 sms\n", + "Extracting layer tree cover from Google Earth Engine for bbox [105.2868306 29.161744 105.7868306 29.661744 ]:\n", + "[########################################] | 100% Completed | 21.90 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [105.7868306 29.161744 106.2868306 29.661744 ]:\n", + "[########################################] | 100% Completed | 22.21 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [106.2868306 29.161744 106.7868306 29.661744 ]:\n", + "[########################################] | 100% Completed | 21.73 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [106.7868306 29.161744 107.2868306 29.661744 ]:\n", + "[########################################] | 100% Completed | 21.42 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [107.2868306 29.161744 107.7868306 29.661744 ]:\n", + "[########################################] | 100% Completed | 21.01 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [107.7868306 29.161744 108.2868306 29.661744 ]:\n", + "[########################################] | 100% Completed | 20.76 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [108.2868306 29.161744 108.7868306 29.661744 ]:\n", + "[########################################] | 100% Completed | 21.54 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [108.7868306 29.161744 109.18802005 29.661744 ]:\n", + "[########################################] | 100% Completed | 18.26 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [105.3797864 29.661744 105.7868306 30.161744 ]:\n", + "[########################################] | 100% Completed | 18.11 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [105.7868306 29.661744 106.2868306 30.161744 ]:\n", + "[########################################] | 100% Completed | 23.68 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [106.2868306 29.661744 106.7868306 30.161744 ]:\n", + "[########################################] | 100% Completed | 22.08 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [106.7868306 29.661744 107.2868306 30.161744 ]:\n", + "[########################################] | 100% Completed | 22.07 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [107.2868306 29.661744 107.7868306 30.161744 ]:\n", + "[########################################] | 100% Completed | 21.64 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [107.7868306 29.661744 108.2868306 30.161744 ]:\n", + "[########################################] | 100% Completed | 21.21 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [108.2868306 29.661744 108.7868306 30.161744 ]:\n", + "[########################################] | 100% Completed | 20.56 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [108.7868306 29.661744 108.8312068 29.67418977]:\n", + "[########################################] | 100% Completed | 828.51 ms\n", + "Extracting layer tree cover from Google Earth Engine for bbox [105.533021 30.161744 105.7868306 30.41773367]:\n", + "[########################################] | 100% Completed | 6.52 ss\n", + "Extracting layer tree cover from Google Earth Engine for bbox [105.7868306 30.161744 106.2868306 30.4346008]:\n", + "[########################################] | 100% Completed | 13.04 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [106.2868306 30.161744 106.6643415 30.3068867]:\n", + "[########################################] | 100% Completed | 5.91 ss\n", + "Extracting layer tree cover from Google Earth Engine for bbox [107.16288016 30.161744 107.2868306 30.32415115]:\n", + "[########################################] | 100% Completed | 2.98 ss\n", + "Extracting layer tree cover from Google Earth Engine for bbox [107.2868306 30.161744 107.7868306 30.661744 ]:\n", + "[########################################] | 100% Completed | 28.51 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [107.7868306 30.161744 108.2868306 30.661744 ]:\n", + "[########################################] | 100% Completed | 22.57 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [108.2868306 30.161744 108.7868306 30.661744 ]:\n", + "[########################################] | 100% Completed | 24.58 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [108.7868306 30.4949047 109.2868306 30.661744 ]:\n", + "[########################################] | 100% Completed | 7.93 ss\n", + "Extracting layer tree cover from Google Earth Engine for bbox [109.2868306 30.4891396 109.57764775 30.661744 ]:\n", + "[########################################] | 100% Completed | 5.93 ss\n", + "Extracting layer tree cover from Google Earth Engine for bbox [107.4199371 30.661744 107.7868306 30.8867283]:\n", + "[########################################] | 100% Completed | 9.34 ss\n", + "Extracting layer tree cover from Google Earth Engine for bbox [107.7868306 30.661744 108.2868306 31.161744 ]:\n", + "[########################################] | 100% Completed | 32.03 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [108.2868306 30.661744 108.7868306 31.161744 ]:\n", + "[########################################] | 100% Completed | 23.36 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [108.7868306 30.661744 109.2868306 31.161744 ]:\n", + "[########################################] | 100% Completed | 27.49 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [109.2868306 30.661744 109.7868306 31.161744 ]:\n", + "[########################################] | 100% Completed | 20.50 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [109.7868306 30.8014232 110.19443696 31.161744 ]:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[########################################] | 100% Completed | 12.80 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [108.0165755 31.161744 108.2868306 31.5102425]:\n", + "[########################################] | 100% Completed | 8.01 ss\n", + "Extracting layer tree cover from Google Earth Engine for bbox [108.2868306 31.161744 108.7868306 31.661744 ]:\n", + "[########################################] | 100% Completed | 20.00 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [108.7868306 31.161744 109.2868306 31.661744 ]:\n", + "[########################################] | 100% Completed | 20.48 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [109.2868306 31.161744 109.7868306 31.661744 ]:\n", + "[########################################] | 100% Completed | 20.45 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [109.7868306 31.161744 110.1944429 31.557555 ]:\n", + "[########################################] | 100% Completed | 14.45 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [108.261167 31.9129007 108.2868306 31.99227509]:\n", + "[########################################] | 100% Completed | 624.92 ms\n", + "Extracting layer tree cover from Google Earth Engine for bbox [108.2868306 31.661744 108.7868306 32.161744 ]:\n", + "[########################################] | 100% Completed | 20.89 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [108.7868306 31.661744 109.2868306 32.04943337]:\n", + "[########################################] | 100% Completed | 16.21 s\n", + "Extracting layer tree cover from Google Earth Engine for bbox [109.2868306 31.661744 109.73672832 31.7328027 ]:\n", + "[########################################] | 100% Completed | 3.26 ss\n", + "Extracting layer tree cover from Google Earth Engine for bbox [108.3644628 32.161744 108.58891065 32.2026956 ]:\n", + "[########################################] | 100% Completed | 1.27 sms\n", + "CPU times: user 14min 19s, sys: 2min 41s, total: 17min\n", + "Wall time: 21min 18s\n" ] }, { @@ -374,30 +438,30 @@ " \n", " \n", " 0\n", - " BRA-Salvador_ADM4-union_1\n", - " ADM4-union\n", - " BRA-Salvador\n", - " BRA-Salvador\n", - " 2022-08-03\n", - " MULTIPOLYGON (((-38.50135 -13.01134, -38.50140...\n", - " 0.283668\n", + " CHN-Chongqing_ADM-1_1\n", + " ADM-1\n", + " CHN-Chongqing\n", + " CHN-Chongqing\n", + " 2022-06-27\n", + " MULTIPOLYGON (((108.50540 32.20270, 108.49620 ...\n", + " 0.586797\n", " \n", " \n", "\n", "" ], "text/plain": [ - " geo_id geo_level geo_name geo_parent_name \\\n", - "0 BRA-Salvador_ADM4-union_1 ADM4-union BRA-Salvador BRA-Salvador \n", + " geo_id geo_level geo_name geo_parent_name \\\n", + "0 CHN-Chongqing_ADM-1_1 ADM-1 CHN-Chongqing CHN-Chongqing \n", "\n", " creation_date geometry \\\n", - "0 2022-08-03 MULTIPOLYGON (((-38.50135 -13.01134, -38.50140... \n", + "0 2022-06-27 MULTIPOLYGON (((108.50540 32.20270, 108.49620 ... \n", "\n", " mean_tree_cover \n", - "0 0.283668 " + "0 0.586797 " ] }, - "execution_count": 9, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } From 307398923e787aacc62def1a600d905e285542db Mon Sep 17 00:00:00 2001 From: Justin Terry Date: Tue, 20 Feb 2024 16:03:07 -0800 Subject: [PATCH 2/5] Allow grouping by a categorical layer --- city_metrix/layers/layer.py | 24 +++++++++++++++++------ tests/conftest.py | 38 ++++++++++++++++++++++++++++++++++++- tests/layers.py | 14 ++++++++++++-- 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/city_metrix/layers/layer.py b/city_metrix/layers/layer.py index 3aebbba..b632bb9 100644 --- a/city_metrix/layers/layer.py +++ b/city_metrix/layers/layer.py @@ -68,12 +68,21 @@ def count(self): def _zonal_stats(self, stats_func): if box(*self.zones.total_bounds).area <= MAX_TILE_SIZE**2: - stats = self._zonal_stats_tile(self.zones, [stats_func])[stats_func] + stats = self._zonal_stats_tile(self.zones, [stats_func]) else: stats = self._zonal_stats_fishnet(stats_func) if self.layer is not None: - pass + stats["layer"] = stats["zone"].astype("uint32").values >> 16 + stats["zone"] = stats["zone"].astype("uint32").values & 65535 + + stats = stats.groupby("zone").apply( + lambda df: df.drop(columns="zone").groupby("layer").sum().to_dict()[stats_func] + ) + + return stats + + return stats[stats_func] def _zonal_stats_fishnet(self, stats_func): # fishnet GeoDataFrame into smaller tiles @@ -101,8 +110,9 @@ def _zonal_stats_fishnet(self, stats_func): ]) aggregated = tile_stats.groupby("zone").apply(_aggregate_stats, stats_func) + aggregated.name = stats_func - return aggregated + return aggregated.reset_index() def _zonal_stats_tile(self, tile_gdf, stats_func): bbox = tile_gdf.total_bounds @@ -112,18 +122,20 @@ def _zonal_stats_tile(self, tile_gdf, stats_func): # align to highest resolution raster, which should be the largest raster # since all are clipped to the extent - raster_data = [data for data in mask_datum + [aggregate_data] + layer_data if isinstance(data, xr.DataArray)] + raster_data = [data for data in mask_datum + [aggregate_data] + [layer_data] if isinstance(data, xr.DataArray)] align_to = sorted(raster_data, key=lambda data: data.size, reverse=True).pop() aggregate_data = self._align(aggregate_data, align_to) mask_datum = [self._align(data, align_to) for data in mask_datum] - layer_data = self._align(layer_data, align_to) if layer_data is not None else None + + if self.layer is not None: + layer_data = self._align(layer_data, align_to) for mask in mask_datum: aggregate_data = aggregate_data.where(~np.isnan(mask)) zones = self._rasterize(tile_gdf, align_to) - if layer_data is not None: + if self.layer is not None: zones = zones + (layer_data.astype("uint32") << 16) stats = zonal_stats(zones, aggregate_data, stats_funcs=stats_func) diff --git a/tests/conftest.py b/tests/conftest.py index 7e935f8..b56318c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -34,6 +34,7 @@ def create_fishnet_grid(min_x, min_y, max_x, max_y, cell_size): ZONES = create_fishnet_grid(106.7, -6.3, 106.8, -6.2, 0.01).reset_index() LARGE_ZONES = create_fishnet_grid(106, -7, 107, -6, 0.1).reset_index() + class MockLayer(Layer): """ Simple mock layer that just rasterizes the zones @@ -66,6 +67,23 @@ def get_data(self, bbox): return mask +class MockGroupByLayer(Layer): + """ + Simple categorical layer with alternating 1s and 2s + """ + def get_data(self, bbox): + group_by_gdf = create_fishnet_grid(*bbox, 0.001).reset_index() + group_by_gdf['index'] = (group_by_gdf['index'] % 2) + 1 + group_by = make_geocube( + vector_data=group_by_gdf, + measurements=['index'], + resolution=(0.001, 0.001), + output_crs=4326, + ).index + + return group_by + + class MockLargeLayer(Layer): """ Simple mock layer that just rasterizes the zones @@ -77,4 +95,22 @@ def get_data(self, bbox): resolution=(0.01, 0.01), output_crs=4326, ).index - return arr \ No newline at end of file + return arr + + +class MockLargeGroupByLayer(Layer): + """ + Large categorical layer with alternating 1s and 2s + """ + + def get_data(self, bbox): + group_by_gdf = create_fishnet_grid(*bbox, 0.01).reset_index() + group_by_gdf['index'] = (group_by_gdf['index'] % 2) + 1 + group_by = make_geocube( + vector_data=group_by_gdf, + measurements=['index'], + resolution=(0.01, 0.01), + output_crs=4326, + ).index + + return group_by \ No newline at end of file diff --git a/tests/layers.py b/tests/layers.py index 9318518..960de2c 100644 --- a/tests/layers.py +++ b/tests/layers.py @@ -2,8 +2,8 @@ from city_metrix.layers import LandsatCollection2, Albedo, LandSurfaceTemperature, EsaWorldCover, EsaWorldCoverClass, TreeCover from city_metrix.layers.layer import get_image_collection -from .conftest import MockLayer, MockMaskLayer, ZONES, LARGE_ZONES, MockLargeLayer - +from .conftest import MockLayer, MockMaskLayer, ZONES, LARGE_ZONES, MockLargeLayer, MockGroupByLayer, \ + MockLargeGroupByLayer import pytest import numpy as np @@ -43,6 +43,16 @@ def test_masks(): assert count == 100 +def test_group_by_layer(): + counts = MockLayer().groupby(ZONES, layer=MockGroupByLayer()).count() + assert all([count == {1: 50.0, 2: 50.0} for count in counts]) + + +def test_group_by_large_layer(): + counts = MockLargeLayer().groupby(LARGE_ZONES, layer=MockLargeGroupByLayer()).count() + assert all([count == {1: 50.0, 2: 50.0} for count in counts]) + + SAMPLE_BBOX = (-38.35530428121955, -12.821710300686393, -38.33813814352424, -12.80363249765361) From 16b7349b5cb2bff8190d57c4a909e9b2256f5dbd Mon Sep 17 00:00:00 2001 From: Justin Terry Date: Tue, 20 Feb 2024 16:08:47 -0800 Subject: [PATCH 3/5] Revert notebook --- notebooks/tutorial/compute indicators.ipynb | 2516 +++++++++---------- 1 file changed, 1243 insertions(+), 1273 deletions(-) diff --git a/notebooks/tutorial/compute indicators.ipynb b/notebooks/tutorial/compute indicators.ipynb index 1bb532e..c95d53a 100644 --- a/notebooks/tutorial/compute indicators.ipynb +++ b/notebooks/tutorial/compute indicators.ipynb @@ -1,1276 +1,5 @@ { - "cells": [ - { - "cell_type": "markdown", - "id": "cdd83ce4-8281-496b-a27e-c17527387fec", - "metadata": {}, - "source": [ - "# Introduction" - ] - }, - { - "cell_type": "markdown", - "id": "9e3282ba-378a-4272-b6ca-dedf965e4172", - "metadata": {}, - "source": [ - "This tutorial provides instructions on nhow to use `city_metrix` to calcaulte indicators based on user-specific geometry." - ] - }, - { - "cell_type": "markdown", - "id": "6f77de84-d7a9-4377-afd5-9cb265112718", - "metadata": {}, - "source": [ - "Every 'indicator' is defined as separate python function:\n", - "\n", - "| Indicator name | function name | Parameters | Method |\n", - "| ---- | ---- | ---- | ---- |\n", - "| Average tree cover | `mean_tree_cover()` | | |\n", - "| Percent of built land without tree cover | `built_land_without_tree_cover()` | | |" - ] - }, - { - "cell_type": "markdown", - "id": "48a31f86-c031-4347-be1d-e631ea0f636e", - "metadata": { - "tags": [] - }, - "source": [ - "# Setting" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "dcc7a7b9-cf15-442c-acfd-fef1806ad05e", - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", - "import geopandas as gpd\n", - "from rasterio.plot import show\n", - "import rasterio\n", - "import pandas as pd" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "6bcc8215-0c19-45bd-8265-2be0c2a83c6b", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'/Users/jt/dev/cities-cif'" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# update the wd path to be able to laod the module\n", - "os.chdir('../..') \n", - "os.getcwd()" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "4ce04cdf-634c-4c9c-8d20-1e2a3caffac2", - "metadata": {}, - "outputs": [], - "source": [ - "os.environ['GCS_BUCKET']='gee-exports'\n", - "os.environ['GOOGLE_APPLICATION_USER']='developers@citiesindicators.iam.gserviceaccount.com'\n", - "os.environ['GOOGLE_APPLICATION_CREDENTIALS']='C:\\\\Users\\Saif.Shabou\\OneDrive - World Resources Institute\\Documents\\cities-indicators-framework\\citymetrix\\credentials-citiesindicators.json'" - ] - }, - { - "cell_type": "markdown", - "id": "67f86f26-b888-4cc2-b500-47ebe9458609", - "metadata": { - "tags": [] - }, - "source": [ - "# Get boundaries" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "acbc5aaa-5f47-44ff-b9ab-b885c06d7424", - "metadata": {}, - "outputs": [ - { - "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", - "
geo_idgeo_levelgeo_namegeo_parent_namecreation_dategeometry
0CHN-Chongqing_ADM-1_1ADM-1CHN-ChongqingCHN-Chongqing2022-06-27MULTIPOLYGON (((108.50540 32.20270, 108.49620 ...
\n", - "
" - ], - "text/plain": [ - " geo_id geo_level geo_name geo_parent_name \\\n", - "0 CHN-Chongqing_ADM-1_1 ADM-1 CHN-Chongqing CHN-Chongqing \n", - "\n", - " creation_date geometry \n", - "0 2022-06-27 MULTIPOLYGON (((108.50540 32.20270, 108.49620 ... " - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# load boundary of a region of interest\n", - "boundary_path = 's3://cities-indicators/data/boundaries/boundary-CHN-Chongqing-ADM1.geojson'\n", - "city_gdf_aoi = gpd.read_file(boundary_path, driver='GeoJSON')\n", - "city_gdf_aoi.head()" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "7af8657e-feed-42b8-b32a-d4ee5754cd4c", - "metadata": {}, - "outputs": [ - { - "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", - "
geo_idgeo_levelgeo_namegeo_parent_namecreation_dategeometry
0CHN-Chongqing_ADM-1_1ADM-1CHN-ChongqingCHN-Chongqing2022-06-27MULTIPOLYGON (((108.50540 32.20270, 108.49620 ...
\n", - "
" - ], - "text/plain": [ - " geo_id geo_level geo_name geo_parent_name \\\n", - "0 CHN-Chongqing_ADM-1_1 ADM-1 CHN-Chongqing CHN-Chongqing \n", - "\n", - " creation_date geometry \n", - "0 2022-06-27 MULTIPOLYGON (((108.50540 32.20270, 108.49620 ... " - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# load boundary of sub-georgaphy\n", - "boundary_path = 'https://cities-indicators.s3.eu-west-3.amazonaws.com/data/boundaries/boundary-BRA-Salvador-ADM4.geojson'\n", - "city_gdf_sub = gpd.read_file(boundary_path, driver='GeoJSON')\n", - "city_gdf_sub.head()" - ] - }, - { - "cell_type": "markdown", - "id": "788d5679-2fc5-4025-996c-25af1d35b900", - "metadata": { - "tags": [] - }, - "source": [ - "# LND-2 Average Tree Cover" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "8bdaa746-b071-4e9f-8959-c8c57921c73c", - "metadata": {}, - "outputs": [], - "source": [ - "from city_metrix.metrics import mean_tree_cover" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "e69f2674-dc78-4098-b423-0686944eabe8", - "metadata": {}, - "outputs": [], - "source": [ - "from city_metrix import mean_tree_cover" - ] - }, - { - "cell_type": "markdown", - "id": "7b71f6e7-9e83-4c88-8a98-9d30958a8dc4", - "metadata": {}, - "source": [ - "## City level - one city" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "8b85131c", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Input covers too much area, splitting into 55 tiles\n", - "Extracting layer tree cover from Google Earth Engine for bbox [106.2998057 28.4538074 106.7868306 28.661744 ]:\n", - "[########################################] | 100% Completed | 9.79 ss\n", - "Extracting layer tree cover from Google Earth Engine for bbox [106.7868306 28.5926236 106.86721119 28.661744 ]:\n", - "[########################################] | 100% Completed | 1.66 sms\n", - "Extracting layer tree cover from Google Earth Engine for bbox [108.37690815 28.1946548 108.7868306 28.661744 ]:\n", - "[########################################] | 100% Completed | 17.68 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [108.7868306 28.161744 109.2868306 28.661744 ]:\n", - "[########################################] | 100% Completed | 21.89 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [109.2868306 28.55722115 109.3171288 28.6316442 ]:\n", - "[########################################] | 100% Completed | 735.05 ms\n", - "Extracting layer tree cover from Google Earth Engine for bbox [105.722691 28.97837894 105.7868306 29.161744 ]:\n", - "[########################################] | 100% Completed | 1.85 sms\n", - "Extracting layer tree cover from Google Earth Engine for bbox [105.7868306 28.72981039 106.2868306 29.161744 ]:\n", - "[########################################] | 100% Completed | 40.28 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [106.2868306 28.661744 106.7868306 29.161744 ]:\n", - "[########################################] | 100% Completed | 22.10 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [106.7868306 28.661744 107.2868306 29.161744 ]:\n", - "[########################################] | 100% Completed | 20.55 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [107.2868306 28.8032582 107.7868306 29.161744 ]:\n", - "[########################################] | 100% Completed | 16.08 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [107.7868306 28.9628515 108.2868306 29.161744 ]:\n", - "[########################################] | 100% Completed | 8.98 ss\n", - "Extracting layer tree cover from Google Earth Engine for bbox [108.2868306 28.661744 108.7868306 29.161744 ]:\n", - "[########################################] | 100% Completed | 21.45 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [108.7868306 28.661744 109.2868306 29.161744 ]:\n", - "[########################################] | 100% Completed | 21.50 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [109.2868306 28.72059265 109.3156967 29.07002707]:\n", - "[########################################] | 100% Completed | 1.60 sms\n", - "Extracting layer tree cover from Google Earth Engine for bbox [105.2868306 29.161744 105.7868306 29.661744 ]:\n", - "[########################################] | 100% Completed | 21.90 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [105.7868306 29.161744 106.2868306 29.661744 ]:\n", - "[########################################] | 100% Completed | 22.21 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [106.2868306 29.161744 106.7868306 29.661744 ]:\n", - "[########################################] | 100% Completed | 21.73 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [106.7868306 29.161744 107.2868306 29.661744 ]:\n", - "[########################################] | 100% Completed | 21.42 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [107.2868306 29.161744 107.7868306 29.661744 ]:\n", - "[########################################] | 100% Completed | 21.01 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [107.7868306 29.161744 108.2868306 29.661744 ]:\n", - "[########################################] | 100% Completed | 20.76 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [108.2868306 29.161744 108.7868306 29.661744 ]:\n", - "[########################################] | 100% Completed | 21.54 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [108.7868306 29.161744 109.18802005 29.661744 ]:\n", - "[########################################] | 100% Completed | 18.26 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [105.3797864 29.661744 105.7868306 30.161744 ]:\n", - "[########################################] | 100% Completed | 18.11 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [105.7868306 29.661744 106.2868306 30.161744 ]:\n", - "[########################################] | 100% Completed | 23.68 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [106.2868306 29.661744 106.7868306 30.161744 ]:\n", - "[########################################] | 100% Completed | 22.08 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [106.7868306 29.661744 107.2868306 30.161744 ]:\n", - "[########################################] | 100% Completed | 22.07 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [107.2868306 29.661744 107.7868306 30.161744 ]:\n", - "[########################################] | 100% Completed | 21.64 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [107.7868306 29.661744 108.2868306 30.161744 ]:\n", - "[########################################] | 100% Completed | 21.21 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [108.2868306 29.661744 108.7868306 30.161744 ]:\n", - "[########################################] | 100% Completed | 20.56 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [108.7868306 29.661744 108.8312068 29.67418977]:\n", - "[########################################] | 100% Completed | 828.51 ms\n", - "Extracting layer tree cover from Google Earth Engine for bbox [105.533021 30.161744 105.7868306 30.41773367]:\n", - "[########################################] | 100% Completed | 6.52 ss\n", - "Extracting layer tree cover from Google Earth Engine for bbox [105.7868306 30.161744 106.2868306 30.4346008]:\n", - "[########################################] | 100% Completed | 13.04 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [106.2868306 30.161744 106.6643415 30.3068867]:\n", - "[########################################] | 100% Completed | 5.91 ss\n", - "Extracting layer tree cover from Google Earth Engine for bbox [107.16288016 30.161744 107.2868306 30.32415115]:\n", - "[########################################] | 100% Completed | 2.98 ss\n", - "Extracting layer tree cover from Google Earth Engine for bbox [107.2868306 30.161744 107.7868306 30.661744 ]:\n", - "[########################################] | 100% Completed | 28.51 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [107.7868306 30.161744 108.2868306 30.661744 ]:\n", - "[########################################] | 100% Completed | 22.57 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [108.2868306 30.161744 108.7868306 30.661744 ]:\n", - "[########################################] | 100% Completed | 24.58 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [108.7868306 30.4949047 109.2868306 30.661744 ]:\n", - "[########################################] | 100% Completed | 7.93 ss\n", - "Extracting layer tree cover from Google Earth Engine for bbox [109.2868306 30.4891396 109.57764775 30.661744 ]:\n", - "[########################################] | 100% Completed | 5.93 ss\n", - "Extracting layer tree cover from Google Earth Engine for bbox [107.4199371 30.661744 107.7868306 30.8867283]:\n", - "[########################################] | 100% Completed | 9.34 ss\n", - "Extracting layer tree cover from Google Earth Engine for bbox [107.7868306 30.661744 108.2868306 31.161744 ]:\n", - "[########################################] | 100% Completed | 32.03 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [108.2868306 30.661744 108.7868306 31.161744 ]:\n", - "[########################################] | 100% Completed | 23.36 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [108.7868306 30.661744 109.2868306 31.161744 ]:\n", - "[########################################] | 100% Completed | 27.49 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [109.2868306 30.661744 109.7868306 31.161744 ]:\n", - "[########################################] | 100% Completed | 20.50 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [109.7868306 30.8014232 110.19443696 31.161744 ]:\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[########################################] | 100% Completed | 12.80 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [108.0165755 31.161744 108.2868306 31.5102425]:\n", - "[########################################] | 100% Completed | 8.01 ss\n", - "Extracting layer tree cover from Google Earth Engine for bbox [108.2868306 31.161744 108.7868306 31.661744 ]:\n", - "[########################################] | 100% Completed | 20.00 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [108.7868306 31.161744 109.2868306 31.661744 ]:\n", - "[########################################] | 100% Completed | 20.48 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [109.2868306 31.161744 109.7868306 31.661744 ]:\n", - "[########################################] | 100% Completed | 20.45 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [109.7868306 31.161744 110.1944429 31.557555 ]:\n", - "[########################################] | 100% Completed | 14.45 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [108.261167 31.9129007 108.2868306 31.99227509]:\n", - "[########################################] | 100% Completed | 624.92 ms\n", - "Extracting layer tree cover from Google Earth Engine for bbox [108.2868306 31.661744 108.7868306 32.161744 ]:\n", - "[########################################] | 100% Completed | 20.89 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [108.7868306 31.661744 109.2868306 32.04943337]:\n", - "[########################################] | 100% Completed | 16.21 s\n", - "Extracting layer tree cover from Google Earth Engine for bbox [109.2868306 31.661744 109.73672832 31.7328027 ]:\n", - "[########################################] | 100% Completed | 3.26 ss\n", - "Extracting layer tree cover from Google Earth Engine for bbox [108.3644628 32.161744 108.58891065 32.2026956 ]:\n", - "[########################################] | 100% Completed | 1.27 sms\n", - "CPU times: user 14min 19s, sys: 2min 41s, total: 17min\n", - "Wall time: 21min 18s\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", - "
geo_idgeo_levelgeo_namegeo_parent_namecreation_dategeometrymean_tree_cover
0CHN-Chongqing_ADM-1_1ADM-1CHN-ChongqingCHN-Chongqing2022-06-27MULTIPOLYGON (((108.50540 32.20270, 108.49620 ...0.586797
\n", - "
" - ], - "text/plain": [ - " geo_id geo_level geo_name geo_parent_name \\\n", - "0 CHN-Chongqing_ADM-1_1 ADM-1 CHN-Chongqing CHN-Chongqing \n", - "\n", - " creation_date geometry \\\n", - "0 2022-06-27 MULTIPOLYGON (((108.50540 32.20270, 108.49620 ... \n", - "\n", - " mean_tree_cover \n", - "0 0.586797 " - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "%%time\n", - "\n", - "# for one geography\n", - "city_gdf_aoi[\"mean_tree_cover\"] = mean_tree_cover(city_gdf_aoi) \n", - "city_gdf_aoi" - ] - }, - { - "cell_type": "markdown", - "id": "ee63ca6a-a5c5-4ce7-82cd-3a35e52d3cd6", - "metadata": {}, - "source": [ - "## Sub-city level" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "71349065-4726-4a12-bbb8-1f998562c338", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Extracting tree cover layer in bbox [-38.6469891 -13.0171475 -38.30455157 -12.75628844]:\n", - "[########################################] | 100% Completed | 14.55 s\n", - "CPU times: total: 17.1 s\n", - "Wall time: 17.8 s\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", - "
geo_idgeo_levelgeo_namegeo_parent_namecreation_dategeometrymean_tree_cover
0BRA-Salvador_ADM4_1ADM4PituaçuBRA-Salvador2022-08-03MULTIPOLYGON (((-38.40125 -12.96457, -38.40126...0.523927
1BRA-Salvador_ADM4_2ADM4PatamaresBRA-Salvador2022-08-03MULTIPOLYGON (((-38.39898 -12.96269, -38.39898...0.433088
2BRA-Salvador_ADM4_3ADM4PiatãBRA-Salvador2022-08-03MULTIPOLYGON (((-38.37346 -12.93345, -38.37386...0.274104
3BRA-Salvador_ADM4_4ADM4Boca do RioBRA-Salvador2022-08-03MULTIPOLYGON (((-38.41741 -12.97578, -38.41746...0.157694
4BRA-Salvador_ADM4_5ADM4Jardim ArmaçãoBRA-Salvador2022-08-03MULTIPOLYGON (((-38.43383 -12.98742, -38.43386...0.210874
\n", - "
" - ], - "text/plain": [ - " geo_id geo_level geo_name geo_parent_name \\\n", - "0 BRA-Salvador_ADM4_1 ADM4 Pituaçu BRA-Salvador \n", - "1 BRA-Salvador_ADM4_2 ADM4 Patamares BRA-Salvador \n", - "2 BRA-Salvador_ADM4_3 ADM4 Piatã BRA-Salvador \n", - "3 BRA-Salvador_ADM4_4 ADM4 Boca do Rio BRA-Salvador \n", - "4 BRA-Salvador_ADM4_5 ADM4 Jardim Armação BRA-Salvador \n", - "\n", - " creation_date geometry \\\n", - "0 2022-08-03 MULTIPOLYGON (((-38.40125 -12.96457, -38.40126... \n", - "1 2022-08-03 MULTIPOLYGON (((-38.39898 -12.96269, -38.39898... \n", - "2 2022-08-03 MULTIPOLYGON (((-38.37346 -12.93345, -38.37386... \n", - "3 2022-08-03 MULTIPOLYGON (((-38.41741 -12.97578, -38.41746... \n", - "4 2022-08-03 MULTIPOLYGON (((-38.43383 -12.98742, -38.43386... \n", - "\n", - " mean_tree_cover \n", - "0 0.523927 \n", - "1 0.433088 \n", - "2 0.274104 \n", - "3 0.157694 \n", - "4 0.210874 " - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "%%time\n", - "\n", - "# for sub geographies\n", - "city_gdf_sub[\"mean_tree_cover\"] = mean_tree_cover(city_gdf_sub) \n", - "city_gdf_sub.head()" - ] - }, - { - "cell_type": "markdown", - "id": "6580e18f-50b1-499c-adb7-9ed604c33e81", - "metadata": {}, - "source": [ - "## Multiple cities" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "5006d2d3-6ef0-4460-b127-f72522e94585", - "metadata": {}, - "outputs": [ - { - "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", - "
geo_idgeo_levelgeo_namegeo_parent_namecreation_dategeometry
0BRA-Salvador_ADM4-union_1ADM4-unionBRA-SalvadorBRA-Salvador2022-08-03MULTIPOLYGON (((-38.50135 -13.01134, -38.50140...
1IDN-Jakarta_ADM-4-union_1ADM-4-unionIDN-JakartaIDN-Jakarta2022-06-27MULTIPOLYGON (((106.78141 -6.31616, 106.78124 ...
\n", - "
" - ], - "text/plain": [ - " geo_id geo_level geo_name geo_parent_name \\\n", - "0 BRA-Salvador_ADM4-union_1 ADM4-union BRA-Salvador BRA-Salvador \n", - "1 IDN-Jakarta_ADM-4-union_1 ADM-4-union IDN-Jakarta IDN-Jakarta \n", - "\n", - " creation_date geometry \n", - "0 2022-08-03 MULTIPOLYGON (((-38.50135 -13.01134, -38.50140... \n", - "1 2022-06-27 MULTIPOLYGON (((106.78141 -6.31616, 106.78124 ... " - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Calculate indicator for multiple regions of interest\n", - "\n", - "boundary_path = 'https://cities-indicators.s3.eu-west-3.amazonaws.com/data/boundaries/boundary-BRA-Salvador-ADM4union.geojson'\n", - "city_Salvador = gpd.read_file(boundary_path, driver='GeoJSON')\n", - "\n", - "boundary_path = 'https://cities-indicators.s3.eu-west-3.amazonaws.com/data/boundaries/boundary-IDN-Jakarta-ADM4union.geojson'\n", - "city_Jakarta = gpd.read_file(boundary_path, driver='GeoJSON')\n", - "\n", - "cities = pd.concat([city_Salvador, city_Jakarta])\n", - "cities = cities.reset_index(drop=True)\n", - "cities" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "47791e3e-3761-404e-96a5-26493cc8ef54", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Extracting tree cover layer in bbox [-38.6469891 -13.0171475 -38.30455157 -12.75628844]:\n", - "[########################################] | 100% Completed | 16.77 s\n", - "Extracting tree cover layer in bbox [106.685589 -6.3650478 106.8530109 -6.0891749]:\n", - "[########################################] | 100% Completed | 9.38 ss\n", - "Extracting tree cover layer in bbox [106.8530109 -6.3744575 106.973975 -6.0895823]:\n", - "[########################################] | 100% Completed | 8.75 ss\n" - ] - } - ], - "source": [ - "cities[\"mean_tree_cover\"] = mean_tree_cover(cities)" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "d58a38fd-adea-4961-b0f8-abac0d0943cf", - "metadata": {}, - "outputs": [ - { - "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", - "
geo_idgeo_levelgeo_namegeo_parent_namecreation_dategeometrymean_tree_cover
0BRA-Salvador_ADM4-union_1ADM4-unionBRA-SalvadorBRA-Salvador2022-08-03MULTIPOLYGON (((-38.50135 -13.01134, -38.50140...0.283667
1IDN-Jakarta_ADM-4-union_1ADM-4-unionIDN-JakartaIDN-Jakarta2022-06-27MULTIPOLYGON (((106.78141 -6.31616, 106.78124 ...0.159044
\n", - "
" - ], - "text/plain": [ - " geo_id geo_level geo_name geo_parent_name \\\n", - "0 BRA-Salvador_ADM4-union_1 ADM4-union BRA-Salvador BRA-Salvador \n", - "1 IDN-Jakarta_ADM-4-union_1 ADM-4-union IDN-Jakarta IDN-Jakarta \n", - "\n", - " creation_date geometry \\\n", - "0 2022-08-03 MULTIPOLYGON (((-38.50135 -13.01134, -38.50140... \n", - "1 2022-06-27 MULTIPOLYGON (((106.78141 -6.31616, 106.78124 ... \n", - "\n", - " mean_tree_cover \n", - "0 0.283667 \n", - "1 0.159044 " - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "cities" - ] - }, - { - "cell_type": "markdown", - "id": "8aa3590f-b78e-4c57-872b-c5056266ff1a", - "metadata": { - "tags": [] - }, - "source": [ - "# HEA-4 Percent of built land without tree cover" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "id": "e5948b81-cb27-4c97-9567-a297e078cdb5", - "metadata": {}, - "outputs": [], - "source": [ - "from city_metrix.metrics import built_land_without_tree_cover" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "id": "10e89534-19d7-4dba-bc98-d4f3dd8b95f4", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Extracting ESA world cover layer in bbox [-38.6469891 -13.0171475 -38.30455157 -12.75628844]:\n", - "[########################################] | 100% Completed | 9.68 ss\n" - ] - }, - { - "ename": "UnboundLocalError", - "evalue": "local variable 'stats_funcs_dict' referenced before assignment", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mUnboundLocalError\u001b[0m Traceback (most recent call last)", - "File \u001b[1;32m:2\u001b[0m\n", - "File \u001b[1;32m~\\OneDrive - World Resources Institute\\Documents\\cities-indicators-framework\\citymetrix\\cities-cif\\city_metrix\\metrics\\built_land_without_tree_cover.py:16\u001b[0m, in \u001b[0;36mbuilt_land_without_tree_cover\u001b[1;34m(zones)\u001b[0m\n\u001b[0;32m 13\u001b[0m built_up_land \u001b[38;5;241m=\u001b[39m EsaWorldCover(land_cover_class\u001b[38;5;241m=\u001b[39mEsaWorldCoverClass\u001b[38;5;241m.\u001b[39mBUILT_UP)\n\u001b[0;32m 14\u001b[0m tree_cover \u001b[38;5;241m=\u001b[39m TreeCover(min_tree_cover\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m)\n\u001b[1;32m---> 16\u001b[0m built_land \u001b[38;5;241m=\u001b[39m \u001b[43mbuilt_up_land\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgroupby\u001b[49m\u001b[43m(\u001b[49m\u001b[43mzones\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcount\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 17\u001b[0m tree_cover_in_built_land \u001b[38;5;241m=\u001b[39m tree_cover\u001b[38;5;241m.\u001b[39mmask(built_up_land)\u001b[38;5;241m.\u001b[39mgroupby(zones)\u001b[38;5;241m.\u001b[39mcount()\n\u001b[0;32m 19\u001b[0m percent_tree_cover_in_built_up_land \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m \u001b[38;5;241m-\u001b[39m (tree_cover_in_built_land\u001b[38;5;241m.\u001b[39mfillna(\u001b[38;5;241m0\u001b[39m) \u001b[38;5;241m/\u001b[39m built_land)\n", - "File \u001b[1;32m~\\OneDrive - World Resources Institute\\Documents\\cities-indicators-framework\\citymetrix\\cities-cif\\city_metrix\\layers\\layer.py:64\u001b[0m, in \u001b[0;36mLayerGroupBy.count\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 63\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcount\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[1;32m---> 64\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_zonal_stats\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcount\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", - "File \u001b[1;32m~\\OneDrive - World Resources Institute\\Documents\\cities-indicators-framework\\citymetrix\\cities-cif\\city_metrix\\layers\\layer.py:68\u001b[0m, in \u001b[0;36mLayerGroupBy._zonal_stats\u001b[1;34m(self, stats_func)\u001b[0m\n\u001b[0;32m 66\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_zonal_stats\u001b[39m(\u001b[38;5;28mself\u001b[39m, stats_func):\n\u001b[0;32m 67\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m box(\u001b[38;5;241m*\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mzones\u001b[38;5;241m.\u001b[39mtotal_bounds)\u001b[38;5;241m.\u001b[39marea \u001b[38;5;241m<\u001b[39m\u001b[38;5;241m=\u001b[39m MAX_TILE_SIZE\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m2\u001b[39m:\n\u001b[1;32m---> 68\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_zonal_stats_tile\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mzones\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstats_func\u001b[49m\u001b[43m)\u001b[49m[stats_func]\n\u001b[0;32m 69\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m 70\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_zonal_stats_fishnet(stats_func)\n", - "File \u001b[1;32m~\\OneDrive - World Resources Institute\\Documents\\cities-indicators-framework\\citymetrix\\cities-cif\\city_metrix\\layers\\layer.py:116\u001b[0m, in \u001b[0;36mLayerGroupBy._zonal_stats_tile\u001b[1;34m(self, tile_gdf, stats_func)\u001b[0m\n\u001b[0;32m 113\u001b[0m aggregate_data \u001b[38;5;241m=\u001b[39m aggregate_data\u001b[38;5;241m.\u001b[39mwhere(\u001b[38;5;241m~\u001b[39mnp\u001b[38;5;241m.\u001b[39misnan(mask))\n\u001b[0;32m 115\u001b[0m zones \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_rasterize(tile_gdf, align_to)\n\u001b[1;32m--> 116\u001b[0m stats \u001b[38;5;241m=\u001b[39m \u001b[43mzonal_stats\u001b[49m\u001b[43m(\u001b[49m\u001b[43mzones\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maggregate_data\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstats_funcs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mstats_func\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 118\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m stats\n", - "File \u001b[1;32m~\\anaconda3\\envs\\citymetrix_env\\lib\\site-packages\\xrspatial\\zonal.py:579\u001b[0m, in \u001b[0;36mstats\u001b[1;34m(zones, values, zone_ids, stats_funcs, nodata_values, return_type)\u001b[0m\n\u001b[0;32m 568\u001b[0m stats_funcs_dict \u001b[38;5;241m=\u001b[39m stats_funcs\u001b[38;5;241m.\u001b[39mcopy()\n\u001b[0;32m 570\u001b[0m mapper \u001b[38;5;241m=\u001b[39m ArrayTypeFunctionMapping(\n\u001b[0;32m 571\u001b[0m numpy_func\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mlambda\u001b[39;00m \u001b[38;5;241m*\u001b[39margs: _stats_numpy(\u001b[38;5;241m*\u001b[39margs, return_type\u001b[38;5;241m=\u001b[39mreturn_type),\n\u001b[0;32m 572\u001b[0m dask_func\u001b[38;5;241m=\u001b[39m_stats_dask_numpy,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 576\u001b[0m ),\n\u001b[0;32m 577\u001b[0m )\n\u001b[0;32m 578\u001b[0m result \u001b[38;5;241m=\u001b[39m mapper(values)(\n\u001b[1;32m--> 579\u001b[0m zones\u001b[38;5;241m.\u001b[39mdata, values\u001b[38;5;241m.\u001b[39mdata, zone_ids, \u001b[43mstats_funcs_dict\u001b[49m, nodata_values,\n\u001b[0;32m 580\u001b[0m )\n\u001b[0;32m 582\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m return_type \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mxarray.DataArray\u001b[39m\u001b[38;5;124m'\u001b[39m:\n\u001b[0;32m 583\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m xr\u001b[38;5;241m.\u001b[39mDataArray(\n\u001b[0;32m 584\u001b[0m result,\n\u001b[0;32m 585\u001b[0m coords\u001b[38;5;241m=\u001b[39m{\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mstats\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;28mlist\u001b[39m(stats_funcs_dict\u001b[38;5;241m.\u001b[39mkeys()), \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mvalues\u001b[38;5;241m.\u001b[39mcoords},\n\u001b[0;32m 586\u001b[0m dims\u001b[38;5;241m=\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mstats\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;241m*\u001b[39mvalues\u001b[38;5;241m.\u001b[39mdims),\n\u001b[0;32m 587\u001b[0m attrs\u001b[38;5;241m=\u001b[39mvalues\u001b[38;5;241m.\u001b[39mattrs\n\u001b[0;32m 588\u001b[0m )\n", - "\u001b[1;31mUnboundLocalError\u001b[0m: local variable 'stats_funcs_dict' referenced before assignment" - ] - } - ], - "source": [ - "%%time\n", - "\n", - "# for one geography\n", - "city_gdf_aoi[\"built_land_without_tree_cover\"] = built_land_without_tree_cover(city_gdf_aoi) \n", - "city_gdf_aoi" - ] - }, - { - "cell_type": "markdown", - "id": "afbb60b1-437f-4af5-99fe-9679559b250b", - "metadata": {}, - "source": [ - "# HEA-3 Built land with Low Surface reflectivity" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "id": "3c61a81e-f575-4d24-863d-ca9b7be34ad5", - "metadata": {}, - "outputs": [], - "source": [ - "from city_metrix.metrics import built_land_with_low_surface_reflectivity" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "id": "748d1d72-c4fe-4c6c-91f6-99dac052bdd8", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Extracting ESA world cover layer:\n", - "[########################################] | 100% Completed | 8.21 ss\n", - "Calculating albedo layer:\n", - "[########################################] | 100% Completed | 84.36 s\n", - "Extracting ESA world cover layer:\n", - "[########################################] | 100% Completed | 8.32 ss\n", - "CPU times: total: 30.5 s\n", - "Wall time: 1min 49s\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", - "
geo_idgeo_levelgeo_namegeo_parent_namecreation_dategeometrymean_tree_coverbuilt_land_without_tree_coverbuilt_land_with_low_surface_reflectivity
0BRA-Salvador_ADM4-union_1ADM4-unionBRA-SalvadorBRA-Salvador2022-08-03MULTIPOLYGON (((-38.50135 -13.01134, -38.50140...28.3667340.9073820.786552
\n", - "
" - ], - "text/plain": [ - " geo_id geo_level geo_name geo_parent_name \\\n", - "0 BRA-Salvador_ADM4-union_1 ADM4-union BRA-Salvador BRA-Salvador \n", - "\n", - " creation_date geometry \\\n", - "0 2022-08-03 MULTIPOLYGON (((-38.50135 -13.01134, -38.50140... \n", - "\n", - " mean_tree_cover built_land_without_tree_cover \\\n", - "0 28.366734 0.907382 \n", - "\n", - " built_land_with_low_surface_reflectivity \n", - "0 0.786552 " - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "%%time\n", - "\n", - "city_gdf_aoi[\"built_land_with_low_surface_reflectivity\"] = built_land_with_low_surface_reflectivity(city_gdf_aoi) \n", - "city_gdf_aoi" - ] - }, - { - "cell_type": "markdown", - "id": "bbfcabaa-2acc-4b9b-bdce-27b415e04354", - "metadata": {}, - "source": [ - "# HEA-2 Built Land With High LST" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "id": "6df5d7a3-1c25-49ac-ab33-9081722168c9", - "metadata": {}, - "outputs": [], - "source": [ - "from city_metrix.metrics import built_land_with_high_land_surface_temperature" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "id": "5288d4e6-e35d-48f4-9ff7-2d84c857b21a", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Extracting ESA world cover layer:\n", - "[########################################] | 100% Completed | 8.65 ss\n", - "Calculating land surface temperature layer:\n", - "[########################################] | 100% Completed | 2.73 ss\n", - "Extracting ESA world cover layer:\n", - "[########################################] | 100% Completed | 9.16 ss\n", - "CPU times: total: 14.8 s\n", - "Wall time: 37.7 s\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", - "
geo_idgeo_levelgeo_namegeo_parent_namecreation_dategeometrymean_tree_coverbuilt_land_without_tree_coverbuilt_land_with_low_surface_reflectivitybuilt_land_with_high_land_surface_temperature
0BRA-Salvador_ADM4-union_1ADM4-unionBRA-SalvadorBRA-Salvador2022-08-03MULTIPOLYGON (((-38.50135 -13.01134, -38.50140...28.3667340.9073820.7865520.0961
\n", - "
" - ], - "text/plain": [ - " geo_id geo_level geo_name geo_parent_name \\\n", - "0 BRA-Salvador_ADM4-union_1 ADM4-union BRA-Salvador BRA-Salvador \n", - "\n", - " creation_date geometry \\\n", - "0 2022-08-03 MULTIPOLYGON (((-38.50135 -13.01134, -38.50140... \n", - "\n", - " mean_tree_cover built_land_without_tree_cover \\\n", - "0 28.366734 0.907382 \n", - "\n", - " built_land_with_low_surface_reflectivity \\\n", - "0 0.786552 \n", - "\n", - " built_land_with_high_land_surface_temperature \n", - "0 0.0961 " - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "%%time\n", - "\n", - "city_gdf_aoi[\"built_land_with_high_land_surface_temperature\"] = built_land_with_high_land_surface_temperature(city_gdf_aoi) \n", - "city_gdf_aoi" - ] - }, - { - "cell_type": "markdown", - "id": "feb93688-9435-47a5-be31-0a806dd48fe1", - "metadata": {}, - "source": [ - "# Percent of Natural areas" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "id": "aee87e06-d16d-4017-a5e1-7fd8cef906d4", - "metadata": {}, - "outputs": [], - "source": [ - "from city_metrix.metrics import natural_areas" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "026261af-a18d-4c0c-bdb8-b11a3c192ddc", - "metadata": {}, - "outputs": [], - "source": [ - "%%time\n", - "\n", - "# for one geography\n", - "city_gdf_aoi[\"natural_areas\"] = natural_areas(city_gdf_aoi) \n", - "city_gdf_aoi" - ] - }, - { - "cell_type": "markdown", - "id": "b05cef42-67cd-4cdc-a7db-5f34ddd7837b", - "metadata": {}, - "source": [ - "# Open Space in built up land" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "id": "b4281469-9ca5-4b2c-a308-32f2e2fd5404", - "metadata": {}, - "outputs": [], - "source": [ - "from city_metrix.metrics import urban_open_space" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "id": "db612f31-eb82-4a10-9433-0907f0a64a0e", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Extracting ESA world cover layer:\n", - "[########################################] | 100% Completed | 12.48 s\n", - "Extracting ESA world cover layer:\n", - "[########################################] | 100% Completed | 9.08 ss\n", - "CPU times: total: 24 s\n", - "Wall time: 31.1 s\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", - "
geo_idgeo_levelgeo_namegeo_parent_namecreation_dategeometrymean_tree_coverbuilt_land_without_tree_coverbuilt_land_with_low_surface_reflectivitybuilt_land_with_high_land_surface_temperatureurban_open_space
0BRA-Salvador_ADM4-union_1ADM4-unionBRA-SalvadorBRA-Salvador2022-08-03MULTIPOLYGON (((-38.50135 -13.01134, -38.50140...28.3667340.9073820.7865520.09610.028378
\n", - "
" - ], - "text/plain": [ - " geo_id geo_level geo_name geo_parent_name \\\n", - "0 BRA-Salvador_ADM4-union_1 ADM4-union BRA-Salvador BRA-Salvador \n", - "\n", - " creation_date geometry \\\n", - "0 2022-08-03 MULTIPOLYGON (((-38.50135 -13.01134, -38.50140... \n", - "\n", - " mean_tree_cover built_land_without_tree_cover \\\n", - "0 28.366734 0.907382 \n", - "\n", - " built_land_with_low_surface_reflectivity \\\n", - "0 0.786552 \n", - "\n", - " built_land_with_high_land_surface_temperature urban_open_space \n", - "0 0.0961 0.028378 " - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "%%time\n", - "\n", - "# for one geography\n", - "city_gdf_aoi[\"urban_open_space\"] = urban_open_space(city_gdf_aoi) \n", - "city_gdf_aoi" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "951151f1-e0f5-4a8a-99a7-19a6f7c84591", - "metadata": {}, - "outputs": [], - "source": [] - } - ], + "cells": [], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", @@ -1288,8 +17,1249 @@ "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.13" + }, + "pycharm": { + "stem_cell": { + "cell_type": "raw", + "source": [ + "{\n", + " \"cells\": [\n", + " {\n", + " \"cell_type\": \"markdown\",\n", + " \"id\": \"cdd83ce4-8281-496b-a27e-c17527387fec\",\n", + " \"metadata\": {},\n", + " \"source\": [\n", + " \"# Introduction\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"markdown\",\n", + " \"id\": \"9e3282ba-378a-4272-b6ca-dedf965e4172\",\n", + " \"metadata\": {},\n", + " \"source\": [\n", + " \"This tutorial provides instructions on nhow to use `city_metrix` to calcaulte indicators based on user-specific geometry.\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"markdown\",\n", + " \"id\": \"6f77de84-d7a9-4377-afd5-9cb265112718\",\n", + " \"metadata\": {},\n", + " \"source\": [\n", + " \"Every 'indicator' is defined as separate python function:\\n\",\n", + " \"\\n\",\n", + " \"| Indicator name | function name | Parameters | Method |\\n\",\n", + " \"| ---- | ---- | ---- | ---- |\\n\",\n", + " \"| Average tree cover | `mean_tree_cover()` | | |\\n\",\n", + " \"| Percent of built land without tree cover | `built_land_without_tree_cover()` | | |\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"markdown\",\n", + " \"id\": \"48a31f86-c031-4347-be1d-e631ea0f636e\",\n", + " \"metadata\": {\n", + " \"tags\": []\n", + " },\n", + " \"source\": [\n", + " \"# Setting\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 1,\n", + " \"id\": \"dcc7a7b9-cf15-442c-acfd-fef1806ad05e\",\n", + " \"metadata\": {},\n", + " \"outputs\": [],\n", + " \"source\": [\n", + " \"import os\\n\",\n", + " \"import geopandas as gpd\\n\",\n", + " \"from rasterio.plot import show\\n\",\n", + " \"import rasterio\\n\",\n", + " \"import pandas as pd\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 2,\n", + " \"id\": \"6bcc8215-0c19-45bd-8265-2be0c2a83c6b\",\n", + " \"metadata\": {},\n", + " \"outputs\": [\n", + " {\n", + " \"data\": {\n", + " \"text/plain\": [\n", + " \"'/Users/jt/dev/cities-cif'\"\n", + " ]\n", + " },\n", + " \"execution_count\": 2,\n", + " \"metadata\": {},\n", + " \"output_type\": \"execute_result\"\n", + " }\n", + " ],\n", + " \"source\": [\n", + " \"# update the wd path to be able to laod the module\\n\",\n", + " \"os.chdir('../..') \\n\",\n", + " \"os.getcwd()\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 3,\n", + " \"id\": \"4ce04cdf-634c-4c9c-8d20-1e2a3caffac2\",\n", + " \"metadata\": {},\n", + " \"outputs\": [],\n", + " \"source\": [\n", + " \"os.environ['GCS_BUCKET']='gee-exports'\\n\",\n", + " \"os.environ['GOOGLE_APPLICATION_USER']='developers@citiesindicators.iam.gserviceaccount.com'\\n\",\n", + " \"os.environ['GOOGLE_APPLICATION_CREDENTIALS']='C:\\\\\\\\Users\\\\Saif.Shabou\\\\OneDrive - World Resources Institute\\\\Documents\\\\cities-indicators-framework\\\\citymetrix\\\\credentials-citiesindicators.json'\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"markdown\",\n", + " \"id\": \"67f86f26-b888-4cc2-b500-47ebe9458609\",\n", + " \"metadata\": {\n", + " \"tags\": []\n", + " },\n", + " \"source\": [\n", + " \"# Get boundaries\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 3,\n", + " \"id\": \"acbc5aaa-5f47-44ff-b9ab-b885c06d7424\",\n", + " \"metadata\": {},\n", + " \"outputs\": [\n", + " {\n", + " \"data\": {\n", + " \"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", + " \"
geo_idgeo_levelgeo_namegeo_parent_namecreation_dategeometry
0BRA-Salvador_ADM4-union_1ADM4-unionBRA-SalvadorBRA-Salvador2022-08-03MULTIPOLYGON (((-38.50135 -13.01134, -38.50140...
\\n\",\n", + " \"
\"\n", + " ],\n", + " \"text/plain\": [\n", + " \" geo_id geo_level geo_name geo_parent_name \\\\\\n\",\n", + " \"0 BRA-Salvador_ADM4-union_1 ADM4-union BRA-Salvador BRA-Salvador \\n\",\n", + " \"\\n\",\n", + " \" creation_date geometry \\n\",\n", + " \"0 2022-08-03 MULTIPOLYGON (((-38.50135 -13.01134, -38.50140... \"\n", + " ]\n", + " },\n", + " \"execution_count\": 3,\n", + " \"metadata\": {},\n", + " \"output_type\": \"execute_result\"\n", + " }\n", + " ],\n", + " \"source\": [\n", + " \"# load boundary of a region of interest\\n\",\n", + " \"boundary_path = 'https://cities-indicators.s3.eu-west-3.amazonaws.com/data/boundaries/boundary-BRA-Salvador-ADM4union.geojson'\\n\",\n", + " \"city_gdf_aoi = gpd.read_file(boundary_path, driver='GeoJSON')\\n\",\n", + " \"city_gdf_aoi.head()\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 4,\n", + " \"id\": \"7af8657e-feed-42b8-b32a-d4ee5754cd4c\",\n", + " \"metadata\": {},\n", + " \"outputs\": [\n", + " {\n", + " \"data\": {\n", + " \"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", + " \"
geo_idgeo_levelgeo_namegeo_parent_namecreation_dategeometry
0BRA-Salvador_ADM4_1ADM4PituaçuBRA-Salvador2022-08-03MULTIPOLYGON (((-38.40125 -12.96457, -38.40126...
1BRA-Salvador_ADM4_2ADM4PatamaresBRA-Salvador2022-08-03MULTIPOLYGON (((-38.39898 -12.96269, -38.39898...
2BRA-Salvador_ADM4_3ADM4PiatãBRA-Salvador2022-08-03MULTIPOLYGON (((-38.37346 -12.93345, -38.37386...
3BRA-Salvador_ADM4_4ADM4Boca do RioBRA-Salvador2022-08-03MULTIPOLYGON (((-38.41741 -12.97578, -38.41746...
4BRA-Salvador_ADM4_5ADM4Jardim ArmaçãoBRA-Salvador2022-08-03MULTIPOLYGON (((-38.43383 -12.98742, -38.43386...
\\n\",\n", + " \"
\"\n", + " ],\n", + " \"text/plain\": [\n", + " \" geo_id geo_level geo_name geo_parent_name \\\\\\n\",\n", + " \"0 BRA-Salvador_ADM4_1 ADM4 Pituaçu BRA-Salvador \\n\",\n", + " \"1 BRA-Salvador_ADM4_2 ADM4 Patamares BRA-Salvador \\n\",\n", + " \"2 BRA-Salvador_ADM4_3 ADM4 Piatã BRA-Salvador \\n\",\n", + " \"3 BRA-Salvador_ADM4_4 ADM4 Boca do Rio BRA-Salvador \\n\",\n", + " \"4 BRA-Salvador_ADM4_5 ADM4 Jardim Armação BRA-Salvador \\n\",\n", + " \"\\n\",\n", + " \" creation_date geometry \\n\",\n", + " \"0 2022-08-03 MULTIPOLYGON (((-38.40125 -12.96457, -38.40126... \\n\",\n", + " \"1 2022-08-03 MULTIPOLYGON (((-38.39898 -12.96269, -38.39898... \\n\",\n", + " \"2 2022-08-03 MULTIPOLYGON (((-38.37346 -12.93345, -38.37386... \\n\",\n", + " \"3 2022-08-03 MULTIPOLYGON (((-38.41741 -12.97578, -38.41746... \\n\",\n", + " \"4 2022-08-03 MULTIPOLYGON (((-38.43383 -12.98742, -38.43386... \"\n", + " ]\n", + " },\n", + " \"execution_count\": 4,\n", + " \"metadata\": {},\n", + " \"output_type\": \"execute_result\"\n", + " }\n", + " ],\n", + " \"source\": [\n", + " \"# load boundary of sub-georgaphy\\n\",\n", + " \"boundary_path = 'https://cities-indicators.s3.eu-west-3.amazonaws.com/data/boundaries/boundary-BRA-Salvador-ADM4.geojson'\\n\",\n", + " \"city_gdf_sub = gpd.read_file(boundary_path, driver='GeoJSON')\\n\",\n", + " \"city_gdf_sub.head()\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"markdown\",\n", + " \"id\": \"788d5679-2fc5-4025-996c-25af1d35b900\",\n", + " \"metadata\": {\n", + " \"tags\": []\n", + " },\n", + " \"source\": [\n", + " \"# LND-2 Average Tree Cover\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 5,\n", + " \"id\": \"8bdaa746-b071-4e9f-8959-c8c57921c73c\",\n", + " \"metadata\": {},\n", + " \"outputs\": [\n", + " {\n", + " \"name\": \"stdout\",\n", + " \"output_type\": \"stream\",\n", + " \"text\": [\n", + " \"Authenticating to GEE with configured credentials file.\\n\"\n", + " ]\n", + " }\n", + " ],\n", + " \"source\": [\n", + " \"from city_metrix.metrics import mean_tree_cover\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 6,\n", + " \"id\": \"e69f2674-dc78-4098-b423-0686944eabe8\",\n", + " \"metadata\": {},\n", + " \"outputs\": [],\n", + " \"source\": [\n", + " \"from city_metrix import mean_tree_cover\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"markdown\",\n", + " \"id\": \"7b71f6e7-9e83-4c88-8a98-9d30958a8dc4\",\n", + " \"metadata\": {},\n", + " \"source\": [\n", + " \"## City level - one city\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 9,\n", + " \"id\": \"8b85131c\",\n", + " \"metadata\": {},\n", + " \"outputs\": [\n", + " {\n", + " \"name\": \"stdout\",\n", + " \"output_type\": \"stream\",\n", + " \"text\": [\n", + " \"Extracting layer tree cover from Google Earth Engine:\\n\",\n", + " \"CPU times: user 6.65 s, sys: 1.12 s, total: 7.77 s\\n\",\n", + " \"Wall time: 12.5 s\\n\"\n", + " ]\n", + " },\n", + " {\n", + " \"data\": {\n", + " \"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", + " \"
geo_idgeo_levelgeo_namegeo_parent_namecreation_dategeometrymean_tree_cover
0BRA-Salvador_ADM4-union_1ADM4-unionBRA-SalvadorBRA-Salvador2022-08-03MULTIPOLYGON (((-38.50135 -13.01134, -38.50140...0.283668
\\n\",\n", + " \"
\"\n", + " ],\n", + " \"text/plain\": [\n", + " \" geo_id geo_level geo_name geo_parent_name \\\\\\n\",\n", + " \"0 BRA-Salvador_ADM4-union_1 ADM4-union BRA-Salvador BRA-Salvador \\n\",\n", + " \"\\n\",\n", + " \" creation_date geometry \\\\\\n\",\n", + " \"0 2022-08-03 MULTIPOLYGON (((-38.50135 -13.01134, -38.50140... \\n\",\n", + " \"\\n\",\n", + " \" mean_tree_cover \\n\",\n", + " \"0 0.283668 \"\n", + " ]\n", + " },\n", + " \"execution_count\": 9,\n", + " \"metadata\": {},\n", + " \"output_type\": \"execute_result\"\n", + " }\n", + " ],\n", + " \"source\": [\n", + " \"%%time\\n\",\n", + " \"\\n\",\n", + " \"# for one geography\\n\",\n", + " \"city_gdf_aoi[\\\"mean_tree_cover\\\"] = mean_tree_cover(city_gdf_aoi) \\n\",\n", + " \"city_gdf_aoi\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"markdown\",\n", + " \"id\": \"ee63ca6a-a5c5-4ce7-82cd-3a35e52d3cd6\",\n", + " \"metadata\": {},\n", + " \"source\": [\n", + " \"## Sub-city level\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 8,\n", + " \"id\": \"71349065-4726-4a12-bbb8-1f998562c338\",\n", + " \"metadata\": {},\n", + " \"outputs\": [\n", + " {\n", + " \"name\": \"stdout\",\n", + " \"output_type\": \"stream\",\n", + " \"text\": [\n", + " \"Extracting tree cover layer in bbox [-38.6469891 -13.0171475 -38.30455157 -12.75628844]:\\n\",\n", + " \"[########################################] | 100% Completed | 14.55 s\\n\",\n", + " \"CPU times: total: 17.1 s\\n\",\n", + " \"Wall time: 17.8 s\\n\"\n", + " ]\n", + " },\n", + " {\n", + " \"data\": {\n", + " \"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", + " \"
geo_idgeo_levelgeo_namegeo_parent_namecreation_dategeometrymean_tree_cover
0BRA-Salvador_ADM4_1ADM4PituaçuBRA-Salvador2022-08-03MULTIPOLYGON (((-38.40125 -12.96457, -38.40126...0.523927
1BRA-Salvador_ADM4_2ADM4PatamaresBRA-Salvador2022-08-03MULTIPOLYGON (((-38.39898 -12.96269, -38.39898...0.433088
2BRA-Salvador_ADM4_3ADM4PiatãBRA-Salvador2022-08-03MULTIPOLYGON (((-38.37346 -12.93345, -38.37386...0.274104
3BRA-Salvador_ADM4_4ADM4Boca do RioBRA-Salvador2022-08-03MULTIPOLYGON (((-38.41741 -12.97578, -38.41746...0.157694
4BRA-Salvador_ADM4_5ADM4Jardim ArmaçãoBRA-Salvador2022-08-03MULTIPOLYGON (((-38.43383 -12.98742, -38.43386...0.210874
\\n\",\n", + " \"
\"\n", + " ],\n", + " \"text/plain\": [\n", + " \" geo_id geo_level geo_name geo_parent_name \\\\\\n\",\n", + " \"0 BRA-Salvador_ADM4_1 ADM4 Pituaçu BRA-Salvador \\n\",\n", + " \"1 BRA-Salvador_ADM4_2 ADM4 Patamares BRA-Salvador \\n\",\n", + " \"2 BRA-Salvador_ADM4_3 ADM4 Piatã BRA-Salvador \\n\",\n", + " \"3 BRA-Salvador_ADM4_4 ADM4 Boca do Rio BRA-Salvador \\n\",\n", + " \"4 BRA-Salvador_ADM4_5 ADM4 Jardim Armação BRA-Salvador \\n\",\n", + " \"\\n\",\n", + " \" creation_date geometry \\\\\\n\",\n", + " \"0 2022-08-03 MULTIPOLYGON (((-38.40125 -12.96457, -38.40126... \\n\",\n", + " \"1 2022-08-03 MULTIPOLYGON (((-38.39898 -12.96269, -38.39898... \\n\",\n", + " \"2 2022-08-03 MULTIPOLYGON (((-38.37346 -12.93345, -38.37386... \\n\",\n", + " \"3 2022-08-03 MULTIPOLYGON (((-38.41741 -12.97578, -38.41746... \\n\",\n", + " \"4 2022-08-03 MULTIPOLYGON (((-38.43383 -12.98742, -38.43386... \\n\",\n", + " \"\\n\",\n", + " \" mean_tree_cover \\n\",\n", + " \"0 0.523927 \\n\",\n", + " \"1 0.433088 \\n\",\n", + " \"2 0.274104 \\n\",\n", + " \"3 0.157694 \\n\",\n", + " \"4 0.210874 \"\n", + " ]\n", + " },\n", + " \"execution_count\": 8,\n", + " \"metadata\": {},\n", + " \"output_type\": \"execute_result\"\n", + " }\n", + " ],\n", + " \"source\": [\n", + " \"%%time\\n\",\n", + " \"\\n\",\n", + " \"# for sub geographies\\n\",\n", + " \"city_gdf_sub[\\\"mean_tree_cover\\\"] = mean_tree_cover(city_gdf_sub) \\n\",\n", + " \"city_gdf_sub.head()\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"markdown\",\n", + " \"id\": \"6580e18f-50b1-499c-adb7-9ed604c33e81\",\n", + " \"metadata\": {},\n", + " \"source\": [\n", + " \"## Multiple cities\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 9,\n", + " \"id\": \"5006d2d3-6ef0-4460-b127-f72522e94585\",\n", + " \"metadata\": {},\n", + " \"outputs\": [\n", + " {\n", + " \"data\": {\n", + " \"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", + " \"
geo_idgeo_levelgeo_namegeo_parent_namecreation_dategeometry
0BRA-Salvador_ADM4-union_1ADM4-unionBRA-SalvadorBRA-Salvador2022-08-03MULTIPOLYGON (((-38.50135 -13.01134, -38.50140...
1IDN-Jakarta_ADM-4-union_1ADM-4-unionIDN-JakartaIDN-Jakarta2022-06-27MULTIPOLYGON (((106.78141 -6.31616, 106.78124 ...
\\n\",\n", + " \"
\"\n", + " ],\n", + " \"text/plain\": [\n", + " \" geo_id geo_level geo_name geo_parent_name \\\\\\n\",\n", + " \"0 BRA-Salvador_ADM4-union_1 ADM4-union BRA-Salvador BRA-Salvador \\n\",\n", + " \"1 IDN-Jakarta_ADM-4-union_1 ADM-4-union IDN-Jakarta IDN-Jakarta \\n\",\n", + " \"\\n\",\n", + " \" creation_date geometry \\n\",\n", + " \"0 2022-08-03 MULTIPOLYGON (((-38.50135 -13.01134, -38.50140... \\n\",\n", + " \"1 2022-06-27 MULTIPOLYGON (((106.78141 -6.31616, 106.78124 ... \"\n", + " ]\n", + " },\n", + " \"execution_count\": 9,\n", + " \"metadata\": {},\n", + " \"output_type\": \"execute_result\"\n", + " }\n", + " ],\n", + " \"source\": [\n", + " \"# Calculate indicator for multiple regions of interest\\n\",\n", + " \"\\n\",\n", + " \"boundary_path = 'https://cities-indicators.s3.eu-west-3.amazonaws.com/data/boundaries/boundary-BRA-Salvador-ADM4union.geojson'\\n\",\n", + " \"city_Salvador = gpd.read_file(boundary_path, driver='GeoJSON')\\n\",\n", + " \"\\n\",\n", + " \"boundary_path = 'https://cities-indicators.s3.eu-west-3.amazonaws.com/data/boundaries/boundary-IDN-Jakarta-ADM4union.geojson'\\n\",\n", + " \"city_Jakarta = gpd.read_file(boundary_path, driver='GeoJSON')\\n\",\n", + " \"\\n\",\n", + " \"cities = pd.concat([city_Salvador, city_Jakarta])\\n\",\n", + " \"cities = cities.reset_index(drop=True)\\n\",\n", + " \"cities\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 10,\n", + " \"id\": \"47791e3e-3761-404e-96a5-26493cc8ef54\",\n", + " \"metadata\": {},\n", + " \"outputs\": [\n", + " {\n", + " \"name\": \"stdout\",\n", + " \"output_type\": \"stream\",\n", + " \"text\": [\n", + " \"Extracting tree cover layer in bbox [-38.6469891 -13.0171475 -38.30455157 -12.75628844]:\\n\",\n", + " \"[########################################] | 100% Completed | 16.77 s\\n\",\n", + " \"Extracting tree cover layer in bbox [106.685589 -6.3650478 106.8530109 -6.0891749]:\\n\",\n", + " \"[########################################] | 100% Completed | 9.38 ss\\n\",\n", + " \"Extracting tree cover layer in bbox [106.8530109 -6.3744575 106.973975 -6.0895823]:\\n\",\n", + " \"[########################################] | 100% Completed | 8.75 ss\\n\"\n", + " ]\n", + " }\n", + " ],\n", + " \"source\": [\n", + " \"cities[\\\"mean_tree_cover\\\"] = mean_tree_cover(cities)\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 11,\n", + " \"id\": \"d58a38fd-adea-4961-b0f8-abac0d0943cf\",\n", + " \"metadata\": {},\n", + " \"outputs\": [\n", + " {\n", + " \"data\": {\n", + " \"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", + " \"
geo_idgeo_levelgeo_namegeo_parent_namecreation_dategeometrymean_tree_cover
0BRA-Salvador_ADM4-union_1ADM4-unionBRA-SalvadorBRA-Salvador2022-08-03MULTIPOLYGON (((-38.50135 -13.01134, -38.50140...0.283667
1IDN-Jakarta_ADM-4-union_1ADM-4-unionIDN-JakartaIDN-Jakarta2022-06-27MULTIPOLYGON (((106.78141 -6.31616, 106.78124 ...0.159044
\\n\",\n", + " \"
\"\n", + " ],\n", + " \"text/plain\": [\n", + " \" geo_id geo_level geo_name geo_parent_name \\\\\\n\",\n", + " \"0 BRA-Salvador_ADM4-union_1 ADM4-union BRA-Salvador BRA-Salvador \\n\",\n", + " \"1 IDN-Jakarta_ADM-4-union_1 ADM-4-union IDN-Jakarta IDN-Jakarta \\n\",\n", + " \"\\n\",\n", + " \" creation_date geometry \\\\\\n\",\n", + " \"0 2022-08-03 MULTIPOLYGON (((-38.50135 -13.01134, -38.50140... \\n\",\n", + " \"1 2022-06-27 MULTIPOLYGON (((106.78141 -6.31616, 106.78124 ... \\n\",\n", + " \"\\n\",\n", + " \" mean_tree_cover \\n\",\n", + " \"0 0.283667 \\n\",\n", + " \"1 0.159044 \"\n", + " ]\n", + " },\n", + " \"execution_count\": 11,\n", + " \"metadata\": {},\n", + " \"output_type\": \"execute_result\"\n", + " }\n", + " ],\n", + " \"source\": [\n", + " \"cities\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"markdown\",\n", + " \"id\": \"8aa3590f-b78e-4c57-872b-c5056266ff1a\",\n", + " \"metadata\": {\n", + " \"tags\": []\n", + " },\n", + " \"source\": [\n", + " \"# HEA-4 Percent of built land without tree cover\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 14,\n", + " \"id\": \"e5948b81-cb27-4c97-9567-a297e078cdb5\",\n", + " \"metadata\": {},\n", + " \"outputs\": [],\n", + " \"source\": [\n", + " \"from city_metrix.metrics import built_land_without_tree_cover\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 15,\n", + " \"id\": \"10e89534-19d7-4dba-bc98-d4f3dd8b95f4\",\n", + " \"metadata\": {},\n", + " \"outputs\": [\n", + " {\n", + " \"name\": \"stdout\",\n", + " \"output_type\": \"stream\",\n", + " \"text\": [\n", + " \"Extracting ESA world cover layer in bbox [-38.6469891 -13.0171475 -38.30455157 -12.75628844]:\\n\",\n", + " \"[########################################] | 100% Completed | 9.68 ss\\n\"\n", + " ]\n", + " },\n", + " {\n", + " \"ename\": \"UnboundLocalError\",\n", + " \"evalue\": \"local variable 'stats_funcs_dict' referenced before assignment\",\n", + " \"output_type\": \"error\",\n", + " \"traceback\": [\n", + " \"\\u001b[1;31m---------------------------------------------------------------------------\\u001b[0m\",\n", + " \"\\u001b[1;31mUnboundLocalError\\u001b[0m Traceback (most recent call last)\",\n", + " \"File \\u001b[1;32m:2\\u001b[0m\\n\",\n", + " \"File \\u001b[1;32m~\\\\OneDrive - World Resources Institute\\\\Documents\\\\cities-indicators-framework\\\\citymetrix\\\\cities-cif\\\\city_metrix\\\\metrics\\\\built_land_without_tree_cover.py:16\\u001b[0m, in \\u001b[0;36mbuilt_land_without_tree_cover\\u001b[1;34m(zones)\\u001b[0m\\n\\u001b[0;32m 13\\u001b[0m built_up_land \\u001b[38;5;241m=\\u001b[39m EsaWorldCover(land_cover_class\\u001b[38;5;241m=\\u001b[39mEsaWorldCoverClass\\u001b[38;5;241m.\\u001b[39mBUILT_UP)\\n\\u001b[0;32m 14\\u001b[0m tree_cover \\u001b[38;5;241m=\\u001b[39m TreeCover(min_tree_cover\\u001b[38;5;241m=\\u001b[39m\\u001b[38;5;241m1\\u001b[39m)\\n\\u001b[1;32m---> 16\\u001b[0m built_land \\u001b[38;5;241m=\\u001b[39m \\u001b[43mbuilt_up_land\\u001b[49m\\u001b[38;5;241;43m.\\u001b[39;49m\\u001b[43mgroupby\\u001b[49m\\u001b[43m(\\u001b[49m\\u001b[43mzones\\u001b[49m\\u001b[43m)\\u001b[49m\\u001b[38;5;241;43m.\\u001b[39;49m\\u001b[43mcount\\u001b[49m\\u001b[43m(\\u001b[49m\\u001b[43m)\\u001b[49m\\n\\u001b[0;32m 17\\u001b[0m tree_cover_in_built_land \\u001b[38;5;241m=\\u001b[39m tree_cover\\u001b[38;5;241m.\\u001b[39mmask(built_up_land)\\u001b[38;5;241m.\\u001b[39mgroupby(zones)\\u001b[38;5;241m.\\u001b[39mcount()\\n\\u001b[0;32m 19\\u001b[0m percent_tree_cover_in_built_up_land \\u001b[38;5;241m=\\u001b[39m \\u001b[38;5;241m1\\u001b[39m \\u001b[38;5;241m-\\u001b[39m (tree_cover_in_built_land\\u001b[38;5;241m.\\u001b[39mfillna(\\u001b[38;5;241m0\\u001b[39m) \\u001b[38;5;241m/\\u001b[39m built_land)\\n\",\n", + " \"File \\u001b[1;32m~\\\\OneDrive - World Resources Institute\\\\Documents\\\\cities-indicators-framework\\\\citymetrix\\\\cities-cif\\\\city_metrix\\\\layers\\\\layer.py:64\\u001b[0m, in \\u001b[0;36mLayerGroupBy.count\\u001b[1;34m(self)\\u001b[0m\\n\\u001b[0;32m 63\\u001b[0m \\u001b[38;5;28;01mdef\\u001b[39;00m \\u001b[38;5;21mcount\\u001b[39m(\\u001b[38;5;28mself\\u001b[39m):\\n\\u001b[1;32m---> 64\\u001b[0m \\u001b[38;5;28;01mreturn\\u001b[39;00m \\u001b[38;5;28;43mself\\u001b[39;49m\\u001b[38;5;241;43m.\\u001b[39;49m\\u001b[43m_zonal_stats\\u001b[49m\\u001b[43m(\\u001b[49m\\u001b[38;5;124;43m\\\"\\u001b[39;49m\\u001b[38;5;124;43mcount\\u001b[39;49m\\u001b[38;5;124;43m\\\"\\u001b[39;49m\\u001b[43m)\\u001b[49m\\n\",\n", + " \"File \\u001b[1;32m~\\\\OneDrive - World Resources Institute\\\\Documents\\\\cities-indicators-framework\\\\citymetrix\\\\cities-cif\\\\city_metrix\\\\layers\\\\layer.py:68\\u001b[0m, in \\u001b[0;36mLayerGroupBy._zonal_stats\\u001b[1;34m(self, stats_func)\\u001b[0m\\n\\u001b[0;32m 66\\u001b[0m \\u001b[38;5;28;01mdef\\u001b[39;00m \\u001b[38;5;21m_zonal_stats\\u001b[39m(\\u001b[38;5;28mself\\u001b[39m, stats_func):\\n\\u001b[0;32m 67\\u001b[0m \\u001b[38;5;28;01mif\\u001b[39;00m box(\\u001b[38;5;241m*\\u001b[39m\\u001b[38;5;28mself\\u001b[39m\\u001b[38;5;241m.\\u001b[39mzones\\u001b[38;5;241m.\\u001b[39mtotal_bounds)\\u001b[38;5;241m.\\u001b[39marea \\u001b[38;5;241m<\\u001b[39m\\u001b[38;5;241m=\\u001b[39m MAX_TILE_SIZE\\u001b[38;5;241m*\\u001b[39m\\u001b[38;5;241m*\\u001b[39m\\u001b[38;5;241m2\\u001b[39m:\\n\\u001b[1;32m---> 68\\u001b[0m \\u001b[38;5;28;01mreturn\\u001b[39;00m \\u001b[38;5;28;43mself\\u001b[39;49m\\u001b[38;5;241;43m.\\u001b[39;49m\\u001b[43m_zonal_stats_tile\\u001b[49m\\u001b[43m(\\u001b[49m\\u001b[38;5;28;43mself\\u001b[39;49m\\u001b[38;5;241;43m.\\u001b[39;49m\\u001b[43mzones\\u001b[49m\\u001b[43m,\\u001b[49m\\u001b[43m \\u001b[49m\\u001b[43mstats_func\\u001b[49m\\u001b[43m)\\u001b[49m[stats_func]\\n\\u001b[0;32m 69\\u001b[0m \\u001b[38;5;28;01melse\\u001b[39;00m:\\n\\u001b[0;32m 70\\u001b[0m \\u001b[38;5;28;01mreturn\\u001b[39;00m \\u001b[38;5;28mself\\u001b[39m\\u001b[38;5;241m.\\u001b[39m_zonal_stats_fishnet(stats_func)\\n\",\n", + " \"File \\u001b[1;32m~\\\\OneDrive - World Resources Institute\\\\Documents\\\\cities-indicators-framework\\\\citymetrix\\\\cities-cif\\\\city_metrix\\\\layers\\\\layer.py:116\\u001b[0m, in \\u001b[0;36mLayerGroupBy._zonal_stats_tile\\u001b[1;34m(self, tile_gdf, stats_func)\\u001b[0m\\n\\u001b[0;32m 113\\u001b[0m aggregate_data \\u001b[38;5;241m=\\u001b[39m aggregate_data\\u001b[38;5;241m.\\u001b[39mwhere(\\u001b[38;5;241m~\\u001b[39mnp\\u001b[38;5;241m.\\u001b[39misnan(mask))\\n\\u001b[0;32m 115\\u001b[0m zones \\u001b[38;5;241m=\\u001b[39m \\u001b[38;5;28mself\\u001b[39m\\u001b[38;5;241m.\\u001b[39m_rasterize(tile_gdf, align_to)\\n\\u001b[1;32m--> 116\\u001b[0m stats \\u001b[38;5;241m=\\u001b[39m \\u001b[43mzonal_stats\\u001b[49m\\u001b[43m(\\u001b[49m\\u001b[43mzones\\u001b[49m\\u001b[43m,\\u001b[49m\\u001b[43m \\u001b[49m\\u001b[43maggregate_data\\u001b[49m\\u001b[43m,\\u001b[49m\\u001b[43m \\u001b[49m\\u001b[43mstats_funcs\\u001b[49m\\u001b[38;5;241;43m=\\u001b[39;49m\\u001b[43mstats_func\\u001b[49m\\u001b[43m)\\u001b[49m\\n\\u001b[0;32m 118\\u001b[0m \\u001b[38;5;28;01mreturn\\u001b[39;00m stats\\n\",\n", + " \"File \\u001b[1;32m~\\\\anaconda3\\\\envs\\\\citymetrix_env\\\\lib\\\\site-packages\\\\xrspatial\\\\zonal.py:579\\u001b[0m, in \\u001b[0;36mstats\\u001b[1;34m(zones, values, zone_ids, stats_funcs, nodata_values, return_type)\\u001b[0m\\n\\u001b[0;32m 568\\u001b[0m stats_funcs_dict \\u001b[38;5;241m=\\u001b[39m stats_funcs\\u001b[38;5;241m.\\u001b[39mcopy()\\n\\u001b[0;32m 570\\u001b[0m mapper \\u001b[38;5;241m=\\u001b[39m ArrayTypeFunctionMapping(\\n\\u001b[0;32m 571\\u001b[0m numpy_func\\u001b[38;5;241m=\\u001b[39m\\u001b[38;5;28;01mlambda\\u001b[39;00m \\u001b[38;5;241m*\\u001b[39margs: _stats_numpy(\\u001b[38;5;241m*\\u001b[39margs, return_type\\u001b[38;5;241m=\\u001b[39mreturn_type),\\n\\u001b[0;32m 572\\u001b[0m dask_func\\u001b[38;5;241m=\\u001b[39m_stats_dask_numpy,\\n\\u001b[1;32m (...)\\u001b[0m\\n\\u001b[0;32m 576\\u001b[0m ),\\n\\u001b[0;32m 577\\u001b[0m )\\n\\u001b[0;32m 578\\u001b[0m result \\u001b[38;5;241m=\\u001b[39m mapper(values)(\\n\\u001b[1;32m--> 579\\u001b[0m zones\\u001b[38;5;241m.\\u001b[39mdata, values\\u001b[38;5;241m.\\u001b[39mdata, zone_ids, \\u001b[43mstats_funcs_dict\\u001b[49m, nodata_values,\\n\\u001b[0;32m 580\\u001b[0m )\\n\\u001b[0;32m 582\\u001b[0m \\u001b[38;5;28;01mif\\u001b[39;00m return_type \\u001b[38;5;241m==\\u001b[39m \\u001b[38;5;124m'\\u001b[39m\\u001b[38;5;124mxarray.DataArray\\u001b[39m\\u001b[38;5;124m'\\u001b[39m:\\n\\u001b[0;32m 583\\u001b[0m \\u001b[38;5;28;01mreturn\\u001b[39;00m xr\\u001b[38;5;241m.\\u001b[39mDataArray(\\n\\u001b[0;32m 584\\u001b[0m result,\\n\\u001b[0;32m 585\\u001b[0m coords\\u001b[38;5;241m=\\u001b[39m{\\u001b[38;5;124m'\\u001b[39m\\u001b[38;5;124mstats\\u001b[39m\\u001b[38;5;124m'\\u001b[39m: \\u001b[38;5;28mlist\\u001b[39m(stats_funcs_dict\\u001b[38;5;241m.\\u001b[39mkeys()), \\u001b[38;5;241m*\\u001b[39m\\u001b[38;5;241m*\\u001b[39mvalues\\u001b[38;5;241m.\\u001b[39mcoords},\\n\\u001b[0;32m 586\\u001b[0m dims\\u001b[38;5;241m=\\u001b[39m(\\u001b[38;5;124m'\\u001b[39m\\u001b[38;5;124mstats\\u001b[39m\\u001b[38;5;124m'\\u001b[39m, \\u001b[38;5;241m*\\u001b[39mvalues\\u001b[38;5;241m.\\u001b[39mdims),\\n\\u001b[0;32m 587\\u001b[0m attrs\\u001b[38;5;241m=\\u001b[39mvalues\\u001b[38;5;241m.\\u001b[39mattrs\\n\\u001b[0;32m 588\\u001b[0m )\\n\",\n", + " \"\\u001b[1;31mUnboundLocalError\\u001b[0m: local variable 'stats_funcs_dict' referenced before assignment\"\n", + " ]\n", + " }\n", + " ],\n", + " \"source\": [\n", + " \"%%time\\n\",\n", + " \"\\n\",\n", + " \"# for one geography\\n\",\n", + " \"city_gdf_aoi[\\\"built_land_without_tree_cover\\\"] = built_land_without_tree_cover(city_gdf_aoi) \\n\",\n", + " \"city_gdf_aoi\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"markdown\",\n", + " \"id\": \"afbb60b1-437f-4af5-99fe-9679559b250b\",\n", + " \"metadata\": {},\n", + " \"source\": [\n", + " \"# HEA-3 Built land with Low Surface reflectivity\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 12,\n", + " \"id\": \"3c61a81e-f575-4d24-863d-ca9b7be34ad5\",\n", + " \"metadata\": {},\n", + " \"outputs\": [],\n", + " \"source\": [\n", + " \"from city_metrix.metrics import built_land_with_low_surface_reflectivity\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 13,\n", + " \"id\": \"748d1d72-c4fe-4c6c-91f6-99dac052bdd8\",\n", + " \"metadata\": {},\n", + " \"outputs\": [\n", + " {\n", + " \"name\": \"stdout\",\n", + " \"output_type\": \"stream\",\n", + " \"text\": [\n", + " \"Extracting ESA world cover layer:\\n\",\n", + " \"[########################################] | 100% Completed | 8.21 ss\\n\",\n", + " \"Calculating albedo layer:\\n\",\n", + " \"[########################################] | 100% Completed | 84.36 s\\n\",\n", + " \"Extracting ESA world cover layer:\\n\",\n", + " \"[########################################] | 100% Completed | 8.32 ss\\n\",\n", + " \"CPU times: total: 30.5 s\\n\",\n", + " \"Wall time: 1min 49s\\n\"\n", + " ]\n", + " },\n", + " {\n", + " \"data\": {\n", + " \"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", + " \"
geo_idgeo_levelgeo_namegeo_parent_namecreation_dategeometrymean_tree_coverbuilt_land_without_tree_coverbuilt_land_with_low_surface_reflectivity
0BRA-Salvador_ADM4-union_1ADM4-unionBRA-SalvadorBRA-Salvador2022-08-03MULTIPOLYGON (((-38.50135 -13.01134, -38.50140...28.3667340.9073820.786552
\\n\",\n", + " \"
\"\n", + " ],\n", + " \"text/plain\": [\n", + " \" geo_id geo_level geo_name geo_parent_name \\\\\\n\",\n", + " \"0 BRA-Salvador_ADM4-union_1 ADM4-union BRA-Salvador BRA-Salvador \\n\",\n", + " \"\\n\",\n", + " \" creation_date geometry \\\\\\n\",\n", + " \"0 2022-08-03 MULTIPOLYGON (((-38.50135 -13.01134, -38.50140... \\n\",\n", + " \"\\n\",\n", + " \" mean_tree_cover built_land_without_tree_cover \\\\\\n\",\n", + " \"0 28.366734 0.907382 \\n\",\n", + " \"\\n\",\n", + " \" built_land_with_low_surface_reflectivity \\n\",\n", + " \"0 0.786552 \"\n", + " ]\n", + " },\n", + " \"execution_count\": 13,\n", + " \"metadata\": {},\n", + " \"output_type\": \"execute_result\"\n", + " }\n", + " ],\n", + " \"source\": [\n", + " \"%%time\\n\",\n", + " \"\\n\",\n", + " \"city_gdf_aoi[\\\"built_land_with_low_surface_reflectivity\\\"] = built_land_with_low_surface_reflectivity(city_gdf_aoi) \\n\",\n", + " \"city_gdf_aoi\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"markdown\",\n", + " \"id\": \"bbfcabaa-2acc-4b9b-bdce-27b415e04354\",\n", + " \"metadata\": {},\n", + " \"source\": [\n", + " \"# HEA-2 Built Land With High LST\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 14,\n", + " \"id\": \"6df5d7a3-1c25-49ac-ab33-9081722168c9\",\n", + " \"metadata\": {},\n", + " \"outputs\": [],\n", + " \"source\": [\n", + " \"from city_metrix.metrics import built_land_with_high_land_surface_temperature\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 15,\n", + " \"id\": \"5288d4e6-e35d-48f4-9ff7-2d84c857b21a\",\n", + " \"metadata\": {},\n", + " \"outputs\": [\n", + " {\n", + " \"name\": \"stdout\",\n", + " \"output_type\": \"stream\",\n", + " \"text\": [\n", + " \"Extracting ESA world cover layer:\\n\",\n", + " \"[########################################] | 100% Completed | 8.65 ss\\n\",\n", + " \"Calculating land surface temperature layer:\\n\",\n", + " \"[########################################] | 100% Completed | 2.73 ss\\n\",\n", + " \"Extracting ESA world cover layer:\\n\",\n", + " \"[########################################] | 100% Completed | 9.16 ss\\n\",\n", + " \"CPU times: total: 14.8 s\\n\",\n", + " \"Wall time: 37.7 s\\n\"\n", + " ]\n", + " },\n", + " {\n", + " \"data\": {\n", + " \"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", + " \"
geo_idgeo_levelgeo_namegeo_parent_namecreation_dategeometrymean_tree_coverbuilt_land_without_tree_coverbuilt_land_with_low_surface_reflectivitybuilt_land_with_high_land_surface_temperature
0BRA-Salvador_ADM4-union_1ADM4-unionBRA-SalvadorBRA-Salvador2022-08-03MULTIPOLYGON (((-38.50135 -13.01134, -38.50140...28.3667340.9073820.7865520.0961
\\n\",\n", + " \"
\"\n", + " ],\n", + " \"text/plain\": [\n", + " \" geo_id geo_level geo_name geo_parent_name \\\\\\n\",\n", + " \"0 BRA-Salvador_ADM4-union_1 ADM4-union BRA-Salvador BRA-Salvador \\n\",\n", + " \"\\n\",\n", + " \" creation_date geometry \\\\\\n\",\n", + " \"0 2022-08-03 MULTIPOLYGON (((-38.50135 -13.01134, -38.50140... \\n\",\n", + " \"\\n\",\n", + " \" mean_tree_cover built_land_without_tree_cover \\\\\\n\",\n", + " \"0 28.366734 0.907382 \\n\",\n", + " \"\\n\",\n", + " \" built_land_with_low_surface_reflectivity \\\\\\n\",\n", + " \"0 0.786552 \\n\",\n", + " \"\\n\",\n", + " \" built_land_with_high_land_surface_temperature \\n\",\n", + " \"0 0.0961 \"\n", + " ]\n", + " },\n", + " \"execution_count\": 15,\n", + " \"metadata\": {},\n", + " \"output_type\": \"execute_result\"\n", + " }\n", + " ],\n", + " \"source\": [\n", + " \"%%time\\n\",\n", + " \"\\n\",\n", + " \"city_gdf_aoi[\\\"built_land_with_high_land_surface_temperature\\\"] = built_land_with_high_land_surface_temperature(city_gdf_aoi) \\n\",\n", + " \"city_gdf_aoi\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"markdown\",\n", + " \"id\": \"feb93688-9435-47a5-be31-0a806dd48fe1\",\n", + " \"metadata\": {},\n", + " \"source\": [\n", + " \"# Percent of Natural areas\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 18,\n", + " \"id\": \"aee87e06-d16d-4017-a5e1-7fd8cef906d4\",\n", + " \"metadata\": {},\n", + " \"outputs\": [],\n", + " \"source\": [\n", + " \"from city_metrix.metrics import natural_areas\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": null,\n", + " \"id\": \"026261af-a18d-4c0c-bdb8-b11a3c192ddc\",\n", + " \"metadata\": {},\n", + " \"outputs\": [],\n", + " \"source\": [\n", + " \"%%time\\n\",\n", + " \"\\n\",\n", + " \"# for one geography\\n\",\n", + " \"city_gdf_aoi[\\\"natural_areas\\\"] = natural_areas(city_gdf_aoi) \\n\",\n", + " \"city_gdf_aoi\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"markdown\",\n", + " \"id\": \"b05cef42-67cd-4cdc-a7db-5f34ddd7837b\",\n", + " \"metadata\": {},\n", + " \"source\": [\n", + " \"# Open Space in built up land\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 21,\n", + " \"id\": \"b4281469-9ca5-4b2c-a308-32f2e2fd5404\",\n", + " \"metadata\": {},\n", + " \"outputs\": [],\n", + " \"source\": [\n", + " \"from city_metrix.metrics import urban_open_space\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": 22,\n", + " \"id\": \"db612f31-eb82-4a10-9433-0907f0a64a0e\",\n", + " \"metadata\": {},\n", + " \"outputs\": [\n", + " {\n", + " \"name\": \"stdout\",\n", + " \"output_type\": \"stream\",\n", + " \"text\": [\n", + " \"Extracting ESA world cover layer:\\n\",\n", + " \"[########################################] | 100% Completed | 12.48 s\\n\",\n", + " \"Extracting ESA world cover layer:\\n\",\n", + " \"[########################################] | 100% Completed | 9.08 ss\\n\",\n", + " \"CPU times: total: 24 s\\n\",\n", + " \"Wall time: 31.1 s\\n\"\n", + " ]\n", + " },\n", + " {\n", + " \"data\": {\n", + " \"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", + " \"
geo_idgeo_levelgeo_namegeo_parent_namecreation_dategeometrymean_tree_coverbuilt_land_without_tree_coverbuilt_land_with_low_surface_reflectivitybuilt_land_with_high_land_surface_temperatureurban_open_space
0BRA-Salvador_ADM4-union_1ADM4-unionBRA-SalvadorBRA-Salvador2022-08-03MULTIPOLYGON (((-38.50135 -13.01134, -38.50140...28.3667340.9073820.7865520.09610.028378
\\n\",\n", + " \"
\"\n", + " ],\n", + " \"text/plain\": [\n", + " \" geo_id geo_level geo_name geo_parent_name \\\\\\n\",\n", + " \"0 BRA-Salvador_ADM4-union_1 ADM4-union BRA-Salvador BRA-Salvador \\n\",\n", + " \"\\n\",\n", + " \" creation_date geometry \\\\\\n\",\n", + " \"0 2022-08-03 MULTIPOLYGON (((-38.50135 -13.01134, -38.50140... \\n\",\n", + " \"\\n\",\n", + " \" mean_tree_cover built_land_without_tree_cover \\\\\\n\",\n", + " \"0 28.366734 0.907382 \\n\",\n", + " \"\\n\",\n", + " \" built_land_with_low_surface_reflectivity \\\\\\n\",\n", + " \"0 0.786552 \\n\",\n", + " \"\\n\",\n", + " \" built_land_with_high_land_surface_temperature urban_open_space \\n\",\n", + " \"0 0.0961 0.028378 \"\n", + " ]\n", + " },\n", + " \"execution_count\": 22,\n", + " \"metadata\": {},\n", + " \"output_type\": \"execute_result\"\n", + " }\n", + " ],\n", + " \"source\": [\n", + " \"%%time\\n\",\n", + " \"\\n\",\n", + " \"# for one geography\\n\",\n", + " \"city_gdf_aoi[\\\"urban_open_space\\\"] = urban_open_space(city_gdf_aoi) \\n\",\n", + " \"city_gdf_aoi\"\n", + " ]\n", + " },\n", + " {\n", + " \"cell_type\": \"code\",\n", + " \"execution_count\": null,\n", + " \"id\": \"951151f1-e0f5-4a8a-99a7-19a6f7c84591\",\n", + " \"metadata\": {},\n", + " \"outputs\": [],\n", + " \"source\": []\n", + " }\n", + " ],\n", + " \"metadata\": {\n", + " \"kernelspec\": {\n", + " \"display_name\": \"Python 3 (ipykernel)\",\n", + " \"language\": \"python\",\n", + " \"name\": \"python3\"\n", + " },\n", + " \"language_info\": {\n", + " \"codemirror_mode\": {\n", + " \"name\": \"ipython\",\n", + " \"version\": 3\n", + " },\n", + " \"file_extension\": \".py\",\n", + " \"mimetype\": \"text/x-python\",\n", + " \"name\": \"python\",\n", + " \"nbconvert_exporter\": \"python\",\n", + " \"pygments_lexer\": \"ipython3\",\n", + " \"version\": \"3.10.13\"\n", + " }\n", + " },\n", + " \"nbformat\": 4,\n", + " \"nbformat_minor\": 5\n", + "}" + ], + "metadata": { + "collapsed": false + } + } } }, "nbformat": 4, "nbformat_minor": 5 -} +} \ No newline at end of file From 586f68c416ef8d191f53482749906259115db743 Mon Sep 17 00:00:00 2001 From: Justin Terry Date: Tue, 20 Feb 2024 16:11:22 -0800 Subject: [PATCH 4/5] Add notebooks to .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 01b3808..a3931f1 100644 --- a/.gitignore +++ b/.gitignore @@ -163,3 +163,6 @@ cython_debug/ /keys keys/ wri-gee-358d958ce7c6.json + +# notebooks, you need to `git add -f` to force them to be changed +.ipynb From 049a93436c37f77b688f497fe92276e4453fd7f7 Mon Sep 17 00:00:00 2001 From: Justin Terry Date: Tue, 20 Feb 2024 16:18:06 -0800 Subject: [PATCH 5/5] Add comments --- city_metrix/layers/layer.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/city_metrix/layers/layer.py b/city_metrix/layers/layer.py index b632bb9..5261ab0 100644 --- a/city_metrix/layers/layer.py +++ b/city_metrix/layers/layer.py @@ -73,12 +73,17 @@ def _zonal_stats(self, stats_func): stats = self._zonal_stats_fishnet(stats_func) if self.layer is not None: + # decode zone and layer value using bit operations stats["layer"] = stats["zone"].astype("uint32").values >> 16 stats["zone"] = stats["zone"].astype("uint32").values & 65535 - stats = stats.groupby("zone").apply( - lambda df: df.drop(columns="zone").groupby("layer").sum().to_dict()[stats_func] - ) + # group layer values together into a dictionary per zone + def group_layer_values(df): + layer_values = df.drop(columns="zone").groupby("layer").sum() + layer_dicts = layer_values.to_dict() + return layer_dicts[stats_func] + + stats = stats.groupby("zone").apply(group_layer_values) return stats @@ -136,6 +141,7 @@ def _zonal_stats_tile(self, tile_gdf, stats_func): zones = self._rasterize(tile_gdf, align_to) if self.layer is not None: + # encode layer into zones by bitshifting zones = zones + (layer_data.astype("uint32") << 16) stats = zonal_stats(zones, aggregate_data, stats_funcs=stats_func)