test_shared_paths.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import unittest
  2. import pytest
  3. from shapely.errors import GeometryTypeError
  4. from shapely.geometry import GeometryCollection, LineString, MultiLineString, Point
  5. from shapely.ops import shared_paths
  6. class SharedPaths(unittest.TestCase):
  7. def test_shared_paths_forward(self):
  8. g1 = LineString([(0, 0), (10, 0), (10, 5), (20, 5)])
  9. g2 = LineString([(5, 0), (15, 0)])
  10. result = shared_paths(g1, g2)
  11. assert isinstance(result, GeometryCollection)
  12. assert len(result.geoms) == 2
  13. a, b = result.geoms
  14. assert isinstance(a, MultiLineString)
  15. assert len(a.geoms) == 1
  16. assert a.geoms[0].coords[:] == [(5, 0), (10, 0)]
  17. assert b.is_empty
  18. def test_shared_paths_forward2(self):
  19. g1 = LineString([(0, 0), (10, 0), (10, 5), (20, 5)])
  20. g2 = LineString([(15, 0), (5, 0)])
  21. result = shared_paths(g1, g2)
  22. assert isinstance(result, GeometryCollection)
  23. assert len(result.geoms) == 2
  24. a, b = result.geoms
  25. assert isinstance(b, MultiLineString)
  26. assert len(b.geoms) == 1
  27. assert b.geoms[0].coords[:] == [(5, 0), (10, 0)]
  28. assert a.is_empty
  29. def test_wrong_type(self):
  30. g1 = Point(0, 0)
  31. g2 = LineString([(5, 0), (15, 0)])
  32. with pytest.raises(GeometryTypeError):
  33. shared_paths(g1, g2)
  34. with pytest.raises(GeometryTypeError):
  35. shared_paths(g2, g1)