Skip to content

Commit

Permalink
Merge pull request #6 from PADAS/ERCS-2647
Browse files Browse the repository at this point in the history
Adding error handling for bounding box
  • Loading branch information
jeslefcourt authored Nov 21, 2024
2 parents 8a55263 + b6545da commit d42bac5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
27 changes: 23 additions & 4 deletions app/actions/configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,33 @@ def validate_json(cls, v):
def validate_bounding_box(cls, v):
if(not v):
raise ValueError("Did not receive a bounding box configuration.")
coords = json.loads(v)
if(len(coords) != 4):
v = json.loads(v)
if(len(v) != 4):
raise ValueError("Did not receive four values in bounding box configuration.")
for coord in coords:
for i in range(0,4):
try:
float(coord)
v[i] = float(v[i])
except:
raise ValueError(f"Could not parse bounding box values {v}.")

if(v[0] < -90 or v[0] > 90):
raise ValueError(f"NE Latitude {v[0]} must be between -90 and 90")

if(v[1] < -180 or v[1] > 180):
raise ValueError(f"NE Longitude {v[1]} must be between -180 and 180")

if(v[2] < -90 or v[2] > 90):
raise ValueError(f"SW Latitude {v[2]} must be between -90 and 90")

if(v[3] < -180 or v[3] > 180):
raise ValueError(f"SW Longitude {v[2]} must be between -180 and 180")

if(v[0] <= v[2]):
raise ValueError(f"NE Latitude {v[0]} must be greater than SW Latitude {v[2]}")

if(v[1] <= v[3]):
raise ValueError(f"NE Longitude {v[1]} must be greater than SW Longitude {v[2]}")

return v

class Config:
Expand Down
11 changes: 3 additions & 8 deletions app/actions/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,19 @@ def get_inaturalist_observations(integration: Integration, config: PullEventsCon

nelat = nelng = swlat = swlng = None
if(config.bounding_box):
nelat, nelng, swlat, swlng = json.loads(config.bounding_box)
nelat, nelng, swlat, swlng = config.bounding_box

target_taxa = []
for taxa in config.taxa:
target_taxa.append(str(taxa))
target_taxa = ",".join(target_taxa)

quality_grade = []
for qg in config.quality_grade:
quality_grade.append(str(qg))
quality_grade = ",".join(quality_grade)

fields = ",".join(["observed_on", "created_at", "id", "captive", "obscured", "place_guess", "quality_grade", "species_guess", "updated_at",
"uri", "photos", "user", "location", "place_ids", "taxon", "photos.large_url", "photos.url", "taxon.id", "taxon.rank", "taxon.name",
"taxon.preferred_common_name", "taxon.wikipedia_url", "taxon.conservation_status", "user.id", "user.name", "user.login",
"annotations.controlled_attribute_id", "annotations.controlled_value_id"])

inat_count_req = get_observations_v2(page = 1, per_page = 0, updated_since = since, project_id = config.projects, quality_grade = quality_grade,
inat_count_req = get_observations_v2(page = 1, per_page = 0, updated_since = since, project_id = config.projects, quality_grade = config.quality_grade,
taxon_id=target_taxa, nelat = float(nelat), nelng = float(nelng), swlat = float(swlat), swlng = float(swlng),
order_by = "updated_at", order="asc")

Expand All @@ -76,7 +71,7 @@ def get_inaturalist_observations(integration: Integration, config: PullEventsCon
logger.debug(f"Loading page {page} of {pages} from iNaturalist")

response = get_observations_v2(page = page, per_page = 200, updated_since = since, project_id = config.projects,
quality_grade = quality_grade, taxon_id = target_taxa,
quality_grade = config.quality_grade, taxon_id = target_taxa,
nelat = nelat, nelng = nelng, swlat = swlat, swlng = swlng,
order_by = 'updated_at', order="asc", fields=fields)

Expand Down

0 comments on commit d42bac5

Please sign in to comment.