From ccc3553f17328044c73463527c147732fd40208c Mon Sep 17 00:00:00 2001 From: Niraj Adhikari Date: Tue, 7 Nov 2023 09:41:39 +0545 Subject: [PATCH] wrapped inside of try except block for view_data_extracts --- src/backend/app/projects/project_routes.py | 80 ++++++++++++---------- 1 file changed, 43 insertions(+), 37 deletions(-) diff --git a/src/backend/app/projects/project_routes.py b/src/backend/app/projects/project_routes.py index 774ff78804..a0802739a0 100644 --- a/src/backend/app/projects/project_routes.py +++ b/src/backend/app/projects/project_routes.py @@ -659,43 +659,49 @@ async def get_data_extracts( aoi: UploadFile, category: Optional[str] = Form(...), ): - # read entire file - await aoi.seek(0) - aoi_content = await aoi.read() - boundary = json.loads(aoi_content) - - # Validatiing Coordinate Reference System - check_crs(boundary) - xlsform = f"{xlsforms_path}/{category}.xls" - config_path = f"{data_models_path}/{category}.yaml" - - # Convert each feature into a Shapely geometry - geometries = [shape(feature["geometry"]) for feature in boundary["features"]] - - # Merge the geometries into a single geometry (as a MultiPolygon) - merged_geometry = unary_union(geometries) - - # Convert the merged MultiPolygon to a single Polygon using convex hull - merged_polygon = merged_geometry.convex_hull - - # Convert the merged polygon back to a GeoJSON-like dictionary - boundary = { - "type": "Feature", - "geometry": mapping(merged_polygon), - "properties": {}, - } - - # # OSM Extracts using raw data api - pg = PostgresClient("underpass", config_path) - data_extract = pg.execQuery(boundary) - log.info("Data extracts process completed") - filter = FilterData(xlsform) - - updated_data_extract = {"type": "FeatureCollection", "features": []} - filtered_data_extract = ( - filter.cleanData(data_extract) if data_extract else updated_data_extract - ) - return filtered_data_extract + try: + # read entire file + await aoi.seek(0) + aoi_content = await aoi.read() + boundary = json.loads(aoi_content) + + # Validatiing Coordinate Reference System + check_crs(boundary) + xlsform = f"{xlsforms_path}/{category}.xls" + config_path = f"{data_models_path}/{category}.yaml" + + if boundary["type"] == "FeatureCollection": + # Convert each feature into a Shapely geometry + geometries = [ + shape(feature["geometry"]) for feature in boundary["features"] + ] + updated_geometry = unary_union(geometries) + else: + updated_geometry = shape(boundary["geometry"]) + + # Convert the merged MultiPolygon to a single Polygon using convex hull + merged_polygon = updated_geometry.convex_hull + + # Convert the merged polygon back to a GeoJSON-like dictionary + boundary = { + "type": "Feature", + "geometry": mapping(merged_polygon), + "properties": {}, + } + + # # OSM Extracts using raw data api + pg = PostgresClient("underpass", config_path) + data_extract = pg.execQuery(boundary) + log.info("Data extracts process completed") + filter = FilterData(xlsform) + + updated_data_extract = {"type": "FeatureCollection", "features": []} + filtered_data_extract = ( + filter.cleanData(data_extract) if data_extract else updated_data_extract + ) + return filtered_data_extract + except Exception as e: + raise HTTPException(status_code=400, detail=str(e)) @router.post("/update-form/{project_id}")