update_meta#

update_meta(arr: Union[numpy.ndarray, numpy.ma.core.MaskedArray], meta: dict) dict[source]#

Basic metadata update from a numpy array. Updates everything that we can find in the array:

  • dtype: array dtype,

  • count: first dimension of the array if the array is in 3D, else 1

  • code:height`

    second dimension of the array

  • width: third dimension of the array

  • nodata: if a masked array is given, nodata is its fill_value

Warning

The array’s shape is interpreted in rasterio’s way (count, height, width) !

>>> raster_path = "path/to/raster.tif"
>>> with rasterio.open(raster_path) as dst:
>>>      meta = dst.meta
>>>      arr = dst.read()
>>> meta
{
    'driver': 'GTiff',
    'dtype': 'float32',
    'nodata': None,
    'width': 300,
    'height': 300,
    'count': 4,
    'crs': CRS.from_epsg(32630),
    'transform': Affine(20.0, 0.0, 630000.0,0.0, -20.0, 4870020.0)
}
>>> new_arr = np.ma.masked_array(arr[:, ::2, ::2].astype(np.uint8), fill_value=0)
>>> new_arr.shape
(4, 150, 150)
>>> new_arr.dtype
dtype('uint8')
>>> new_arr.fill_value
0
>>> update_meta(new_arr, meta)
{
    'driver': 'GTiff',
    'dtype': dtype('uint8'),
    'nodata': 0,
    'width': 150,
    'height': 150,
    'count': 4,
    'crs': CRS.from_epsg(32630),
    'transform': Affine(20.0, 0.0, 630000.0, 0.0, -20.0, 4870020.0)
}
Parameters
  • arr (Union[np.ndarray, np.ma.masked_array]) – Array from which to update the metadata

  • meta (dict) – Metadata to update

Returns

Update metadata

Return type

dict