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

Allow passing len-1 array for x and y coordinates in Point(..) (compat with 1.8) #1590

Merged
merged 2 commits into from
Oct 29, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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