eumap.raster.write_new_raster

write_new_raster(fn_base_raster, fn_new_raster, data, spatial_win=None, dtype=None, nodata=None, fit_in_dtype=False)[source]

Save an array to a raster file using as reference a base raster. GeoTIFF is the only output format supported. It always replaces the np.nan value by the specified nodata.

Parameters
  • fn_base_raster (str) – The base raster path used to retrieve the parameters (height, width, n_bands, crs, dtype, transform) for the new raster.

  • fn_new_raster (str) – The path for the new raster. It creates the folder hierarchy if not exists.

  • data (array) – 3D data array where the last dimension is the number of bands for the new raster. For 2D array it saves only one band.

  • spatial_win (Optional[Window]) – Save the data considering a spatial window, even if the fn_base_rasters refers to a bigger area. For example, it’s possible to have a base raster covering the whole Europe and save the data using a window that cover just part of Wageningen. By default is None saving the raster data in position 0, 0 of the raster grid.

  • dtype (Optional[str]) – Convert the data to a specific dtype before save it. By default is None using the same dtype from the base raster.

  • nodata – Use the specified value as nodata for the new raster. By default is None using the same nodata from the base raster.

  • fit_in_dtype – If True the values outside of dtype range are truncated to the minimum and maximum representation. It’s also change the minimum and maximum data values, if they exist, to avoid overlap with nodata (see the _fit_in_dtype function). For example, if dtype='uint8' and nodata=0, all data values equal to 0 are re-scaled to 1 in the new rasters.

Returns

The path of the new raster.

Return type

Path

Examples

>>> import rasterio
>>> from eumap.raster import read_rasters, save_rasters
>>>
>>> # EUMAP COG layers - NDVI seasons for 2019
>>> raster_files = [
>>>     'http://s3.eu-central-1.wasabisys.com/eumap/lcv/lcv_ndvi_landsat.glad.ard_p50_30m_0..0cm_201903_eumap_epsg3035_v1.0.tif', # winter
>>> ]
>>>
>>> # 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(raster_files=raster_files, spatial_win=window, verbose=True)
>>>
>>> # Save in the current execution folder
>>> fn_new_raster = './lcv_ndvi_landsat.glad.ard_p50_30m_0..0cm_201903_wageningen_epsg3035_v1.0.tif',
>>>
>>> write_new_raster(raster_files[0], fn_new_raster, data, spatial_win=window)