write

Contents

write#

write(xds: DataArray | Dataset, output_path: str | CloudPath | Path = None, tags: dict = None, write_cogs_with_dask: bool = True, compute: bool = True, **kwargs) None[source]#

Write raster to disk. (encapsulation of rasterio’s function, because for now rioxarray to_raster doesn’t work as expected)

Metadata will be created with the xarray metadata (i.e. width, height, count, type…) The driver is GTiff by default, and no nodata value is provided. The file will be compressed if the raster is a mask (saved as uint8).

If not overwritten, sets the nodata according to dtype:

  • uint8: 255

  • int8: -128

  • uint16, uint32, int32, int64, uint64, int: 65535

  • :code:int16, float32, float64, float128, float: -9999

Default parameters

  • Compress with LZW option by default. To disable it, add the compress=None parameter.

  • predictor set to 2 for float data, to 3 for interger data by default. To disable it, add the predictor=None parameter.

  • tiled set to True by default.

  • driver is GTiff by default. BigTiff option is set according to the estimated output weight

Parameters:
  • xds (AnyXrDataStructure) – Path to the raster or a rasterio dataset or a xarray

  • output_path (AnyPathStrType) – Path where to save it (directories should be existing)

  • tags (dict) – Tags that will be written in your file

  • write_cogs_with_dask (bool) – If odc-geo and imagecodecs are installed, write your COGs with Dask. Otherwise, the array will be loaded into memory before writing it on disk (and can cause MemoryErrors).

  • compute (bool) – If True (default) and data is a dask array, then compute and save the data immediately. If False, return a dask Delayed object. Call “.compute()” on the Delayed object to compute the result later. Call dask.compute(delayed1, delayed2) to save multiple delayed files at once.

  • **kwargs – Overloading metadata, ie nodata=255 or dtype=np.uint8

Examples

>>> raster_path = "path/to/raster.tif"
>>> raster_out = "path/to/out.tif"
>>>
>>> # Read raster
>>> xds = read(raster_path)
>>>
>>> # Rewrite it
>>> write(xds, raster_out)