reproject

Contents

reproject#

reproject(src_xda: str | CloudPath | Path | tuple[ndarray | MaskedArray, dict] | DataArray | Dataset | DatasetReader | DatasetWriter, pixel_size: float = None, shape: tuple = None, resampling: Resampling = Resampling.bilinear, dst_crs: CRS = None, dst_transform: Affine = None, gcps: GroundControlPoint = None, nodata: float = None, num_threads: int = None, use_dask: bool = None, ortho_path: str | CloudPath | Path = None, name: str = None, rpcs: RPC = None, dem_path: str = None, extent: GeoDataFrame = None, vcrs: str | int = None, caching_folder: str | CloudPath | Path = None, **kwargs) DataArray[source]#

Function handling all cases of reprojection, with and without RPCS, with and without dask or chunked arrays.

Wrappers around: - odc.geo.xr_reproject for chunked arrays (in a dask-backed way) - rioxarray.reprjoect for xarray data - rasterio.reproject as a fallback (especially for Python 3.9)

Warning

RPC management is much more complex and necessits more arguments.

RPC reprojection is not daskified yet. See this `issue<https://github.com/opendatacube/odc-geo/issues/193>`_.

Cloud-stored DEM cannot be used as is. They will be cached and then use. Performance can be affected.

Orthorectifying data with RPC is using a DEM. This DEM should have its height based on the ellispoid. If your DEM doesn’t have a vertical CRS set, please provide it with vcrs.

Note that in some cases, the vertical CRS is set by default. This library recognizes files with Copernicus or COPDEM in it (based on EGM08). xdem may recognize other files. See `the documentation<https://xdem.readthedocs.io/en/stable/vertical_ref.html>`_ for more insights.

Parameters:
  • src_xda (AnyRasterType) – Raster to be reprojected

  • pixel_size (float) – Destination pixel size (in the destination CRS units). If not provided, the pixel size will be deducted from other parameters.

  • shape (tuple) – Destination shape (height, width). If not provided, the shape will be deducted from other parameters. Supersedes pixel_size.

  • resampling (Resampling) – Resampling method. Defaults to Resampling.bilinear.

  • dst_crs (CRS) – Destination CRS. If not provided, the CRS will be retrieved from the source raster.

  • dst_transform (Affine) – Destination transfgorm. If not provided, the transform will be computed from other parameters.

  • gcps ((control.GroundControlPoint, CRS)) – Ground Control Points

  • nodata (float) – Nodata value for the destination raster. If not provided, the nodata value will be retrieved from the source raster.

  • num_threads (int) – Number of threads to use for parallel processing. If not provided, set to your number of CPU minus 2.

  • use_dask (bool) – Flag to use dask for parallel processing. If not provided, the flag will be set to None and dask used if the data is chunked.

  • ortho_path (AnyPathStrType) – Path to the orthorectified array

  • name (str) – Name of the output DataArray.

  • rpcs (rpc.RPC) – RPC to orthorectify some raster

  • dem_path (AnyPathStrType) – Path to the DEM, only used if rpcs is given.

  • extent (gpd.GeoDataFrame) – GeoDataFrame containing extent of the raster to extract the DEM, only used if rpcs is given.

  • vcrs (str | int) – Vertical CRS of the DEM, only used if rpcs is given. Can be automatically deducted in some cases (COPDEM, etc.). Better to set it tu be sure.

  • caching_folder (AnyPathStrType) – Folder where to cache temporary files. If not provided, a temporary folder will be created and deleted in the end of the process. Only used if rpcs is given.

  • **kwargs – Other arguments to pass to reproject or write

Returns:

Reprojected raster

Return type:

xr.DataArray

Examples

Example with RPCs >>> # Imports >>> import logging >>> from sertit import AnyPath, logs, rasters, vectors >>> logs.init_logger(logging.getLogger(“sertit”)) >>> >>> # Data >>> spot_path = AnyPath(“IMG_SPOT7_MS_001_A”, “DIM_SPOT7_MS_201602150257025_SEN_1671661101.XML”) >>> copdem_path = AnyPath(“Copernicus_DSM_10_S07_00_E106_00_DEM.tif”) >>> >>> # Reproject >>> with rasterio.open(spot_path) as ds: >>> rpc_reproj = rasters.reproject(spot_path, rpcs=ds.rpcs, dem_path=copdem_path, dst_crs=”epsg:4326”, nodata=0) [WARNING] - Reprojection with RPCs doesn’t work with Dask. Computing the raster. [DEBUG] - Orthorectifying data with Copernicus_DSM_10_S07_00_E106_00_DEM.tif >>> >>> # See if we have a CRS >>> rpc_reproj.rio.crs.to_epsg() 4326 >>> >>> # See if the corrdinates are correctly set (lon = 106,x, lat = -6,x) >>> # These coordinates correspond to the tile of the DEM in input (S07_00_E106_00) >>> rpc_reproj.coords Coordinates:

  • x (x) float64 40kB 106.6 106.6 106.6 106.6 … 107.0 107.0 107.0

  • y (y) float64 46kB -6.0 -6.0 -6.0 -6.0 … -6.421 -6.421 -6.421

  • band (band) int64 32B 1 2 3 4 spatial_ref int64 8B 0

Example of very simple reprojections >>> # Reproject to change the pixel size of the raster >>> rasters.reproject(xda, pixel_size=60)

>>> # Reproject to a specific shape
>>> rasters.reproject(xda, shape=(161, 232))
>>> # Reproject to a specific CRS
>>> rasters.reproject(xda, dst_crs="EPSG:4326")