Skip to content

Commit

Permalink
fix: remove convex hull to avoid getting features outside aoi
Browse files Browse the repository at this point in the history
  • Loading branch information
Sujanadh committed Dec 9, 2024
1 parent 32b77e3 commit 17db07e
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions osm_rawdata/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
import requests
from geojson import Feature, FeatureCollection
from geojson import Polygon as GeojsonPolygon
from shapely import to_geojson, wkt
from shapely.geometry import Polygon, shape
from shapely import wkt
from shapely.geometry import MultiPolygon, Polygon, mapping, shape
from shapely.ops import unary_union

# Find the other files for this project
Expand Down Expand Up @@ -759,15 +759,20 @@ def execQuery(
shape(feature.get("geometry"))
for feature in boundary.get("features", [])
]
merged_geom = unary_union(geometries)
merged_geom = (
unary_union(geometries) if len(geometries) > 1 else geometries[0]
)
elif geom_type == "Feature":
merged_geom = shape(boundary.get("geometry"))
else:
merged_geom = shape(boundary)

# Convert the merged geoms to a single Polygon GeoJSON using convex hull
aoi_polygon = json.loads(to_geojson(merged_geom.convex_hull))
aoi_shape = shape(aoi_polygon)
if isinstance(merged_geom, MultiPolygon):
aoi_shape = MultiPolygon(
[Polygon(poly.exterior) for poly in merged_geom.geoms]
)
elif isinstance(merged_geom, Polygon):
aoi_shape = Polygon(merged_geom.exterior)

if self.dbshell:
log.info("Extracting features from Postgres...")
Expand All @@ -784,7 +789,9 @@ def execQuery(
collection = FeatureCollection(alldata)
else:
log.info("Extracting features via remote call...")
json_config = self.createJson(self.qc, aoi_polygon, allgeom, extra_params)
json_config = self.createJson(
self.qc, mapping(merged_geom), allgeom, extra_params
)
collection = self.queryRemote(json_config)
# bind_zip=False, data is not zipped, return URL directly
if not json.loads(json_config).get("bind_zip", True):
Expand Down

0 comments on commit 17db07e

Please sign in to comment.