holoviz / spatialpandas

Pandas extension arrays for spatial/geometric operations
BSD 2-Clause "Simplified" License
308 stars 25 forks source link

Construct `PolygonArray` or `MultiPolygonArray` from Exterior Coordinates #132

Closed philipc2 closed 1 year ago

philipc2 commented 1 year ago

Closes #122

Overview

https://github.com/holoviz/spatialpandas/blob/dd941f2f5f6994c6f09996e9b8af315e16d00051/spatialpandas/geometry/polygon.py#L40-L42

https://github.com/holoviz/spatialpandas/blob/dd941f2f5f6994c6f09996e9b8af315e16d00051/spatialpandas/geometry/multipolygon.py#L40-L48

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 commented 1 year ago

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.

hoxbro commented 1 year ago

Thank you for the PR! :+1: