Datasetwrapper

class sds_data_model.raster.DatasetWrapper(dataset)

A wrapper for an xarray.dataset.

This class forms a light wrapper around the xarray.Dataset class. It’s key feature is a method to ingest many types of raster files, and resample the data to the data model format of British National Grid extent and 10m spatial resolution.

During raster resampling, linear interpolation is used, unless categorical data is provided, in which case, nearest neighbour interpolation is applied.

dataset

An xarray Dataset containing one or more DataArrays.

Type

Dataset

from_files()

Read in a raster from file at 10m cell size and British National Grid extent.

Parameters
  • data_path (str) –

  • bands (Optional[Union[List[int], List[str]]]) –

  • categorical (Union[bool, Dict[Union[int, str], bool]]) –

  • nodata (Optional[float]) –

  • out_path (Optional[str]) –

  • overwrite (bool) –

  • chunks (Optional[Union[int, Dict[Any, Any], Literal['auto']]]) –

  • engine (Optional[str]) –

  • decode_coords (Optional[Union[bool, Literal['coordinates', 'all']]]) –

Return type

sds_data_model.raster._DatasetWrapper

Returns

Dataset

Return type

_DatasetWrapper

Parameters

dataset (xarray.core.dataset.Dataset) –

__init__(dataset)
Parameters

dataset (xarray.core.dataset.Dataset) –

Return type

None

Methods

__init__(dataset)

from_files(data_path[, bands, categorical, ...])

Read in a raster from file at 10m cell size and British National Grid extent.

Attributes

dataset

classmethod from_files(data_path, bands=None, categorical=False, nodata=None, out_path=None, overwrite=False, chunks='auto', engine=None, decode_coords='all')

Read in a raster from file at 10m cell size and British National Grid extent.

Examples

Read 2 bands from raster, specifying that the first is categorical.

>>> dset_wrap_from_tif = DatasetWrapper.from_files(
        data_path="3_band_raster.tif",
        bands=[1, 2],
        categorical={1: True, 2: False},
    )

Read 2 bands from raster, specifying both are categorical.

>>> dset_wrap_from_zarr = DatasetWrapper.from_files(
        data_path="multiband_raster.zarr",
        bands=["classification", "code"],
        categorical=True
    )
Parameters
  • data_path (str) – File path to raster.

  • bands (Union[List[int], List[str], None]) – List of bands to select from the raster. Defaults to None.

  • categorical (Union[bool, Dict[Union[int, str], bool]]) – bool or dict mapping ({band : bool}) of the interpolation used to resample. Defaults to False.

  • nodata (Optional[float]) – Value that will fill the grid where there is no data (if it is not None). Defaults to None.

  • out_path (Optional[str]) – Path to write reshaped data. Defaults to None.

  • overwrite (bool) – Overwrite existing zarr? Defaults to False.

  • chunks (Optional[Union[int, Dict[Any, Any], Literal["auto"]]]) – Chunk size or state. Passed to chunks in xarray.open_dataset. Defaults to “auto”.

  • engine (Optional[str]) – Engine used by open_dataset. Defaults to None.

  • decode_coords (Optional[Union[bool, Literal["coordinates", "all"]]]) – Value used by open_dataset. Variable upon engine selection. Defaults to “all”.

Returns

A thin wrapper around an xarray.Dataset containing

`xarray.DataArray with 10m cell size and British National Grid extent.

Return type

_DatasetWrapper