mdbartos / pysheds

:earth_americas: Simple and fast watershed delineation in python.
GNU General Public License v3.0
708 stars 191 forks source link

How to save raster object to tiff file? #193

Closed rishav-karanjit closed 2 years ago

eorland commented 2 years ago

I'd look at the to_raster method on line 321 in sgrid.py:

def to_raster(self, data, file_name, target_view=None, profile=None,
                  blockxsize=256, blockysize=256, interpolation='nearest',
                  apply_input_mask=False, apply_output_mask=True,
                  inherit_nodata=True, affine=None, shape=None, crs=None,
                  mask=None, nodata=None, dtype=None, **kwargs):
        """
        Writes gridded data to a raster.
        Parameters
        ----------
        data: Raster
            Raster dataset to write.
        file_name : str
                    Name of file or path to write to.
        target_view : ViewFinder
                    ViewFinder to use when writing data. Defaults to self.viewfinder.
        profile : dict
                    Profile of driver for writing data. See rasterio documentation.
        blockxsize : int
                        Size of blocks in horizontal direction. See rasterio documentation.
        blockysize : int
                        Size of blocks in vertical direction. See rasterio documentation.
        interpolation : 'nearest', 'linear'
                        Interpolation method to be used if spatial reference systems
                        are not congruent.
        apply_input_mask : bool
                            If True, mask the input Raster according to self.mask.
        apply_output_mask : bool
                            If True, mask the output Raster according to target_view.mask.
        inherit_nodata : bool
                         If True, output Raster inherits `nodata` value from `data`.
                         If False, output Raster uses `nodata` value from grid's viewfinder.
        affine : affine.Affine
                    Affine transformation matrix (overrides target_view.affine)
        shape : tuple of ints (length 2)
                Shape of desired Raster (overrides target_view.shape)
        crs : pyproj.Proj
                Coordinate reference system (overrides target_view.crs)
        mask : np.ndarray or Raster
                Boolean array to mask output (overrides target_view.mask)
        nodata : int or float
                    Value indicating no data in output Raster (overrides target_view.nodata)
        dtype : numpy datatype
                Desired datatype of the output array.
        """
        if target_view is None:
            target_view = self.viewfinder
        return pysheds.io.to_raster(data, file_name, target_view=target_view,
                                    profile=profile, blockxsize=blockxsize,
                                    blockysize=blockysize,
                                    interpolation=interpolation,
                                    apply_input_mask=apply_input_mask,
                                    apply_output_mask=apply_output_mask,
                                    inherit_nodata=inherit_nodata,
                                    affine=affine, shape=shape, crs=crs,
                                    mask=mask, nodata=nodata, dtype=dtype,
                                    **kwargs)
rishav-karanjit commented 2 years ago

Thank you @eorland. But, I am wondering how do I save pysheds.sview.Raster type data to tiff file?

mdbartos commented 2 years ago

See 'Writing to raster files' here: https://mattbartos.com/pysheds/file-io.html

If you want tiff, use .tiff extension in the filename. It uses rasterio under the hood.

rishav-karanjit commented 2 years ago

thank you