Skip to content

Commit

Permalink
Improve cosmetics.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chahan Kropf committed Dec 13, 2023
1 parent a748ee0 commit 652a2b8
Showing 1 changed file with 91 additions and 86 deletions.
177 changes: 91 additions & 86 deletions climada/hazard/centroids/centr.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,58 +359,6 @@ def union(self, *others):
# remove duplicate points
return Centroids.remove_duplicate_points(centroids)

def get_closest_point(self, x_lon, y_lat):
"""Returns closest centroid and its index to a given point.
Parameters
----------
x_lon : float
x coord (lon)
y_lat : float
y coord (lat)
Returns
-------
x_close : float
x-coordinate (longitude) of closest centroid.
y_close : float
y-coordinate (latitude) of closest centroids.
idx_close : int
Index of centroid in internal ordering of centroids.
"""
close_idx = self.geometry.distance(Point(x_lon, y_lat)).values.argmin()
return self.lon[close_idx], self.lat[close_idx], close_idx

# NOT REALLY AN ELEVATION FUNCTION, JUST READ RASTER
def get_elevation(self, topo_path):
"""Return elevation attribute for every pixel or point in meters.
Parameters
----------
topo_path : str
Path to a raster file containing gridded elevation data.
"""
return u_coord.read_raster_sample(topo_path, self.lat, self.lon)

def get_dist_coast(self, signed=False, precomputed=False):
"""Get dist_coast attribute for every pixel or point in meters.
Parameters
----------
signed : bool
If True, use signed distances (positive off shore and negative on land). Default: False.
precomputed : bool
If True, use precomputed distances (from NASA). Works only for crs=epsg:4326
Default: False.
"""
ne_geom = self._ne_crs_geom()
if precomputed:
return u_coord.dist_to_coast_nasa(
ne_geom.y.values, ne_geom.x.values, highres=True, signed=signed)
else:
LOGGER.debug('Computing distance to coast for %s centroids.', str(self.size))
return u_coord.dist_to_coast(ne_geom, signed=signed)

@classmethod
def remove_duplicate_points(cls, centroids):
"""Return a copy of centroids with removed duplicated points
Expand Down Expand Up @@ -491,7 +439,7 @@ def select_mask(self, sel_cen=None, reg_id=None, extent=None):
return sel_cen

#TODO replace with nice Geodataframe util plot method.

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

View check run for this annotation

Jenkins - WCR / Pylint

fixme

NORMAL: TODO replace with nice Geodataframe util plot method.
Raw output
no description found
def plot(self, ax=None, figsize=(9, 13), latlon_bounds_buffer=0.0, **kwargs):
def plot(self, ax=None, figsize=(9, 13), latlon_bounds_buffer=0.0, shapes=True, **kwargs):

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

View check run for this annotation

Jenkins - WCR / Pylint

invalid-name

LOW: Argument name "ax" 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...).
"""Plot centroids scatter points over earth.
Parameters
Expand Down Expand Up @@ -523,7 +471,9 @@ def plot(self, ax=None, figsize=(9, 13), latlon_bounds_buffer=0.0, **kwargs):
else:
self.gdf.copy().to_crs(proj_plot).plot(figsize=figsize, **kwargs)

u_plot.add_shapes(ax)
if shapes:
u_plot.add_shapes(ax)

plt.tight_layout()
return ax

Expand Down Expand Up @@ -553,6 +503,89 @@ def get_area_pixel(self, min_resol=1.0e-8):
area_pixel = xy_pixels.to_crs(crs={'proj': 'cea'}).area.values
return area_pixel

def get_closest_point(self, x_lon, y_lat):
"""Returns closest centroid and its index to a given point.
Parameters
----------
x_lon : float
x coord (lon)
y_lat : float
y coord (lat)
Returns
-------
x_close : float
x-coordinate (longitude) of closest centroid.
y_close : float
y-coordinate (latitude) of closest centroids.
idx_close : int
Index of centroid in internal ordering of centroids.
"""
close_idx = self.geometry.distance(Point(x_lon, y_lat)).values.argmin()
return self.lon[close_idx], self.lat[close_idx], close_idx

# NOT REALLY AN ELEVATION FUNCTION, JUST READ RASTER
def get_elevation(self, topo_path):
"""Return elevation attribute for every pixel or point in meters.
Parameters
----------
topo_path : str
Path to a raster file containing gridded elevation data.
"""
return u_coord.read_raster_sample(topo_path, self.lat, self.lon)

def get_dist_coast(self, signed=False, precomputed=False):
"""Get dist_coast attribute for every pixel or point in meters.
Parameters
----------
signed : bool
If True, use signed distances (positive off shore and negative on land). Default: False.
precomputed : bool
If True, use precomputed distances (from NASA). Works only for crs=epsg:4326
Default: False.
"""
ne_geom = self._ne_crs_geom()
if precomputed:

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

View check run for this annotation

Jenkins - WCR / Pylint

no-else-return

LOW: Unnecessary "else" after "return"
Raw output
Used in order to highlight an unnecessary block of code following an ifcontaining a return statement. As such, it will warn when it encounters anelse following a chain of ifs, all of them containing a return statement.
return u_coord.dist_to_coast_nasa(
ne_geom.y.values, ne_geom.x.values, highres=True, signed=signed)
else:
LOGGER.debug('Computing distance to coast for %s centroids.', str(self.size))
return u_coord.dist_to_coast(ne_geom, signed=signed)
def get_meta(self, resolution=None):
"""
Returns a meta raster based on the centroids bounds.
When resolution is None it is estimated from the centroids
by assuming that they form a regular raster.
Parameters
----------
resolution : int, optional
Resolution of the raster.
By default None (resolution is estimated from centroids)
Returns
-------
meta: dict
meta raster representation of the centroids
"""
if resolution is None:
resolution = np.abs(u_coord.get_resolution(self.lat, self.lon)).min()
xmin, ymin, xmax, ymax = self.gdf.total_bounds
rows, cols, ras_trans = u_coord.pts_to_raster_meta(
(xmin, ymin, xmax, ymax), (resolution, -resolution)

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

View check run for this annotation

Jenkins - WCR / Pylint

invalid-unary-operand-type

HIGH: bad operand type for unary -: NoneType
Raw output
no description found
)
meta = {
'crs': self.crs,
'height': rows,
'width': cols,
'transform': ras_trans,
}
return meta


'''

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

View check run for this annotation

Jenkins - WCR / Pylint

pointless-string-statement

NORMAL: String statement has no effect
Raw output
Used when a string is used as a statement (which of course has no effect).This is a particular case of W0104 with its own message so you can easilydisable it if you're using those strings as documentation, instead ofcomments.
I/O methods
Expand Down Expand Up @@ -810,6 +843,10 @@ def _legacy_from_hdf5(cls, data):
geometry=gpd.points_from_xy(x=longitude, y=latitude, crs=crs)
)

'''

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

View check run for this annotation

Jenkins - WCR / Pylint

pointless-string-statement

NORMAL: String statement has no effect
Raw output
Used when a string is used as a statement (which of course has no effect).This is a particular case of W0104 with its own message so you can easilydisable it if you're using those strings as documentation, instead ofcomments.
Private methods
'''


def _ne_crs_geom(self):
"""Return `geometry` attribute in the CRS of Natural Earth.
Expand All @@ -822,38 +859,6 @@ def _ne_crs_geom(self):
return self.gdf.geometry
return self.gdf.geometry.to_crs(u_coord.NE_CRS)

def get_meta(self, resolution=None):
"""
Returns a meta raster based on the centroids bounds.
When resolution is None it is estimated from the centroids
by assuming that they form a regular raster.
Parameters
----------
resolution : int, optional
Resolution of the raster.
By default None (resolution is estimated from centroids)
Returns
-------
meta: dict
meta raster representation of the centroids
"""
if resolution is None:
resolution = np.abs(u_coord.get_resolution(self.lat, self.lon)).min()
xmin, ymin, xmax, ymax = self.gdf.total_bounds
rows, cols, ras_trans = u_coord.pts_to_raster_meta(
(xmin, ymin, xmax, ymax), (resolution, -resolution)
)
meta = {
'crs': self.crs,
'height': rows,
'width': cols,
'transform': ras_trans,
}
return meta

def _set_region_id(self, level='country', overwrite=False):
"""Set region_id as country ISO numeric code attribute for every pixel or point.
Expand Down

0 comments on commit 652a2b8

Please sign in to comment.