Skip to content

Commit

Permalink
Merge pull request #180 from GispoCoding/178-fix-kriging-interpolatio…
Browse files Browse the repository at this point in the history
…n-grid-creation

178 fix kriging interpolation grid creation
  • Loading branch information
nmaarnio authored Sep 7, 2023
2 parents b1d22e5 + 81e5312 commit 23b4fa6
Show file tree
Hide file tree
Showing 3 changed files with 672 additions and 23 deletions.
30 changes: 18 additions & 12 deletions eis_toolkit/vector_processing/kriging_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import geopandas as gpd
import numpy as np
import numpy.ma as ma
from beartype import beartype
from beartype.typing import Literal, Optional, Tuple
from pykrige.ok import OrdinaryKriging
from pykrige.uk import UniversalKriging
from rasterio import transform

from eis_toolkit.exceptions import EmptyDataFrameException, InvalidParameterValueException

Expand All @@ -17,13 +17,13 @@ def _kriging(
resolution: Tuple[Number, Number],
extent: Optional[Tuple[Number, Number, Number, Number]],
variogram_model: Literal,
coordinates_type: Literal,
method: Literal,
drift_terms: list,
) -> Tuple[np.ndarray, dict]:

points = np.array(list(data.geometry.apply(lambda geom: [geom.x, geom.y])))
x = points[:, 0]
y = points[:, 1]
x = data.geometry.x
y = data.geometry.y
z = data[target_column].values

if extent is None:
Expand All @@ -35,20 +35,23 @@ def _kriging(
else:
grid_x_min, grid_x_max, grid_y_min, grid_y_max = extent

grid_x = np.linspace(grid_x_min, grid_x_max, resolution[0])
grid_y = np.linspace(grid_y_min, grid_y_max, resolution[1])
grid_x = np.arange(grid_x_min, grid_x_max + resolution[0], resolution[0])
grid_y = np.arange(grid_y_min, grid_y_max + resolution[1], resolution[1])

if method == "universal":
universal_kriging = UniversalKriging(x, y, z, variogram_model=variogram_model, drift_terms=drift_terms)
z_interpolated, _ = universal_kriging.execute("grid", grid_x, grid_y)

if method == "ordinary":
ordinary_kriging = OrdinaryKriging(x, y, z, variogram_model=variogram_model)
ordinary_kriging = OrdinaryKriging(x, y, z, variogram_model=variogram_model, coordinates_type=coordinates_type)
z_interpolated, _ = ordinary_kriging.execute("grid", grid_x, grid_y)

z_interpolated = ma.getdata(z_interpolated)

out_meta = {"crs": data.crs, "width": len(grid_x), "height": len(grid_y)}
out_meta = {
"crs": data.crs,
"width": len(grid_x),
"height": len(grid_y),
"transform": transform.from_bounds(grid_x_min, grid_y_min, grid_x_max, grid_y_max, len(grid_x), len(grid_y)),
}

return z_interpolated, out_meta

Expand All @@ -60,6 +63,7 @@ def kriging(
resolution: Tuple[Number, Number],
extent: Optional[Tuple[Number, Number, Number, Number]] = None,
variogram_model: Literal["linear", "power", "gaussian", "spherical", "exponential", "hole-effect"] = "linear",
coordinates_type: Literal["euclidean", "geographic"] = "geographic",
method: Literal["ordinary", "universal"] = "ordinary",
drift_terms: list = ["regional_linear"],
) -> Tuple[np.ndarray, dict]:
Expand All @@ -69,10 +73,12 @@ def kriging(
Args:
data: GeoDataFrame containing the input data.
target_column: The column name with values for each geometry.
resolution: Size of the output grid.
resolution: The resolution i.e. cell size of the output raster.
extent: The extent of the output grid.
If None, calculate extent from the input vector data.
variogram_model: Variogram model to be used. Defaults to 'linear'.
coordinates_type: Determines are coordinates on a plane ('euclidean') or a sphere ('geographic').
Defaults to 'geographic'.
method: Kriging method. Defaults to 'ordinary'.
drift_terms: Drift terms used in universal kriging.
Expand Down Expand Up @@ -103,7 +109,7 @@ def kriging(
)

data_interpolated, out_meta = _kriging(
data, target_column, resolution, extent, variogram_model, method, drift_terms
data, target_column, resolution, extent, variogram_model, coordinates_type, method, drift_terms
)

return data_interpolated, out_meta
Loading

0 comments on commit 23b4fa6

Please sign in to comment.