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 (transform needs to contain upper left corner!)

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

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 that either raster meta attribute is set or points lat, lon and geometry.crs. Check attributes sizes

equal(centr)[source]

Return true if two centroids equal, false otherwise

Parameters

centr (Centroids) – centroids to compare

Returns

bool

set_raster_from_pix_bounds(xf_lat, xo_lon, d_lat, d_lon, n_lat, n_lon, crs={'init': 'epsg:4326', 'no_defs': True})[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={'init': 'epsg:4326', 'no_defs': True})[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={'init': 'epsg:4326', 'no_defs': True})[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

np.array

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. Returns intensity array with shape (len(inten_name), len(geometry)).

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

np.array

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. Raster needs to have the same resolution

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), y_close (float), idx_close (int)

set_region_id(scheduler=None)[source]

Set region_id as country ISO numeric code attribute for every pixel or point

Parameter:
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

Parameter:
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]

Computes approximated area_pixel values: differentiated per latitude. area in m*m. Faster than set_area_pixel

Parameter:
min_resol (float, optional): if centroids are points, use this minimum

resolution in lat and lon. Default: 1.0e-8

set_dist_coast(scheduler=None)[source]

Set dist_coast attribute for every pixel or point. Distance to coast is computed in meters.

Parameter:
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

Parameter:
scheduler (str): used for dask map_partitions. “threads”,

“synchronous” or “processes”

remove_duplicate_points(scheduler=None)[source]

Return Centroids with removed duplicated points

Parameter:
scheduler (str): used for dask map_partitions. “threads”,

“synchronous” or “processes”

Returns

Centroids

select(reg_id=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

  • sel_cen (np.array) – 1-dim mask

Returns

Centroids

set_lat_lon_to_meta(min_resol=1e-08)[source]

Compute meta from lat and lon values. To match the existing lat and lon, lat and lon need to start from the upper left corner!!

Parameter:
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, **kwargs)[source]

Plot centroids scatter points over earth.

Parameters
  • axis (matplotlib.axes._subplots.AxesSubplot, optional) – axis to use

  • kwargs (optional) – arguments for scatter matplotlib function

Returns

matplotlib.axes._subplots.AxesSubplot

calc_pixels_polygons(scheduler=None)[source]

Return a GeoSeries with a polygon for every pixel

Parameter:
scheduler (str): used for dask map_partitions. “threads”,

“synchronous” or “processes”

Returns

GeoSeries

empty_geometry_points()[source]

Removes points in geometry. Useful when centroids is used in multiprocessing function

write_hdf5(file_data)[source]

Write centroids attributes into hdf5 format.

Parameter:
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.

Parameter:
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 of GeoSeries with Points from latitude and longitude attributes if geometry not present.

Parameter:
scheduler (str): used for dask map_partitions. “threads”,

“synchronous” or “processes”