imod.prepare.reproject#

imod.prepare.reproject(source, like=None, src_crs=None, dst_crs=None, method='nearest', use_src_attrs=False, src_nodata=nan, **reproject_kwargs)[source]#

Reprojects and/or resamples a 2D xarray DataArray to a different cellsize or coordinate system.

  • To resample to a new cellsize in the same projection: provide only like.

  • To only reproject: provide only src_crs and src_crs.

  • To reproject and resample to a specific domain: provide src_crs, src_crs, and like.

Note: when only like is provided, Cartesian (projected) coordinates are a ssumed for resampling. In case of non-Cartesian coordinates, specify src_crs and dst_crs for correct resampling.

Parameters:
  • source (xarray DataArray) – The DataArray to be resampled and/or reprojected. Must contain dimensions y and x.

  • like (xarray DataArray) – Example DataArray that shows what the resampled result should look like in terms of coordinates. Must contain dimensions y and x.

  • src_crs (string, dict, rasterio.crs.CRS) –

    Coordinate system of source. Options:

    • string: e.g. "EPSG:4326"

    • rasterio.crs.CRS

  • dst_crs (string, dict, rasterio.crs.CRS) –

    Coordinate system of result. Options:

    • string: e.g. "EPSG:4326"

    • rasterio.crs.CRS

  • use_src_attrs (boolean) – If True: Use metadata in source.attrs, as generated by xarray.open_rasterio(), to do reprojection.

  • method (string) –

    The method to use for resampling/reprojection. Defaults to “nearest”. GDAL methods are available:

    • nearest

    • bilinear

    • cubic

    • cubic_spline

    • lanczos

    • average

    • mode

    • gauss

    • max

    • min

    • med (50th percentile)

    • q1 (25th percentile)

    • q3 (75th percentile)

  • reproject_kwargs (dict, optional) – keyword arguments for rasterio.warp.reproject().

Returns:

Resampled/reprojected DataArray.

Return type:

xarray.DataArray

Examples

Resample a DataArray a to a new cellsize, using an existing DataArray b:

>>> c = imod.rasterio.reproject(source=a, like=b)

Resample a DataArray to a new cellsize of 100.0, by creating a like DataArray first: (Note that dy must be negative, as is usual for geospatial grids.)

>>> dims = ("y", "x")
>>> coords = {"y": np.arange(200_000.0, 100_000.0, -100.0), "x": np.arange(0.0, 100_000.0, 100.0)}
>>> b = xr.DataArray(data=np.empty((200, 100)), coords=coords, dims=dims)
>>> c = imod.rasterio.reproject(source=a, like=b)

Reproject a DataArray from one coordinate system (WGS84, EPSG:4326) to another (UTM30N, EPSG:32630):

>>> c = imod.rasterio.reproject(source=a, src_crs="EPSG:4326", dst_crs="EPSG:32630")

Get the reprojected DataArray in the desired shape and coordinates by providing like:

>>> c = imod.rasterio.reproject(source=a, like=b, src_crs="EPSG:4326", dst_crs="EPSG:32630")

Open a single band raster, and reproject to RD new coordinate system (EPSG:28992), without explicitly specifying src_crs. src_crs is taken from a.attrs, so the raster file has to include coordinate system metadata for this to work.

>>> a = rioxarray.open_rasterio("example.tif").squeeze("band")
>>> c = imod.rasterio.reproject(source=a, use_src_attrs=True, dst_crs="EPSG:28992")

In case of a rotated source, provide src_transform directly or use_src_attrs=True to rely on generated attributes:

>>> rotated = rioxarray.open_rasterio("rotated_example.tif").squeeze("band")
>>> c = imod.rasterio.reproject(source=rotated, dst_crs="EPSG:28992", reproject_kwargs={"src_transform":affine.Affine(...)})
>>> c = imod.rasterio.reproject(source=rotated, dst_crs="EPSG:28992", use_src_attrs=True)