From 96273f2de5b7e74393c05e4db44bbcbac9af68f2 Mon Sep 17 00:00:00 2001 From: Niraj Adhikari Date: Tue, 7 Nov 2023 09:10:46 +0545 Subject: [PATCH] used unary_union and convex_hull from shapely to process multipolygon geojson --- src/backend/app/projects/project_routes.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/backend/app/projects/project_routes.py b/src/backend/app/projects/project_routes.py index a216466e42..774ff78804 100644 --- a/src/backend/app/projects/project_routes.py +++ b/src/backend/app/projects/project_routes.py @@ -39,6 +39,8 @@ from osm_fieldwork.make_data_extract import getChoices from osm_fieldwork.xlsforms import xlsforms_path from osm_rawdata.postgres import PostgresClient +from shapely.geometry import mapping, shape +from shapely.ops import unary_union from sqlalchemy.orm import Session from ..central import central_crud @@ -665,12 +667,28 @@ async def get_data_extracts( # 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": []}