Skip to content

Commit 448b65d

Browse files
committed
Fixed application of distance transform udf
1 parent 0e800d1 commit 448b65d

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

efast/distance_transform_udf.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
# #from scipy.ndimage import distance_transform_edt
2-
# #import numpy as np
3-
# import xarray as xr
1+
from scipy.ndimage import distance_transform_edt
2+
import numpy as np
3+
import xarray as xr
44
from openeo.udf import XarrayDataCube
55

66
def apply_datacube(cube: XarrayDataCube, context: dict) -> XarrayDataCube:
7-
#distance = distance_transform_edt(cube)
8-
#return np.clip(distance, 0, 255)
9-
return cube
7+
array = cube.get_array()
8+
distance = distance_transform_edt(array)
9+
clipped = np.clip(distance, 0, 255)
10+
return XarrayDataCube(xr.DataArray(clipped, dims=["bands", "t", "y", "x"]))

efast/efast_openeo.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,33 @@ def extract_mask(cube):
4646
def distance_to_clouds(
4747
cube: openeo.DataCube, tolerance_percentage=0.05, ratio=30, max_distance=255
4848
):
49-
udf = openeo.UDF.from_file("efast/distance_transform_udf.py")
50-
# kernel_size = np.ceil(max_distance)
51-
# gaussian_1d = scipy.signal.windows.gaussian(M=kernel_size, std=255 / 4)
52-
# kernel = np.outer(gaussian_1d, gaussian_1d)
53-
# kernel /= kernel.sum()
49+
return _distance_to_clouds_udf(cube, tolerance_percentage=tolerance_percentage, ratio=ratio, max_distance=max_distance)
50+
51+
52+
def _distance_to_clouds_kernel(
53+
cube: openeo.DataCube, tolerance_percentage=0.05, ratio=30, max_distance=7
54+
):
55+
kernel_size = np.ceil(max_distance)
56+
gaussian_1d = scipy.signal.windows.gaussian(M=kernel_size, std=255 / 4)
57+
kernel = np.outer(gaussian_1d, gaussian_1d)
58+
kernel /= kernel.sum()
59+
60+
dtc = cube.apply_kernel(kernel)
61+
return dtc
62+
5463

55-
# dtc = 1 - cube.apply_kernel(kernel)
56-
# dtc = cube.apply_neighborhood(
57-
# udf,
58-
# size=[
59-
# {"dimension": "x", "value": 61, "unit": "px"},
60-
# {"dimension": "y", "value": 61, "unit": "px"},
61-
# ],
62-
# overlap=[],
63-
# )
64-
dtc = cube.apply(udf)
64+
# TODO implement max_distance as a parameter to the UDF
65+
# TODO replace hard coded tile size (366)
66+
def _distance_to_clouds_udf(
67+
cube: openeo.DataCube, tolerance_percentage=0.05, ratio=30, max_distance=255
68+
):
69+
udf = openeo.UDF.from_file("efast/distance_transform_udf.py")
70+
dtc = cube.apply_neighborhood(
71+
udf,
72+
size=[
73+
{"dimension": "x", "value": 366, "unit": "px"},
74+
{"dimension": "y", "value": 366, "unit": "px"},
75+
],
76+
overlap=[],
77+
)
6578
return dtc

0 commit comments

Comments
 (0)