demcompare.img_tools

This module contains generic functions associated to raster images. It consists mainly on wrappers to rasterio functions.

Module Contents

Functions

convert_pix_to_coord(→ Tuple[numpy.ndarray, numpy.ndarray])

Convert input (row, col) pixels to dataset geographic coordinates

crop_rasterio_source_with_roi(→ Tuple[numpy.ndarray, ...)

Transforms the input Region of Interest to polygon and

compute_gdal_translate_bounds(→ Tuple[float, float, ...)

Obtain the gdal coordinate bounds to apply the translation offsets to

demcompare.img_tools.convert_pix_to_coord(transform_array: List | numpy.ndarray, row: float | int | numpy.ndarray, col: float | int | numpy.ndarray) Tuple[numpy.ndarray, numpy.ndarray][source]

Convert input (row, col) pixels to dataset geographic coordinates from affine rasterio transform in upper left convention. See: https://gdal.org/tutorials/geotransforms_tut.html

Parameters:
  • transform_array (List or np.ndarray) – Array containing 6 Affine Geo Transform coefficients

  • row (float, int or np.ndarray) – row to convert

  • col (float, int or np.ndarray) – column to convert

Returns:

converted x,y in geographic coordinates from affine transform

Return type:

Tuple[np.ndarray, np.ndarray]

demcompare.img_tools.crop_rasterio_source_with_roi(src: rasterio.DatasetReader, roi: List[float]) Tuple[numpy.ndarray, rasterio.Affine][source]

Transforms the input Region of Interest to polygon and crops the input rasterio source DEM and its transform. If the ROI is outside of the input DEM, an exception is raised.

Parameters:
  • src (rasterio.DatasetReader) – input source dataset in rasterio format

  • roi (List[float]) – region of interest to crop

Returns:

cropped dem and its affine transform

Return type:

Tuple[np.ndarray, Affine]

demcompare.img_tools.compute_gdal_translate_bounds(y_offset: float | int | numpy.ndarray, x_offset: float | int | numpy.ndarray, shape: Tuple[int, int], georef_transform: numpy.ndarray) Tuple[float, float, float, float][source]

Obtain the gdal coordinate bounds to apply the translation offsets to the DEM to coregister/translate with gdal.

The offsets can be applied with the command line: gdal_translate -a_ullr <ulx> <uly> <lrx> <lry> /path_to_original_dem.tif /path_to_coregistered_dem.tif

Parameters:
  • y_offset (Union[float, int, ndarray]) – y pixel offset

  • x_offset (Union[float, int, ndarray]) – x pixel offset

  • shape (Tuple[int, int]) – rasterio tuple containing x size and y size

  • georef_transform (np.ndarray) – Array with 6 Affine Geo Transform coefficients

Returns:

coordinate bounds to apply the offsets

Return type:

Tuple[float,float,float,float]