veranda.raster.native package

Submodules

veranda.raster.native.geotiff module

Manages I/O for a GeoTIFF file.

class veranda.raster.native.geotiff.GeoTiffFile(filepath, mode='r', geotrans=(0, 1, 0, 0, 0, 1), sref_wkt=None, raster_shape=None, compression='LZW', metadata=None, is_bigtiff=False, is_tiled=True, blocksize=(512, 512), n_bands=1, dtypes='uint8', scale_factors=1, offsets=0, nodatavals=255, color_tbls=None, color_intprs=None, overwrite=False, auto_decode=False)[source]

Bases: object

GDAL wrapper for reading or writing a GeoTIFF file.

close()[source]

Close the dataset.

property color_interps: List[int]

Color interpretation values of the different bands.

property color_tables: List[ColorTable]

Color tables of the different bands.

property dtypes: List[str]

Data types in NumPy-style format.

flush()[source]

Flush data on disk.

static is_file_bigtiff(filepath) bool[source]

Determines if the given GeoTIFF is a BigTIFF file or not.

Parameters:

filepath (str) – Full file path to a GeoTIFF file.

Returns:

True if the given file is a BigTIFF, else False.

Return type:

bool

property n_bands: int

Number of bands.

property nodatavals: List[int]

No data values of the different bands.

property offsets: List[float]

Offsets of the different bands.

read(row=0, col=0, n_rows=None, n_cols=None, bands=None, decoder=None, decoder_kwargs=None) dict[source]

Read data from a GeoTIFF file.

Parameters:
  • row (int, optional) – Row number/index. Defaults to 0.

  • col (int, optional) – Column number/index. Defaults to 0.

  • n_rows (int, optional) – Number of rows of the reading window (counted from row). If None (default), then the full extent of the raster is used.

  • n_cols (int, optional) – Number of columns of the reading window (counted from col). If None (default), then the full extent of the raster is used.

  • bands (int or tuple or list, optional) – Band numbers of the GeoTIFF file to read data from. Defaults to None, i.e. all available bands will be used.

  • decoder (callable, optional) – Decoding function expecting a NumPy array as input.

  • decoder_kwargs (dict, optional) – Keyword arguments for the decoder.

Returns:

data – Dictionary mapping band numbers to NumPy arrays read from disk.

Return type:

dict

property scale_factors: List[float]

Scale factors of the different bands.

write(data, row=0, col=0, encoder=None, encoder_kwargs=None)[source]

Writes a NumPy array to a GeoTIFF file.

Parameters:
  • data (dict of np.array or np.array) – Dictionary mapping band numbers with a 2D NumPy array or 3D numpy array, where the first dimension must correspond to the number of bands.

  • row (int, optional) – Offset row number/index (defaults to 0).

  • col (int, optional) – Offset column number/index (defaults to 0).

  • encoder (callable, optional) – Function allowing to encode a NumPy array before writing it to disk.

  • encoder_kwargs (dict, optional) – Keyword arguments for the encoder.

veranda.raster.native.geotiff.create_vrt_file(filepaths, vrt_filepath, shape, sref_wkt, geotrans, bands=1)[source]

Creates a VRT file stack from a list of file paths.

Parameters:
  • filepaths (list of str) – Full system path to the files to stack.

  • vrt_filepath (str) – Full system path to the VRT file to create.

  • shape (2-tuple) – Shape (rows, columns) of the raster stack.

  • sref_wkt (str) – Coordinate Reference System (CRS) information in WKT format.

  • geotrans (6-tuple or list, optional) –

    Geo-transformation parameters with the following entries:

    0: Top-left X coordinate 1: W-E pixel sampling 2: Rotation, 0 if image is axis-parallel 3: Top-left Y coordinate 4: Rotation, 0 if image is axis-parallel 5: N-S pixel sampling (negative value if North-up)

  • bands (tuple or list or int, optional) – Band number(s). Defaults to 1.

veranda.raster.native.netcdf module

Manages I/O for a NetCDF file.

class veranda.raster.native.netcdf.NetCdf4File(filepath, mode='r', data_variables=None, stack_dims=None, space_dims=None, scale_factors=1, offsets=0, nodatavals=127, dtypes='int8', zlibs=True, complevels=2, chunksizes=None, var_chunk_caches=None, attrs=None, geotrans=(0, 1, 0, 0, 0, 1), sref_wkt=None, metadata=None, nc_format='NETCDF4_CLASSIC', overwrite=True, auto_decode=False)[source]

Bases: object

Wrapper for reading and writing NetCDF files with netCDF4 as a backend. By default, it will create three predefined dimensions ‘time’, ‘y’, ‘x’, with time as an unlimited dimension and x, y are either defined by a pre-defined shape or by an in-memory dataset.

The spatial dimensions are always limited, but can have an arbitrary name, whereas the dimension(s) which are used to stack spatial data are unlimited by default and can also have an arbitrary name.

property all_variables: list

Returns all relevant (data, spatial and stack) variables of the NetCDF file.

close()[source]

Close file.

static get_metadata(src_var) dict[source]

Collects all metadata attributes from a NetCDF4 variable.

Parameters:

src_var (netCDF4.Variable) – NetCDF4 variable to extract metadata from.

Returns:

attrs – Metadata attributes stored in the NetCDF4 variable.

Return type:

dict

property raster_shape: Tuple[int, int]

Tuple specifying the shape of the raster (defined by the spatial dimensions).

read(row=0, col=0, n_rows=None, n_cols=None, data_variables=None, decoder=None, decoder_kwargs=None) Dataset[source]

Read data from a NetCDF file.

Parameters:
  • row (int, optional) – Row number/index. Defaults to 0.

  • col (int, optional) – Column number/index. Defaults to 0.

  • n_rows (int, optional) – Number of rows of the reading window (counted from row). If None (default), then the full extent of the raster is used.

  • n_cols (int, optional) – Number of columns of the reading window (counted from col). If None (default), then the full extent of the raster is used.

  • data_variables (list, optional) – Data variables to read. Default is to read all available data variables.

  • decoder (callable, optional) – Decoding function expecting a NumPy array as input.

  • decoder_kwargs (dict, optional) – Keyword arguments for the decoder.

Returns:

data – Data stored in the NetCDF file.

Return type:

xarray.Dataset

write(ds, row=0, col=0, encoder=None, encoder_kwargs=None)[source]

Write an xarray dataset into a NetCDF file.

Parameters:
  • ds (xarray.Dataset) – Dataset to write to disk.

  • row (int, optional) – Offset row number/index (defaults to 0).

  • col (int, optional) – Offset column number/index (defaults to 0).

  • encoder (callable, optional) – Encoding function expecting an xarray.DataArray as input.

  • encoder_kwargs (dict, optional) – Keyword arguments for the encoder.

class veranda.raster.native.netcdf.NetCdfXrFile(filepath, mode='r', data_variables=None, stack_dims=None, space_dims=None, scale_factors=1, offsets=0, nodatavals=127, dtypes='int8', compressions=None, chunksizes=None, attrs=None, geotrans=(0, 1, 0, 0, 0, 1), sref_wkt=None, nc_format='NETCDF4_CLASSIC', metadata=None, overwrite=True, auto_decode=False, engine='netcdf4')[source]

Bases: object

Wrapper for reading and writing NetCDF files with xarray as a backend. By default, it will create three predefined dimensions ‘time’, ‘y’, ‘x’, but spatial dimensions or dimensions which are used to stack data can also have an arbitrary name.

property all_variables

Returns all relevant (data, spatial and stack) variables of the xarray dataset.

close()[source]

Close file.

property raster_shape: Tuple[int, int]

Tuple specifying the raster_shape of the raster (defined by the spatial dimensions).

Type:

2-tuple

read(row=0, col=0, n_rows=None, n_cols=None, data_variables=None, decoder=None, decoder_kwargs=None) Dataset[source]

Read mosaic from netCDF4 file.

Parameters:
  • row (int, optional) – Row number/index. Defaults to 0.

  • col (int, optional) – Column number/index. Defaults to 0.

  • n_rows (int, optional) – Number of rows of the reading window (counted from row). If None (default), then the full extent of the raster is used.

  • n_cols (int, optional) – Number of columns of the reading window (counted from col). If None (default), then the full extent of the raster is used.

  • data_variables (list, optional) – Data variables to read. Default is to read all available data variables.

  • decoder (callable, optional) – Decoding function expecting an xarray.DataArray as input.

  • decoder_kwargs (dict, optional) – Keyword arguments for the decoder.

Returns:

data – Data stored in the NetCDF file.

Return type:

xarray.Dataset

write(ds, data_variables=None, encoder=None, encoder_kwargs=None, compute=True)[source]

Write data to a NetCDF file.

Parameters:
  • ds (xarray.Dataset) – Dataset to write to disk.

  • data_variables (list, optional) – Data variables to write. Default is to write all available data variables.

  • encoder (callable, optional) – Encoding function expecting an xarray.DataArray as input.

  • encoder_kwargs (dict, optional) – Keyword arguments for the encoder.

  • compute (bool, optional) – If True (default), compute immediately, otherwise only a delayed dask array is stored.

Module contents