update_meta#
- update_meta(arr: ndarray | 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 arraynodata
: 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) !
- Parameters:
arr (AnyNumpyArray) – Array from which to update the metadata
meta (dict) – Metadata to update
- Returns:
Update metadata
- Return type:
Example
>>> raster_path = "path/to/raster.tif" >>> with rasterio.open(raster_path) as ds: >>> meta = ds.meta >>> arr = ds.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) }