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

Optimize _remove_polygon_holes and _create_gdf for performance #1205

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Read the v2 [migration guide](https://github.com/gboeing/osmnx/issues/1123)
- rename osm_xml module to \_osm_xml to make it private, as all its functions are private (#1113)
- rename private \_downloader module to \_http (#1114)
- remove unnecessary private \_api module (#1114)
- optimize \_remove_polygon_holes function for improved performance with complex geometries (#1200)
- enhance efficiency of \_create_gdf function in feature processing (#1200)

## 1.9.4 (2024-07-24)

Expand Down
19 changes: 13 additions & 6 deletions osmnx/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from shapely import MultiPolygon
from shapely import Point
from shapely import Polygon
from shapely import prepare
from shapely.errors import GEOSException
from shapely.ops import linemerge
from shapely.ops import polygonize
Expand Down Expand Up @@ -409,8 +410,15 @@ def _create_gdf(

# convert the elements into a GeoDataFrame of features
idx = ["element", "id"]
features = _process_features(elements, set(tags.keys()))
gdf = gpd.GeoDataFrame(features, geometry="geometry", crs=settings.default_crs).set_index(idx)
gdf = (
gpd.GeoDataFrame(
data=_process_features(elements, set(tags.keys())),
geometry="geometry",
crs=settings.default_crs,
)
.set_index(idx)
.sort_index()
)
return _filter_features(gdf, polygon, tags)


Expand Down Expand Up @@ -636,10 +644,9 @@ def _remove_polygon_holes(
# otherwise, remove from each outer poly each inner poly it contains
polygons_with_holes = []
for outer in outer_polygons:
for inner in inner_polygons:
if outer.contains(inner):
outer = outer.difference(inner) # noqa: PLW2901
polygons_with_holes.append(outer)
prepare(outer)
holes = [inner for inner in inner_polygons if outer.contains(inner)]
polygons_with_holes.append(outer.difference(unary_union(holes)))
geometry = unary_union(polygons_with_holes)

# ensure returned geometry is a Polygon or MultiPolygon
Expand Down