revrt.utilities.handlers.LayeredFile#

class LayeredFile(fp)[source]#

Bases: object

Handler for file containing GeoTIFF layers

Parameters:

fp (path-like) – Path to layered file on disk.

Methods

create_new(template_file[, overwrite, ...])

Create a new layered file

extract_all_layers(out_dir[, ds_chunks, lock])

Extract all layers from file and save to disk as GeoTIFFs

extract_layers(layers[, ds_chunks, lock])

Extract layers from file and save to disk as GeoTIFFs

layer_profile(layer)

Get layer profile as dictionary

layer_to_geotiff(layer, geotiff[, ...])

Extract layer from file and write to GeoTIFF file

layers_to_file(layers[, check_tiff, ...])

Transfer GeoTIFF layers into layered file

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

Transfer GeoTIFF to layered file

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

Write a layer to the file

Attributes

LATITUDE

Name of latitude values layer in LayeredFile

LONGITUDE

Name of longitude values layer in LayeredFile

SUPPORTED_FILE_ENDINGS

Supported template file endings

data_layers

Available data layers in file

layers

All available layers in file

profile

Template layer profile

shape

Template layer shape

SUPPORTED_FILE_ENDINGS = {'.tif', '.tiff', '.zarr'}#

Supported template file endings

LATITUDE = 'latitude'#

Name of latitude values layer in LayeredFile

LONGITUDE = 'longitude'#

Name of longitude values layer in LayeredFile

property profile[source]#

Template layer profile

Type:

dict

property shape#

Template layer shape

Type:

tuple

property layers#

All available layers in file

Type:

list

property data_layers#

Available data layers in file

Type:

list

layer_profile(layer)[source]#

Get layer profile as dictionary

Parameters:

layer (str) – Name of layer in file to get profile for.

Returns:

dict – Dictionary containing layer profile information, including the following keys:

  • ”nodata”: NoData value for layer

  • ”width”: width of layer

  • ”height”: height of layer

  • ”crs”: pyproj.crs.CRS object for layer

  • ”count”: number of bands in layer

  • ”dtype”: data type of layer

  • ”transform”: Affine transform for layer

create_new(template_file, overwrite=False, chunk_x=2048, chunk_y=2048, read_chunks='auto')[source]#

Create a new layered file

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

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

  • chunk_x, chunk_y (int, default 2048) – Chunk size of x and y dimension for newly-created layered file. By default, 2048.

  • read_chunks (int | str, default "auto") – Chunk size to use when reading the template file. This will be passed down as the chunks argument to rioxarray.open_rasterio() or xarray.open_dataset(), depending on what template file is passed in. By default, "auto".

Returns:

LayeredFile – This LayeredFile object with a corresponding file on disk.

write_layer(values, layer_name, description=None, overwrite=False, nodata=None)[source]#

Write a layer to the file

Parameters:
  • values (array-like) – Layer data (can be numpy array, xarray.DataArray, or dask.array).

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

  • description (str, optional) – Description of layer being added. By default, None.

  • overwrite (bool, default False) – Option to overwrite layer data if layer already exists in LayeredFile.

    Important

    When overwriting data, the encoding (and therefore things like data type, nodata value, etc) is not allowed to change. If you need to overwrite an existing layer with a new type of data, manually remove it from the file first.

    By default, False.

  • nodata (int | float, optional) – Optional nodata value for the raster layer. This value will be added to the layer’s attributes meta dictionary under the “nodata” key.

    Warning

    rioxarray does not recognize the “nodata” value when reading from a zarr file (because zarr uses the _FillValue encoding internally). To get the correct “nodata” value back when reading a LayeredFile, you can either 1) read from da.rio.encoded_nodata or 2) check the layer’s attributes for the "nodata" key, and if present, use da.rio.write_nodata to write the nodata value so that da.rio.nodata gives the right value.

Raises:
write_geotiff_to_file(geotiff, layer_name, check_tiff=True, description=None, overwrite=True, nodata=None, tiff_chunks='auto')[source]#

Transfer GeoTIFF to layered file

Parameters:
  • geotiff (path-like) – Path to GeoTIFF file.

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

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

  • description (str, optional) – Description of layer being added. By default, None.

  • overwrite (bool, default False) – Option to overwrite layer data if layer already exists in LayeredFile.

    Important

    When overwriting data, the encoding (and therefore things like data type, nodata value, etc) is not allowed to change. If you need to overwrite an existing layer with a new type of data, manually remove it from the file first.

    By default, False.

  • nodata (int | float, optional) – Optional nodata value for the raster layer. This value will be added to the layer’s attributes meta dictionary under the “nodata” key.

    Warning

    rioxarray does not recognize the “nodata” value when reading from a zarr file (because zarr uses the _FillValue encoding internally). To get the correct “nodata” value back when reading a LayeredFile, you can either 1) read from da.rio.encoded_nodata or 2) check the layer’s attributes for the "nodata" key, and if present, use da.rio.write_nodata to write the nodata value so that da.rio.nodata gives the right value.

  • tiff_chunks (int | str, default "auto") – Chunk size to use when reading the GeoTIFF file. This will be passed down as the chunks argument to rioxarray.open_rasterio(). By default, "auto".

layer_to_geotiff(layer, geotiff, ds_chunks='auto', lock=None, **profile_kwargs)[source]#

Extract layer from file and write to GeoTIFF file

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

  • geotiff (path-like) – Path to output GeoTIFF file.

  • ds_chunks (int | str, default "auto") – Chunk size to use when reading the LayeredFile. This will be passed down as the chunks argument to xarray.open_dataset(). By default, "auto".

  • lock (bool | `dask.distributed.Lock`, optional) – Lock to use to write data using dask. If not supplied, a single process is used for writing data to the GeoTIFF. By default, None.

  • **profile_kwargs – Additional keyword arguments to pass into writing the raster. The following attributes ar ignored (they are set using properties of the source LayeredFile):

    • nodata

    • transform

    • crs

    • count

    • width

    • height

layers_to_file(layers, check_tiff=True, descriptions=None, overwrite=False, nodata=None)[source]#

Transfer GeoTIFF layers into layered file

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

Parameters:
  • layers (list | dict) – Dictionary mapping layer names to GeoTIFFs filepaths. Each GeoTIFF will be loaded into the LayeredFile user the layer name. If a list of GeoTIFFs filepaths is provided, the file name stems are used as the layer names.

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

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

  • overwrite (bool, default False) – Option to overwrite layer data if layer already exists in LayeredFile.

    Important

    When overwriting data, the encoding (and therefore things like data type, nodata value, etc) is not allowed to change. If you need to overwrite an existing layer with a new type of data, manually remove it from the file first.

    By default, False.

  • nodata (int | float, optional) – Optional nodata value for the raster layer. This value will be added to the layer’s attributes meta dictionary under the “nodata” key.

    Warning

    rioxarray does not recognize the “nodata” value when reading from a zarr file (because zarr uses the _FillValue encoding internally). To get the correct “nodata” value back when reading a LayeredFile, you can either 1) read from da.rio.encoded_nodata or 2) check the layer’s attributes for the "nodata" key, and if present, use da.rio.write_nodata to write the nodata value so that da.rio.nodata gives the right value.

Returns:

str – String representation of path to output layered file.

extract_layers(layers, ds_chunks='auto', lock=None, **profile_kwargs)[source]#

Extract layers from file and save to disk as GeoTIFFs

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

  • ds_chunks (int | str, default "auto") – Chunk size to use when reading the LayeredFile. This will be passed down as the chunks argument to xarray.open_dataset(). By default, "auto".

  • lock (bool | `dask.distributed.Lock`, optional) – Lock to use to write data using dask. If not supplied, a single process is used for writing data to the GeoTIFFs. By default, None.

  • **profile_kwargs – Additional keyword arguments to pass into writing the raster. The following attributes ar ignored (they are set using properties of the source LayeredFile):

    • nodata

    • transform

    • crs

    • count

    • width

    • height

extract_all_layers(out_dir, ds_chunks='auto', lock=None, **profile_kwargs)[source]#

Extract all layers from file and save to disk as GeoTIFFs

Parameters:
  • out_dir (path-like) – Path to output directory into which layers should be saved as GeoTIFFs. This directory will be created if it does not already exist.

  • ds_chunks (int | str, default "auto") – Chunk size to use when reading the LayeredFile. This will be passed down as the chunks argument to xarray.open_dataset(). By default, "auto".

  • lock (bool | `dask.distributed.Lock`, optional) – Lock to use to write data using dask. If not supplied, a single process is used for writing data to the GeoTIFFs. By default, None.

  • **profile_kwargs – Additional keyword arguments to pass into writing the raster. The following attributes ar ignored (they are set using properties of the source LayeredFile):

    • nodata

    • transform

    • crs

    • count

    • width

    • height

Returns:

dict – Dictionary mapping layer names to GeoTIFF files created.