1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
import unittest
from shapely.geometry import LineString, Point, Polygon
from shapely.geometry.base import dump_coords
from shapely.ops import polygonize, polygonize_full
class PolygonizeTestCase(unittest.TestCase):
def test_polygonize(self):
lines = [
LineString([(0, 0), (1, 1)]),
LineString([(0, 0), (0, 1)]),
LineString([(0, 1), (1, 1)]),
LineString([(1, 1), (1, 0)]),
LineString([(1, 0), (0, 0)]),
LineString([(5, 5), (6, 6)]),
Point(0, 0),
]
result = list(polygonize(lines))
assert all(isinstance(x, Polygon) for x in result)
def test_polygonize_full(self):
lines2 = [
[(0, 0), (1, 1)],
[(0, 0), (0, 1)],
[(0, 1), (1, 1)],
[(1, 1), (1, 0)],
[(1, 0), (0, 0)],
[(5, 5), (6, 6)],
[(1, 1), (100, 100)],
]
result2, cuts, dangles, invalids = polygonize_full(lines2)
assert len(result2.geoms) == 2
assert all(isinstance(x, Polygon) for x in result2.geoms)
assert list(cuts.geoms) == []
assert all(isinstance(x, LineString) for x in dangles.geoms)
assert dump_coords(dangles) == [
[(1.0, 1.0), (100.0, 100.0)],
[(5.0, 5.0), (6.0, 6.0)],
]
assert list(invalids.geoms) == []
|