Geometry.area: Unable to perform this geometry operation. Please specify a non-zero error margin. #1920
Muhammad-Waqas5012
started this conversation in
General
Replies: 1 comment
-
The error message already shows you what needs to be changed. Try nReserve_geojson.geometry().area(1) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I want to Calculate the area of each GeoJSON file's feature in Acres approximately, and Also Calculate the area of six classes of NDVI reclassified in acres approximately. Then divide the area of each class into the total area of the GeoJSON file's feature. Then as a result produce PNGS, these PNGS files must not contain reclassified NDVI. I want this information in PNG, "GeoJSON file" "Feature Number" has a total area, "Calculated area in acres approximately", and Class 1 which means no vegetation has "Calculated area of Class 1 in acres approximately", and Class 2 which means very low vegetation has "Calculated area of Class 2 in acres approximately", and Class 3 which means sparse vegetation has "Calculated area of Class 3 in acres approximately", and Class 4 which means mid-seasoned vegetation has "Calculated area of Class 4 in acres approximately", and Class 5 which means mid dense vegetation has "Calculated area of Class 5 in acres approximately", and Class 6 which means dense and healthy vegetation has "Calculated area of Class 6 in acres approximately". But I am facing this problem.
`
Import Libraries
import geopandas as gpd
import ee
import geemap
import os
import glob
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from PIL import Image
Authenticate Earth Engine (Note: if already not) or Authentication is expired, Remove "#" to run the next line.
ee.Authenticate()
Initialize Earth Engine
ee.Initialize()
Specify the folder path containing GeoJSON files
folder_path = 'D:/Test'
Get a list of all GeoJSON files in the folder
geojson_files = glob.glob(folder_path + '/*.geojson')
Define a function to calculate NDVI, and clip to AOI based on feature ID
def calculate_and_clip_ndvi(image, feature_id):
ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI')
return ndvi.clip(nReserve_geojson.filter(ee.Filter.eq('Id', feature_id)))
Define a function to calculate area in acres from square meters
def square_meters_to_acres(area):
return ee.Number(area).divide(4046.86).getInfo()
Define a function to reclassify NDVI values into six classes
def reclassify_ndvi(ndvi):
return ndvi.where(ndvi.lt(0), -1)
.where(ndvi.gte(0).And(ndvi.lt(0.2)), 1)
.where(ndvi.gte(0.2).And(ndvi.lt(0.4)), 2)
.where(ndvi.gte(0.4).And(ndvi.lt(0.6)), 3)
.where(ndvi.gte(0.6).And(ndvi.lt(0.8)), 4)
.where(ndvi.gte(0.8), 5)
Define a function to create PNG with required information
def create_png(feature_id, total_area_acres, class_areas_acres):
plt.figure(figsize=(10, 6))
plt.bar(class_labels, class_areas_acres)
plt.xlabel('NDVI Classes')
plt.ylabel('Area (acres)')
plt.title(f'NDVI Classification for Feature {feature_id}')
plt.savefig(f'Feature_{feature_id}_NDVI_Classification.png')
plt.close()
Iterate over each GeoJSON file
for geojson_file in geojson_files:
# Load the GeoJSON file using geopandas
nReserve = gpd.read_file(geojson_file)
`
HttpError Traceback (most recent call last)
~\AppData\Roaming\Python\Python39\site-packages\ee\data.py in _execute_cloud_call(call, num_retries)
353 try:
--> 354 return call.execute(num_retries=num_retries)
355 except googleapiclient.errors.HttpError as e:
~\AppData\Roaming\Python\Python39\site-packages\googleapiclient_helpers.py in positional_wrapper(*args, **kwargs)
129 logger.warning(message)
--> 130 return wrapped(*args, **kwargs)
131
~\AppData\Roaming\Python\Python39\site-packages\googleapiclient\http.py in execute(self, http, num_retries)
937 if resp.status >= 300:
--> 938 raise HttpError(resp, content, uri=self.uri)
939 return self.postproc(resp, content)
HttpError: <HttpError 400 when requesting https://earthengine.googleapis.com/v1/projects/earthengine-legacy/value:compute?prettyPrint=false&alt=json returned "Geometry.area: Unable to perform this geometry operation. Please specify a non-zero error margin.". Details: "Geometry.area: Unable to perform this geometry operation. Please specify a non-zero error margin.">
During handling of the above exception, another exception occurred:
EEException Traceback (most recent call last)
in
64
65 # Calculate area of feature in acres
---> 66 feature_area = square_meters_to_acres(nReserve_geojson.geometry().area())
67
68 # Calculate NDVI for the last 5 days
in square_meters_to_acres(area)
29 # Define a function to calculate area in acres from square meters
30 def square_meters_to_acres(area):
---> 31 return ee.Number(area).divide(4046.86).multiply(1).getInfo()
32
33 # Define a function to reclassify NDVI values into six classes
~\AppData\Roaming\Python\Python39\site-packages\ee\computedobject.py in getInfo(self)
103 The object can evaluate to anything.
104 """
--> 105 return data.computeValue(self)
106
107 def encode(self, encoder: Optional[Callable[..., Any]]) -> Dict[str, Any]:
~\AppData\Roaming\Python\Python39\site-packages\ee\data.py in computeValue(obj)
1038 _maybe_populate_workload_tag(body)
1039
-> 1040 return _execute_cloud_call(
1041 _get_cloud_projects()
1042 .value()
~\AppData\Roaming\Python\Python39\site-packages\ee\data.py in _execute_cloud_call(call, num_retries)
354 return call.execute(num_retries=num_retries)
355 except googleapiclient.errors.HttpError as e:
--> 356 raise _translate_cloud_exception(e) # pylint: disable=raise-missing-from
357
358
EEException: Geometry.area: Unable to perform this geometry operation. Please specify a non-zero error margin.
Beta Was this translation helpful? Give feedback.
All reactions