test_polygonize.py 1.4 KB

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