test_clip_by_rect.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. """
  2. Tests for GEOSClipByRect based on unit tests from libgeos.
  3. There are some expected differences due to Shapely's handling of empty
  4. geometries.
  5. """
  6. import pytest
  7. from shapely.ops import clip_by_rect
  8. from shapely.wkt import dumps as dump_wkt, loads as load_wkt
  9. def test_point_outside():
  10. """Point outside"""
  11. geom1 = load_wkt("POINT (0 0)")
  12. geom2 = clip_by_rect(geom1, 10, 10, 20, 20)
  13. assert dump_wkt(geom2, rounding_precision=0) == "GEOMETRYCOLLECTION EMPTY"
  14. def test_point_inside():
  15. """Point inside"""
  16. geom1 = load_wkt("POINT (15 15)")
  17. geom2 = clip_by_rect(geom1, 10, 10, 20, 20)
  18. assert dump_wkt(geom2, rounding_precision=0) == "POINT (15 15)"
  19. def test_point_on_boundary():
  20. """Point on boundary"""
  21. geom1 = load_wkt("POINT (15 10)")
  22. geom2 = clip_by_rect(geom1, 10, 10, 20, 20)
  23. assert dump_wkt(geom2, rounding_precision=0) == "GEOMETRYCOLLECTION EMPTY"
  24. def test_line_outside():
  25. """Line outside"""
  26. geom1 = load_wkt("LINESTRING (0 0, -5 5)")
  27. geom2 = clip_by_rect(geom1, 10, 10, 20, 20)
  28. assert dump_wkt(geom2, rounding_precision=0) == "GEOMETRYCOLLECTION EMPTY"
  29. def test_line_inside():
  30. """Line inside"""
  31. geom1 = load_wkt("LINESTRING (15 15, 16 15)")
  32. geom2 = clip_by_rect(geom1, 10, 10, 20, 20)
  33. assert dump_wkt(geom2, rounding_precision=0) == "LINESTRING (15 15, 16 15)"
  34. def test_line_on_boundary():
  35. """Line on boundary"""
  36. geom1 = load_wkt("LINESTRING (10 15, 10 10, 15 10)")
  37. geom2 = clip_by_rect(geom1, 10, 10, 20, 20)
  38. assert dump_wkt(geom2, rounding_precision=0) == "GEOMETRYCOLLECTION EMPTY"
  39. def test_line_splitting_rectangle():
  40. """Line splitting rectangle"""
  41. geom1 = load_wkt("LINESTRING (10 5, 25 20)")
  42. geom2 = clip_by_rect(geom1, 10, 10, 20, 20)
  43. assert dump_wkt(geom2, rounding_precision=0) == "LINESTRING (15 10, 20 15)"
  44. @pytest.mark.xfail(reason="TODO issue to CCW")
  45. def test_polygon_shell_ccw_fully_on_rectangle_boundary():
  46. """Polygon shell (CCW) fully on rectangle boundary"""
  47. geom1 = load_wkt("POLYGON ((10 10, 20 10, 20 20, 10 20, 10 10))")
  48. geom2 = clip_by_rect(geom1, 10, 10, 20, 20)
  49. assert (
  50. dump_wkt(geom2, rounding_precision=0)
  51. == "POLYGON ((10 10, 20 10, 20 20, 10 20, 10 10))"
  52. )
  53. @pytest.mark.xfail(reason="TODO issue to CW")
  54. def test_polygon_shell_cc_fully_on_rectangle_boundary():
  55. """Polygon shell (CW) fully on rectangle boundary"""
  56. geom1 = load_wkt("POLYGON ((10 10, 10 20, 20 20, 20 10, 10 10))")
  57. geom2 = clip_by_rect(geom1, 10, 10, 20, 20)
  58. assert (
  59. dump_wkt(geom2, rounding_precision=0)
  60. == "POLYGON ((10 10, 20 10, 20 20, 10 20, 10 10))"
  61. )
  62. def polygon_hole_ccw_fully_on_rectangle_boundary():
  63. """Polygon hole (CCW) fully on rectangle boundary"""
  64. geom1 = load_wkt(
  65. "POLYGON ((0 0, 0 30, 30 30, 30 0, 0 0), (10 10, 20 10, 20 20, 10 20, 10 10))"
  66. )
  67. geom2 = clip_by_rect(geom1, 10, 10, 20, 20)
  68. assert dump_wkt(geom2, rounding_precision=0) == "GEOMETRYCOLLECTION EMPTY"
  69. def polygon_hole_cw_fully_on_rectangle_boundary():
  70. """Polygon hole (CW) fully on rectangle boundary"""
  71. geom1 = load_wkt(
  72. "POLYGON ((0 0, 0 30, 30 30, 30 0, 0 0), (10 10, 10 20, 20 20, 20 10, 10 10))"
  73. )
  74. geom2 = clip_by_rect(geom1, 10, 10, 20, 20)
  75. assert dump_wkt(geom2, rounding_precision=0) == "GEOMETRYCOLLECTION EMPTY"
  76. def polygon_fully_within_rectangle():
  77. """Polygon fully within rectangle"""
  78. wkt = "POLYGON ((1 1, 1 30, 30 30, 30 1, 1 1), (10 10, 20 10, 20 20, 10 20, 10 10))"
  79. geom1 = load_wkt(wkt)
  80. geom2 = clip_by_rect(geom1, 0, 0, 40, 40)
  81. assert dump_wkt(geom2, rounding_precision=0) == wkt
  82. def polygon_overlapping_rectangle():
  83. """Polygon overlapping rectangle"""
  84. wkt = "POLYGON ((0 0, 0 30, 30 30, 30 0, 0 0), (10 10, 20 10, 20 20, 10 20, 10 10))"
  85. geom1 = load_wkt(wkt)
  86. geom2 = clip_by_rect(geom1, 5, 5, 15, 15)
  87. assert (
  88. dump_wkt(geom2, rounding_precision=0)
  89. == "POLYGON ((5 5, 5 15, 10 15, 10 10, 15 10, 15 5, 5 5))"
  90. )