openforis-whisp 2.0.0b1__py3-none-any.whl → 2.0.0b3__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/utils.py CHANGED
@@ -3,6 +3,8 @@ import ee
3
3
  import math
4
4
  import os
5
5
  import pandas as pd
6
+ import random
7
+ import numpy as np
6
8
 
7
9
  import urllib.request
8
10
  import os
@@ -12,6 +14,9 @@ import importlib.resources as pkg_resources
12
14
  from dotenv import load_dotenv
13
15
  from pathlib import Path
14
16
 
17
+ from shapely.geometry import Polygon, Point, mapping
18
+ from shapely.validation import make_valid
19
+
15
20
  from .logger import StdoutLogger
16
21
 
17
22
 
@@ -160,31 +165,31 @@ class DotEnvNotFoundError(FileNotFoundError):
160
165
  def get_example_geojson(filename="geojson_example.geojson", cache=True):
161
166
  """
162
167
  Download example geojson file for testing whisp functionality.
163
-
168
+
164
169
  Parameters:
165
170
  -----------
166
171
  filename : str
167
172
  Local filename to save the geojson
168
173
  cache : bool
169
174
  If True, cache file in user directory to avoid re-downloading
170
-
175
+
171
176
  Returns:
172
177
  --------
173
178
  str
174
179
  Path to the downloaded geojson file
175
180
  """
176
181
  url = "https://raw.githubusercontent.com/forestdatapartnership/whisp/main/tests/fixtures/geojson_example.geojson"
177
-
182
+
178
183
  if cache:
179
184
  cache_dir = os.path.join(os.path.expanduser("~"), ".whisp_cache")
180
185
  os.makedirs(cache_dir, exist_ok=True)
181
186
  filepath = os.path.join(cache_dir, filename)
182
-
187
+
183
188
  if os.path.exists(filepath):
184
189
  return filepath
185
190
  else:
186
191
  filepath = filename
187
-
192
+
188
193
  try:
189
194
  urllib.request.urlretrieve(url, filepath)
190
195
  return filepath
@@ -192,3 +197,291 @@ def get_example_geojson(filename="geojson_example.geojson", cache=True):
192
197
  raise RuntimeError(f"Failed to download example geojson: {e}")
193
198
 
194
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
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: openforis-whisp
3
- Version: 2.0.0b1
3
+ Version: 2.0.0b3
4
4
  Summary: Whisp (What is in that plot) is an open-source solution which helps to produce relevant forest monitoring information and support compliance with deforestation-related regulations.
5
5
  License: MIT
6
6
  Keywords: whisp,geospatial,data-processing
@@ -0,0 +1,16 @@
1
+ openforis_whisp/__init__.py,sha256=bnEZ4_X-mJInltSKVI0STfvrb09Df-z21buIVFDif5w,2524
2
+ openforis_whisp/data_conversion.py,sha256=Mc6dXbvoHBeRzl3o83pyKeI5_sPC8Yc90Tj4bN6_Bv8,17519
3
+ openforis_whisp/datasets.py,sha256=hb8Y35vTcQQNUH_z2_l8Pu6Sjn_E8BzSow1-qAfs9bQ,50194
4
+ openforis_whisp/logger.py,sha256=n9k0EhAZYZKesnfskv8KyWnkGbjqRqk84ulx9-u_Jsc,2308
5
+ openforis_whisp/parameters/config_runtime.py,sha256=NOo39MAi60XCwEx5pwkS0EHKJBh0XY1q06y4j0HAABg,1421
6
+ openforis_whisp/parameters/lookup_context_and_metadata.csv,sha256=KgK0ik_Gd4t_Nq5cUkGPT4ZFZVO93HWSG82jRrOukt4,1298
7
+ openforis_whisp/parameters/lookup_gee_datasets.csv,sha256=UDvZrQsL5rXJn6CW6P3wofUrPLRmUFZWt6ETbXaxBMs,17454
8
+ openforis_whisp/pd_schemas.py,sha256=W_ocS773LHfc05dJqvWRa-bRdX0wKFoNp0lMxgFx94Y,2681
9
+ openforis_whisp/reformat.py,sha256=rtkKs8z1mJd5JD9rXuMk1tbbbTvQxCCh68tA4hIQAv8,25445
10
+ openforis_whisp/risk.py,sha256=d_Di5XB8BnHdVXG56xdHTcpB4-CIF5vo2ZRMQRG7Pek,34420
11
+ openforis_whisp/stats.py,sha256=1ikeV8UYpL8O5HZJY8lPXrhQwZ9D1IglbOsagZHCYdA,54000
12
+ openforis_whisp/utils.py,sha256=5HHtbK62Swn4-jnlSe1Jc-hVnJhLKMuDW0_ayHY7mIg,17130
13
+ openforis_whisp-2.0.0b3.dist-info/LICENSE,sha256=nqyqICO95iw_iwzP1t_IIAf7ZX3DPbL_M9WyQfh2q1k,1085
14
+ openforis_whisp-2.0.0b3.dist-info/METADATA,sha256=Opn73PWlsOQWTiwZ-HYvLkrPh4jYQELtSIIqDf4MsoQ,16684
15
+ openforis_whisp-2.0.0b3.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
16
+ openforis_whisp-2.0.0b3.dist-info/RECORD,,
@@ -1,15 +0,0 @@
1
- """
2
- !!! BAD PRACTICE, ALWAYS IMPORT YOUR MODULES EXPLICITELY !!!
3
-
4
- Module to gather all parameters.
5
-
6
- If you use a module import all the functions here you only have 1 call to make
7
- """
8
-
9
- # from .config_runtime import *
10
-
11
- # from .config_asr_url_info import *
12
-
13
- # from .config_asr_credentials import *
14
-
15
- # from parameters.config_ceo import *
@@ -1,17 +0,0 @@
1
- openforis_whisp/__init__.py,sha256=W-uMGp87DCuqJWY31fyYkzDXXZp3g1HOXZoHThJqvJU,2462
2
- openforis_whisp/data_conversion.py,sha256=_HSjYozNO1xAOAk-uGmzTVCTOc3W7x3GDlvEUgrnj_Q,16909
3
- openforis_whisp/datasets.py,sha256=gQg-JjcZuCd8-4_J2CN4oNwo-2qwNmKpwS_JV6zf-Jc,51516
4
- openforis_whisp/logger.py,sha256=n9k0EhAZYZKesnfskv8KyWnkGbjqRqk84ulx9-u_Jsc,2308
5
- openforis_whisp/parameters/__init__.py,sha256=KL7iORJVjSpZatYjoyWckcmQJnE89_DBC8R6_0_eR6o,349
6
- openforis_whisp/parameters/config_runtime.py,sha256=NOo39MAi60XCwEx5pwkS0EHKJBh0XY1q06y4j0HAABg,1421
7
- openforis_whisp/parameters/lookup_context_and_metadata.csv,sha256=KgK0ik_Gd4t_Nq5cUkGPT4ZFZVO93HWSG82jRrOukt4,1298
8
- openforis_whisp/parameters/lookup_gee_datasets.csv,sha256=rYAMwbd1kwakgucdXrDicJfu4Nvjk8LWBYN7FuIpdDY,17626
9
- openforis_whisp/pd_schemas.py,sha256=W_ocS773LHfc05dJqvWRa-bRdX0wKFoNp0lMxgFx94Y,2681
10
- openforis_whisp/reformat.py,sha256=rtkKs8z1mJd5JD9rXuMk1tbbbTvQxCCh68tA4hIQAv8,25445
11
- openforis_whisp/risk.py,sha256=_YMF-2X1OZXrNMFdNPuJicnG8ktAhlFToJfthWwiRHE,35111
12
- openforis_whisp/stats.py,sha256=uwyiPXVptpCFSviz-_otXCpHeHnY2IwE2dPwvI7tAAM,45226
13
- openforis_whisp/utils.py,sha256=ywOl-Hd2FzSYrOVIjtqGaNoZqkI34UChkZMbdjgXWZ0,6492
14
- openforis_whisp-2.0.0b1.dist-info/LICENSE,sha256=nqyqICO95iw_iwzP1t_IIAf7ZX3DPbL_M9WyQfh2q1k,1085
15
- openforis_whisp-2.0.0b1.dist-info/METADATA,sha256=NDh_KnflmxTUaA2-w3yTCrZYKFcbgvH8NJu5soe7doc,16684
16
- openforis_whisp-2.0.0b1.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
17
- openforis_whisp-2.0.0b1.dist-info/RECORD,,