test_voronoi_diagram.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. """
  2. Test cases for Voronoi Diagram creation.
  3. Overall, I'm trying less to test the correctness of the result
  4. and more to cover input cases and behavior, making sure
  5. that we return a sane result without error or raise a useful one.
  6. """
  7. import numpy as np
  8. import pytest
  9. from shapely.geometry import MultiPoint
  10. from shapely.ops import voronoi_diagram
  11. from shapely.wkt import loads as load_wkt
  12. def test_no_regions():
  13. mp = MultiPoint(points=[(0.5, 0.5)])
  14. with np.errstate(invalid="ignore"):
  15. regions = voronoi_diagram(mp)
  16. assert len(regions.geoms) == 0
  17. def test_two_regions():
  18. mp = MultiPoint(points=[(0.5, 0.5), (1.0, 1.0)])
  19. regions = voronoi_diagram(mp)
  20. assert len(regions.geoms) == 2
  21. def test_edges():
  22. mp = MultiPoint(points=[(0.5, 0.5), (1.0, 1.0)])
  23. regions = voronoi_diagram(mp, edges=True)
  24. assert len(regions.geoms) == 1
  25. # can be LineString or MultiLineString depending on the GEOS version
  26. assert all(r.geom_type.endswith("LineString") for r in regions.geoms)
  27. def test_smaller_envelope():
  28. mp = MultiPoint(points=[(0.5, 0.5), (1.0, 1.0)])
  29. poly = load_wkt("POLYGON ((0 0, 0.5 0, 0.5 0.5, 0 0.5, 0 0))")
  30. regions = voronoi_diagram(mp, envelope=poly)
  31. assert len(regions.geoms) == 2
  32. assert sum(r.area for r in regions.geoms) > poly.area
  33. def test_larger_envelope():
  34. """When the envelope we specify is larger than the
  35. area of the input feature, the created regions should
  36. expand to fill that area."""
  37. mp = MultiPoint(points=[(0.5, 0.5), (1.0, 1.0)])
  38. poly = load_wkt("POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))")
  39. regions = voronoi_diagram(mp, envelope=poly)
  40. assert len(regions.geoms) == 2
  41. assert sum(r.area for r in regions.geoms) == poly.area
  42. def test_from_polygon():
  43. poly = load_wkt("POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))")
  44. regions = voronoi_diagram(poly)
  45. assert len(regions.geoms) == 4
  46. def test_from_polygon_with_enough_tolerance():
  47. poly = load_wkt("POLYGON ((0 0, 0.5 0, 0.5 0.5, 0 0.5, 0 0))")
  48. regions = voronoi_diagram(poly, tolerance=1.0)
  49. assert len(regions.geoms) == 2
  50. def test_from_polygon_without_enough_tolerance():
  51. poly = load_wkt("POLYGON ((0 0, 0.5 0, 0.5 0.5, 0 0.5, 0 0))")
  52. with pytest.raises(ValueError) as exc:
  53. voronoi_diagram(poly, tolerance=0.6)
  54. assert "Could not create Voronoi Diagram with the specified inputs" in str(
  55. exc.value
  56. )
  57. assert "Try running again with default tolerance value." in str(exc.value)
  58. def test_from_polygon_without_floating_point_coordinates():
  59. poly = load_wkt("POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))")
  60. with pytest.raises(ValueError) as exc:
  61. voronoi_diagram(poly, tolerance=0.1)
  62. assert "Could not create Voronoi Diagram with the specified inputs" in str(
  63. exc.value
  64. )
  65. assert "Try running again with default tolerance value." in str(exc.value)
  66. def test_from_multipoint_without_floating_point_coordinates():
  67. """A Multipoint with the same "shape" as the above Polygon raises the same error."""
  68. mp = load_wkt("MULTIPOINT (0 0, 1 0, 1 1, 0 1)")
  69. with pytest.raises(ValueError) as exc:
  70. voronoi_diagram(mp, tolerance=0.1)
  71. assert "Could not create Voronoi Diagram with the specified inputs" in str(
  72. exc.value
  73. )
  74. assert "Try running again with default tolerance value." in str(exc.value)
  75. def test_from_multipoint_with_tolerace_without_floating_point_coordinates():
  76. """This multipoint will not work with a tolerance value."""
  77. mp = load_wkt("MULTIPOINT (0 0, 1 0, 1 2, 0 1)")
  78. with pytest.raises(ValueError) as exc:
  79. voronoi_diagram(mp, tolerance=0.1)
  80. assert "Could not create Voronoi Diagram with the specified inputs" in str(
  81. exc.value
  82. )
  83. assert "Try running again with default tolerance value." in str(exc.value)
  84. def test_from_multipoint_without_tolerace_without_floating_point_coordinates():
  85. """But it's fine without it."""
  86. mp = load_wkt("MULTIPOINT (0 0, 1 0, 1 2, 0 1)")
  87. regions = voronoi_diagram(mp)
  88. assert len(regions.geoms) == 4