Extract and Interpolate Data on a Raster¶
The pixels of a raster contain data points that can contain various information, such as elevation or eligibility criteria coded as integers. To access this data, Geotiff files can be converted to NumPy arrays for easy inspection.
In [1]:
Copied!
# extract value at a given location by interpolating
import pathlib
import geokit.core.raster
from geokit.core.get_test_data import get_test_data
path_to_test_data = get_test_data(file_name="gsa-ghi-like.tif")
# extract matrix
geokit.core.raster.extractMatrix(path_to_test_data)
# extract value at a given location by interpolating
import pathlib
import geokit.core.raster
from geokit.core.get_test_data import get_test_data
path_to_test_data = get_test_data(file_name="gsa-ghi-like.tif")
# extract matrix
geokit.core.raster.extractMatrix(path_to_test_data)
Out[1]:
array([[2.877, 2.878, 2.879, ..., 2.834, 2.834, 2.835],
[2.875, 2.877, 2.878, ..., 2.835, 2.835, 2.835],
[2.874, 2.876, 2.877, ..., 2.836, 2.836, 2.837],
...,
[2.927, 2.934, 2.94 , ..., 2.966, 2.976, 2.981],
[2.936, 2.939, 2.944, ..., 2.987, 2.985, 2.986],
[2.943, 2.946, 2.951, ..., 2.99 , 2.989, 2.989]],
shape=(180, 180), dtype=float32)
Interpolate Data¶
Often data is necessary which lies in between points of the raster. This can be estimated using interpolation. Geokit provides
- near -> 0
- linear-spline -> 2
- cubic-spline -> 4
- average -> 3
- func -> 3
In [2]:
Copied!
value_near = geokit.core.raster.interpolateValues(
source=path_to_test_data,
points=[(5.6, 51.1), (5.7, 51.1), (5.8, 51.1)],
)
print(value_near)
value_interp = geokit.core.raster.interpolateValues(
source=path_to_test_data,
points=[(5.6, 51.1), (5.7, 51.1), (5.8, 51.1)],
mode="linear-spline",
)
print(value_interp)
value_near = geokit.core.raster.interpolateValues(
source=path_to_test_data,
points=[(5.6, 51.1), (5.7, 51.1), (5.8, 51.1)],
)
print(value_near)
value_interp = geokit.core.raster.interpolateValues(
source=path_to_test_data,
points=[(5.6, 51.1), (5.7, 51.1), (5.8, 51.1)],
mode="linear-spline",
)
print(value_interp)
[2.927 2.936 2.948] [2.926 2.93450004 2.94799995]