test_linestring.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import numpy as np
  2. import pytest
  3. import shapely
  4. from shapely import LinearRing, LineString, Point
  5. from shapely.coords import CoordinateSequence
  6. def test_from_coordinate_sequence():
  7. # From coordinate tuples
  8. line = LineString([(1.0, 2.0), (3.0, 4.0)])
  9. assert len(line.coords) == 2
  10. assert line.coords[:] == [(1.0, 2.0), (3.0, 4.0)]
  11. line = LineString([(1.0, 2.0), (3.0, 4.0)])
  12. assert line.coords[:] == [(1.0, 2.0), (3.0, 4.0)]
  13. def test_from_coordinate_sequence_z():
  14. line = LineString([(1.0, 2.0, 3.0), (3.0, 4.0, 5.0)])
  15. assert line.has_z
  16. assert line.coords[:] == [(1.0, 2.0, 3.0), (3.0, 4.0, 5.0)]
  17. def test_from_points():
  18. # From Points
  19. line = LineString([Point(1.0, 2.0), Point(3.0, 4.0)])
  20. assert line.coords[:] == [(1.0, 2.0), (3.0, 4.0)]
  21. line = LineString([Point(1.0, 2.0), Point(3.0, 4.0)])
  22. assert line.coords[:] == [(1.0, 2.0), (3.0, 4.0)]
  23. def test_from_mix():
  24. # From mix of tuples and Points
  25. line = LineString([Point(1.0, 2.0), (2.0, 3.0), Point(3.0, 4.0)])
  26. assert line.coords[:] == [(1.0, 2.0), (2.0, 3.0), (3.0, 4.0)]
  27. def test_from_linestring():
  28. # From another linestring
  29. line = LineString([(1.0, 2.0), (3.0, 4.0)])
  30. copy = LineString(line)
  31. assert copy.coords[:] == [(1.0, 2.0), (3.0, 4.0)]
  32. assert copy.geom_type == "LineString"
  33. def test_from_linearring():
  34. coords = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 0.0)]
  35. ring = LinearRing(coords)
  36. copy = LineString(ring)
  37. assert copy.coords[:] == coords
  38. assert copy.geom_type == "LineString"
  39. def test_from_linestring_z():
  40. coords = [(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)]
  41. line = LineString(coords)
  42. copy = LineString(line)
  43. assert copy.coords[:] == coords
  44. assert copy.geom_type == "LineString"
  45. def test_from_generator():
  46. gen = (coord for coord in [(1.0, 2.0), (3.0, 4.0)])
  47. line = LineString(gen)
  48. assert line.coords[:] == [(1.0, 2.0), (3.0, 4.0)]
  49. def test_from_empty():
  50. line = LineString()
  51. assert line.is_empty
  52. assert isinstance(line.coords, CoordinateSequence)
  53. assert line.coords[:] == []
  54. line = LineString([])
  55. assert line.is_empty
  56. assert isinstance(line.coords, CoordinateSequence)
  57. assert line.coords[:] == []
  58. def test_from_numpy():
  59. # Construct from a numpy array
  60. line = LineString(np.array([[1.0, 2.0], [3.0, 4.0]]))
  61. assert line.coords[:] == [(1.0, 2.0), (3.0, 4.0)]
  62. def test_numpy_empty_linestring_coords():
  63. # Check empty
  64. line = LineString([])
  65. la = np.asarray(line.coords)
  66. assert la.shape == (0, 2)
  67. def test_numpy_object_array():
  68. geom = LineString([(0.0, 0.0), (0.0, 1.0)])
  69. ar = np.empty(1, object)
  70. ar[:] = [geom]
  71. assert ar[0] == geom
  72. @pytest.mark.filterwarnings("ignore:Creating an ndarray from ragged nested sequences:")
  73. def test_from_invalid_dim():
  74. # TODO(shapely-2.0) better error message?
  75. # pytest.raises(
  76. # ValueError, match="at least 2 coordinate tuples|at least 2 coordinates"
  77. # ):
  78. with pytest.raises(shapely.GEOSException):
  79. LineString([(1, 2)])
  80. # exact error depends on numpy version
  81. with pytest.raises((ValueError, TypeError)):
  82. LineString([(1, 2, 3), (4, 5)])
  83. with pytest.raises((ValueError, TypeError)):
  84. LineString([(1, 2), (3, 4, 5)])
  85. msg = r"The ordinate \(last\) dimension should be 2 or 3, got {}"
  86. with pytest.raises(ValueError, match=msg.format(4)):
  87. LineString([(1, 2, 3, 4), (4, 5, 6, 7)])
  88. with pytest.raises(ValueError, match=msg.format(1)):
  89. LineString([(1,), (4,)])
  90. def test_from_single_coordinate():
  91. """Test for issue #486"""
  92. coords = [[-122.185933073564, 37.3629353839073]]
  93. with pytest.raises(shapely.GEOSException):
  94. ls = LineString(coords)
  95. ls.geom_type # caused segfault before fix
  96. class TestLineString:
  97. def test_linestring(self):
  98. # From coordinate tuples
  99. line = LineString([(1.0, 2.0), (3.0, 4.0)])
  100. assert len(line.coords) == 2
  101. assert line.coords[:] == [(1.0, 2.0), (3.0, 4.0)]
  102. # Bounds
  103. assert line.bounds == (1.0, 2.0, 3.0, 4.0)
  104. # Coordinate access
  105. assert tuple(line.coords) == ((1.0, 2.0), (3.0, 4.0))
  106. assert line.coords[0] == (1.0, 2.0)
  107. assert line.coords[1] == (3.0, 4.0)
  108. with pytest.raises(IndexError):
  109. line.coords[2] # index out of range
  110. # Geo interface
  111. assert line.__geo_interface__ == {
  112. "type": "LineString",
  113. "coordinates": ((1.0, 2.0), (3.0, 4.0)),
  114. }
  115. def test_linestring_empty(self):
  116. # Test Non-operability of Null geometry
  117. l_null = LineString()
  118. assert l_null.wkt == "LINESTRING EMPTY"
  119. assert l_null.length == 0.0
  120. def test_equals_argument_order(self):
  121. """
  122. Test equals predicate functions correctly regardless of the order
  123. of the inputs. See issue #317.
  124. """
  125. coords = ((0, 0), (1, 0), (1, 1), (0, 0))
  126. ls = LineString(coords)
  127. lr = LinearRing(coords)
  128. assert ls.__eq__(lr) is False # previously incorrectly returned True
  129. assert lr.__eq__(ls) is False
  130. assert (ls == lr) is False
  131. assert (lr == ls) is False
  132. ls_clone = LineString(coords)
  133. lr_clone = LinearRing(coords)
  134. assert ls.__eq__(ls_clone) is True
  135. assert lr.__eq__(lr_clone) is True
  136. assert (ls == ls_clone) is True
  137. assert (lr == lr_clone) is True
  138. def test_numpy_linestring_coords(self):
  139. from numpy.testing import assert_array_equal
  140. line = LineString([(1.0, 2.0), (3.0, 4.0)])
  141. expected = np.array([[1.0, 2.0], [3.0, 4.0]])
  142. # Coordinate sequences can be adapted as well
  143. la = np.asarray(line.coords)
  144. assert_array_equal(la, expected)
  145. def test_linestring_immutable():
  146. line = LineString([(1.0, 2.0), (3.0, 4.0)])
  147. with pytest.raises(AttributeError):
  148. line.coords = [(-1.0, -1.0), (1.0, 1.0)]
  149. with pytest.raises(TypeError):
  150. line.coords[0] = (-1.0, -1.0)
  151. def test_linestring_array_coercion():
  152. # don't convert to array of coordinates, keep objects
  153. line = LineString([(1.0, 2.0), (3.0, 4.0)])
  154. arr = np.array(line)
  155. assert arr.ndim == 0
  156. assert arr.size == 1
  157. assert arr.dtype == np.dtype("object")
  158. assert arr.item() == line
  159. def test_offset_curve_deprecate_positional():
  160. line_string = LineString([(1.0, 2.0), (3.0, 4.0)])
  161. with pytest.deprecated_call(
  162. match="positional argument `quad_segs` for `offset_curve` is deprecated"
  163. ):
  164. line_string.offset_curve(1.0, 8)
  165. with pytest.deprecated_call(
  166. match="positional arguments `quad_segs` and `join_style` "
  167. "for `offset_curve` are deprecated"
  168. ):
  169. line_string.offset_curve(1.0, 8, "round")
  170. with pytest.deprecated_call(
  171. match="positional arguments `quad_segs`, `join_style`, and `mitre_limit` "
  172. "for `offset_curve` are deprecated"
  173. ):
  174. line_string.offset_curve(1.0, 8, "round", 5.0)