Skip to content

Commit

Permalink
centroids.centr.from_csv: write class method
Browse files Browse the repository at this point in the history
  • Loading branch information
sarah-hlsn committed Dec 4, 2023
1 parent 1ab1b69 commit 19b02a5
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion climada/hazard/centroids/centr.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@
}
"""Excel variable names"""

DEF_VAR_CSV = {
'lat': 'latitude',
'lon': 'longitude',
}
"""CSV variable names"""

LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -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
----------
Expand All @@ -251,6 +257,31 @@ def from_exposures(cls, exposures):
"""
gdf = exposures.gdf[['geometry', 'region_id']]
return cls.from_geodataframe(gdf)

Check warning on line 260 in climada/hazard/centroids/centr.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
#TODO: Check whether other variables are necessary, e.g. dist to coast

Check warning on line 261 in climada/hazard/centroids/centr.py

View check run for this annotation

Jenkins - WCR / Pylint

fixme

NORMAL: TODO: Check whether other variables are necessary, e.g. dist to coast
Raw output
no description found
@classmethod
def from_csv(cls, file_path, crs=DEF_CRS, var_names=DEF_VAR_CSV):

Check warning on line 263 in climada/hazard/centroids/centr.py

View check run for this annotation

Jenkins - WCR / Pylint

dangerous-default-value

NORMAL: Dangerous default value DEF_VAR_CSV (builtins.dict) as argument
Raw output
Used when a mutable value as list or dictionary is detected in a default valuefor an argument.
"""
Generate centroids from a csv file.

Check warning on line 266 in climada/hazard/centroids/centr.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
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

Check warning on line 275 in climada/hazard/centroids/centr.py

View check run for this annotation

Jenkins - WCR / Pylint

trailing-whitespace

LOW: Trailing whitespace
Raw output
Used when there is whitespace between the end of a line and the newline.
Returns
-------
Centroids
Centroids built from the csv file
"""
df = pd.read_csv(file_path)

Check warning on line 281 in climada/hazard/centroids/centr.py

View check run for this annotation

Jenkins - WCR / Pylint

invalid-name

LOW: Variable name "df" doesn't conform to '(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$' pattern
Raw output
Used when the name doesn't match the regular expression associated to its type(constant, variable, class...).
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):
Expand Down

0 comments on commit 19b02a5

Please sign in to comment.