environmentaltools.spatial.interp
- environmentaltools.spatial.interp(base, data, dist=100, method='linear', fill_values=nan)[source]
Interpolate spatial data onto a regular grid or specified points.
Performs 2D spatial interpolation from scattered base data points to either a regular grid or specified target points. Uses scipy.interpolate.griddata for the interpolation.
- Parameters:
base (pd.DataFrame) – Base spatial data for interpolation. Must contain columns ‘x’, ‘y’, and ‘z’ where z is the variable to interpolate.
data (pd.DataFrame or dict) – Target interpolation domain. If DataFrame with two rows, defines rectangular bounds [xmin, xmax], [ymin, ymax] for regular grid creation. If dict with ‘x’ and ‘y’ keys, contains target point coordinates for interpolation.
dist (float, optional) – Grid spacing in same units as coordinates when creating regular grid. Only used if data is DataFrame with bounds. Default is 100.
method (str, optional) – Interpolation method: ‘linear’, ‘nearest’, or ‘cubic’. Default is ‘linear’. See scipy.interpolate.griddata for details.
fill_values (float, optional) – Value used to fill points outside the convex hull of base data. Default is np.nan.
- Returns:
x (np.ndarray) – X coordinates of interpolation points (2D meshgrid or flattened array)
y (np.ndarray) – Y coordinates of interpolation points (2D meshgrid or flattened array)
z (np.ndarray) – Interpolated values at (x, y) locations
df (pd.DataFrame) – Interpolation results as DataFrame with columns [‘x’, ‘y’, ‘z’]
Examples
>>> import pandas as pd >>> import numpy as np >>> # Create base data >>> base = pd.DataFrame({'x': [0, 1, 2], 'y': [0, 1, 2], 'z': [10, 20, 30]}) >>> # Define interpolation bounds >>> bounds = pd.DataFrame({'x': [0, 2], 'y': [0, 2]}) >>> x, y, z, df = interp(base, bounds, dist=0.5)