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.nanvalue by the specifiednodata.- 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 thefn_base_rastersrefers 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 isNonesaving the raster data in position0, 0of the raster grid.dtype (
Optional[str]) – Convert the data to a specificdtypebefore save it. By default isNoneusing the samedtypefrom the base raster.nodata – Use the specified value as
nodatafor the new raster. By default isNoneusing the samenodatafrom the base raster.fit_in_dtype – If
Truethe values outside ofdtyperange are truncated to the minimum and maximum representation. It’s also change the minimum and maximum data values, if they exist, to avoid overlap withnodata(see the_fit_in_dtypefunction). For example, ifdtype='uint8'andnodata=0, all data values equal to0are re-scaled to1in 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)