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

Construct PolygonArray or MultiPolygonArray from Exterior Coordinates #132

Merged
merged 12 commits into from
Nov 13, 2023

Conversation

philipc2
Copy link
Collaborator

@philipc2 philipc2 commented Nov 5, 2023

Closes #122

Overview

  • Introduces the PolygonArray.from_exterior_coords() and MultiPolygonArray.from_exterior_coords() class methods, which take-in an list/array of exterior coordinates equivalent to the following which are derived from Shapely Polygons/MultiPolygons

if shape.exterior is not None:
exterior = np.asarray(shape.exterior.coords).ravel()
polygon_coords = [exterior]

if isinstance(shape, sg.MultiPolygon):
multipolygon = []
for polygon in shape.geoms:
polygon_coords = Polygon._shapely_to_coordinates(polygon)
multipolygon.append(polygon_coords)
return multipolygon
elif isinstance(shape, sg.Polygon):
return [Polygon._shapely_to_coordinates(shape)]

Usage

In UXarray, we use SpatialPandas paired with Datashader to visualize geoscience data that resides on unstructured grids as Polygons. Our unstructured grid is represented as a set of connectivity and coordinate arrays, which we would like to avoid needing to convert to Shapely, because we can derive the exterior of each Polygon quickly using the connectivity and coordinate information.

Performance Comparison

Initial benchmarks, comparing across n_polygon & n_vertices

All benchmarks are run on an NCAR Casper HPC Node, with 4 CPU Threads and 128gb of Memory

Test 1

(40,000,000 Closed Polygons with 7 total verticies)

Method / # of Polygons 400,000 4,000,000 40,000,000
Construct with Exterior Coord Array 1.02s 9.74s 97.0s
Create Shapely Polygons + Construct with Shapely Polygons 16.2s 159s 1560s
Only Construct with Shapely Polygons 15.2s 149s 1500s

Roughly a 16x speedup compared to using Shapely

@philipc2 philipc2 changed the title DRAFT: Construct PolygonArray using Numpy DRAFT: Construct PolygonArray from an Array of Exterior Coordinates Nov 6, 2023
@philipc2
Copy link
Collaborator Author

philipc2 commented Nov 6, 2023

Depending on any initial feedback from the developers here, I'd be willing to continue implementing this to support Polygons with holes, MultiPolygons, etc.

While this PR focusses on Polygons specifically, I believe other geometries could benefit from this type of construction support.

@philipc2 philipc2 changed the title DRAFT: Construct PolygonArray from an Array of Exterior Coordinates DRAFT: Construct PolygonArray from an Array/List of Exterior Coordinates Nov 6, 2023
@philipc2 philipc2 changed the title DRAFT: Construct PolygonArray from an Array/List of Exterior Coordinates DRAFT: Construct PolygonArray or MultiPolygonArray from Exterior Coordinates Nov 8, 2023
@philipc2 philipc2 marked this pull request as ready for review November 8, 2023 08:03
@philipc2 philipc2 self-assigned this Nov 8, 2023
@philipc2 philipc2 added the enhancement New feature or request label Nov 8, 2023
@philipc2 philipc2 changed the title DRAFT: Construct PolygonArray or MultiPolygonArray from Exterior Coordinates Construct PolygonArray or MultiPolygonArray from Exterior Coordinates Nov 8, 2023
Copy link
Member

@hoxbro hoxbro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic added seems simple enough not to add a lot of tests, but it would still be nice with one for both polygon and multi-polygon.

Maybe also some tests that handle edge cases like wrong input shape/size.

spatialpandas/geometry/multipolygon.py Outdated Show resolved Hide resolved
spatialpandas/geometry/multipolygon.py Show resolved Hide resolved
@philipc2 philipc2 requested a review from hoxbro November 8, 2023 09:01
@hoxbro
Copy link
Member

hoxbro commented Nov 13, 2023

Thank you for the PR! 👍

@hoxbro hoxbro merged commit aeceb76 into holoviz:main Nov 13, 2023
9 checks passed
@philipc2 philipc2 deleted the philipc2/polygon-array-from-numpy branch November 13, 2023 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Faster construction of PolygonArray and MultiPolygonArray
2 participants