From 012f7ce434973350c30b38328c2a8101e0fc3e0c Mon Sep 17 00:00:00 2001 From: kshtiijrajsharma Date: Fri, 29 Mar 2024 01:47:05 +0545 Subject: [PATCH 1/2] hotfix : add area to geom validation --- src/app.py | 2 +- src/validation/models.py | 77 +++++++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 33 deletions(-) diff --git a/src/app.py b/src/app.py index 9fcb21f7..7bf2fa4e 100644 --- a/src/app.py +++ b/src/app.py @@ -1037,7 +1037,7 @@ def get_osm_analytics_meta_stats(self): try: query = generate_polygon_stats_graphql_query(self.INPUT_GEOM) payload = {"query": query} - response = requests.post(self.API_URL, json=payload, timeout=60) + response = requests.post(self.API_URL, json=payload, timeout=30) response.raise_for_status() # Raise an HTTPError for bad responses return response.json() except Exception as e: diff --git a/src/validation/models.py b/src/validation/models.py index 0e6b4b39..56f59a6c 100644 --- a/src/validation/models.py +++ b/src/validation/models.py @@ -300,22 +300,22 @@ class StatsRequestParams(BaseModel, GeometryValidatorMixin): max_length=3, example="NPL", ) - geometry: Optional[ - Union[Polygon, MultiPolygon, Feature, FeatureCollection] - ] = Field( - default=None, - example={ - "type": "Polygon", - "coordinates": [ - [ - [83.96919250488281, 28.194446860487773], - [83.99751663208006, 28.194446860487773], - [83.99751663208006, 28.214869548073377], - [83.96919250488281, 28.214869548073377], - [83.96919250488281, 28.194446860487773], - ] - ], - }, + geometry: Optional[Union[Polygon, MultiPolygon, Feature, FeatureCollection]] = ( + Field( + default=None, + example={ + "type": "Polygon", + "coordinates": [ + [ + [83.96919250488281, 28.194446860487773], + [83.99751663208006, 28.194446860487773], + [83.99751663208006, 28.214869548073377], + [83.96919250488281, 28.214869548073377], + [83.96919250488281, 28.194446860487773], + ] + ], + }, + ) ) @validator("geometry", pre=True, always=True) @@ -327,6 +327,19 @@ def set_geometry_or_iso3(cls, value, values): raise ValueError("Either geometry or iso3 should be supplied.") return value + @validator("geometry", pre=True, always=True) + def validate_geometry_area(cls, value): + """Validate that the geometry area does not exceed threshold.""" + if value is not None: + geometry_json = json.loads(value.model_dump_json()) + area_m2 = cls.calculate_geometry_area(geometry_json) + max_area = 10000 + if area_m2 * 1e-6 > max_area: + raise ValueError( + f"The area of the geometry should not exceed {max_area} square km." + ) + return value + ### HDX BLock @@ -612,22 +625,22 @@ class DynamicCategoriesModel(BaseModel, GeometryValidatorMixin): } ], ) - geometry: Optional[ - Union[Polygon, MultiPolygon, Feature, FeatureCollection] - ] = Field( - default=None, - example={ - "type": "Polygon", - "coordinates": [ - [ - [83.96919250488281, 28.194446860487773], - [83.99751663208006, 28.194446860487773], - [83.99751663208006, 28.214869548073377], - [83.96919250488281, 28.214869548073377], - [83.96919250488281, 28.194446860487773], - ] - ], - }, + geometry: Optional[Union[Polygon, MultiPolygon, Feature, FeatureCollection]] = ( + Field( + default=None, + example={ + "type": "Polygon", + "coordinates": [ + [ + [83.96919250488281, 28.194446860487773], + [83.99751663208006, 28.194446860487773], + [83.99751663208006, 28.214869548073377], + [83.96919250488281, 28.214869548073377], + [83.96919250488281, 28.194446860487773], + ] + ], + }, + ) ) @validator("geometry", pre=True, always=True) From 85fb6340ee8ef6cbb3ee861deba3d3d8c94972c0 Mon Sep 17 00:00:00 2001 From: kshitijrajsharma Date: Fri, 29 Mar 2024 02:43:58 +0545 Subject: [PATCH 2/2] fix(stats): adds area threshold in the stats api along with timeout reduced to 30 second --- API/raw_data.py | 2 +- src/validation/models.py | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/API/raw_data.py b/API/raw_data.py index f247f9f1..441def4c 100644 --- a/API/raw_data.py +++ b/API/raw_data.py @@ -482,7 +482,7 @@ def get_osm_current_snapshot_as_plain_geojson( """ area_m2 = area(json.loads(params.geometry.model_dump_json())) area_km2 = area_m2 * 1e-6 - if area_km2 > 10: + if area_km2 > 5: raise HTTPException( status_code=400, detail=[ diff --git a/src/validation/models.py b/src/validation/models.py index 56f59a6c..2ff45ab3 100644 --- a/src/validation/models.py +++ b/src/validation/models.py @@ -23,6 +23,7 @@ from typing import Dict, List, Optional, Union # Third party imports +from area import area from geojson_pydantic import Feature, FeatureCollection, MultiPolygon, Polygon from geojson_pydantic.types import BBox from pydantic import BaseModel as PydanticModel @@ -331,12 +332,12 @@ def set_geometry_or_iso3(cls, value, values): def validate_geometry_area(cls, value): """Validate that the geometry area does not exceed threshold.""" if value is not None: - geometry_json = json.loads(value.model_dump_json()) - area_m2 = cls.calculate_geometry_area(geometry_json) + geometry_json = value + area_m2 = area(geometry_json) max_area = 10000 if area_m2 * 1e-6 > max_area: raise ValueError( - f"The area of the geometry should not exceed {max_area} square km." + f"The area {area_m2 * 1e-6} sqkm of the geometry should not exceed {max_area} square km." ) return value