diff --git a/CHANGELOG.md b/CHANGELOG.md index 8246c8940..f37b12a61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,7 +27,11 @@ Code freeze date: YYYY-MM-DD - In `climada.util.plot.geo_im_from_array`, NaNs are plotted in gray while cells with no centroid are not plotted [#929](https://github.com/CLIMADA-project/climada_python/pull/929) - Renamed `climada.util.plot.subplots_from_gdf` to `climada.util.plot.plot_from_gdf` [#929](https://github.com/CLIMADA-project/climada_python/pull/929) -- Exposures complete overhaul. Notably the _geometry_ column of the inherent `GeoDataFrame` is set up at initialization, while latitude and longitude column are no longer present there (the according arrays can be retrieved as properties of the Exposures object: `exp.latitude` instead of `exp.gdf.latitude.values`). +- Exposures complete overhaul. Notably + - the _geometry_ column of the inherent `GeoDataFrame` is set up at initialization + - latitude and longitude column are no longer present there (the according arrays can be retrieved as properties of the Exposures object: `exp.latitude` instead of `exp.gdf.latitude.values`). + - `Exposures.gdf` has been renamed to `Exposures.data` (it still works though, as it is a property now pointing to the latter) + - the `check` method does not add a default "IMPF_" column to the GeoDataFrame anymore ### Fixed diff --git a/climada/engine/impact_calc.py b/climada/engine/impact_calc.py index d0fc05286..3b4345581 100644 --- a/climada/engine/impact_calc.py +++ b/climada/engine/impact_calc.py @@ -116,6 +116,7 @@ def impact(self, save_mat=True, assign_centroids=True, apply_deductible_to_mat : apply deductible to impact matrix apply_cover_to_mat : apply cover to impact matrix """ + # TODO: consider refactoring, making use of Exposures.hazard_impf # check for compatibility of exposures and hazard type if all(name not in self.exposures.gdf.columns for name in ['if_', f'if_{self.hazard.haz_type}', diff --git a/climada/entity/exposures/base.py b/climada/entity/exposures/base.py index f456b5daa..57d0d9caa 100644 --- a/climada/entity/exposures/base.py +++ b/climada/entity/exposures/base.py @@ -226,9 +226,8 @@ def hazard_centroids(self, haz_type=""): return self.data[INDICATOR_CENTR].values raise ValueError("Missing hazard centroids.") - @property - def _meta(self): - """Metadata dictionary, containing raster information derived from geometry""" + def derive_raster(self): + """Metadata dictionary, containing raster information, derived from the geometry""" if not self.data.size: return None _r, meta = u_coord.points_to_raster(self.data) @@ -388,7 +387,9 @@ def __init__(self, def __str__(self): return '\n'.join( [f"{md}: {self.__dict__[md]}" for md in type(self)._metadata] + - [f"crs: {self.crs}", "data:", str(self.gdf)] + [f"crs: {self.crs}", f"data: ({self.data.shape[0]} entries)", + str(self.data) if self.data.shape[0] < 10 else + str(pd.concat([self.data[:4], self.data[-4:]]))] ) def _access_item(self, *args): @@ -403,8 +404,6 @@ def check(self): """Check Exposures consistency. Reports missing columns in log messages. - If no ``impf_*`` column is present in the dataframe, a default column ``impf_`` is added - with default impact function id 1. """ # mandatory columns for var in self.vars_oblig: @@ -427,8 +426,7 @@ def check(self): col for col in self.gdf.columns if col.startswith(INDICATOR_IMPF) or col.startswith(INDICATOR_IMPF_OLD) ]: - LOGGER.info("Setting %s to default impact functions ids 1.", INDICATOR_IMPF) - self.gdf[INDICATOR_IMPF] = 1 + LOGGER.warning("There are no impact functions assigned to the exposures") # optional columns except centr_* for var in sorted(set(self.vars_opt).difference([INDICATOR_CENTR])): diff --git a/climada/entity/exposures/test/test_base.py b/climada/entity/exposures/test/test_base.py index 22027caf6..d79321de8 100644 --- a/climada/entity/exposures/test/test_base.py +++ b/climada/entity/exposures/test/test_base.py @@ -532,30 +532,28 @@ def test_get_impf_column(self): self.assertRaises(ValueError, expo.get_impf_column, 'HAZ') # removed impf column - expo.gdf.drop(columns='impf_NA', inplace=True) + expo.data.drop(columns='impf_NA', inplace=True) self.assertRaises(ValueError, expo.get_impf_column, 'NA') self.assertRaises(ValueError, expo.get_impf_column) # default (anonymous) impf column - expo.check() + expo.data['impf_'] = 1 self.assertEqual('impf_', expo.get_impf_column()) self.assertEqual('impf_', expo.get_impf_column('HAZ')) # rename impf column to old style column name - expo.gdf.rename(columns={'impf_': 'if_'}, inplace=True) - expo.check() + expo.data.rename(columns={'impf_': 'if_'}, inplace=True) self.assertEqual('if_', expo.get_impf_column()) self.assertEqual('if_', expo.get_impf_column('HAZ')) # rename impf column to old style column name - expo.gdf.rename(columns={'if_': 'if_NA'}, inplace=True) - expo.check() + expo.data.rename(columns={'if_': 'if_NA'}, inplace=True) self.assertEqual('if_NA', expo.get_impf_column('NA')) self.assertRaises(ValueError, expo.get_impf_column) self.assertRaises(ValueError, expo.get_impf_column, 'HAZ') # add anonymous impf column - expo.gdf['impf_'] = expo.region_id + expo.data['impf_'] = expo.region_id self.assertEqual('if_NA', expo.get_impf_column('NA')) self.assertEqual('impf_', expo.get_impf_column()) self.assertEqual('impf_', expo.get_impf_column('HAZ')) diff --git a/doc/tutorial/climada_entity_Exposures.ipynb b/doc/tutorial/climada_entity_Exposures.ipynb index 9289ed15b..08a89b407 100644 --- a/doc/tutorial/climada_entity_Exposures.ipynb +++ b/doc/tutorial/climada_entity_Exposures.ipynb @@ -28,8 +28,9 @@ "\n", "### What does an exposure look like in CLIMADA?\n", "\n", - "An exposure is represented in the class `Exposures`, which contains a [geopandas](https://geopandas.readthedocs.io/en/latest/gallery/cartopy_convert.html) [GeoDataFrame](https://geopandas.readthedocs.io/en/latest/docs/user_guide/data_structures.html#geodataframe) that is accessible through the `Exposures` `data` attribute.\n", - "Certain columns of `data` will be present for each `Exposures` object, others are optional but have a special meaning within `Exposures` and others are fully optional and just contain additional data.\n", + "An exposure is represented in the class `Exposures`, which contains a [geopandas](https://geopandas.readthedocs.io/en/latest/gallery/cartopy_convert.html) [GeoDataFrame](https://geopandas.readthedocs.io/en/latest/docs/user_guide/data_structures.html#geodataframe) that is accessible through the `Exposures.data` attribute.\n", + "A \"geometry\" column is initialized in the `GeoDataFrame` of the `Exposures` object, other columns are optional at first but some have to be present or make a difference when it comes to do calculations.\n", + "Apart from these special columns the data frame may contain additional columns, they will simply be ignored in the context of CLIMADA.\n", "\n", "The full list of meaningful columns is this:" ] @@ -39,24 +40,16 @@ "metadata": {}, "source": [ "\n", - "
\n", - "\n", - "| Mandatory columns | Data Type | Description |\n", - "| :-------------------- | :------------ | :------------------------------------------------------------------------------------- |\n", - "| `geometry` | shapely.Point | the geometry column of the `GeoDataFrame`, i.e., longitude (x) and latitude (y) |\n", - "| `value` | float | a value for each exposure |\n", - "\n", - "
\n", - "
\n", - "\n", - "| Optional columns | Data Type | Description |\n", - "| :-------------------- | :------------ | :------------------------------------------------------------------------------------- |\n", - "| `region_id` | int | region id (e.g. country ISO code) for each exposure |\n", - "| `category_id` | int | category id (e.g. building code) for each exposure |\n", - "| `impf_*` | int | impact functions ids for hazard types.
important attribute, since it relates the exposures to the hazard by specifying the impf_act functions.
Ideally it should be set to the specific hazard (e.g. `impf_TC`) so that different hazards can be set
in the same Exposures (e.g. `impf_TC` and `impf_FL`).
If not provided, set to default `impf_` with ids 1 in check(). |\n", - "| `centr_*` | int | centroids index for hazard type.
There might be different hazards defined: centr_TC, centr_FL, ...
Computed in method `assign_centroids()` |\n", - "| `deductible` | float | deductible value for each exposure.
Used for insurance |\n", - "| `cover` | float | cover value for each exposure.
Used for insurance |" + "| Column | Data Type | Description | Meaningful in |\n", + "| :-------------------- | :------------ | :------------------------------------------------------------------------------------- | - |\n", + "| `geometry` | Point | the geometry column of the `GeoDataFrame`, i.e., latitude (y) and longitude (x) | centroids assignment |\n", + "| `value` | float | a value for each exposure | impact calculation |\n", + "| `impf_*` | int | impact functions ids for hazard types.
important attribute, since it relates the exposures to the hazard by specifying the impf_act functions.
Ideally it should be set to the specific hazard (e.g. `impf_TC`) so that different hazards can be set
in the same Exposures (e.g. `impf_TC` and `impf_FL`). | impact calculation |\n", + "| `centr_*` | int | centroids index for hazard type.
There might be different hazards defined: centr_TC, centr_FL, ...
Computed in method `assign_centroids()` | impact calculation |\n", + "| `deductible` | float | deductible value for each exposure.
Used for insurance | impact calculation |\n", + "| `cover` | float | cover value for each exposure.
Used for insurance | impact calculation |\n", + "| `region_id` | int | region id (e.g. country ISO code) for each exposure | aggregation |\n", + "| `category_id` | int | category id (e.g. building code) for each exposure | aggregation |" ] }, { @@ -103,25 +96,68 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Exposures from a pandas DataFrame\n", - "\n", - "In case you are unfamiliar with the data structure, check out the [pandas DataFrame documentation](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html)." + "### Exposures from plain data" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 46, "metadata": {}, "outputs": [ { - "name": "stderr", + "name": "stdout", "output_type": "stream", "text": [ - "/Users/lseverino/miniforge3/envs/climada_env/lib/python3.9/site-packages/dask/dataframe/_pyarrow_compat.py:17: FutureWarning: Minimal version of pyarrow will soon be increased to 14.0.1. You are using 12.0.1. Please consider upgrading.\n", - " warnings.warn(\n" + "description: random values in a square\n", + "ref_year: 2018\n", + "value_unit: CHF\n", + "crs: EPSG:7316\n", + "data: (9 entries)\n", + " region_id impf_ geometry value\n", + "0 1 0 POINT (4.000 1.000) 0.035321\n", + "1 1 1 POINT (4.000 2.000) 0.570256\n", + "2 1 2 POINT (4.000 3.000) 0.927632\n", + "3 1 3 POINT (5.000 1.000) 0.805402\n", + "4 1 4 POINT (5.000 2.000) 0.236179\n", + "5 1 5 POINT (5.000 3.000) 0.848296\n", + "6 1 6 POINT (6.000 1.000) 0.520281\n", + "7 1 7 POINT (6.000 2.000) 0.036442\n", + "8 1 8 POINT (6.000 3.000) 0.780934\n" ] } ], + "source": [ + "import numpy as np\n", + "from climada.entity import Exposures\n", + "\n", + "latitude = [1, 2, 3] * 3\n", + "longitude = [4] * 3 + [5] * 3 + [6] * 3\n", + "exp_arr = Exposures(\n", + " lat=latitude, # list or array\n", + " lon=longitude, # instead of lat and lon one can provide an array of Points through the geometry argument\n", + " value= np.random.random_sample(len(latitude)), # a list or an array of floats\n", + " value_unit='CHF',\n", + " crs='EPSG:7316', # different formats are possible\n", + " description=\"random values in a square\",\n", + " data={'region_id':1, 'impf_': range(len(latitude))} # data can also be an array or a data frame\n", + ")\n", + "print(exp_arr)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Exposures from a pandas DataFrame\n", + "\n", + "In case you are unfamiliar with the data structure, check out the [pandas DataFrame documentation](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html)." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], "source": [ "import numpy as np\n", "from pandas import DataFrame\n", @@ -132,7 +168,7 @@ "exp_df = DataFrame()\n", "n_exp = 100*100\n", "# provide value\n", - "exp_df['value'] = np.arange(n_exp)\n", + "exp_df['value'] = np.random.random_sample(n_exp)\n", "# provide latitude and longitude\n", "lat, lon = np.mgrid[15 : 35 : complex(0, np.sqrt(n_exp)), 20 : 40 : complex(0, np.sqrt(n_exp))]\n", "exp_df['latitude'] = lat.flatten()\n", @@ -141,47 +177,153 @@ }, { "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], - "source": [ - "# For each exposure entry, specify which impact function should be taken for which hazard type.\n", - "# In this case, we only specify the IDs for tropical cyclone (TC); here, each exposure entry will be treated with\n", - "# the same impact function: the one that has ID '1':\n", - "# Of course, this will only be relevant at later steps during impact calculations.\n", - "exp_df['impf_TC'] = np.ones(n_exp, int)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, + "execution_count": 23, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "exp_df is a DataFrame: \n", - "exp_df looks like:\n", - " value latitude longitude impf_TC\n", - "0 0 15.0 20.000000 1\n", - "1 1 15.0 20.202020 1\n", - "2 2 15.0 20.404040 1\n", - "3 3 15.0 20.606061 1\n", - "4 4 15.0 20.808081 1\n" - ] + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
valuelatitudelongitudeimpf_TC
00.53376415.020.0000001
10.99599315.020.2020201
20.60352315.020.4040401
30.75425315.020.6060611
40.30506615.020.8080811
...............
99950.48241635.039.1919191
99960.06904435.039.3939391
99970.11656035.039.5959601
99980.23985635.039.7979801
99990.09956835.040.0000001
\n", + "

10000 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " value latitude longitude impf_TC\n", + "0 0.533764 15.0 20.000000 1\n", + "1 0.995993 15.0 20.202020 1\n", + "2 0.603523 15.0 20.404040 1\n", + "3 0.754253 15.0 20.606061 1\n", + "4 0.305066 15.0 20.808081 1\n", + "... ... ... ... ...\n", + "9995 0.482416 35.0 39.191919 1\n", + "9996 0.069044 35.0 39.393939 1\n", + "9997 0.116560 35.0 39.595960 1\n", + "9998 0.239856 35.0 39.797980 1\n", + "9999 0.099568 35.0 40.000000 1\n", + "\n", + "[10000 rows x 4 columns]" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "# Let's have a look at the pandas DataFrame\n", - "print('exp_df is a DataFrame:', str(type(exp_df)))\n", - "print('exp_df looks like:')\n", - "print(exp_df.head())" + "# For each exposure entry, specify which impact function should be taken for which hazard type.\n", + "# In this case, we only specify the IDs for tropical cyclone (TC); here, each exposure entry will be treated with\n", + "# the same impact function: the one that has ID '1':\n", + "# Of course, this will only be relevant at later steps during impact calculations.\n", + "exp_df['impf_TC'] = np.ones(n_exp, int)\n", + "exp_df" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 24, "metadata": {}, "outputs": [ { @@ -190,22 +332,7 @@ "text": [ "exp has the type: \n", "and contains a GeoDataFrame exp.gdf: \n", - "2024-04-12 14:39:01,086 - climada.util.coordinates - INFO - Setting geometry points.\n", - "\n", - "check method logs:\n", - "2024-04-12 14:39:01,093 - climada.entity.exposures.base - INFO - category_id not set.\n", - "2024-04-12 14:39:01,093 - climada.entity.exposures.base - INFO - cover not set.\n", - "2024-04-12 14:39:01,093 - climada.entity.exposures.base - INFO - deductible not set.\n", - "2024-04-12 14:39:01,094 - climada.entity.exposures.base - INFO - region_id not set.\n", - "2024-04-12 14:39:01,094 - climada.entity.exposures.base - INFO - centr_ not set.\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/lseverino/Documents/PhD/workspace/climada_python/climada/util/coordinates.py:2755: FutureWarning: You are adding a column named 'geometry' to a GeoDataFrame constructed without an active geometry column. Currently, this automatically sets the active geometry column to 'geometry' but in the future that will no longer happen. Instead, either provide geometry to the GeoDataFrame constructor (GeoDataFrame(... geometry=GeoSeries()) or use `set_geometry('geometry')` to explicitly set the active geometry column.\n", - " df_val['geometry'] = gpd.GeoSeries(\n" + "\n" ] } ], @@ -213,51 +340,38 @@ "# Generate Exposures from the pandas DataFrame. This step converts the DataFrame into\n", "# a CLIMADA Exposures instance!\n", "exp = Exposures(exp_df)\n", - "print('exp has the type:', str(type(exp)))\n", - "print('and contains a GeoDataFrame exp.gdf:', str(type(exp.gdf)))\n", - "\n", - "# Apply the check() method in the end. It puts metadata that has not been assigned,\n", - "# and points out missing mandatory data\n", - "exp.check()" + "print(f\"exp has the type: {type(exp)}\")\n", + "print(f\"and contains a GeoDataFrame exp.gdf: {type(exp.gdf)}\\n\")" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "\n", - "exp looks like:\n", "description: None\n", "ref_year: 2018\n", "value_unit: USD\n", - "meta: {'crs': 'EPSG:4326'}\n", "crs: EPSG:4326\n", - "data:\n", - " value latitude longitude impf_TC geometry\n", - "0 0 15.0 20.000000 1 POINT (20.00000 15.00000)\n", - "1 1 15.0 20.202020 1 POINT (20.20202 15.00000)\n", - "2 2 15.0 20.404040 1 POINT (20.40404 15.00000)\n", - "3 3 15.0 20.606061 1 POINT (20.60606 15.00000)\n", - "4 4 15.0 20.808081 1 POINT (20.80808 15.00000)\n", - "... ... ... ... ... ...\n", - "9995 9995 35.0 39.191919 1 POINT (39.19192 35.00000)\n", - "9996 9996 35.0 39.393939 1 POINT (39.39394 35.00000)\n", - "9997 9997 35.0 39.595960 1 POINT (39.59596 35.00000)\n", - "9998 9998 35.0 39.797980 1 POINT (39.79798 35.00000)\n", - "9999 9999 35.0 40.000000 1 POINT (40.00000 35.00000)\n", - "\n", - "[10000 rows x 5 columns]\n" + "data: (10000 entries)\n", + " value impf_TC geometry\n", + "0 0.533764 1 POINT (20.00000 15.00000)\n", + "1 0.995993 1 POINT (20.20202 15.00000)\n", + "2 0.603523 1 POINT (20.40404 15.00000)\n", + "3 0.754253 1 POINT (20.60606 15.00000)\n", + "9996 0.069044 1 POINT (39.39394 35.00000)\n", + "9997 0.116560 1 POINT (39.59596 35.00000)\n", + "9998 0.239856 1 POINT (39.79798 35.00000)\n", + "9999 0.099568 1 POINT (40.00000 35.00000)\n" ] } ], "source": [ "# let's have a look at the Exposures instance we created!\n", - "print('\\n' + 'exp looks like:')\n", "print(exp)" ] }, @@ -274,28 +388,14 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "metadata": {}, "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "World is a GeoDataFrame: \n", - "World looks like:\n", - " name geometry\n", - "0 Vatican City POINT (12.45339 41.90328)\n", - "1 San Marino POINT (12.44177 43.93610)\n", - "2 Vaduz POINT (9.51667 47.13372)\n", - "3 Lobamba POINT (31.20000 -26.46667)\n", - "4 Luxembourg POINT (6.13000 49.61166)\n" - ] - }, { "name": "stderr", "output_type": "stream", "text": [ - "/var/folders/y5/t1z41tgj7dv50sm2_29dn8740000gp/T/ipykernel_16894/4205155986.py:6: FutureWarning: The geopandas.dataset module is deprecated and will be removed in GeoPandas 1.0. You can get the original 'naturalearth_cities' data from https://www.naturalearthdata.com/downloads/110m-cultural-vectors/.\n", + "C:\\Users\\me\\AppData\\Local\\Temp\\ipykernel_31104\\2272990317.py:6: FutureWarning: The geopandas.dataset module is deprecated and will be removed in GeoPandas 1.0. You can get the original 'naturalearth_cities' data from https://www.naturalearthdata.com/downloads/110m-cultural-vectors/.\n", " world = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))\n" ] } @@ -306,54 +406,66 @@ "from climada.entity import Exposures\n", "\n", "# Read spatial info from an external file into GeoDataFrame\n", - "world = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))\n", - "print('World is a GeoDataFrame:', str(type(world)))\n", - "print('World looks like:')\n", - "print(world.head())" + "world = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "\n", - "exp_gpd is an Exposures: \n", - "2024-04-12 14:41:09,131 - climada.entity.exposures.base - INFO - Setting latitude and longitude attributes.\n" + "description: None\n", + "ref_year: 2018\n", + "value_unit: USD\n", + "crs: EPSG:4326\n", + "data: (243 entries)\n", + " name value geometry\n", + "0 Vatican City 0.876947 POINT (12.45339 41.90328)\n", + "1 San Marino 0.895454 POINT (12.44177 43.93610)\n", + "2 Vaduz 0.373366 POINT (9.51667 47.13372)\n", + "3 Lobamba 0.422729 POINT (31.20000 -26.46667)\n", + "239 São Paulo 0.913955 POINT (-46.62697 -23.55673)\n", + "240 Sydney 0.514479 POINT (151.21255 -33.87137)\n", + "241 Singapore 0.830635 POINT (103.85387 1.29498)\n", + "242 Hong Kong 0.764571 POINT (114.18306 22.30693)\n" ] } ], "source": [ "# Generate Exposures: value, latitude and longitude for each exposure entry.\n", + "world['value'] = np.random.random_sample(world.shape[0])\n", "# Convert GeoDataFrame into Exposure instance\n", "exp_gpd = Exposures(world)\n", - "print('\\n' + 'exp_gpd is an Exposures:', str(type(exp_gpd)))\n", - "# add random values to entries\n", - "exp_gpd.gdf['value'] = np.arange(world.shape[0])\n", - "# set latitude and longitude attributes from geometry\n", - "exp_gpd.set_lat_lon()" + "print(exp_gpd)" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "\n", - "check method logs:\n", - "2024-04-12 14:41:24,338 - climada.entity.exposures.base - INFO - category_id not set.\n", - "2024-04-12 14:41:24,341 - climada.entity.exposures.base - INFO - cover not set.\n", - "2024-04-12 14:41:24,343 - climada.entity.exposures.base - INFO - deductible not set.\n", - "2024-04-12 14:41:24,344 - climada.entity.exposures.base - INFO - region_id not set.\n", - "2024-04-12 14:41:24,344 - climada.entity.exposures.base - INFO - centr_ not set.\n" + "description: None\n", + "ref_year: 2018\n", + "value_unit: USD\n", + "crs: EPSG:4326\n", + "data: (243 entries)\n", + " name value geometry impf_TC\n", + "0 Vatican City 0.876947 POINT (12.45339 41.90328) 1\n", + "1 San Marino 0.895454 POINT (12.44177 43.93610) 1\n", + "2 Vaduz 0.373366 POINT (9.51667 47.13372) 1\n", + "3 Lobamba 0.422729 POINT (31.20000 -26.46667) 1\n", + "239 São Paulo 0.913955 POINT (-46.62697 -23.55673) 1\n", + "240 Sydney 0.514479 POINT (151.21255 -33.87137) 1\n", + "241 Singapore 0.830635 POINT (103.85387 1.29498) 1\n", + "242 Hong Kong 0.764571 POINT (114.18306 22.30693) 1\n" ] } ], @@ -362,73 +474,7 @@ "# In this case, we only specify the IDs for tropical cyclone (TC); here, each exposure entry will be treated with\n", "# the same impact function: the one that has ID '1':\n", "# Of course, this will only be relevant at later steps during impact calculations.\n", - "exp_gpd.gdf['impf_TC'] = np.ones(world.shape[0], int)\n", - "print('\\n' + 'check method logs:')\n", - "\n", - "# as always, run check method to assign meta-data and check for missing mandatory variables.\n", - "exp_gpd.check()" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\u001b[1;03;30;30mexp_gpd looks like:\u001b[0m\n", - "ref_year: 2018\n", - "value_unit: USD\n", - "meta: {'crs': \n", - "Name: WGS 84\n", - "Axis Info [ellipsoidal]:\n", - "- Lat[north]: Geodetic latitude (degree)\n", - "- Lon[east]: Geodetic longitude (degree)\n", - "Area of Use:\n", - "- name: World.\n", - "- bounds: (-180.0, -90.0, 180.0, 90.0)\n", - "Datum: World Geodetic System 1984\n", - "- Ellipsoid: WGS 84\n", - "- Prime Meridian: Greenwich\n", - "}\n", - "crs: epsg:4326\n", - "data:\n", - " name geometry value latitude longitude \\\n", - "0 Vatican City POINT (12.45339 41.90328) 0 41.903282 12.453387 \n", - "1 San Marino POINT (12.44177 43.93610) 1 43.936096 12.441770 \n", - "2 Vaduz POINT (9.51667 47.13372) 2 47.133724 9.516669 \n", - "3 Luxembourg POINT (6.13000 49.61166) 3 49.611660 6.130003 \n", - "4 Palikir POINT (158.14997 6.91664) 4 6.916644 158.149974 \n", - ".. ... ... ... ... ... \n", - "197 Cairo POINT (31.24802 30.05191) 197 30.051906 31.248022 \n", - "198 Tokyo POINT (139.74946 35.68696) 198 35.686963 139.749462 \n", - "199 Paris POINT (2.33139 48.86864) 199 48.868639 2.331389 \n", - "200 Santiago POINT (-70.66899 -33.44807) 200 -33.448068 -70.668987 \n", - "201 Singapore POINT (103.85387 1.29498) 201 1.294979 103.853875 \n", - "\n", - " impf_TC \n", - "0 1 \n", - "1 1 \n", - "2 1 \n", - "3 1 \n", - "4 1 \n", - ".. ... \n", - "197 1 \n", - "198 1 \n", - "199 1 \n", - "200 1 \n", - "201 1 \n", - "\n", - "[202 rows x 6 columns]\n" - ] - } - ], - "source": [ - "# let's have a look at the Exposures instance we created!\n", - "print('\\n' + '\\x1b[1;03;30;30m' + 'exp_gpd looks like:' + '\\x1b[0m')\n", + "exp_gpd.data['impf_TC'] = np.ones(world.shape[0], int)\n", "print(exp_gpd)" ] }, @@ -441,7 +487,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 30, "metadata": {}, "outputs": [ { @@ -474,73 +520,197 @@ " \n", " \n", " name\n", - " geometry\n", " value\n", - " latitude\n", - " longitude\n", " impf_TC\n", + " geometry\n", " \n", " \n", " \n", " \n", " 11\n", " Tarawa\n", - " POINT (173.01757 1.33819)\n", - " 11\n", - " 1.338188\n", - " 173.017571\n", + " 0.107688\n", " 1\n", + " POINT (173.01757 1.33819)\n", " \n", " \n", " 15\n", " Kigali\n", - " POINT (30.05859 -1.95164)\n", - " 15\n", - " -1.951644\n", - " 30.058586\n", + " 0.218687\n", " 1\n", + " POINT (30.05859 -1.95164)\n", " \n", " \n", " 17\n", " Juba\n", - " POINT (31.58003 4.82998)\n", - " 17\n", - " 4.829975\n", - " 31.580026\n", + " 0.763743\n", " 1\n", + " POINT (31.58003 4.82998)\n", " \n", " \n", " 31\n", " Putrajaya\n", - " POINT (101.69504 2.93252)\n", - " 31\n", - " 2.932515\n", - " 101.695037\n", + " 0.533607\n", " 1\n", + " POINT (101.69504 2.93252)\n", " \n", " \n", " 37\n", " Bujumbura\n", + " 0.127881\n", + " 1\n", " POINT (29.36001 -3.37609)\n", - " 37\n", - " -3.376087\n", - " 29.360006\n", + " \n", + " \n", + " 58\n", + " Kampala\n", + " 0.079019\n", " 1\n", + " POINT (32.58138 0.31860)\n", + " \n", + " \n", + " 75\n", + " Mogadishu\n", + " 0.696766\n", + " 1\n", + " POINT (45.36473 2.06863)\n", + " \n", + " \n", + " 88\n", + " Quito\n", + " 0.212070\n", + " 1\n", + " POINT (-78.50200 -0.21304)\n", + " \n", + " \n", + " 93\n", + " Malabo\n", + " 0.088459\n", + " 1\n", + " POINT (8.78328 3.75002)\n", + " \n", + " \n", + " 99\n", + " Libreville\n", + " 0.929139\n", + " 1\n", + " POINT (9.45796 0.38539)\n", + " \n", + " \n", + " 108\n", + " Brazzaville\n", + " 0.795766\n", + " 1\n", + " POINT (15.28274 -4.25724)\n", + " \n", + " \n", + " 113\n", + " Bandar Seri Begawan\n", + " 0.655856\n", + " 1\n", + " POINT (114.93328 4.88333)\n", + " \n", + " \n", + " 116\n", + " Bangui\n", + " 0.398002\n", + " 1\n", + " POINT (18.55829 4.36664)\n", + " \n", + " \n", + " 117\n", + " Yaoundé\n", + " 0.240599\n", + " 1\n", + " POINT (11.51470 3.86865)\n", + " \n", + " \n", + " 134\n", + " Victoria\n", + " 0.956208\n", + " 1\n", + " POINT (55.44999 -4.61663)\n", + " \n", + " \n", + " 135\n", + " São Tomé\n", + " 0.726704\n", + " 1\n", + " POINT (6.72965 0.33747)\n", + " \n", + " \n", + " 138\n", + " Malé\n", + " 0.996017\n", + " 1\n", + " POINT (73.50890 4.17204)\n", + " \n", + " \n", + " 158\n", + " Kuala Lumpur\n", + " 0.880473\n", + " 1\n", + " POINT (101.68870 3.13980)\n", + " \n", + " \n", + " 201\n", + " Kinshasa\n", + " 0.074387\n", + " 1\n", + " POINT (15.31303 -4.32778)\n", + " \n", + " \n", + " 228\n", + " Nairobi\n", + " 0.297170\n", + " 1\n", + " POINT (36.81471 -1.28140)\n", + " \n", + " \n", + " 230\n", + " Bogota\n", + " 0.420891\n", + " 1\n", + " POINT (-74.08529 4.59837)\n", + " \n", + " \n", + " 241\n", + " Singapore\n", + " 0.830635\n", + " 1\n", + " POINT (103.85387 1.29498)\n", " \n", " \n", "\n", "" ], "text/plain": [ - " name geometry value latitude longitude impf_TC\n", - "11 Tarawa POINT (173.01757 1.33819) 11 1.338188 173.017571 1\n", - "15 Kigali POINT (30.05859 -1.95164) 15 -1.951644 30.058586 1\n", - "17 Juba POINT (31.58003 4.82998) 17 4.829975 31.580026 1\n", - "31 Putrajaya POINT (101.69504 2.93252) 31 2.932515 101.695037 1\n", - "37 Bujumbura POINT (29.36001 -3.37609) 37 -3.376087 29.360006 1" + " name value impf_TC geometry\n", + "11 Tarawa 0.107688 1 POINT (173.01757 1.33819)\n", + "15 Kigali 0.218687 1 POINT (30.05859 -1.95164)\n", + "17 Juba 0.763743 1 POINT (31.58003 4.82998)\n", + "31 Putrajaya 0.533607 1 POINT (101.69504 2.93252)\n", + "37 Bujumbura 0.127881 1 POINT (29.36001 -3.37609)\n", + "58 Kampala 0.079019 1 POINT (32.58138 0.31860)\n", + "75 Mogadishu 0.696766 1 POINT (45.36473 2.06863)\n", + "88 Quito 0.212070 1 POINT (-78.50200 -0.21304)\n", + "93 Malabo 0.088459 1 POINT (8.78328 3.75002)\n", + "99 Libreville 0.929139 1 POINT (9.45796 0.38539)\n", + "108 Brazzaville 0.795766 1 POINT (15.28274 -4.25724)\n", + "113 Bandar Seri Begawan 0.655856 1 POINT (114.93328 4.88333)\n", + "116 Bangui 0.398002 1 POINT (18.55829 4.36664)\n", + "117 Yaoundé 0.240599 1 POINT (11.51470 3.86865)\n", + "134 Victoria 0.956208 1 POINT (55.44999 -4.61663)\n", + "135 São Tomé 0.726704 1 POINT (6.72965 0.33747)\n", + "138 Malé 0.996017 1 POINT (73.50890 4.17204)\n", + "158 Kuala Lumpur 0.880473 1 POINT (101.68870 3.13980)\n", + "201 Kinshasa 0.074387 1 POINT (15.31303 -4.32778)\n", + "228 Nairobi 0.297170 1 POINT (36.81471 -1.28140)\n", + "230 Bogota 0.420891 1 POINT (-74.08529 4.59837)\n", + "241 Singapore 0.830635 1 POINT (103.85387 1.29498)" ] }, - "execution_count": 11, + "execution_count": 30, "metadata": {}, "output_type": "execute_result" } @@ -548,15 +718,15 @@ "source": [ "# Example 1: extract data in a region: latitudes between -5 and 5\n", "sel_exp = exp_gpd.copy() # to keep the original exp_gpd Exposures data\n", - "sel_exp.gdf = sel_exp.gdf.cx[:, -5:5]\n", + "sel_exp.data = sel_exp.data.cx[:, -5:5]\n", "\n", "print('\\n' + 'sel_exp contains a subset of the original data')\n", - "sel_exp.gdf.head()" + "sel_exp.data" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 29, "metadata": {}, "outputs": [ { @@ -589,83 +759,69 @@ " \n", " \n", " name\n", - " geometry\n", " value\n", - " latitude\n", - " longitude\n", " impf_TC\n", + " geometry\n", " \n", " \n", " \n", " \n", " 36\n", " Porto-Novo\n", - " POINT (2.61663 6.48331)\n", - " 36\n", - " 6.483311\n", - " 2.616626\n", + " 0.573619\n", " 1\n", + " POINT (2.61663 6.48331)\n", " \n", " \n", " 46\n", " Lomé\n", - " POINT (1.22081 6.13388)\n", - " 46\n", - " 6.133883\n", - " 1.220811\n", + " 0.176892\n", " 1\n", + " POINT (1.22081 6.13388)\n", " \n", " \n", " 93\n", " Malabo\n", - " POINT (8.78328 3.75002)\n", - " 93\n", - " 3.750015\n", - " 8.783278\n", + " 0.088459\n", " 1\n", + " POINT (8.78328 3.75002)\n", " \n", " \n", " 123\n", " Cotonou\n", - " POINT (2.40435 6.36298)\n", - " 123\n", - " 6.362980\n", - " 2.404355\n", + " 0.441703\n", " 1\n", + " POINT (2.40435 6.36298)\n", " \n", " \n", " 135\n", " São Tomé\n", - " POINT (6.72965 0.33747)\n", - " 135\n", - " 0.337466\n", - " 6.729650\n", + " 0.726704\n", " 1\n", + " POINT (6.72965 0.33747)\n", " \n", " \n", " 225\n", " Lagos\n", - " POINT (3.38959 6.44521)\n", - " 225\n", - " 6.445208\n", - " 3.389585\n", + " 0.990135\n", " 1\n", + " POINT (3.38959 6.44521)\n", " \n", " \n", "\n", "" ], "text/plain": [ - " name geometry value latitude longitude impf_TC\n", - "36 Porto-Novo POINT (2.61663 6.48331) 36 6.483311 2.616626 1\n", - "46 Lomé POINT (1.22081 6.13388) 46 6.133883 1.220811 1\n", - "93 Malabo POINT (8.78328 3.75002) 93 3.750015 8.783278 1\n", - "123 Cotonou POINT (2.40435 6.36298) 123 6.362980 2.404355 1\n", - "135 São Tomé POINT (6.72965 0.33747) 135 0.337466 6.729650 1\n", - "225 Lagos POINT (3.38959 6.44521) 225 6.445208 3.389585 1" + " name value impf_TC geometry\n", + "36 Porto-Novo 0.573619 1 POINT (2.61663 6.48331)\n", + "46 Lomé 0.176892 1 POINT (1.22081 6.13388)\n", + "93 Malabo 0.088459 1 POINT (8.78328 3.75002)\n", + "123 Cotonou 0.441703 1 POINT (2.40435 6.36298)\n", + "135 São Tomé 0.726704 1 POINT (6.72965 0.33747)\n", + "225 Lagos 0.990135 1 POINT (3.38959 6.44521)" ] }, - "execution_count": 12, + "execution_count": 29, "metadata": {}, "output_type": "execute_result" } @@ -676,23 +832,22 @@ "sel_polygon = exp_gpd.copy() # to keep the original exp_gpd Exposures data\n", "\n", "poly = Polygon([(0, -10), (0, 10), (10, 5)])\n", - "sel_polygon.gdf = sel_polygon.gdf[sel_polygon.gdf.intersects(poly)]\n", + "sel_polygon.data = sel_polygon.gdf[sel_polygon.gdf.intersects(poly)]\n", "\n", "# Let's have a look. Again, the sub-selection is a GeoDataFrame!\n", "print('\\n' + 'sel_exp contains a subset of the original data')\n", - "sel_polygon.gdf" + "sel_polygon.data" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "2024-04-12 14:42:09,423 - climada.entity.exposures.base - INFO - Setting latitude and longitude attributes.\n", "\n", "the crs has changed to EPSG:3395\n", "the values for latitude and longitude are now according to the new coordinate system: \n" @@ -720,91 +875,69 @@ " \n", " \n", " name\n", - " geometry\n", " value\n", - " latitude\n", - " longitude\n", " impf_TC\n", + " geometry\n", " \n", " \n", " \n", " \n", " 36\n", " Porto-Novo\n", - " POINT (291281.418 718442.692)\n", - " 36\n", - " 718442.691819\n", - " 291281.418257\n", + " 0.573619\n", " 1\n", + " POINT (291281.418 718442.692)\n", " \n", " \n", " 46\n", " Lomé\n", - " POINT (135900.092 679566.331)\n", - " 46\n", - " 679566.330586\n", - " 135900.092271\n", + " 0.176892\n", " 1\n", + " POINT (135900.092 679566.331)\n", " \n", " \n", " 93\n", " Malabo\n", - " POINT (977749.979 414955.553)\n", - " 93\n", - " 414955.553292\n", - " 977749.978796\n", + " 0.088459\n", " 1\n", + " POINT (977749.979 414955.553)\n", " \n", " \n", " 123\n", " Cotonou\n", - " POINT (267651.551 705052.049)\n", - " 123\n", - " 705052.049006\n", - " 267651.551008\n", + " 0.441703\n", " 1\n", + " POINT (267651.551 705052.049)\n", " \n", " \n", " 135\n", " São Tomé\n", - " POINT (749141.190 37315.322)\n", - " 135\n", - " 37315.322206\n", - " 749141.189651\n", + " 0.726704\n", " 1\n", + " POINT (749141.190 37315.322)\n", " \n", " \n", " 225\n", " Lagos\n", - " POINT (377326.898 714202.107)\n", - " 225\n", - " 714202.106826\n", - " 377326.898464\n", + " 0.990135\n", " 1\n", + " POINT (377326.898 714202.107)\n", " \n", " \n", "\n", "" ], "text/plain": [ - " name geometry value latitude \\\n", - "36 Porto-Novo POINT (291281.418 718442.692) 36 718442.691819 \n", - "46 Lomé POINT (135900.092 679566.331) 46 679566.330586 \n", - "93 Malabo POINT (977749.979 414955.553) 93 414955.553292 \n", - "123 Cotonou POINT (267651.551 705052.049) 123 705052.049006 \n", - "135 São Tomé POINT (749141.190 37315.322) 135 37315.322206 \n", - "225 Lagos POINT (377326.898 714202.107) 225 714202.106826 \n", - "\n", - " longitude impf_TC \n", - "36 291281.418257 1 \n", - "46 135900.092271 1 \n", - "93 977749.978796 1 \n", - "123 267651.551008 1 \n", - "135 749141.189651 1 \n", - "225 377326.898464 1 " + " name value impf_TC geometry\n", + "36 Porto-Novo 0.573619 1 POINT (291281.418 718442.692)\n", + "46 Lomé 0.176892 1 POINT (135900.092 679566.331)\n", + "93 Malabo 0.088459 1 POINT (977749.979 414955.553)\n", + "123 Cotonou 0.441703 1 POINT (267651.551 705052.049)\n", + "135 São Tomé 0.726704 1 POINT (749141.190 37315.322)\n", + "225 Lagos 0.990135 1 POINT (377326.898 714202.107)" ] }, - "execution_count": 13, + "execution_count": 31, "metadata": {}, "output_type": "execute_result" } @@ -815,20 +948,20 @@ "sel_polygon.to_crs(epsg=3395, inplace=True)\n", "print('\\n' + 'the crs has changed to ' +str(sel_polygon.crs))\n", "print('the values for latitude and longitude are now according to the new coordinate system: ')\n", - "sel_polygon.gdf" + "sel_polygon.data" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "exp_all type and number of rows: 25\n", - "number of unique rows: 23\n" + "exp_all type and number of rows: 28\n", + "number of unique rows: 26\n" ] }, { @@ -853,80 +986,61 @@ " \n", " \n", " name\n", - " geometry\n", " value\n", - " latitude\n", - " longitude\n", " impf_TC\n", + " geometry\n", " \n", " \n", " \n", " \n", - " 0\n", - " Lome\n", - " POINT (135900.088 679566.334)\n", - " 36\n", - " 679566.333952\n", - " 1.359001e+05\n", + " 23\n", + " Kuala Lumpur\n", + " 0.880473\n", " 1\n", + " POINT (11319934.225 347356.996)\n", " \n", " \n", - " 1\n", - " Malabo\n", - " POINT (977749.984 414955.551)\n", - " 84\n", - " 414955.550857\n", - " 9.777500e+05\n", + " 24\n", + " Kinshasa\n", + " 0.074387\n", " 1\n", + " POINT (1704638.257 -479002.730)\n", " \n", " \n", - " 2\n", - " Cotonou\n", - " POINT (280307.458 709388.810)\n", - " 113\n", - " 709388.810160\n", - " 2.803075e+05\n", + " 25\n", + " Nairobi\n", + " 0.297170\n", " 1\n", + " POINT (4098194.882 -141701.948)\n", " \n", " \n", - " 3\n", - " Sao Tome\n", - " POINT (749550.327 36865.909)\n", - " 125\n", - " 36865.908682\n", - " 7.495503e+05\n", + " 26\n", + " Bogota\n", + " 0.420891\n", " 1\n", + " POINT (-8247136.736 509015.405)\n", " \n", " \n", - " 4\n", - " Tarawa\n", - " POINT (19260227.883 147982.749)\n", - " 9\n", - " 147982.748978\n", - " 1.926023e+07\n", + " 27\n", + " Singapore\n", + " 0.830635\n", " 1\n", + " POINT (11560960.460 143203.754)\n", " \n", " \n", "\n", "" ], "text/plain": [ - " name geometry value latitude \\\n", - "0 Lome POINT (135900.088 679566.334) 36 679566.333952 \n", - "1 Malabo POINT (977749.984 414955.551) 84 414955.550857 \n", - "2 Cotonou POINT (280307.458 709388.810) 113 709388.810160 \n", - "3 Sao Tome POINT (749550.327 36865.909) 125 36865.908682 \n", - "4 Tarawa POINT (19260227.883 147982.749) 9 147982.748978 \n", - "\n", - " longitude impf_TC \n", - "0 1.359001e+05 1 \n", - "1 9.777500e+05 1 \n", - "2 2.803075e+05 1 \n", - "3 7.495503e+05 1 \n", - "4 1.926023e+07 1 " + " name value impf_TC geometry\n", + "23 Kuala Lumpur 0.880473 1 POINT (11319934.225 347356.996)\n", + "24 Kinshasa 0.074387 1 POINT (1704638.257 -479002.730)\n", + "25 Nairobi 0.297170 1 POINT (4098194.882 -141701.948)\n", + "26 Bogota 0.420891 1 POINT (-8247136.736 509015.405)\n", + "27 Singapore 0.830635 1 POINT (11560960.460 143203.754)" ] }, - "execution_count": 13, + "execution_count": 35, "metadata": {}, "output_type": "execute_result" } @@ -940,7 +1054,7 @@ "print('number of unique rows:', exp_all.gdf.drop_duplicates().shape[0])\n", "\n", "# NaNs will appear in the missing values\n", - "exp_all.gdf.head()" + "exp_all.data.tail()" ] }, { @@ -966,7 +1080,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 42, "metadata": {}, "outputs": [ { @@ -1102,7 +1216,7 @@ "4 1 1 5 1 5 " ] }, - "execution_count": 15, + "execution_count": 42, "metadata": {}, "output_type": "execute_result" } @@ -1131,7 +1245,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 43, "metadata": {}, "outputs": [ { @@ -1139,193 +1253,37 @@ "output_type": "stream", "text": [ "\n", - "exp_templ is now an Exposures: \n", - "\n", - "set_geometry logs:\n", - "2024-04-12 14:44:29,822 - climada.util.coordinates - INFO - Setting geometry points.\n", + "exp_templ is now an Exposures: description: None\n", + "ref_year: 2018\n", + "value_unit: USD\n", + "crs: EPSG:4326\n", + "data: (24 entries)\n", + " value deductible cover region_id category_id impf_TC \\\n", + "0 1.392750e+10 0 1.392750e+10 1 1 1 \n", + "1 1.259606e+10 0 1.259606e+10 1 1 1 \n", + "2 1.259606e+10 0 1.259606e+10 1 1 1 \n", + "3 1.259606e+10 0 1.259606e+10 1 1 1 \n", + "20 1.259760e+10 0 1.259760e+10 1 1 1 \n", + "21 1.281454e+10 0 1.281454e+10 1 1 1 \n", + "22 1.262176e+10 0 1.262176e+10 1 1 1 \n", + "23 1.259754e+10 0 1.259754e+10 1 1 1 \n", "\n", - "check exp_templ:\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/lseverino/Documents/PhD/workspace/climada_python/climada/util/coordinates.py:2755: FutureWarning: You are adding a column named 'geometry' to a GeoDataFrame constructed without an active geometry column. Currently, this automatically sets the active geometry column to 'geometry' but in the future that will no longer happen. Instead, either provide geometry to the GeoDataFrame constructor (GeoDataFrame(... geometry=GeoSeries()) or use `set_geometry('geometry')` to explicitly set the active geometry column.\n", - " df_val['geometry'] = gpd.GeoSeries(\n" + " centr_TC impf_FL centr_FL geometry \n", + "0 1 1 1 POINT (-80.12880 26.93390) \n", + "1 2 1 2 POINT (-80.09828 26.95720) \n", + "2 3 1 3 POINT (-80.74895 26.78385) \n", + "3 4 1 4 POINT (-80.55070 26.64552) \n", + "20 21 1 21 POINT (-80.06858 26.71255) \n", + "21 22 1 22 POINT (-80.09070 26.66490) \n", + "22 23 1 23 POINT (-80.12540 26.66470) \n", + "23 24 1 24 POINT (-80.15140 26.66315) \n" ] } ], "source": [ "# Generate an Exposures instance from the dataframe.\n", "exp_templ = Exposures(exp_templ)\n", - "print('\\n' + 'exp_templ is now an Exposures:', str(type(exp_templ)))\n", - "\n", - "# run check method to include metadata and check for missing mandatory parameters\n", - "print('\\n' + 'check exp_templ:')\n", - "exp_templ.check()" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "exp_templ.gdf looks like:\n" - ] - }, - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
latitudelongitudevaluedeductiblecoverregion_idcategory_idimpf_TCcentr_TCimpf_FLcentr_FLgeometry
026.933899-80.1287991.392750e+1001.392750e+10111111POINT (-80.12880 26.93390)
126.957203-80.0982841.259606e+1001.259606e+10111212POINT (-80.09828 26.95720)
226.783846-80.7489471.259606e+1001.259606e+10111313POINT (-80.74895 26.78385)
326.645524-80.5507041.259606e+1001.259606e+10111414POINT (-80.55070 26.64552)
426.897796-80.5969291.259606e+1001.259606e+10111515POINT (-80.59693 26.89780)
\n", - "
" - ], - "text/plain": [ - " latitude longitude value deductible cover region_id \\\n", - "0 26.933899 -80.128799 1.392750e+10 0 1.392750e+10 1 \n", - "1 26.957203 -80.098284 1.259606e+10 0 1.259606e+10 1 \n", - "2 26.783846 -80.748947 1.259606e+10 0 1.259606e+10 1 \n", - "3 26.645524 -80.550704 1.259606e+10 0 1.259606e+10 1 \n", - "4 26.897796 -80.596929 1.259606e+10 0 1.259606e+10 1 \n", - "\n", - " category_id impf_TC centr_TC impf_FL centr_FL \\\n", - "0 1 1 1 1 1 \n", - "1 1 1 2 1 2 \n", - "2 1 1 3 1 3 \n", - "3 1 1 4 1 4 \n", - "4 1 1 5 1 5 \n", - "\n", - " geometry \n", - "0 POINT (-80.12880 26.93390) \n", - "1 POINT (-80.09828 26.95720) \n", - "2 POINT (-80.74895 26.78385) \n", - "3 POINT (-80.55070 26.64552) \n", - "4 POINT (-80.59693 26.89780) " - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Let's have a look at our Exposures instance!\n", - "print('\\n' + 'exp_templ.gdf looks like:')\n", - "exp_templ.gdf.head()" + "print('\\n' + 'exp_templ is now an Exposures:', exp_templ)" ] }, { @@ -1346,7 +1304,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "2024-04-12 14:44:52,508 - climada.util.coordinates - INFO - Reading /Users/lseverino/climada/demo/data/SC22000_VE__M1.grd.gz\n" + "2024-10-04 17:19:03,632 - climada.util.coordinates - INFO - Reading C:\\Users\\me\\climada\\demo\\data\\SC22000_VE__M1.grd.gz\n" ] } ], @@ -1371,122 +1329,36 @@ "name": "stdout", "output_type": "stream", "text": [ - "2024-04-12 14:44:53,848 - climada.entity.exposures.base - INFO - Setting impf_ to default impact functions ids 1.\n", - "2024-04-12 14:44:53,849 - climada.entity.exposures.base - INFO - category_id not set.\n", - "2024-04-12 14:44:53,850 - climada.entity.exposures.base - INFO - cover not set.\n", - "2024-04-12 14:44:53,850 - climada.entity.exposures.base - INFO - deductible not set.\n", - "2024-04-12 14:44:53,851 - climada.entity.exposures.base - INFO - geometry not set.\n", - "2024-04-12 14:44:53,851 - climada.entity.exposures.base - INFO - region_id not set.\n", - "2024-04-12 14:44:53,852 - climada.entity.exposures.base - INFO - centr_ not set.\n", - "Meta: {'driver': 'GSBG', 'dtype': 'float32', 'nodata': 1.701410009187828e+38, 'width': 50, 'height': 60, 'count': 1, 'crs': CRS.from_epsg(4326), 'transform': Affine(0.009000000000000341, 0.0, -69.2471495969998,\n", - " 0.0, -0.009000000000000341, 10.248220966978932)}\n" - ] - } - ], - "source": [ - "# As always, run the check method, such that metadata can be assigned and checked for missing mandatory parameters.\n", - "exp_raster.check()\n", - "print('Meta:', exp_raster.meta)" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "exp_raster looks like:\n" + "2024-10-04 17:19:03,725 - climada.util.coordinates - INFO - Raster from resolution 0.009000000000000341 to 0.009000000000000341.\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", - "
longitudelatitudevalueimpf_
0-69.2426510.2437210.01
1-69.2336510.2437210.01
2-69.2246510.2437210.01
3-69.2156510.2437210.01
4-69.2066510.2437210.01
\n", - "
" - ], "text/plain": [ - " longitude latitude value impf_\n", - "0 -69.24265 10.243721 0.0 1\n", - "1 -69.23365 10.243721 0.0 1\n", - "2 -69.22465 10.243721 0.0 1\n", - "3 -69.21565 10.243721 0.0 1\n", - "4 -69.20665 10.243721 0.0 1" + "{'crs': \n", + " Name: WGS 84\n", + " Axis Info [ellipsoidal]:\n", + " - Lat[north]: Geodetic latitude (degree)\n", + " - Lon[east]: Geodetic longitude (degree)\n", + " Area of Use:\n", + " - name: World.\n", + " - bounds: (-180.0, -90.0, 180.0, 90.0)\n", + " Datum: World Geodetic System 1984 ensemble\n", + " - Ellipsoid: WGS 84\n", + " - Prime Meridian: Greenwich,\n", + " 'height': 60,\n", + " 'width': 50,\n", + " 'transform': Affine(0.009000000000000341, 0.0, -69.2471495969998,\n", + " 0.0, -0.009000000000000341, 10.248220966978932)}" ] }, - "execution_count": 21, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "# Let's have a look at the Exposures instance!\n", - "print('\\n' + 'exp_raster looks like:')\n", - "exp_raster.gdf.head()" + "exp_raster.derive_raster()" ] }, { @@ -1504,14 +1376,46 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "\n" + "2024-10-04 17:19:04,888 - climada.entity.exposures.base - INFO - Reading C:\\Users\\me\\climada\\demo\\data\\exp_demo_today.h5\n", + "description: None\n", + "ref_year: 2016\n", + "value_unit: USD\n", + "crs: EPSG:4326\n", + "data: (50 entries)\n", + " value impf_TC deductible cover category_id region_id \\\n", + "0 1.392750e+10 1 0.0 1.392750e+10 1 1.0 \n", + "1 1.259606e+10 1 0.0 1.259606e+10 1 1.0 \n", + "2 1.259606e+10 1 0.0 1.259606e+10 1 1.0 \n", + "3 1.259606e+10 1 0.0 1.259606e+10 1 1.0 \n", + "46 1.264524e+10 1 0.0 1.264524e+10 1 1.0 \n", + "47 1.281438e+10 1 0.0 1.281438e+10 1 1.0 \n", + "48 1.260291e+10 1 0.0 1.260291e+10 1 1.0 \n", + "49 1.262482e+10 1 0.0 1.262482e+10 1 1.0 \n", + "\n", + " geometry \n", + "0 POINT (-80.12880 26.93390) \n", + "1 POINT (-80.09828 26.95720) \n", + "2 POINT (-80.74895 26.78385) \n", + "3 POINT (-80.55070 26.64552) \n", + "46 POINT (-80.11640 26.34907) \n", + "47 POINT (-80.08385 26.34635) \n", + "48 POINT (-80.24130 26.34802) \n", + "49 POINT (-80.15886 26.34796) \n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "c:\\Users\\me\\miniconda3\\envs\\climada_env\\Lib\\pickle.py:1718: UserWarning: Unpickling a shapely <2.0 geometry object. Please save the pickle again; shapely 2.1 will not have this compatibility.\n", + " setstate(state)\n" ] } ], @@ -1521,17 +1425,7 @@ "from climada.util.constants import EXP_DEMO_H5\n", "\n", "exp_hdf5 = Exposures.from_hdf5(EXP_DEMO_H5)\n", - "exp_hdf5.check()\n", - "print(type(exp_hdf5))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Before you leave ...\n", - "\n", - "After defining an `Exposures` instance use always the `check()` method to see which attributes are missing. This method will raise an ERROR if `value`, `longitude` or `latitude` ar missing and an INFO messages for the the optional variables not set." + "print(exp_hdf5)" ] }, { @@ -1804,7 +1698,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.18" + "version": "3.11.9" }, "latex_envs": { "LaTeX_envs_menu_present": true,