test_buffer.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import unittest
  2. import pytest
  3. from shapely import geometry
  4. from shapely.constructive import BufferCapStyle, BufferJoinStyle
  5. from shapely.geometry.base import CAP_STYLE, JOIN_STYLE
  6. @pytest.mark.parametrize("distance", [float("nan"), float("inf")])
  7. def test_non_finite_distance(distance):
  8. g = geometry.Point(0, 0)
  9. with pytest.raises(ValueError, match="distance must be finite"):
  10. g.buffer(distance)
  11. class BufferTests(unittest.TestCase):
  12. """Test Buffer Point/Line/Polygon with and without single_sided params"""
  13. def test_empty(self):
  14. g = geometry.Point(0, 0)
  15. h = g.buffer(0)
  16. assert h.is_empty
  17. def test_point(self):
  18. g = geometry.Point(0, 0)
  19. h = g.buffer(1, quad_segs=1)
  20. assert h.geom_type == "Polygon"
  21. expected_coord = [(1.0, 0.0), (0, -1.0), (-1.0, 0), (0, 1.0), (1.0, 0.0)]
  22. for index, coord in enumerate(h.exterior.coords):
  23. assert coord[0] == pytest.approx(expected_coord[index][0])
  24. assert coord[1] == pytest.approx(expected_coord[index][1])
  25. def test_point_single_sidedd(self):
  26. g = geometry.Point(0, 0)
  27. h = g.buffer(1, quad_segs=1, single_sided=True)
  28. assert h.geom_type == "Polygon"
  29. expected_coord = [(1.0, 0.0), (0, -1.0), (-1.0, 0), (0, 1.0), (1.0, 0.0)]
  30. for index, coord in enumerate(h.exterior.coords):
  31. assert coord[0] == pytest.approx(expected_coord[index][0])
  32. assert coord[1] == pytest.approx(expected_coord[index][1])
  33. def test_line(self):
  34. g = geometry.LineString([[0, 0], [0, 1]])
  35. h = g.buffer(1, quad_segs=1)
  36. assert h.geom_type == "Polygon"
  37. expected_coord = [
  38. (-1.0, 1.0),
  39. (0, 2.0),
  40. (1.0, 1.0),
  41. (1.0, 0.0),
  42. (0, -1.0),
  43. (-1.0, 0.0),
  44. (-1.0, 1.0),
  45. ]
  46. for index, coord in enumerate(h.exterior.coords):
  47. assert coord[0] == pytest.approx(expected_coord[index][0])
  48. assert coord[1] == pytest.approx(expected_coord[index][1])
  49. def test_line_single_sideded_left(self):
  50. g = geometry.LineString([[0, 0], [0, 1]])
  51. h = g.buffer(1, quad_segs=1, single_sided=True)
  52. assert h.geom_type == "Polygon"
  53. expected_coord = [(0.0, 1.0), (0.0, 0.0), (-1.0, 0.0), (-1.0, 1.0), (0.0, 1.0)]
  54. for index, coord in enumerate(h.exterior.coords):
  55. assert coord[0] == pytest.approx(expected_coord[index][0])
  56. assert coord[1] == pytest.approx(expected_coord[index][1])
  57. def test_line_single_sideded_right(self):
  58. g = geometry.LineString([[0, 0], [0, 1]])
  59. h = g.buffer(-1, quad_segs=1, single_sided=True)
  60. assert h.geom_type == "Polygon"
  61. expected_coord = [(0.0, 0.0), (0.0, 1.0), (1.0, 1.0), (1.0, 0.0), (0.0, 0.0)]
  62. for index, coord in enumerate(h.exterior.coords):
  63. assert coord[0] == pytest.approx(expected_coord[index][0])
  64. assert coord[1] == pytest.approx(expected_coord[index][1])
  65. def test_polygon(self):
  66. g = geometry.Polygon([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]])
  67. h = g.buffer(1, quad_segs=1)
  68. assert h.geom_type == "Polygon"
  69. expected_coord = [
  70. (-1.0, 0.0),
  71. (-1.0, 1.0),
  72. (0.0, 2.0),
  73. (1.0, 2.0),
  74. (2.0, 1.0),
  75. (2.0, 0.0),
  76. (1.0, -1.0),
  77. (0.0, -1.0),
  78. (-1.0, 0.0),
  79. ]
  80. for index, coord in enumerate(h.exterior.coords):
  81. assert coord[0] == pytest.approx(expected_coord[index][0])
  82. assert coord[1] == pytest.approx(expected_coord[index][1])
  83. def test_polygon_single_sideded(self):
  84. g = geometry.Polygon([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]])
  85. h = g.buffer(1, quad_segs=1, single_sided=True)
  86. assert h.geom_type == "Polygon"
  87. expected_coord = [
  88. (-1.0, 0.0),
  89. (-1.0, 1.0),
  90. (0.0, 2.0),
  91. (1.0, 2.0),
  92. (2.0, 1.0),
  93. (2.0, 0.0),
  94. (1.0, -1.0),
  95. (0.0, -1.0),
  96. (-1.0, 0.0),
  97. ]
  98. for index, coord in enumerate(h.exterior.coords):
  99. assert coord[0] == pytest.approx(expected_coord[index][0])
  100. assert coord[1] == pytest.approx(expected_coord[index][1])
  101. def test_enum_values(self):
  102. assert CAP_STYLE.round == 1
  103. assert CAP_STYLE.round == BufferCapStyle.round
  104. assert CAP_STYLE.flat == 2
  105. assert CAP_STYLE.flat == BufferCapStyle.flat
  106. assert CAP_STYLE.square == 3
  107. assert CAP_STYLE.square == BufferCapStyle.square
  108. assert JOIN_STYLE.round == 1
  109. assert JOIN_STYLE.round == BufferJoinStyle.round
  110. assert JOIN_STYLE.mitre == 2
  111. assert JOIN_STYLE.mitre == BufferJoinStyle.mitre
  112. assert JOIN_STYLE.bevel == 3
  113. assert JOIN_STYLE.bevel == BufferJoinStyle.bevel
  114. def test_cap_style(self):
  115. g = geometry.LineString([[0, 0], [1, 0]])
  116. h = g.buffer(1, cap_style=BufferCapStyle.round)
  117. assert h == g.buffer(1, cap_style=CAP_STYLE.round)
  118. assert h == g.buffer(1, cap_style="round")
  119. h = g.buffer(1, cap_style=BufferCapStyle.flat)
  120. assert h == g.buffer(1, cap_style=CAP_STYLE.flat)
  121. assert h == g.buffer(1, cap_style="flat")
  122. h = g.buffer(1, cap_style=BufferCapStyle.square)
  123. assert h == g.buffer(1, cap_style=CAP_STYLE.square)
  124. assert h == g.buffer(1, cap_style="square")
  125. def test_buffer_style(self):
  126. g = geometry.LineString([[0, 0], [1, 0]])
  127. h = g.buffer(1, join_style=BufferJoinStyle.round)
  128. assert h == g.buffer(1, join_style=JOIN_STYLE.round)
  129. assert h == g.buffer(1, join_style="round")
  130. h = g.buffer(1, join_style=BufferJoinStyle.mitre)
  131. assert h == g.buffer(1, join_style=JOIN_STYLE.mitre)
  132. assert h == g.buffer(1, join_style="mitre")
  133. h = g.buffer(1, join_style=BufferJoinStyle.bevel)
  134. assert h == g.buffer(1, join_style=JOIN_STYLE.bevel)
  135. assert h == g.buffer(1, join_style="bevel")
  136. def test_deprecated_quadsegs():
  137. point = geometry.Point(0, 0)
  138. with pytest.warns(FutureWarning):
  139. result = point.buffer(1, quadsegs=1)
  140. expected = point.buffer(1, quad_segs=1)
  141. assert result.equals(expected)
  142. def test_deprecated_resolution():
  143. point = geometry.Point(0, 0)
  144. with pytest.deprecated_call(match="Use 'quad_segs' instead"):
  145. result = point.buffer(1, resolution=1)
  146. expected = point.buffer(1, quad_segs=1)
  147. assert result.equals(expected)