Skip to content

Commit

Permalink
Allow passing len-1 array for x and y coordinates in Point(..) (compa…
Browse files Browse the repository at this point in the history
…t with 1.8) (#1590)
  • Loading branch information
jorisvandenbossche committed Oct 29, 2022
1 parent 386af23 commit bef2c9c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
8 changes: 3 additions & 5 deletions shapely/geometry/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,12 @@ def __new__(self, *args):
if not hasattr(coords, "__getitem__"): # generators
coords = list(coords)

if isinstance(coords[0], tuple):
coords = coords[0]

geom = shapely.points(coords)
coords = np.asarray(coords).squeeze()
else:
# 2 or 3 args
geom = shapely.points(np.array(args))
coords = np.array(args).squeeze()

geom = shapely.points(coords)
if not isinstance(geom, Point):
raise ValueError("Invalid values passed to Point constructor")
return geom
Expand Down
13 changes: 13 additions & 0 deletions shapely/tests/geometry/test_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def test_from_sequence():
# From coordinate sequence
p = Point([(3.0, 4.0)])
assert p.coords[:] == [(3.0, 4.0)]
p = Point([[3.0, 4.0]])
assert p.coords[:] == [(3.0, 4.0)]

# 3D
p = Point((3.0, 4.0, 5.0))
Expand All @@ -53,6 +55,17 @@ def test_from_numpy():
assert p.coords[:] == [(1.0, 2.0, 3.0)]


def test_from_numpy_xy():
# Construct from separate x, y numpy arrays - if those are length 1,
# this is allowed for compat with shapely 1.8
# (https://github.com/shapely/shapely/issues/1587)
p = Point(np.array([1.0]), np.array([2.0]))
assert p.coords[:] == [(1.0, 2.0)]

p = Point(np.array([1.0]), np.array([2.0]), np.array([3.0]))
assert p.coords[:] == [(1.0, 2.0, 3.0)]


def test_from_point():
# From another point
p = Point(3.0, 4.0)
Expand Down

0 comments on commit bef2c9c

Please sign in to comment.