Skip to content

Commit

Permalink
BUG: fix Polygon() constructor from a LineString
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche committed Dec 29, 2021
1 parent 48cf81e commit 16ed417
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions shapely/geometry/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ def __new__(self, shell=None, holes=None):
# conversion of coords to 2D array failed, this might be due
# to inconsistent coordinate dimensionality
raise ValueError("Inconsistent coordinate dimensionality")
else:
if not isinstance(shell, LinearRing):
shell = LinearRing(shell)

geom = shapely.polygons(shell, holes=holes)
if not isinstance(geom, Polygon):
Expand Down
12 changes: 12 additions & 0 deletions shapely/tests/geometry/test_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ def test_polygon_from_linearring():
assert polygon.interiors[i].coords[:] == holes[i].coords[:]


def test_polygon_from_linestring():
coords = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]
line = LineString(coords)
polygon = Polygon(line)
assert polygon.exterior.coords[:] == coords

# from unclosed linestring
line = LineString(coords[:-1])
polygon = Polygon(line)
assert polygon.exterior.coords[:] == coords


def test_polygon_from_polygon():
coords = [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0)]
polygon = Polygon(coords, [((0.25, 0.25), (0.25, 0.5), (0.5, 0.5), (0.5, 0.25))])
Expand Down

0 comments on commit 16ed417

Please sign in to comment.