openforis-whisp 1.0.0a1__py3-none-any.whl → 2.0.0a1__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.
@@ -31,7 +31,7 @@ except Exception as e:
31
31
 
32
32
  from openforis_whisp.datasets import (
33
33
  combine_datasets,
34
- )
34
+ )
35
35
 
36
36
  from openforis_whisp.stats import (
37
37
  whisp_stats_ee_to_ee,
@@ -49,7 +49,10 @@ from openforis_whisp.stats import (
49
49
  )
50
50
 
51
51
  # temporary parameters to be removed once isio3 to iso2 conversion server side is implemented
52
- from openforis_whisp.parameters.config_runtime import iso3_country_column, iso2_country_column
52
+ from openforis_whisp.parameters.config_runtime import (
53
+ iso3_country_column,
54
+ iso2_country_column,
55
+ )
53
56
 
54
57
  from openforis_whisp.reformat import (
55
58
  validate_dataframe_using_lookups,
@@ -67,6 +70,6 @@ from openforis_whisp.data_conversion import (
67
70
  convert_ee_to_geojson,
68
71
  )
69
72
 
70
- from openforis_whisp.risk import whisp_risk
73
+ from openforis_whisp.risk import whisp_risk, detect_unit_type
71
74
 
72
75
  from openforis_whisp.utils import get_example_data_path
@@ -44,12 +44,16 @@ def convert_ee_to_geojson(ee_object, filename=None, indent=2, **kwargs):
44
44
  raise Exception(e)
45
45
 
46
46
 
47
- def convert_geojson_to_ee(geojson_filepath: Any) -> ee.FeatureCollection:
47
+ def convert_geojson_to_ee(
48
+ geojson_filepath: Any, enforce_wgs84: bool = True
49
+ ) -> ee.FeatureCollection:
48
50
  """
49
51
  Reads a GeoJSON file from the given path and converts it to an Earth Engine FeatureCollection.
52
+ Optionally checks and converts the CRS to WGS 84 (EPSG:4326) if needed.
50
53
 
51
54
  Args:
52
55
  geojson_filepath (Any): The filepath to the GeoJSON file.
56
+ enforce_wgs84 (bool): Whether to enforce WGS 84 projection (EPSG:4326). Defaults to True.
53
57
 
54
58
  Returns:
55
59
  ee.FeatureCollection: Earth Engine FeatureCollection created from the GeoJSON.
@@ -58,12 +62,23 @@ def convert_geojson_to_ee(geojson_filepath: Any) -> ee.FeatureCollection:
58
62
  file_path = os.path.abspath(geojson_filepath)
59
63
  print(f"Reading GeoJSON file from: {file_path}")
60
64
 
61
- with open(file_path, "r") as f:
62
- geojson_data = json.load(f)
65
+ # Use GeoPandas to read the file and handle CRS
66
+ gdf = gpd.read_file(file_path)
67
+
68
+ # Check and convert CRS if needed
69
+ if enforce_wgs84:
70
+ if gdf.crs is None:
71
+ print("Warning: Input GeoJSON has no CRS defined, assuming WGS 84")
72
+ elif gdf.crs != "EPSG:4326":
73
+ print(f"Converting CRS from {gdf.crs} to WGS 84 (EPSG:4326)")
74
+ gdf = gdf.to_crs("EPSG:4326")
75
+
76
+ # Convert to GeoJSON
77
+ geojson_data = json.loads(gdf.to_json())
63
78
  else:
64
79
  raise ValueError("Input must be a file path (str or Path)")
65
80
 
66
- validation_errors = validate_geojson(geojson_filepath)
81
+ validation_errors = validate_geojson(geojson_data)
67
82
  if validation_errors:
68
83
  raise ValueError(f"GeoJSON validation errors: {validation_errors}")
69
84
 
@@ -208,7 +223,7 @@ def validate_geojson(input_data: Any) -> List[str]:
208
223
  """
209
224
  Validates GeoJSON data and filters out certain non-critical errors.
210
225
 
211
- :param input_data: GeoJSON data as a string or a file path
226
+ :param input_data: GeoJSON data as a string, dict, or a file path
212
227
  :return: List of validation errors
213
228
  """
214
229
  errors = []
@@ -217,18 +232,22 @@ def validate_geojson(input_data: Any) -> List[str]:
217
232
  try:
218
233
  with open(input_data, "r") as f:
219
234
  geojson_data = f.read()
235
+ geojson_obj = json.loads(geojson_data)
220
236
  except Exception as e:
221
237
  errors.append(f"Error reading file: {e}")
222
238
  return errors
239
+ elif isinstance(input_data, dict):
240
+ geojson_obj = input_data
223
241
  else:
224
242
  geojson_data = input_data
243
+ try:
244
+ geojson_obj = json.loads(geojson_data)
245
+ except ValueError as e:
246
+ errors.append(f"Invalid GeoJSON: {e}")
247
+ return errors
225
248
 
226
- try:
227
- geojson_obj = json.loads(geojson_data)
228
- if "type" not in geojson_obj:
229
- errors.append("Missing 'type' field in GeoJSON.")
230
- except ValueError as e:
231
- errors.append(f"Invalid GeoJSON: {e}")
249
+ if "type" not in geojson_obj:
250
+ errors.append("Missing 'type' field in GeoJSON.")
232
251
 
233
252
  return errors
234
253
 
@@ -269,7 +288,9 @@ def create_feature_collection(geojson_obj: Any) -> FeatureCollection:
269
288
  return FeatureCollection(features)
270
289
 
271
290
 
272
- def convert_csv_to_geojson(csv_filepath: str, geojson_filepath: str, geo_column: str = "geo"):
291
+ def convert_csv_to_geojson(
292
+ csv_filepath: str, geojson_filepath: str, geo_column: str = "geo"
293
+ ):
273
294
  """
274
295
  Convert a CSV file with a geo column into a GeoJSON file.
275
296
 
@@ -295,7 +316,9 @@ def convert_csv_to_geojson(csv_filepath: str, geojson_filepath: str, geo_column:
295
316
  print(f"An error occurred while converting CSV to GeoJSON: {e}")
296
317
 
297
318
 
298
- def convert_df_to_geojson(df: pd.DataFrame, geojson_filepath: str, geo_column: str = "geo"):
319
+ def convert_df_to_geojson(
320
+ df: pd.DataFrame, geojson_filepath: str, geo_column: str = "geo"
321
+ ):
299
322
  """
300
323
  Convert a DataFrame with a geo column into a GeoJSON file.
301
324