Skip to content

Commit

Permalink
used unary_union and convex_hull from shapely to process multipolygon…
Browse files Browse the repository at this point in the history
… geojson
  • Loading branch information
nrjadkry committed Nov 7, 2023
1 parent e965977 commit 96273f2
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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": []}
Expand Down

0 comments on commit 96273f2

Please sign in to comment.