openforis-whisp 2.0.0a6__py3-none-any.whl → 2.0.0b2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- openforis_whisp/__init__.py +75 -75
- openforis_whisp/data_conversion.py +504 -493
- openforis_whisp/datasets.py +1352 -1384
- openforis_whisp/logger.py +75 -75
- openforis_whisp/parameters/config_runtime.py +44 -44
- openforis_whisp/parameters/lookup_context_and_metadata.csv +13 -13
- openforis_whisp/parameters/lookup_gee_datasets.csv +1 -3
- openforis_whisp/pd_schemas.py +77 -77
- openforis_whisp/reformat.py +696 -495
- openforis_whisp/risk.py +848 -771
- openforis_whisp/stats.py +1320 -1134
- openforis_whisp/utils.py +487 -154
- {openforis_whisp-2.0.0a6.dist-info → openforis_whisp-2.0.0b2.dist-info}/LICENSE +21 -21
- {openforis_whisp-2.0.0a6.dist-info → openforis_whisp-2.0.0b2.dist-info}/METADATA +2 -2
- openforis_whisp-2.0.0b2.dist-info/RECORD +16 -0
- {openforis_whisp-2.0.0a6.dist-info → openforis_whisp-2.0.0b2.dist-info}/WHEEL +1 -1
- openforis_whisp/parameters/__init__.py +0 -15
- openforis_whisp-2.0.0a6.dist-info/RECORD +0 -17
openforis_whisp/utils.py
CHANGED
|
@@ -1,154 +1,487 @@
|
|
|
1
|
-
import base64
|
|
2
|
-
import ee
|
|
3
|
-
import math
|
|
4
|
-
import os
|
|
5
|
-
import pandas as pd
|
|
6
|
-
|
|
7
|
-
import
|
|
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
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
#
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
def
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
#
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
1
|
+
import base64
|
|
2
|
+
import ee
|
|
3
|
+
import math
|
|
4
|
+
import os
|
|
5
|
+
import pandas as pd
|
|
6
|
+
import random
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
import urllib.request
|
|
10
|
+
import os
|
|
11
|
+
|
|
12
|
+
import importlib.resources as pkg_resources
|
|
13
|
+
|
|
14
|
+
from dotenv import load_dotenv
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
|
|
17
|
+
from shapely.geometry import Polygon, Point, mapping
|
|
18
|
+
from shapely.validation import make_valid
|
|
19
|
+
|
|
20
|
+
from .logger import StdoutLogger
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
logger = StdoutLogger(__name__)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def get_example_data_path(filename):
|
|
27
|
+
"""
|
|
28
|
+
Get the path to an example data file included in the package.
|
|
29
|
+
|
|
30
|
+
Parameters:
|
|
31
|
+
-----------
|
|
32
|
+
filename : str
|
|
33
|
+
The name of the example data file.
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
--------
|
|
37
|
+
str
|
|
38
|
+
The path to the example data file.
|
|
39
|
+
"""
|
|
40
|
+
return os.path.join("..", "tests", "fixtures", filename)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def load_env_vars() -> None:
|
|
44
|
+
"""Loads the environment variables required for testing the codebase.
|
|
45
|
+
|
|
46
|
+
Returns
|
|
47
|
+
-------
|
|
48
|
+
out : None
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
all_dotenv_paths = [Path(__file__).parents[2] / ".env", Path.cwd() / ".env"]
|
|
52
|
+
dotenv_loaded = False
|
|
53
|
+
|
|
54
|
+
for dotenv_path in all_dotenv_paths:
|
|
55
|
+
logger.logger.debug(f"dotenv_path: {dotenv_path}")
|
|
56
|
+
if dotenv_path.exists():
|
|
57
|
+
dotenv_loaded = load_dotenv(dotenv_path)
|
|
58
|
+
break
|
|
59
|
+
|
|
60
|
+
if not dotenv_loaded:
|
|
61
|
+
raise DotEnvNotFoundError
|
|
62
|
+
logger.logger.info(f"Loaded evironment variables from '{dotenv_path}'")
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def init_ee() -> None:
|
|
66
|
+
"""Initialize earth engine according to the environment"""
|
|
67
|
+
|
|
68
|
+
# only do the initialization if the credential are missing
|
|
69
|
+
if not ee.data._credentials:
|
|
70
|
+
|
|
71
|
+
# if in test env use the private key
|
|
72
|
+
if "EE_PRIVATE_KEY" in os.environ:
|
|
73
|
+
|
|
74
|
+
# key need to be decoded in a file
|
|
75
|
+
content = base64.b64decode(os.environ["EE_PRIVATE_KEY"]).decode()
|
|
76
|
+
with open("ee_private_key.json", "w") as f:
|
|
77
|
+
f.write(content)
|
|
78
|
+
|
|
79
|
+
# connection to the service account
|
|
80
|
+
service_account = "test-sepal-ui@sepal-ui.iam.gserviceaccount.com"
|
|
81
|
+
credentials = ee.ServiceAccountCredentials(
|
|
82
|
+
service_account, "ee_private_key.json"
|
|
83
|
+
)
|
|
84
|
+
ee.Initialize(credentials)
|
|
85
|
+
logger.logger.info(f"Used env var")
|
|
86
|
+
|
|
87
|
+
# if in local env use the local user credential
|
|
88
|
+
else:
|
|
89
|
+
try:
|
|
90
|
+
load_env_vars()
|
|
91
|
+
logger.logger.info("Called 'ee.Initialize()'.")
|
|
92
|
+
ee.Initialize(project=os.environ["PROJECT"])
|
|
93
|
+
except ee.ee_exception.EEException:
|
|
94
|
+
logger.logger.info("Called 'ee.Authenticate()'.")
|
|
95
|
+
ee.Authenticate()
|
|
96
|
+
ee.Initialize(project=os.environ["PROJECT"])
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def clear_ee_credentials():
|
|
100
|
+
|
|
101
|
+
path_to_creds = Path().home() / ".config" / "earthengine" / "credentials"
|
|
102
|
+
if not path_to_creds.exists():
|
|
103
|
+
logger.logger.error(
|
|
104
|
+
f"GEE credentials file '{path_to_creds}' not found, could not de-authenticate."
|
|
105
|
+
)
|
|
106
|
+
else:
|
|
107
|
+
path_to_creds.unlink()
|
|
108
|
+
logger.logger.warning(f"GEE credentials file deleted.")
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def remove_geometry_from_feature_collection(feature_collection):
|
|
112
|
+
"""Define the function to remove geometry from features in a feature collection"""
|
|
113
|
+
# Function to remove geometry from features
|
|
114
|
+
def remove_geometry(feature):
|
|
115
|
+
# Remove the geometry property
|
|
116
|
+
feature = feature.setGeometry(None)
|
|
117
|
+
return feature
|
|
118
|
+
|
|
119
|
+
# Apply the function to remove geometry to the feature collection
|
|
120
|
+
feature_collection_no_geometry = feature_collection.map(remove_geometry)
|
|
121
|
+
return feature_collection_no_geometry
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
# Compute centroids of each polygon including the external_id_column
|
|
125
|
+
def get_centroid(feature, external_id_column="external_id"):
|
|
126
|
+
keepProperties = [external_id_column]
|
|
127
|
+
# Get the centroid of the feature's geometry.
|
|
128
|
+
centroid = feature.geometry().centroid(1)
|
|
129
|
+
# Return a new Feature, copying properties from the old Feature.
|
|
130
|
+
return ee.Feature(centroid).copyProperties(feature, keepProperties)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def buffer_point_to_required_area(feature, area, area_unit):
|
|
134
|
+
"""buffers feature to get a given area (needs math library); area unit in 'ha' or 'km2' (the default)"""
|
|
135
|
+
area = feature.get("REP_AREA")
|
|
136
|
+
|
|
137
|
+
# buffer_size = get_radius_m_to_buffer_for_given_area(area,"km2")# should work but untested in this function
|
|
138
|
+
|
|
139
|
+
buffer_size = (
|
|
140
|
+
(ee.Number(feature.get("REP_AREA")).divide(math.pi)).sqrt().multiply(1000)
|
|
141
|
+
) # calculating radius in metres from REP_AREA in km2
|
|
142
|
+
|
|
143
|
+
return ee.Feature(feature).buffer(buffer_size, 1)
|
|
144
|
+
### buffering (incl., max error parameter should be 0m. But put as 1m anyhow - doesn't seem to make too much of a difference for speed)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def get_radius_m_to_buffer_to_required_area(area, area_unit="km2"):
|
|
148
|
+
"""gets radius in metres to buffer to get an area (needs math library); area unit ha or km2 (the default)"""
|
|
149
|
+
if area_unit == "km2":
|
|
150
|
+
unit_fix_factor = 1000
|
|
151
|
+
elif area_unit == "ha":
|
|
152
|
+
unit_fix_factor = 100
|
|
153
|
+
radius = ee.Number(area).divide(math.pi).sqrt().multiply(unit_fix_factor)
|
|
154
|
+
return radius
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
class DotEnvNotFoundError(FileNotFoundError):
|
|
158
|
+
def __init__(self) -> None:
|
|
159
|
+
super().__init__(
|
|
160
|
+
"Running tests requires setting an appropriate '.env' in the root directory or in your current working "
|
|
161
|
+
"directory. You may copy and edit the '.env.template' file from the root directory or from the README.",
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def get_example_geojson(filename="geojson_example.geojson", cache=True):
|
|
166
|
+
"""
|
|
167
|
+
Download example geojson file for testing whisp functionality.
|
|
168
|
+
|
|
169
|
+
Parameters:
|
|
170
|
+
-----------
|
|
171
|
+
filename : str
|
|
172
|
+
Local filename to save the geojson
|
|
173
|
+
cache : bool
|
|
174
|
+
If True, cache file in user directory to avoid re-downloading
|
|
175
|
+
|
|
176
|
+
Returns:
|
|
177
|
+
--------
|
|
178
|
+
str
|
|
179
|
+
Path to the downloaded geojson file
|
|
180
|
+
"""
|
|
181
|
+
url = "https://raw.githubusercontent.com/forestdatapartnership/whisp/main/tests/fixtures/geojson_example.geojson"
|
|
182
|
+
|
|
183
|
+
if cache:
|
|
184
|
+
cache_dir = os.path.join(os.path.expanduser("~"), ".whisp_cache")
|
|
185
|
+
os.makedirs(cache_dir, exist_ok=True)
|
|
186
|
+
filepath = os.path.join(cache_dir, filename)
|
|
187
|
+
|
|
188
|
+
if os.path.exists(filepath):
|
|
189
|
+
return filepath
|
|
190
|
+
else:
|
|
191
|
+
filepath = filename
|
|
192
|
+
|
|
193
|
+
try:
|
|
194
|
+
urllib.request.urlretrieve(url, filepath)
|
|
195
|
+
return filepath
|
|
196
|
+
except Exception as e:
|
|
197
|
+
raise RuntimeError(f"Failed to download example geojson: {e}")
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def generate_random_polygon(
|
|
201
|
+
min_lon, min_lat, max_lon, max_lat, min_area_ha=1, max_area_ha=10, vertex_count=20
|
|
202
|
+
):
|
|
203
|
+
"""
|
|
204
|
+
Generate a random polygon with exact vertex count control.
|
|
205
|
+
|
|
206
|
+
Parameters
|
|
207
|
+
----------
|
|
208
|
+
min_lon : float
|
|
209
|
+
Minimum longitude
|
|
210
|
+
min_lat : float
|
|
211
|
+
Minimum latitude
|
|
212
|
+
max_lon : float
|
|
213
|
+
Maximum longitude
|
|
214
|
+
max_lat : float
|
|
215
|
+
Maximum latitude
|
|
216
|
+
min_area_ha : float
|
|
217
|
+
Minimum area in hectares
|
|
218
|
+
max_area_ha : float
|
|
219
|
+
Maximum area in hectares
|
|
220
|
+
vertex_count : int
|
|
221
|
+
Exact number of vertices for the polygon
|
|
222
|
+
|
|
223
|
+
Returns
|
|
224
|
+
-------
|
|
225
|
+
tuple
|
|
226
|
+
(Polygon, actual_area_ha)
|
|
227
|
+
"""
|
|
228
|
+
target_area_ha = random.uniform(min_area_ha, max_area_ha)
|
|
229
|
+
center_lon = random.uniform(min_lon, max_lon)
|
|
230
|
+
center_lat = random.uniform(min_lat, max_lat)
|
|
231
|
+
|
|
232
|
+
# Estimate radius for target area
|
|
233
|
+
target_area_m2 = target_area_ha * 10000 # hectares to square meters
|
|
234
|
+
radius_meters = math.sqrt(target_area_m2 / math.pi)
|
|
235
|
+
radius_degrees = radius_meters / (111320 * math.cos(math.radians(center_lat)))
|
|
236
|
+
|
|
237
|
+
# Create center point
|
|
238
|
+
center_point = Point(center_lon, center_lat)
|
|
239
|
+
|
|
240
|
+
# Use buffer with resolution to control vertices for smaller vertex counts
|
|
241
|
+
if vertex_count <= 50:
|
|
242
|
+
poly = center_point.buffer(radius_degrees, resolution=vertex_count // 4)
|
|
243
|
+
|
|
244
|
+
# Manual vertex creation for higher vertex counts
|
|
245
|
+
if vertex_count > 50:
|
|
246
|
+
angles = np.linspace(0, 2 * math.pi, vertex_count, endpoint=False)
|
|
247
|
+
|
|
248
|
+
base_radius = radius_degrees
|
|
249
|
+
|
|
250
|
+
# Smooth sine wave variations for natural look
|
|
251
|
+
freq1 = random.uniform(2, 5)
|
|
252
|
+
amp1 = random.uniform(0.08, 0.15)
|
|
253
|
+
freq2 = random.uniform(8, 15)
|
|
254
|
+
amp2 = random.uniform(0.03, 0.08)
|
|
255
|
+
|
|
256
|
+
radius_variation = amp1 * np.sin(
|
|
257
|
+
freq1 * angles + random.uniform(0, 2 * math.pi)
|
|
258
|
+
) + amp2 * np.sin(freq2 * angles + random.uniform(0, 2 * math.pi))
|
|
259
|
+
|
|
260
|
+
radii = base_radius * (1.0 + radius_variation)
|
|
261
|
+
radii = np.maximum(radii, base_radius * 0.6)
|
|
262
|
+
|
|
263
|
+
xs = center_lon + radii * np.cos(angles)
|
|
264
|
+
ys = center_lat + radii * np.sin(angles)
|
|
265
|
+
|
|
266
|
+
xs = np.clip(xs, min_lon, max_lon)
|
|
267
|
+
ys = np.clip(ys, min_lat, max_lat)
|
|
268
|
+
|
|
269
|
+
vertices = list(zip(xs, ys))
|
|
270
|
+
vertices.append(vertices[0])
|
|
271
|
+
|
|
272
|
+
poly = Polygon(vertices)
|
|
273
|
+
|
|
274
|
+
if not poly.is_valid:
|
|
275
|
+
poly = make_valid(poly)
|
|
276
|
+
if hasattr(poly, "geoms"):
|
|
277
|
+
poly = max(poly.geoms, key=lambda p: p.area)
|
|
278
|
+
|
|
279
|
+
else:
|
|
280
|
+
# Resample to get exact vertex count for buffered circles
|
|
281
|
+
coords = list(poly.exterior.coords)
|
|
282
|
+
|
|
283
|
+
if len(coords) - 1 != vertex_count:
|
|
284
|
+
angles = np.linspace(0, 2 * math.pi, vertex_count, endpoint=False)
|
|
285
|
+
|
|
286
|
+
new_coords = []
|
|
287
|
+
for angle in angles:
|
|
288
|
+
x = center_lon + radius_degrees * math.cos(angle)
|
|
289
|
+
y = center_lat + radius_degrees * math.sin(angle)
|
|
290
|
+
|
|
291
|
+
dx = random.uniform(-radius_degrees * 0.08, radius_degrees * 0.08)
|
|
292
|
+
dy = random.uniform(-radius_degrees * 0.08, radius_degrees * 0.08)
|
|
293
|
+
|
|
294
|
+
new_x = np.clip(x + dx, min_lon, max_lon)
|
|
295
|
+
new_y = np.clip(y + dy, min_lat, max_lat)
|
|
296
|
+
new_coords.append((new_x, new_y))
|
|
297
|
+
|
|
298
|
+
new_coords.append(new_coords[0])
|
|
299
|
+
poly = Polygon(new_coords)
|
|
300
|
+
|
|
301
|
+
# Calculate actual area
|
|
302
|
+
area_sq_degrees = poly.area
|
|
303
|
+
area_sq_meters = (
|
|
304
|
+
area_sq_degrees * (111320 * math.cos(math.radians(center_lat))) ** 2
|
|
305
|
+
)
|
|
306
|
+
actual_area_ha = area_sq_meters / 10000
|
|
307
|
+
|
|
308
|
+
return poly, actual_area_ha
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
def generate_test_polygons(
|
|
312
|
+
bounds,
|
|
313
|
+
num_polygons=25,
|
|
314
|
+
min_area_ha=1,
|
|
315
|
+
max_area_ha=10,
|
|
316
|
+
min_number_vert=10,
|
|
317
|
+
max_number_vert=20,
|
|
318
|
+
):
|
|
319
|
+
"""
|
|
320
|
+
Generate synthetic test polygons with exact vertex count control.
|
|
321
|
+
|
|
322
|
+
This utility is useful for testing WHISP processing with controlled test data,
|
|
323
|
+
especially when you need polygons with specific characteristics (area, complexity).
|
|
324
|
+
|
|
325
|
+
Parameters
|
|
326
|
+
----------
|
|
327
|
+
bounds : list or ee.Geometry
|
|
328
|
+
Either a list of [min_lon, min_lat, max_lon, max_lat] or an Earth Engine Geometry.
|
|
329
|
+
Examples:
|
|
330
|
+
- Simple bounds: [-81.0, -19.3, -31.5, 9.6]
|
|
331
|
+
- EE Geometry: ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017').filter(
|
|
332
|
+
ee.Filter.eq('country_na', 'Brazil')).first().geometry()
|
|
333
|
+
num_polygons : int, optional
|
|
334
|
+
Number of polygons to generate (default: 25)
|
|
335
|
+
min_area_ha : float, optional
|
|
336
|
+
Minimum area in hectares (default: 1)
|
|
337
|
+
max_area_ha : float, optional
|
|
338
|
+
Maximum area in hectares (default: 10)
|
|
339
|
+
min_number_vert : int, optional
|
|
340
|
+
Minimum number of vertices per polygon (default: 10)
|
|
341
|
+
max_number_vert : int, optional
|
|
342
|
+
Maximum number of vertices per polygon (default: 20)
|
|
343
|
+
|
|
344
|
+
Returns
|
|
345
|
+
-------
|
|
346
|
+
dict
|
|
347
|
+
GeoJSON FeatureCollection with generated polygons. Each feature includes:
|
|
348
|
+
- internal_id: Sequential ID starting from 1
|
|
349
|
+
- requested_vertices: Number of vertices requested
|
|
350
|
+
- actual_vertices: Actual number of vertices created
|
|
351
|
+
- requested_area_ha: Target area in hectares
|
|
352
|
+
- actual_area_ha: Actual area in hectares
|
|
353
|
+
|
|
354
|
+
Examples
|
|
355
|
+
--------
|
|
356
|
+
>>> import openforis_whisp as whisp
|
|
357
|
+
>>> import ee
|
|
358
|
+
>>>
|
|
359
|
+
>>> # Using simple bounds (list)
|
|
360
|
+
>>> bounds_list = [-81.0, -19.3, -31.5, 9.6]
|
|
361
|
+
>>> geojson = whisp.generate_test_polygons(bounds_list, num_polygons=100)
|
|
362
|
+
>>>
|
|
363
|
+
>>> # Using Earth Engine Geometry
|
|
364
|
+
>>> brazil = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017').filter(
|
|
365
|
+
... ee.Filter.eq('country_na', 'Brazil')
|
|
366
|
+
... ).first().geometry()
|
|
367
|
+
>>> geojson = whisp.generate_test_polygons(brazil, num_polygons=100,
|
|
368
|
+
... min_area_ha=100, max_area_ha=1000)
|
|
369
|
+
>>>
|
|
370
|
+
>>> # Save to file
|
|
371
|
+
>>> import json
|
|
372
|
+
>>> with open('test_polygons.geojson', 'w') as f:
|
|
373
|
+
... json.dump(geojson, f)
|
|
374
|
+
"""
|
|
375
|
+
|
|
376
|
+
# Handle Earth Engine Geometry or simple bounds
|
|
377
|
+
if hasattr(bounds, "bounds"): # It's an ee.Geometry
|
|
378
|
+
logger.logger.info("Extracting bounds from Earth Engine Geometry...")
|
|
379
|
+
try:
|
|
380
|
+
bounds_geom = (
|
|
381
|
+
bounds.bounds()
|
|
382
|
+
if not hasattr(bounds, "coordinates")
|
|
383
|
+
or bounds.type().getInfo() != "Rectangle"
|
|
384
|
+
else bounds
|
|
385
|
+
)
|
|
386
|
+
bounds_coords = bounds_geom.coordinates().getInfo()[0]
|
|
387
|
+
min_lon = min(coord[0] for coord in bounds_coords)
|
|
388
|
+
max_lon = max(coord[0] for coord in bounds_coords)
|
|
389
|
+
min_lat = min(coord[1] for coord in bounds_coords)
|
|
390
|
+
max_lat = max(coord[1] for coord in bounds_coords)
|
|
391
|
+
logger.logger.info(
|
|
392
|
+
f"Bounds: [{min_lon:.2f}, {min_lat:.2f}, {max_lon:.2f}, {max_lat:.2f}]"
|
|
393
|
+
)
|
|
394
|
+
except Exception as e:
|
|
395
|
+
raise ValueError(
|
|
396
|
+
f"Failed to extract bounds from Earth Engine Geometry: {e}"
|
|
397
|
+
)
|
|
398
|
+
elif isinstance(bounds, (list, tuple)) and len(bounds) == 4:
|
|
399
|
+
min_lon, min_lat, max_lon, max_lat = bounds
|
|
400
|
+
else:
|
|
401
|
+
raise ValueError(
|
|
402
|
+
"bounds must be either:\n"
|
|
403
|
+
" - A list of [min_lon, min_lat, max_lon, max_lat]\n"
|
|
404
|
+
" - An Earth Engine Geometry (ee.Geometry, ee.Feature.geometry(), etc.)"
|
|
405
|
+
)
|
|
406
|
+
|
|
407
|
+
# Validate parameters
|
|
408
|
+
if min_number_vert > max_number_vert:
|
|
409
|
+
raise ValueError(
|
|
410
|
+
f"min_number_vert ({min_number_vert}) cannot be greater than max_number_vert ({max_number_vert})"
|
|
411
|
+
)
|
|
412
|
+
if min_area_ha > max_area_ha:
|
|
413
|
+
raise ValueError(
|
|
414
|
+
f"min_area_ha ({min_area_ha}) cannot be greater than max_area_ha ({max_area_ha})"
|
|
415
|
+
)
|
|
416
|
+
if num_polygons < 1:
|
|
417
|
+
raise ValueError(f"num_polygons must be at least 1 (got {num_polygons})")
|
|
418
|
+
|
|
419
|
+
logger.logger.info(
|
|
420
|
+
f"Generating {num_polygons} test polygons with {min_number_vert}-{max_number_vert} vertices..."
|
|
421
|
+
)
|
|
422
|
+
|
|
423
|
+
features = []
|
|
424
|
+
|
|
425
|
+
# Pre-generate all random values
|
|
426
|
+
vertex_counts = np.random.randint(
|
|
427
|
+
min_number_vert, max_number_vert + 1, num_polygons
|
|
428
|
+
)
|
|
429
|
+
target_areas = np.random.uniform(min_area_ha, max_area_ha, num_polygons)
|
|
430
|
+
|
|
431
|
+
for i in range(num_polygons):
|
|
432
|
+
if i > 0 and i % 250 == 0:
|
|
433
|
+
logger.logger.info(
|
|
434
|
+
f"Generated {i}/{num_polygons} polygons ({i/num_polygons*100:.0f}%)..."
|
|
435
|
+
)
|
|
436
|
+
|
|
437
|
+
requested_vertices = vertex_counts[i]
|
|
438
|
+
|
|
439
|
+
polygon, actual_area = generate_random_polygon(
|
|
440
|
+
min_lon,
|
|
441
|
+
min_lat,
|
|
442
|
+
max_lon,
|
|
443
|
+
max_lat,
|
|
444
|
+
min_area_ha=target_areas[i] * 0.9,
|
|
445
|
+
max_area_ha=target_areas[i] * 1.1,
|
|
446
|
+
vertex_count=requested_vertices,
|
|
447
|
+
)
|
|
448
|
+
|
|
449
|
+
actual_vertex_count = len(list(polygon.exterior.coords)) - 1
|
|
450
|
+
|
|
451
|
+
properties = {
|
|
452
|
+
"internal_id": i + 1,
|
|
453
|
+
"requested_vertices": int(requested_vertices),
|
|
454
|
+
"actual_vertices": int(actual_vertex_count),
|
|
455
|
+
"requested_area_ha": round(target_areas[i], 2),
|
|
456
|
+
"actual_area_ha": round(actual_area, 2),
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
feature = {
|
|
460
|
+
"type": "Feature",
|
|
461
|
+
"properties": properties,
|
|
462
|
+
"geometry": mapping(polygon),
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
features.append(feature)
|
|
466
|
+
|
|
467
|
+
logger.logger.info(f"Generated {num_polygons} polygons!")
|
|
468
|
+
|
|
469
|
+
# Print summary statistics
|
|
470
|
+
actual_vertex_counts = [f["properties"]["actual_vertices"] for f in features]
|
|
471
|
+
requested_vertex_counts = [f["properties"]["requested_vertices"] for f in features]
|
|
472
|
+
|
|
473
|
+
logger.logger.info(
|
|
474
|
+
f"Vertex count - Requested: {min(requested_vertex_counts)}-{max(requested_vertex_counts)}, "
|
|
475
|
+
f"Actual: {min(actual_vertex_counts)}-{max(actual_vertex_counts)}"
|
|
476
|
+
)
|
|
477
|
+
|
|
478
|
+
actual_area_counts = [f["properties"]["actual_area_ha"] for f in features]
|
|
479
|
+
requested_area_counts = [f["properties"]["requested_area_ha"] for f in features]
|
|
480
|
+
|
|
481
|
+
logger.logger.info(
|
|
482
|
+
f"Area (ha) - Requested: {min(requested_area_counts):.1f}-{max(requested_area_counts):.1f}, "
|
|
483
|
+
f"Actual: {min(actual_area_counts):.1f}-{max(actual_area_counts):.1f}"
|
|
484
|
+
)
|
|
485
|
+
|
|
486
|
+
geojson = {"type": "FeatureCollection", "features": features}
|
|
487
|
+
return geojson
|