test_multilinestring.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import numpy as np
  2. import pytest
  3. from shapely import LineString, MultiLineString
  4. from shapely.errors import EmptyPartError
  5. from shapely.geometry.base import dump_coords
  6. from shapely.tests.geometry.test_multi import MultiGeometryTestCase
  7. class TestMultiLineString(MultiGeometryTestCase):
  8. def test_multilinestring(self):
  9. # From coordinate tuples
  10. geom = MultiLineString([[(1.0, 2.0), (3.0, 4.0)]])
  11. assert isinstance(geom, MultiLineString)
  12. assert len(geom.geoms) == 1
  13. assert dump_coords(geom) == [[(1.0, 2.0), (3.0, 4.0)]]
  14. # From lines
  15. a = LineString([(1.0, 2.0), (3.0, 4.0)])
  16. ml = MultiLineString([a])
  17. assert len(ml.geoms) == 1
  18. assert dump_coords(ml) == [[(1.0, 2.0), (3.0, 4.0)]]
  19. # From another multi-line
  20. ml2 = MultiLineString(ml)
  21. assert len(ml2.geoms) == 1
  22. assert dump_coords(ml2) == [[(1.0, 2.0), (3.0, 4.0)]]
  23. # Sub-geometry Access
  24. geom = MultiLineString([(((0.0, 0.0), (1.0, 2.0)))])
  25. assert isinstance(geom.geoms[0], LineString)
  26. assert dump_coords(geom.geoms[0]) == [(0.0, 0.0), (1.0, 2.0)]
  27. with pytest.raises(IndexError): # index out of range
  28. geom.geoms[1]
  29. # Geo interface
  30. assert geom.__geo_interface__ == {
  31. "type": "MultiLineString",
  32. "coordinates": (((0.0, 0.0), (1.0, 2.0)),),
  33. }
  34. def test_from_multilinestring_z(self):
  35. coords1 = [(0.0, 1.0, 2.0), (3.0, 4.0, 5.0)]
  36. coords2 = [(6.0, 7.0, 8.0), (9.0, 10.0, 11.0)]
  37. # From coordinate tuples
  38. ml = MultiLineString([coords1, coords2])
  39. copy = MultiLineString(ml)
  40. assert isinstance(copy, MultiLineString)
  41. assert copy.geom_type == "MultiLineString"
  42. assert len(copy.geoms) == 2
  43. assert dump_coords(copy.geoms[0]) == coords1
  44. assert dump_coords(copy.geoms[1]) == coords2
  45. def test_numpy(self):
  46. # Construct from a numpy array
  47. geom = MultiLineString([np.array(((0.0, 0.0), (1.0, 2.0)))])
  48. assert isinstance(geom, MultiLineString)
  49. assert len(geom.geoms) == 1
  50. assert dump_coords(geom) == [[(0.0, 0.0), (1.0, 2.0)]]
  51. def test_subgeom_access(self):
  52. line0 = LineString([(0.0, 1.0), (2.0, 3.0)])
  53. line1 = LineString([(4.0, 5.0), (6.0, 7.0)])
  54. self.subgeom_access_test(MultiLineString, [line0, line1])
  55. def test_create_multi_with_empty_component(self):
  56. msg = "Can't create MultiLineString with empty component"
  57. with pytest.raises(EmptyPartError, match=msg):
  58. MultiLineString([LineString([(0, 0), (1, 1), (2, 2)]), LineString()]).wkt
  59. def test_numpy_object_array():
  60. geom = MultiLineString([[[5.0, 6.0], [7.0, 8.0]]])
  61. ar = np.empty(1, object)
  62. ar[:] = [geom]
  63. assert ar[0] == geom