UjjwalKGupta commited on
Commit
3898abc
·
verified ·
1 Parent(s): b2bf18c
Files changed (1) hide show
  1. app.py +6 -20
app.py CHANGED
@@ -381,29 +381,15 @@ def calculate_geometry_metrics(file_url: str):
381
  found, and returns the results in JSON format.
382
  """
383
  try:
384
- # 1. Fetch and read the spatial file from the URL
385
- input_gdf = get_gdf_from_url(file_url)
386
-
387
- # 2. Preprocess the GeoDataFrame
388
- processed_gdf = preprocess_gdf(input_gdf)
389
-
390
- # 3. Isolate the first valid polygon
391
- polygon_gdf = processed_gdf[processed_gdf.geometry.type == 'Polygon']
392
- if polygon_gdf.empty:
393
- raise ValueError("No valid polygon found in the provided file.")
394
-
395
- # Take just the first polygon for this example
396
- first_polygon_gdf = polygon_gdf.iloc[[0]]
397
-
398
- # 4. Convert to a suitable Coordinate Reference System for measurement
399
- metric_gdf = to_best_crs(first_polygon_gdf)
400
 
401
  # 5. Calculate area and perimeter
402
- area_sq_meters = metric_gdf.area.item()
403
- perimeter_meters = metric_gdf.length.item()
404
 
405
- area_hectares = area_sq_meters / 10000
406
-
407
  # 6. Return the results
408
  return {
409
  "area": round(area_hectares, 4),
 
381
  found, and returns the results in JSON format.
382
  """
383
  try:
384
+ input_gdf = get_gdf_from_file(file_obj) if file_obj is not None else get_gdf_from_url(url_str)
385
+ input_gdf = preprocess_gdf(input_gdf)
386
+ geometry_gdf = next((input_gdf.iloc[[i]] for i in range(len(input_gdf)) if is_valid_polygon(input_gdf.iloc[[i]])), None)
387
+ geometry_gdf = to_best_crs(geometry_gdf)
 
 
 
 
 
 
 
 
 
 
 
 
388
 
389
  # 5. Calculate area and perimeter
390
+ area_sq_meters = geometry_gdf.area.item()/ 10000
391
+ perimeter_meters = geometry_gdf.length.item()
392
 
 
 
393
  # 6. Return the results
394
  return {
395
  "area": round(area_hectares, 4),