test_plotting.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import pytest
  2. from numpy.testing import assert_allclose
  3. from shapely import (
  4. LineString,
  5. MultiLineString,
  6. MultiPolygon,
  7. Point,
  8. box,
  9. get_coordinates,
  10. )
  11. from shapely.ops import orient
  12. from shapely.plotting import patch_from_polygon, plot_line, plot_points, plot_polygon
  13. pytest.importorskip("matplotlib")
  14. def test_patch_from_polygon():
  15. poly = box(0, 0, 1, 1)
  16. artist = patch_from_polygon(poly, facecolor="red", edgecolor="blue", linewidth=3)
  17. assert equal_color(artist.get_facecolor(), "red")
  18. assert equal_color(artist.get_edgecolor(), "blue")
  19. assert artist.get_linewidth() == 3
  20. def test_patch_from_polygon_with_interior():
  21. poly = box(0, 0, 1, 1).difference(box(0.2, 0.2, 0.5, 0.5))
  22. artist = patch_from_polygon(poly, facecolor="red", edgecolor="blue", linewidth=3)
  23. assert equal_color(artist.get_facecolor(), "red")
  24. assert equal_color(artist.get_edgecolor(), "blue")
  25. assert artist.get_linewidth() == 3
  26. def test_patch_from_multipolygon():
  27. poly = box(0, 0, 1, 1).union(box(2, 2, 3, 3))
  28. artist = patch_from_polygon(poly, facecolor="red", edgecolor="blue", linewidth=3)
  29. assert equal_color(artist.get_facecolor(), "red")
  30. assert equal_color(artist.get_edgecolor(), "blue")
  31. assert artist.get_linewidth() == 3
  32. def test_plot_polygon():
  33. poly = box(0, 0, 1, 1)
  34. artist, _ = plot_polygon(poly)
  35. plot_coords = artist.get_path().vertices
  36. assert_allclose(plot_coords, get_coordinates(poly))
  37. # overriding default styling
  38. artist = plot_polygon(poly, add_points=False, color="red", linewidth=3)
  39. assert equal_color(artist.get_facecolor(), "red", alpha=0.3)
  40. assert equal_color(artist.get_edgecolor(), "red", alpha=1.0)
  41. assert artist.get_linewidth() == 3
  42. def test_plot_polygon_with_interior():
  43. poly = box(0, 0, 1, 1).difference(box(0.2, 0.2, 0.5, 0.5))
  44. artist, _ = plot_polygon(poly)
  45. plot_coords = artist.get_path().vertices
  46. assert_allclose(plot_coords, get_coordinates(orient(poly)))
  47. def test_plot_multipolygon():
  48. poly = box(0, 0, 1, 1).union(box(2, 2, 3, 3))
  49. artist, _ = plot_polygon(poly)
  50. plot_coords = artist.get_path().vertices
  51. assert_allclose(plot_coords, get_coordinates(poly))
  52. def test_plot_multipolygon_with_interior():
  53. poly1 = box(0, 0, 1, 1).difference(box(0.2, 0.2, 0.5, 0.5))
  54. poly2 = box(3, 3, 6, 6).difference(box(4, 4, 5, 5))
  55. poly = MultiPolygon([poly1, poly2])
  56. artist, _ = plot_polygon(poly)
  57. plot_coords = artist.get_path().vertices
  58. assert_allclose(plot_coords, get_coordinates(orient(poly)))
  59. def test_plot_line():
  60. line = LineString([(0, 0), (1, 0), (1, 1)])
  61. artist, _ = plot_line(line)
  62. plot_coords = artist.get_path().vertices
  63. assert_allclose(plot_coords, get_coordinates(line))
  64. # overriding default styling
  65. artist = plot_line(line, add_points=False, color="red", linewidth=3)
  66. assert equal_color(artist.get_edgecolor(), "red")
  67. assert equal_color(artist.get_facecolor(), "none")
  68. assert artist.get_linewidth() == 3
  69. def test_plot_multilinestring():
  70. line = MultiLineString(
  71. [LineString([(0, 0), (1, 0), (1, 1)]), LineString([(2, 2), (3, 3)])]
  72. )
  73. artist, _ = plot_line(line)
  74. plot_coords = artist.get_path().vertices
  75. assert_allclose(plot_coords, get_coordinates(line))
  76. def test_plot_points():
  77. for geom in [Point(0, 0), LineString([(0, 0), (1, 0), (1, 1)]), box(0, 0, 1, 1)]:
  78. artist = plot_points(geom)
  79. plot_coords = artist.get_path().vertices
  80. assert_allclose(plot_coords, get_coordinates(geom))
  81. assert artist.get_linestyle() == "None"
  82. # overriding default styling
  83. geom = Point(0, 0)
  84. artist = plot_points(geom, color="red", marker="+", fillstyle="top")
  85. assert artist.get_color() == "red"
  86. assert artist.get_marker() == "+"
  87. assert artist.get_fillstyle() == "top"
  88. def equal_color(actual, expected, alpha=None):
  89. from matplotlib import colors
  90. conv = colors.colorConverter
  91. return actual == conv.to_rgba(expected, alpha=alpha)