Skip to content

Commit

Permalink
PERF: improve MultiPoint(..) constructor from numpy array of coordina…
Browse files Browse the repository at this point in the history
…te values
  • Loading branch information
jorisvandenbossche committed Jan 3, 2024
1 parent 9c852a6 commit 3477f0a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions benchmarks/benchmarks.py
Expand Up @@ -68,6 +68,12 @@ def time_linestring_from_numpy(self):
def time_linearring_from_numpy(self):
shapely.LinearRing(self.coords)

def test_polygon_from_numpy(self):
shapely.Polygon(self.coords)

def test_multipoint_from_numpy(self):
shapely.MultiPoint(self.coords)


class ConstructiveSuite:
"""Benchmarks constructive functions on a set of 10,000 points"""
Expand Down
7 changes: 7 additions & 0 deletions shapely/geometry/multipoint.py
@@ -1,5 +1,6 @@
"""Collections of points and related utilities
"""
import numpy as np

import shapely
from shapely.errors import EmptyPartError
Expand Down Expand Up @@ -48,6 +49,12 @@ def __new__(self, points=None):
elif isinstance(points, MultiPoint):
return points

if isinstance(points, np.ndarray) and np.issubdtype(points.dtype, np.number):
geom = shapely.multipoints(points)
if not isinstance(geom, MultiPoint):
raise ValueError("Invalid values passed to MultiPoint constructor")
return geom

m = len(points)
subs = []
for i in range(m):
Expand Down

0 comments on commit 3477f0a

Please sign in to comment.