Skip to content

Commit

Permalink
wrapped inside of try except block for view_data_extracts
Browse files Browse the repository at this point in the history
  • Loading branch information
nrjadkry committed Nov 7, 2023
1 parent 96273f2 commit ccc3553
Showing 1 changed file with 43 additions and 37 deletions.
80 changes: 43 additions & 37 deletions src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down

0 comments on commit ccc3553

Please sign in to comment.