From 19b02a563d7a479b9a75eeb51cf1a87ec5612b03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sarah=20H=C3=BClsen?= <49907095+sarah-hlsn@users.noreply.github.com> Date: Mon, 4 Dec 2023 15:37:42 +0100 Subject: [PATCH] centroids.centr.from_csv: write class method --- climada/hazard/centroids/centr.py | 33 ++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/climada/hazard/centroids/centr.py b/climada/hazard/centroids/centr.py index 00a7af1966..54c1565d6e 100644 --- a/climada/hazard/centroids/centr.py +++ b/climada/hazard/centroids/centr.py @@ -74,6 +74,12 @@ } """Excel variable names""" +DEF_VAR_CSV = { + 'lat': 'latitude', + 'lon': 'longitude', +} +"""CSV variable names""" + LOGGER = logging.getLogger(__name__) @@ -236,7 +242,7 @@ def from_geodataframe(cls, gdf): @classmethod def from_exposures(cls, exposures): """ - Generate centroids form the location of an exposures. + Generate centroids from the location of an exposures. Parameters ---------- @@ -251,6 +257,31 @@ def from_exposures(cls, exposures): """ gdf = exposures.gdf[['geometry', 'region_id']] return cls.from_geodataframe(gdf) + + #TODO: Check whether other variables are necessary, e.g. dist to coast + @classmethod + def from_csv(cls, file_path, crs=DEF_CRS, var_names=DEF_VAR_CSV): + """ + Generate centroids from a csv file. + + Parameters + ---------- + file_path : str + path to csv file to be read + crs : dict() or rasterio.crs.CRS, optional + CRS. Default: DEF_CRS + var_names : dict, default + name of the variables + + Returns + ------- + Centroids + Centroids built from the csv file + """ + df = pd.read_csv(file_path) + geometry = gpd.points_from_xy(df[var_names['lat']], df[var_names['lon']]) + gdf = gpd.GeoDataFrame(df, crs=crs, geometry=geometry) + return cls.from_geodataframe(gdf) @classmethod def from_pnt_bounds(cls, points_bounds, res, crs=DEF_CRS):