variete.VRaster#

VRaster#

class variete.VRaster(steps=None)[source]#

Bases: object

A “Virtual Raster” containing information on how to process a raster on disk.

A VRaster has no data loaded in memory, other than the processing steps to take when evaluating. Evaluation is mainly done through the VRaster.read() or VRaster.write() functions.

__init__(steps=None)[source]#
add(other)[source]#

Perform addition on the VRaster

Parameters

other (int | float | VRaster) – A constant value or another VRaster to add.

Return type

A new VRaster

property bounds: BoundingBox#
copy()[source]#
Return type

VRaster

property crs: CRS#
divide(other)[source]#

Perform division on the VRaster

Parameters

other (int | float | VRaster) – A constant value or another VRaster to divide.

Return type

A new VRaster.

inverse()[source]#

Invert the VRaster (1 / x)

Return type

A new VRaster.

property last: VRTDataset#
classmethod load_file(filepath)[source]#

Load a VRaster from a file.

Parameters

filepath (str | Path) – The filepath to a GDAL-supported dataset.

Return type

A newly created VRaster

multiply(other)[source]#

Perform multiplication on the VRaster

Parameters

other (int | float | VRaster) – A constant value or another VRaster to multiply.

Return type

A new VRaster

property n_bands: int#
property nodata: int | float | None#

Get the first nodata value in the raster.

read(band=None, out=None, window=None, masked=False, **kwargs)[source]#

Read the contents of a VRaster into memory.

Parameters
Return type

A numpy array of shape (bands, height, width) or a numpy masked_array

replace_nodata(value)[source]#

Replace all nodata pixels with the given value.

Parameters

value (int | float) – The value to replace nodata with

Return type

A new VRaster

property res: tuple[float, float]#
sample(x_coord, y_coord, band=1, masked=False)[source]#

Sample values at the given georeferenced coordinates of a VRaster.

Parameters
  • x_coord (Union[float, Iterable[float]]) – The x (easting/longitude) coordinate(s) to sample

  • y_coord (Union[float, Iterable[float]]) – The x (northing/latitude) coordinate(s) to sample

  • band (int | list[int]) – The band(s) to sample from. Defaults to the first.

  • masked (bool) – Return a masked array with nodata values masked out.

Return type

int | float | ndarray[Any, dtype[Any]] | MaskedArray[Any, Any]

Returns

  • If one coordinate and one band – One sampled value

  • If multiple coordinates and/or multiple bands – An array of coordinates

sample_rowcol(row, col, band=1, masked=False)[source]#

Sample values at the given row(s) and column(s) of a VRaster.

Parameters
  • row (Union[float, Iterable[float]]) – The row(s) to sample.

  • y_coord – The column(s) to sample.

  • band (int | list[int]) – The band(s) to sample from. Defaults to the first.

  • masked (bool) – Return a masked array with nodata values masked out.

Return type

int | float | ndarray[Any, dtype[Any]] | MaskedArray[Any, Any]

Returns

  • If one coordinate and one band – One sampled value

  • If multiple coordinates and/or multiple bands – An array of coordinates

save_vrt(filepath)[source]#

Save the VRaster as a VRT or a stack of VRTs.

If the VRaster is nested (depends on more than one VRTDataset), all dependents will be saved too.

Parameters

filepath (str | Path) – The filepath to save the VRT. Multiple VRTs may be saved with suffixes.

Return type

A list of filepaths that were created (multiple in case of a nested VRaster).

property shape: tuple[int, int]#
steps: list[VRasterStep]#
subtract(other)[source]#

Perform subtraction on the VRaster

Parameters

other (int | float | VRaster) – A constant value or another VRaster to subtract.

Return type

A new VRaster

property transform: Affine#
warp(reference=None, crs=None, res=None, shape=None, bounds=None, transform=None, resampling='bilinear', dst_nodata=None, multithread=False)[source]#

Warp the VRaster to new bounds, resolutions and/or coordinate systems.

This wraps the functionality of gdal.Warp

Parameters
  • reference (Optional[VRaster]) – Optional: A reference VRaster to get the CRS, transform and shape from. Note: It silently overrides the shape, crs and transform arguments. If only parts of the reference parameters should be used, supply them directly instead (e.g. VRaster.crs).

  • crs (UnionType[CRS, int, str, None]) – The target coordinate reference system (CRS). If an integer is given, it’s parsed as an EPSG code (e.g. 4326 -> WGS84).

  • res (UnionType[tuple[float, float], float, None]) – The target resolution in georeferenced units. If only one value is given, it is used for both axes.

  • shape (Optional[tuple[int, int]]) – The target shape of the VRaster in pixels as (height, width).

  • bounds (UnionType[BoundingBox, list[float], None]) – The target corner bounds of the VRaster. If a list is given, it’s parsed as [xmin, ymin, xmax, ymax]

  • transform (Optional[Affine]) – The target affine transform of the VRaster.

  • resampling (Resampling | str) – The target resampling algorithm, e.g. “bilinear” or “cubic_spline”. See rio.warp.Resampling for all available algorithms.

  • dst_nodata (UnionType[int, float, None]) – Destination nodata value to use after warping. Defaults to the source nodata value.

  • multithread (bool) – Use multithreading for the warp operation.

Return type

A new VRaster

write(filepath, format=None, tiled=None, compress='deflate', predictor=None, zlevel=None, creation_options=None, progress=False, callback=None)[source]#

Write the VRaster to a file.

Parameters
  • filepath (Path | str) – The output filepath to write the file.

  • format (Optional[str]) – The output format (e.g. “GTiff”). If not given, the format is inferred from the filename.

  • tiled (Optional[bool]) – Whether to write the blocks in tiles (True) or in strips (False)

  • compress (Optional[str]) – What compression algorithm to use.

  • predictor (Union[Literal[1], Literal[2], Literal[3], None]) – Which compression predictor to use (only valid in some compression schemes).

  • zlevel (UnionType[int, str, None]) – The level of compression to use. For deflate, valid numbers range between 1 and 12

  • creation_options (Optional[dict[str, str | int | bool]]) – Other creation options to provide to GDAL as a {key: value} dictionary

  • progress (bool) – Whether to show a tqdm progress bar. tqdm needs to be installed for this to work.

  • callback (Optional[Callable[[float, Any, Any], Any]]) – A callback function for the writer that takes three positional arguments. The first argument is the progress, ranging from 0-1.

Raises
  • AssertionError – If any requirement for file creation is not filled.

  • ValueError – If the provided arguments are incompatible.

Return type

None