reVX.handlers.layered_h5.LayeredH5

class LayeredH5(h5_file, hsds=False, chunks=(128, 128), template_file=None)[source]

Bases: object

Handler for HDF5 file containing GeTIFF layers.

This handler represents an HDF5 file that stores various layers (i.e. exclusion layers, setback layers, transmission layers, etc). This file contains profile information, and this handler can be used to convert to and from such files.

Parameters:
  • h5_file (path-like) – Path to HDF5 layered file. If this file is to be created, a template_file must be provided (and must exist on disk). Otherwise, the template_file input can be ignored and this input will be used as the template file.

  • hsds (bool, optional) – Boolean flag to use h5pyd to handle HDF5 ‘files’ hosted on AWS behind HSDS. By default, False.

  • chunks (tuple, optional) – Chunk size of exclusions in HDF5 file and any output GeoTIFFs. By default, (128, 128).

  • template_file (path-like, optional) – Path to template GeoTIFF (*.tif or *.tiff) or HDF5 (*.h5) file containing the profile and transform to be used for the layered file. If None, then the h5_file input is used as the template. By default, None.

Methods

create_new([overwrite])

Create a new layered HDF5 file.

extract_all_layers(out_dir)

Extract all layers from HDF5 file and save to disk as GeoTIFFs.

extract_layers(layers)

Extract layers from HDF5 file and save to disk as GeoTIFFs.

layer_to_geotiff(layer, geotiff)

Extract layer from HDF5 file and write to GeoTIFF file.

layers_to_h5(layers[, replace, check_tiff, ...])

Transfer GeoTIFF layers into layered HDF5 file.

load_data_using_h5_profile(geotiff[, band, ...])

Load GeoTIFF data, converting to H5 profile if necessary.

reproject(src_raster, src_profile[, dtype, ...])

Reproject a raster onto the template raster and transform.

save_data_using_h5_profile(data, geotiff)

Write to GeoTIFF file.

write_geotiff_to_h5(geotiff, layer_name[, ...])

Transfer GeoTIFF to HDF5 confirming it matches existing layers.

write_layer_to_h5(values, layer_name[, ...])

Write a layer to the HDF5 file.

Attributes

SUPPORTED_FILE_ENDINGS

Supported template file endings.

layers

Available layers in HDF5 file.

profile

Template layer profile.

shape

Template layer shape.

template_file

Path to template file.

SUPPORTED_FILE_ENDINGS = {'.h5', '.tif', '.tiff'}

Supported template file endings.

property template_file

Path to template file.

Type:

str

property profile

Template layer profile.

Type:

dict

property shape

Template layer shape.

Type:

tuple

property layers

Available layers in HDF5 file.

Type:

list

create_new(overwrite=False)[source]

Create a new layered HDF5 file.

Parameters:

overwrite (bool, optional) – Overwrite HDF5 file if is exists. By default, False.

write_layer_to_h5(values, layer_name, profile=None, description=None, scale_factor=None)[source]

Write a layer to the HDF5 file.

Parameters:
  • values (ndarray) – Layer data.

  • layer_name (str) – Dataset name in HDF5 file.

  • profile (dict, optional) – Layer profile (attributes). If None, the profile from the Layered HDF5 file is used instead.

  • description (str, optional) – Description of layer being added. By default, None, which does not store a description in the layer attributes.

  • scale_factor (int | float, optional) – Scale factor to use to scale geotiff data when added to the HDF5 file. By default, None, which does not scale the values.

write_geotiff_to_h5(geotiff, layer_name, check_tiff=True, transform_atol=0.01, description=None, scale_factor=None, dtype='int16', replace=True)[source]

Transfer GeoTIFF to HDF5 confirming it matches existing layers.

Parameters:
  • geotiff (str) – Path to GeoTIFF file.

  • layer_name (str) – Name of layer to be written to HDF5 file.

  • check_tiff (bool, optional) – Option to check GeoTIFF profile, CRS, and shape against layered HDF5 profile, CRS, and shape. By default, True.

  • transform_atol (float, optional) – Absolute tolerance parameter when comparing GeoTIFF transform data. By default, 0.01.

  • description (str, optional) – Optional description of layer. By default, None.

  • scale_factor (int | float, optional) – Scale factor to use to scale GeoTIFF data when added to the layered HDF5 file, by default None

  • dtype (str, optional) – Dtype to save GeoTIFF data as in the layered HDF5 file. Only used when scale_factor input is not None. By default, "int16".

  • replace (bool, optional) – Option to replace existing layer (if any). By default, True.

layer_to_geotiff(layer, geotiff)[source]

Extract layer from HDF5 file and write to GeoTIFF file.

Parameters:
  • layer (str) – Layer to extract,

  • geotiff (str) – Path to output GeoTIFF file.

save_data_using_h5_profile(data, geotiff)[source]

Write to GeoTIFF file.

Parameters:
  • layer (str) – Layer to extract,

  • geotiff (str) – Path to output GeoTIFF file.

load_data_using_h5_profile(geotiff, band=1, reproject=False, skip_profile_test=False)[source]

Load GeoTIFF data, converting to H5 profile if necessary.

Parameters:
  • geotiff (str) – Path to GeoTIFF from which data should be read.

  • band (int, optional) – Band to load from GeoTIFF. By default, 1.

  • reproject (bool, optional) – Reproject raster to standard CRS and transform if True. By default, False.

  • skip_profile_test (bool, optional) – Skip checking that shape, transform, and CRS match template raster if True. By default, False.

Returns:

array-like – Raster data.

reproject(src_raster, src_profile, dtype='float32', init_dest=-1.0)[source]

Reproject a raster onto the template raster and transform.

Parameters:
  • src_raster (array-like) – Source raster.

  • src_profile (dict) – Source raster profile.

  • dtype (np.dtype, optional) – Data type for destination raster. By default, "float32".

  • init_des (float, optional) – Value for cells outside of boundary of src_raster. By default, -1.0.

Returns:

array-like – Source data reprojected into the template projection.

layers_to_h5(layers, replace=True, check_tiff=True, transform_atol=0.01, descriptions=None, scale_factors=None)[source]

Transfer GeoTIFF layers into layered HDF5 file.

If layered HDF5 file does not exist, it is created and populated.

Parameters:
  • layers (list | dict) – List of geotiffs to load or dictionary mapping goetiffs to the layers to load.

  • replace (bool, optional) – Option to replace existing layers if needed. By default, True

  • check_tiff (bool, optional) – Flag to check tiff profile and coordinates against layered HDF5 profile and coordinates. By default, True.

  • transform_atol (float, optional) – Absolute tolerance parameter when comparing GeoTIFF and layered HDF5 transforms. By default, 0.01.

  • description (dict, optional) – Mapping of layer name to layer description of layers. By default, None, which does not store any descriptions.

  • scale_factor (dict, optional) – Scale factors and dtypes to use when scaling given layers. By default, None, which does not apply any scale factors.

extract_layers(layers)[source]

Extract layers from HDF5 file and save to disk as GeoTIFFs.

Parameters:

layers (dict) – Dictionary mapping layer names to GeoTIFF files to create.

extract_all_layers(out_dir)[source]

Extract all layers from HDF5 file and save to disk as GeoTIFFs.

Parameters:

out_dir (str) – Path to output directory into which layers should be saved as GeoTIFFs.