Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove convex hull to avoid getting features outside aoi #41

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading