Skip to content

Commit

Permalink
centroids.centr.from_excel: update method for gdf
Browse files Browse the repository at this point in the history
  • Loading branch information
sarah-hlsn committed Dec 5, 2023
1 parent 5e4e03d commit a426247
Showing 1 changed file with 42 additions and 33 deletions.
75 changes: 42 additions & 33 deletions climada/hazard/centroids/centr.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,31 +257,6 @@ 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['lon']], df[var_names['lat']])
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 Expand Up @@ -675,6 +650,37 @@ def from_vector_file(cls, file_name, dst_crs=None):
if dst_crs is not None:
centroids.gdf.geometry.to_crs(dst_crs, inplace=True)
return centroids

Check warning on line 653 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 654 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=None, var_names=None):
"""
Generate centroids from a csv file with column names in var_names.

Check warning on line 659 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. Default: DEF_VAR_CSV

Check warning on line 668 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 with data from the given csv file
"""
if crs is None:
crs = DEF_CRS

if var_names is None:
var_names = DEF_VAR_CSV

df = pd.read_csv(file_path)

Check warning on line 680 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['lon']], df[var_names['lat']])
gdf = gpd.GeoDataFrame(df, crs=crs, geometry=geometry)
return cls.from_geodataframe(gdf)

#TODO: this method is badly written but kept for backwards compatibility. It should be improved.

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

View check run for this annotation

Jenkins - WCR / Pylint

fixme

NORMAL: TODO: this method is badly written but kept for backwards compatibility. It should be improved.
Raw output
no description found
@classmethod
Expand All @@ -685,6 +691,8 @@ def from_excel(cls, file_name, var_names=None):
----------
file_name : str
absolute or relative file name
crs : dict() or rasterio.crs.CRS, optional
CRS. Default: DEF_CRS
var_names : dict, default
name of the variables
Expand All @@ -695,27 +703,28 @@ def from_excel(cls, file_name, var_names=None):
Returns
-------
centr : Centroids
Centroids with data from the given file
Centroids with data from the given excel file
"""
if crs is None:
crs = DEF_VAR_EXCEL

if var_names is None:
var_names = DEF_VAR_EXCEL

try:
dfr = pd.read_excel(file_name, var_names['sheet_name'])
df = pd.read_excel(file_name, var_names['sheet_name'])

Check warning on line 715 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...).
try:
region_id = dfr[var_names['col_name']['region_id']]
region_id = df[var_names['col_name']['region_id']]

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

View check run for this annotation

Jenkins - WCR / Pylint

unused-variable

NORMAL: Unused variable 'region_id'
Raw output
Used when a variable is defined but not used.
except KeyError:
region_id = None
pass

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

View check run for this annotation

Jenkins - WCR / Pylint

unnecessary-pass

NORMAL: Unnecessary pass statement
Raw output
Used when a "pass" statement that can be avoided is encountered.

except KeyError as err:
raise KeyError("Not existing variable: %s" % str(err)) from err

return cls(
longitude=dfr[var_names['col_name']['lon']],
latitude=dfr[var_names['col_name']['lat']],
region_id=region_id
)
geometry = gpd.points_from_xy(df[var_names['lon']], df[var_names['lat']])
gdf = gpd.GeoDataFrame(df, crs=crs, geometry=geometry)
return cls.from_geodataframe(gdf)

def write_hdf5(self, file_name, mode='w'):
"""Write data frame and metadata in hdf5 format
Expand Down

0 comments on commit a426247

Please sign in to comment.