skmap.io.base.read_rasters#

read_rasters(raster_files=[], band=1, window=None, bounds=None, dtype='float32', n_jobs=8, data_mask=None, scale=1.0, expected_shape=None, try_without_window=False, gdal_opts={}, overview=None, max_rasters=None, verbose=False)[source]#

Read raster files aggregating them into a single array. Only the first band of each raster is read.

The nodata value is replaced by np.nan in case of dtype=float*, and for dtype=*int* it’s replaced by the the lowest possible value inside the range (for int16 this value is -32768).

Parameters:
  • raster_files (Union[List, str]) – A list with the raster paths. Provide it and the raster_dirs is ignored.

  • window (Window | None) – Read the data according to the spatial window. By default is None, reading all the raster data.

  • dtype (str) – Convert the read data to specific dtype. By default it reads in float16 to save memory, however pay attention in the precision limitations for this dtype [1].

  • n_jobs (int) – Number of parallel jobs used to read the raster files.

  • data_mask (ndarray[tuple[Any, ...], dtype[float32]]) – A array with the same space dimensions of the read data, where all the values equal 0 are converted to np.nan.

  • expected_shape – The expected size (space dimension) of the read data. In case of error in reading any of the raster files, this is used to create a empty 2D array. By default is None, throwing a exception if the raster doesn’t exists.

  • try_without_window (bool) – First, try to read using window, if fails try to read without it.

  • overview – Overview level to be read. In COG files are usually [2, 4, 8, 16, 32, 64, 128, 256].

  • verbose – Use True to print the reading progress.

Returns:

A 3D array, where the last dimension refers to the read files, and a list containing the read paths.

Return type:

ndarray[tuple[Any, ...], dtype[float32]]

References

[1] Float16 Precision

Examples

>>> import rasterio
>>> from skmap.io.base import read_rasters
>>>
>>> # skmap COG layers - NDVI seasons for 2000
>>> # these actually 404
>>> raster_files = [
...     'http://s3.eu-central-1.wasabisys.com/skmap/lcv/lcv_ndvi_landsat.glad.ard_p50_30m_0..0cm_200003_skmap_epsg3035_v1.0.tif', # winter
...     'http://s3.eu-central-1.wasabisys.com/skmap/lcv/lcv_ndvi_landsat.glad.ard_p50_30m_0..0cm_200006_skmap_epsg3035_v1.0.tif', # spring
...     'http://s3.eu-central-1.wasabisys.com/skmap/lcv/lcv_ndvi_landsat.glad.ard_p50_30m_0..0cm_200009_skmap_epsg3035_v1.0.tif', # summer
...     'http://s3.eu-central-1.wasabisys.com/skmap/lcv/lcv_ndvi_landsat.glad.ard_p50_30m_0..0cm_200012_skmap_epsg3035_v1.0.tif'  # fall
... ]
>>>
>>> # Transform for the EPSG:3035
>>> eu_transform = rasterio.open(raster_files[0]).transform
>>> # Bounding box window over Wageningen, NL
>>> window = rasterio.windows.from_bounds(left=4020659, bottom=3213544, right=4023659, top=3216544, transform=eu_transform)
>>>
>>> data, _ = read_rasters_cpp(raster_files=raster_files, window=window, verbose=True)
>>> print(f'Data shape: {data.shape}')