Skip to content

Commit

Permalink
refactor: merged multi polygon to single polygon (#1426)
Browse files Browse the repository at this point in the history
Co-authored-by: sujanadh <sujanadh07@gmail.com>
  • Loading branch information
Sujanadh and sujanadh authored Apr 5, 2024
1 parent c779987 commit 853304f
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@
from osm_fieldwork.xlsforms.entities import registration_form
from osm_rawdata.postgres import PostgresClient
from shapely import wkt
from shapely.geometry import (
Polygon,
shape,
)
from shapely.geometry import MultiPolygon, Polygon, mapping, shape
from shapely.ops import unary_union
from sqlalchemy import and_, column, func, inspect, select, table, text
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.orm import Session
Expand Down Expand Up @@ -393,12 +391,19 @@ def remove_z_dimension(coord):

# Merge multiple geometries into single polygon
if multi_polygons:
geometry = multi_polygons[0]
for geom in multi_polygons[1:]:
geometry = geometry.union(geom)
for feature in features:
feature["geometry"] = geometry
merged_polygon = unary_union(multi_polygons)
if isinstance(merged_polygon, MultiPolygon):
merged_polygon = merged_polygon.convex_hull
merged_geojson = mapping(merged_polygon)
features = [
{
"type": "Feature",
"properties": {},
"geometry": merged_geojson,
}
]
boundary["features"] = features

return await run_in_threadpool(
lambda: split_by_square(
boundary,
Expand Down

0 comments on commit 853304f

Please sign in to comment.