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

remove_nan_and_flatten(→ numpy.ndarray)

Function for removing NaNs from a numpy array (data)

compute_surface_normal(→ numpy.ndarray)

Return the surface normal vector at each pixel.

neighbour_interpol(→ numpy.ndarray)

Nearest neighbor interpolation function.

calc_spatial_freq_2d(s_y, s_x[, edge])

Calculation of normalized spatial frequencies

calc_spatial_freq_1d(→ numpy.ndarray)

Calculation of normalized frequencies

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]

demcompare.img_tools.remove_nan_and_flatten(data: numpy.ndarray) numpy.ndarray[source]

Function for removing NaNs from a numpy array (data) If data has a dimension >1, the function returns a row vector (1D) without NaNs

Parameters:

data (np.ndarray) – array of values

Returns:

array of values without Nans

Return type:

np.ndarray

demcompare.img_tools.compute_surface_normal(data: numpy.ndarray, dx: numpy.float64, dy: numpy.float64) numpy.ndarray[source]

Return the surface normal vector at each pixel. First: compute the gradient in every direction at each pixel. Finally: compute the cross product of the 2 gradient vectors.

Parameters:
  • data (np.ndarray) – 2D (row, col) np.ndarray containing the image

  • dx (np.float64) – DEM’s resolution in the X direction

  • dy (np.float64) – DEM’s resolution in the Y direction

Returns:

vector (3D, row, col) normal to the surface for each pixel

Return type:

np.ndarray

demcompare.img_tools.neighbour_interpol(data2d: numpy.ndarray, no_values_location: numpy.ndarray) numpy.ndarray[source]

Nearest neighbor interpolation function. Applied to DEM containing no data values for calculating curvature.

Parameters:
  • data2d (np.ndarray) – 2D (row, col) np.ndarray containing the image

  • no_values_location (np.ndarray) – 2D (row, col) np.ndarray containing the no data values

Returns:

2D interpolated np.ndarray

Return type:

np.ndarray

demcompare.img_tools.calc_spatial_freq_2d(s_y: int, s_x: int, edge: float = np.pi)[source]

Calculation of normalized spatial frequencies for a 2D image/matrix of size s_y * s_x.

Parameters:
  • s_y (int) – number of rows of the image

  • s_x (int) – number of columns of the image

Returns:

normalized spatial frequencies

Return type:

(np.ndarray, np.ndarray)

demcompare.img_tools.calc_spatial_freq_1d(n: int, edge: float = np.pi) numpy.ndarray[source]

Calculation of normalized frequencies between [-edge, +edge] of a vector of n samples. n is even or odd. In both cases, the vector contains the zero frequency at the center.

Parameters:
  • n (int) – frequency vector size

  • edge (float) – maximum frequency

Returns:

frequencies between -edge and +edge

Return type:

np.ndarray