eodal.utils.arrays module

Utilities to interact with 2d arrays

Copyright (C) 2022 Lukas Valentin Graf

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

eodal.utils.arrays.array_from_points(gdf: GeoDataFrame, band_name_src: str, pixres_x: int | float, pixres_y: int | float, nodata_dst: int | float | None = 0, dtype_src: str | None = 'float32') array[source]

Converts a GeoDataFrame with POINT features into a 2-d np.ndarray using the full spatial extent of the input features

Note

Currently, only nearest neighbor interpolation is supported

Parameters:
  • gdfGeoDataFrame with POINT features to convert to raster

  • band_name_src – name of GeoDataFrame column to rasterize

  • pixres_x – spatial resolution of the output raster (in units of the CRS of the gdf input) in x direction

  • pixres_y – spatial resolution of the output raster (in units of the CRS of the gdf input) in y direction

  • nodata_dst – no data values to assign to empty cells. Zero by default.

  • dtype_src – data type of the resulting raster array. Per default “float32” is used.

Returns:

2-d numpy.ndarray with rasterized POINT features

eodal.utils.arrays.count_valid(in_array: array | MaskedArray, no_data_value: int | float | None = 0.0) int[source]

Counts the number of valid (i.e., non no-data) elements in a 2-d array. If a masked array is provided, the number of valid elements is the number of not-masked array elements.

Parameters:
  • in_array – two-dimensional array to analyze. Can be an ordinary numpy.ndarray or a masked array.

  • no_data_value – no data value indicating invalid array elements. Default set to zero. Ignored if in_array is a MaskedArray

Returns:

number of invalid array elements.

eodal.utils.arrays.upsample_array(in_array: array, scaling_factor: int) array[source]

takes a 2-dimensional input array (i.e., image matrix) and splits every array cell (i.e., pixel) into X smaller ones having all the same value as the “super” cell they belong to, where X is the scaling factor (X>=1). This way the input image matrix gets a higher spatial resolution without changing any of the original pixel values.

The value of the scaling_factor determines the spatial resolution of the output. If scaling_factor = 1 then the input and the output array are the same. If scaling_factor = 2 then the output array has a spatial resolution two times higher then the input (e.g. from 20 to 10 m), and so on.

Parameters:
  • array_in – 2-d array (image matrix)

  • scaling_factor – factor for increasing spatial resolution. Must be greater than/ equal to 1

Returns:

upsampled array with pixel values in target spatial resolution