climada.hazard.centroids package

climada.hazard.centroids.centr module

class climada.hazard.centroids.centr.Centroids[source]

Bases: object

Contains raster or vector centroids.

Raster data can be set with set_raster_file() or set_meta(). Vector data can be set with set_lat_lon() or set_vector_file().

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__()[source]

Initialize to None raster and vector

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.

static from_geodataframe(gdf, geometry_alias='geom')[source]

Create Centroids instance from GeoDataFrame.

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.

set_raster_from_pix_bounds(xf_lat, xo_lon, d_lat, d_lon, n_lat, n_lon, crs='EPSG:4326')[source]

Set raster metadata (meta attribute) from pixel border data

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

set_raster_from_pnt_bounds(points_bounds, res, crs='EPSG:4326')[source]

Set raster metadata (meta attribute) from 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

set_lat_lon(lat, lon, crs='EPSG:4326')[source]

Set Centroids points 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

set_raster_file(file_name, band=[1], src_crs=None, window=False, geometry=False, dst_crs=False, transform=None, width=None, height=None, resampling=<Resampling.nearest: 0>)[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_pth (str) – path of the file

  • band (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 (shapely.geometry, optional) – consider pixels only in shape

  • 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=['intensity'], dst_crs=None)[source]

Read vector file format supported by fiona.

Each intensity name is considered an event.

Parameters
  • file_name (str) – vector file with format supported by fiona and ‘geometry’ field.

  • inten_name (list(str)) – list of names of the columns of the intensity of each event.

  • dst_crs (crs, optional) – reproject to given crs

Returns

inten – Sparse intensity array of shape (len(inten_name), len(geometry)).

Return type

scipy.sparse.csr_matrix

read_mat(file_name, var_names={'field_names': ['centroids', 'hazard'], 'var_name': {'admin0_iso3': 'admin0_ISO3', 'admin0_name': 'admin0_name', 'comment': 'comment', 'dist_coast': 'distance2coast_km', 'lat': 'lat', 'lon': 'lon', 'region_id': 'NatId'}})[source]

Read centroids from CLIMADA’s MATLAB version.

Parameters
  • file_name (str) – absolute or relative file name

  • var_names (dict, default) – name of the variables

Raises

KeyError

read_excel(file_name, var_names={'col_name': {'lat': 'latitude', 'lon': 'longitude', 'region_id': 'region_id'}, 'sheet_name': 'centroids'})[source]

Read centroids from 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

clear()[source]

Clear vector and raster data.

append(centr)[source]

Append raster or points.

Parameters

centr (Centroids) – If it’s a raster, it needs to have the same meta attribute. If it’s of non-raster form, it’s geometry needs to have the same CRS.

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(scheduler=None)[source]

Return Centroids with removed duplicated points

Parameters

scheduler (str) – used for dask map_partitions. “threads”, “synchronous” or “processes”

Returns

cen – Sub-selection of this object.

Return type

Centroids

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

Centroids

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.

set_meta_to_lat_lon()[source]

Compute lat and lon of every pixel center from meta raster.

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.

read_hdf5(file_data)[source]

Read centroids attributes from hdf5.

Parameters

file_data (str or h5) – If string, path to read data. If h5 object, the datasets will be read from there.

property crs

Get CRS of raster or vector.

property size

Get size 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. Might take some time.

set_geometry_points(scheduler=None)[source]

Set geometry attribute with Points from lat/lon attributes.

Parameters

scheduler (str) – used for dask map_partitions. “threads”, “synchronous” or “processes”