climada.hazard.centroids package#
climada.hazard.centroids.centr module#
- class climada.hazard.centroids.centr.Centroids(lat: ndarray | None = None, lon: ndarray | None = None, geometry: GeoSeries | None = None, meta: Dict[Any, Any] | None = None, area_pixel: ndarray | None = None, on_land: ndarray | None = None, region_id: ndarray | None = None, elevation: ndarray | None = None, dist_coast: ndarray | None = None)[source]#
Bases:
object
Contains raster or vector centroids.
- meta#
rasterio meta dictionary containing raster properties: width, height, crs and transform must be present at least. The affine ransformation needs to be shearless (only stretching) and have positive x- and negative y-orientation.
- Type:
dict, optional
- lat#
latitude of size size
- Type:
np.array, optional
- lon#
longitude of size size
- Type:
np.array, optional
- geometry#
contains lat and lon crs. Might contain geometry points for lat and lon
- Type:
gpd.GeoSeries, optional
- area_pixel#
area of size size
- Type:
np.array, optional
- dist_coast#
distance to coast of size size
- Type:
np.array, optional
- on_land#
on land (True) and on sea (False) of size size
- Type:
np.array, optional
- region_id#
country region code of size size
- Type:
np.array, optional
- elevation#
elevation of size size
- Type:
np.array, optional
- vars_check = {'area_pixel', 'dist_coast', 'elevation', 'geometry', 'lat', 'lon', 'on_land', 'region_id'}#
Variables whose size will be checked
- __init__(lat: ndarray | None = None, lon: ndarray | None = None, geometry: GeoSeries | None = None, meta: Dict[Any, Any] | None = None, area_pixel: ndarray | None = None, on_land: ndarray | None = None, region_id: ndarray | None = None, elevation: ndarray | None = None, dist_coast: ndarray | None = None)[source]#
Initialization
- Parameters:
lat (np.array, optional) – latitude of size size. Defaults to empty array
lon (np.array, optional) – longitude of size size. Defaults to empty array
geometry (gpd.GeoSeries, optional) – contains lat and lon crs. Might contain geometry points for lat and lon. Defaults to empty gpd.Geoseries with crs=DEF_CRS
meta (dict, optional) – rasterio meta dictionary containing raster properties: width, height, crs and transform must be present at least. The affine ransformation needs to be shearless (only stretching) and have positive x- and negative y-orientation. Defaults to empty dict()
area_pixel (np.array, optional) – area of size size. Defaults to empty array
on_land (np.array, optional) – on land (True) and on sea (False) of size size. Defaults to empty array
region_id (np.array, optional) – country region code of size size, Defaults to empty array
elevation (np.array, optional) – elevation of size size. Defaults to empty array
dist_coast (np.array, optional) – distance to coast of size size. Defaults to empty array
- check()[source]#
Check integrity of stored information.
Checks that either meta attribute is set, or lat, lon and geometry.crs. Checks sizes of (optional) data attributes.
- equal(centr)[source]#
Return True if two centroids equal, False otherwise
- Parameters:
centr (Centroids) – centroids to compare
- Returns:
eq
- Return type:
bool
- static from_base_grid(land=False, res_as=360, base_file=None)[source]#
Initialize from base grid data provided with CLIMADA
- Parameters:
land (bool, optional) – If True, restrict to grid points on land. Default: False.
res_as (int, optional) – Base grid resolution in arc-seconds (one of 150, 360). Default: 360.
base_file (str, optional) – If set, read this file instead of one provided with climada.
- classmethod from_geodataframe(gdf, geometry_alias='geom')[source]#
Create Centroids instance from GeoDataFrame.
Deprecated since version 3.3: This method will be removed in a future version. Pass the data you want to construct the Centroids with to the constructor instead.
The geometry, lat, and lon attributes are set from the GeoDataFrame.geometry attribute, while the columns are copied as attributes to the Centroids object in the form of numpy.ndarrays using pandas.Series.to_numpy. The Series dtype will thus be respected.
Columns named lat or lon are ignored, as they would overwrite the coordinates extracted from the point features. If the geometry attribute bears an alias, it can be dropped by setting the geometry_alias parameter.
If the GDF includes a region_id column, but no on_land column, then on_land=True is inferred for those centroids that have a set region_id.
Example
>>> gdf = geopandas.read_file('centroids.shp') >>> gdf.region_id = gdf.region_id.astype(int) # type coercion >>> centroids = Centroids.from_geodataframe(gdf)
- Parameters:
gdf (GeoDataFrame) – Where the geometry column needs to consist of point features. See above for details on processing.
geometry_alias (str, opt) – Alternate name for the geometry column; dropped to avoid duplicate assignment.
- Returns:
centr – Centroids with data from given GeoDataFrame
- Return type:
- classmethod from_pix_bounds(xf_lat, xo_lon, d_lat, d_lon, n_lat, n_lon, crs='EPSG:4326')[source]#
Create Centroids object with meta attribute according to pixel border data.
Deprecated since version 3.3: This method will be removed in a future version. CLIMADA will only support regular grids with a constant lat/lon resolution then. Use
from_pnt_bounds()
instead.- Parameters:
xf_lat (float) – upper latitude (top)
xo_lon (float) – left longitude
d_lat (float) – latitude step (negative)
d_lon (float) – longitude step (positive)
n_lat (int) – number of latitude points
n_lon (int) – number of longitude points
crs (dict() or rasterio.crs.CRS, optional) – CRS. Default: DEF_CRS
- Returns:
centr – Centroids with meta according to given pixel border data.
- Return type:
- set_raster_from_pnt_bounds(*args, **kwargs)[source]#
This function is deprecated, use Centroids.from_pnt_bounds instead.
- classmethod from_pnt_bounds(points_bounds, res, crs='EPSG:4326')[source]#
Create Centroids object with meta attribute according to points border data.
raster border = point border + res/2
- Parameters:
points_bounds (tuple) – points’ lon_min, lat_min, lon_max, lat_max
res (float) – desired resolution in same units as points_bounds
crs (dict() or rasterio.crs.CRS, optional) – CRS. Default: DEF_CRS
- Returns:
centr – Centroids with meta according to given points border data.
- Return type:
- set_lat_lon(*args, **kwargs)[source]#
This function is deprecated, use Centroids.from_lat_lon instead.
- classmethod from_lat_lon(lat, lon, crs='EPSG:4326')[source]#
Create Centroids object from given latitude, longitude and CRS.
- Parameters:
lat (np.array) – latitude
lon (np.array) – longitude
crs (dict() or rasterio.crs.CRS, optional) – CRS. Default: DEF_CRS
- Returns:
centr – Centroids with points according to given coordinates
- Return type:
- set_raster_file(file_name, band=None, **kwargs)[source]#
This function is deprecated, use Centroids.from_raster_file and Centroids.values_from_raster_files instead.
- classmethod from_raster_file(file_name, src_crs=None, window=None, geometry=None, dst_crs=None, transform=None, width=None, height=None, resampling=Resampling.nearest)[source]#
Create a new Centroids object from a raster file
Select region using window or geometry. Reproject input by providing dst_crs and/or (transform, width, height).
- Parameters:
file_name (str) – path of the file
src_crs (crs, optional) – source CRS. Provide it if error without it.
window (rasterio.windows.Window, optional) – window to read
geometry (list of shapely.geometry, optional) – consider pixels only within these shapes
dst_crs (crs, optional) – reproject to given crs
transform (rasterio.Affine) – affine transformation to apply
wdith (float) – number of lons for transform
height (float) – number of lats for transform
resampling (rasterio.warp,.Resampling optional) – resampling function used for reprojection to dst_crs
- Returns:
centr – Centroids with meta attribute according to the given raster file
- Return type:
- values_from_raster_files(file_names, band=None, src_crs=None, window=None, geometry=None, dst_crs=None, transform=None, width=None, height=None, resampling=Resampling.nearest)[source]#
Read raster of bands and set 0 values to the masked ones.
Each band is an event. Select region using window or geometry. Reproject input by proving dst_crs and/or (transform, width, height).
- Parameters:
file_names (str) – path of the file
band (list(int), optional) – band number to read. Default: [1]
src_crs (crs, optional) – source CRS. Provide it if error without it.
window (rasterio.windows.Window, optional) – window to read
geometry (list of shapely.geometry, optional) – consider pixels only within these shapes
dst_crs (crs, optional) – reproject to given crs
transform (rasterio.Affine) – affine transformation to apply
wdith (float) – number of lons for transform
height (float) – number of lats for transform
resampling (rasterio.warp,.Resampling optional) – resampling function used for reprojection to dst_crs
- Raises:
ValueError –
- Returns:
inten – Each row is an event.
- Return type:
scipy.sparse.csr_matrix
- set_vector_file(file_name, inten_name=None, **kwargs)[source]#
This function is deprecated, use Centroids.from_vector_file and Centroids.values_from_vector_files instead.
- classmethod from_vector_file(file_name, dst_crs=None)[source]#
Create Centroids object from vector file (any format supported by fiona).
- Parameters:
file_name (str) – vector file with format supported by fiona and ‘geometry’ field.
dst_crs (crs, optional) – reproject to given crs
- Returns:
centr – Centroids with points according to the given vector file
- Return type:
- values_from_vector_files(file_names, val_names=None, dst_crs=None)[source]#
Read intensity or other data from vector files, making sure that geometry is compatible.
If the geometry of the shapes in any of the given files does not agree with the geometry of this Centroids instance, a ValueError is raised.
- Parameters:
file_names (list(str)) – vector files with format supported by fiona and ‘geometry’ field.
val_names (list(str), optional) – list of names of the columns of the values. Default: [‘intensity’]
dst_crs (crs, optional) – reproject to given crs
- Raises:
ValueError –
- Returns:
values – Sparse array of shape (len(val_name), len(geometry)).
- Return type:
scipy.sparse.csr_matrix
- classmethod from_mat(file_name, var_names=None)[source]#
Read centroids from CLIMADA’s MATLAB version.
- Parameters:
file_name (str) – absolute or relative file name
var_names (dict, optional) – name of the variables
- Raises:
KeyError –
- Returns:
centr – Centroids with data from the given file
- Return type:
- classmethod from_excel(file_name, var_names=None)[source]#
Generate a new centroids object from an excel file with column names in var_names.
- Parameters:
file_name (str) – absolute or relative file name
var_names (dict, default) – name of the variables
- Raises:
KeyError –
- Returns:
centr – Centroids with data from the given file
- Return type:
- append(centr)[source]#
Append centroids points.
If centr or self are rasters they are converted to points first using Centroids.set_meta_to_lat_lon. Note that self is modified in-place, and meta is set to {}. Thus, raster information in self is lost.
Note: this is a wrapper for centroids.union.
- Parameters:
centr (Centroids) – Centroids to append. The centroids need to have the same CRS.
See also
union
Union of Centroid objects.
- union(*others)[source]#
Create the union of centroids from the inputs.
The centroids are combined together point by point. Rasters are converted to points and raster information is lost in the output. All centroids must have the same CRS.
In any case, the attribute .geometry is computed for all centroids. This requires a CRS to be defined. If Centroids.crs is None, the default DEF_CRS is set for all centroids (self and others).
When at least one centroids has one of the following property defined, it is also computed for all others. .area_pixel, .dist_coast, .on_land, .region_id, .elevetaion’
!Caution!: the input objects (self and others) are modified in place. Missing properties are added, existing ones are not overwritten.
- Parameters:
others (any number of climada.hazard.Centroids()) – Centroids to form the union with
- Returns:
centroids – Centroids containing the union of the centroids in others.
- Return type:
- Raises:
ValueError –
- get_closest_point(x_lon, y_lat, scheduler=None)[source]#
Returns closest centroid and its index to a given point.
- Parameters:
x_lon (float) – x coord (lon)
y_lat (float) – y coord (lat)
scheduler (str) – used for dask map_partitions. “threads”, “synchronous” or “processes”
- 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.
- set_region_id(scheduler=None)[source]#
Set region_id as country ISO numeric code attribute for every pixel or point.
- Parameters:
scheduler (str) – used for dask map_partitions. “threads”, “synchronous” or “processes”
- set_area_pixel(min_resol=1e-08, scheduler=None)[source]#
Set area_pixel attribute for every pixel or point (area in m*m).
- Parameters:
min_resol (float, optional) – if centroids are points, use this minimum resolution in lat and lon. Default: 1.0e-8
scheduler (str) – used for dask map_partitions. “threads”, “synchronous” or “processes”
- set_area_approx(min_resol=1e-08)[source]#
Set area_pixel attribute for every pixel or point (approximate area in m*m).
Values are differentiated per latitude. Faster than set_area_pixel.
- Parameters:
min_resol (float, optional) – if centroids are points, use this minimum resolution in lat and lon. Default: 1.0e-8
- set_elevation(topo_path)[source]#
Set elevation attribute for every pixel or point in meters.
- Parameters:
topo_path (str) – Path to a raster file containing gridded elevation data.
- set_dist_coast(signed=False, precomputed=False, scheduler=None)[source]#
Set 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). Default: False.
scheduler (str) – Used for dask map_partitions. “threads”, “synchronous” or “processes”
- set_on_land(scheduler=None)[source]#
Set on_land attribute for every pixel or point.
- Parameters:
scheduler (str) – used for dask map_partitions. “threads”, “synchronous” or “processes”
- remove_duplicate_points()[source]#
Return Centroids with removed duplicated points
- Returns:
cen – Sub-selection of this object.
- Return type:
- select(reg_id=None, extent=None, sel_cen=None)[source]#
Return Centroids with points in the given reg_id or within mask
- Parameters:
reg_id (int) – region to filter according to region_id values
extent (tuple) – Format (min_lon, max_lon, min_lat, max_lat) tuple. If min_lon > lon_max, the extend crosses the antimeridian and is [lon_max, 180] + [-180, lon_min] Borders are inclusive.
sel_cen (np.array) – 1-dim mask, overrides reg_id and extent
- Returns:
cen – Sub-selection of this object
- Return type:
- select_mask(reg_id=None, extent=None)[source]#
Make mask of selected centroids
- Parameters:
reg_id (int) – region to filter according to region_id values
extent (tuple) – Format (min_lon, max_lon, min_lat, max_lat) tuple. If min_lon > lon_max, the extend crosses the antimeridian and is [lon_max, 180] + [-180, lon_min] Borders are inclusive.
- Returns:
sel_cen – 1d mask of selected centroids
- Return type:
1d array of booleans
- set_lat_lon_to_meta(min_resol=1e-08)[source]#
Compute meta from lat and lon values.
- Parameters:
min_resol (float, optional) – Minimum centroids resolution to use in the raster. Default: 1.0e-8.
- plot(axis=None, figsize=(9, 13), **kwargs)[source]#
Plot centroids scatter points over earth.
- Parameters:
axis (matplotlib.axes._subplots.AxesSubplot, optional) – axis to use
figsize ((float, float), optional) – figure size for plt.subplots The default is (9, 13)
kwargs (optional) – arguments for scatter matplotlib function
- Returns:
axis
- Return type:
matplotlib.axes._subplots.AxesSubplot
- calc_pixels_polygons(scheduler=None)[source]#
Return a gpd.GeoSeries with a polygon for every pixel
- Parameters:
scheduler (str) – used for dask map_partitions. “threads”, “synchronous” or “processes”
- Returns:
geo
- Return type:
gpd.GeoSeries
- empty_geometry_points()[source]#
Removes all points in geometry.
Useful when centroids is used in multiprocessing function.
- write_hdf5(file_data)[source]#
Write centroids attributes into hdf5 format.
- Parameters:
file_data (str or h5) – If string, path to write data. If h5 object, the datasets will be generated there.
- classmethod from_hdf5(file_data)[source]#
Create a centroids object from a HDF5 file.
- Parameters:
file_data (str or h5) – If string, path to read data. If h5 object, the datasets will be read from there.
- Returns:
centr – Centroids with data from the given file
- Return type:
- property crs#
Get CRS of raster or vector.
- property size#
Get number of pixels or points.
- property shape#
Get shape of rastered data.
- property total_bounds#
Get total bounds (left, bottom, right, top).
- property coord#
Get [lat, lon] array.