-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgap_filling.py
More file actions
94 lines (80 loc) · 2.59 KB
/
Copy pathgap_filling.py
File metadata and controls
94 lines (80 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# ---
# title: Gap Filling Functions
# author: Brendan Casey
# created: 2026-07-10
# notes:
# Fill gaps in Earth Engine imagery using Inverse
# Distance Weighting (IDW) interpolation, applied band by
# band. Each band is sampled to points, interpolated, and
# recombined into a single multi-band image.
# ---
import ee
def apply_idw_interpolation(image, aoi, range_m, gamma,
num_pixels):
"""Fill image gaps with IDW interpolation.
Parameters
----------
image : ee.Image
Input image with gaps to fill.
aoi : ee.Geometry
Area of interest for interpolation.
range_m : float
Maximum distance (in meters) to search for values.
gamma : float
Decay factor for the inverse distance weighting.
num_pixels : int
Number of pixels to sample for interpolation.
Returns
-------
ee.Image
The image with gaps filled by interpolation.
"""
band_names = image.bandNames()
# Interpolate a single band by name.
def interpolate_band(band_name):
band_name = ee.String(band_name)
# Turn each sampled pixel into a point feature.
def to_point(sample):
lat = sample.get("latitude")
lon = sample.get("longitude")
value = sample.get(band_name)
return ee.Feature(
ee.Geometry.Point([lon, lat])
).set(band_name, value)
# Sample the band to get known values.
samples = (
image.select([band_name])
.addBands(ee.Image.pixelLonLat())
.sample(
region=aoi,
numPixels=num_pixels,
scale=30,
projection="EPSG:4326",
)
.map(to_point)
)
# Global mean and standard deviation of samples.
stats = samples.reduceColumns(
reducer=ee.Reducer.mean().combine(
reducer2=ee.Reducer.stdDev(),
sharedInputs=True,
),
selectors=[band_name],
)
# Apply IDW interpolation.
interpolated = samples.inverseDistance(
range=range_m,
propertyName=band_name,
mean=stats.get("mean"),
stdDev=stats.get("stdDev"),
gamma=gamma,
)
return interpolated.rename(band_name)
# Interpolate every band and combine into one image.
interpolated_bands = band_names.map(interpolate_band)
interpolated_image = (
ee.ImageCollection(interpolated_bands)
.toBands()
.clip(aoi)
)
return interpolated_image