test_vectorized.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import unittest
  2. import numpy as np
  3. import pytest
  4. from shapely.geometry import MultiPolygon, Point, box
  5. @pytest.mark.filterwarnings("ignore:The 'shapely.vectorized:")
  6. class VectorizedContainsTestCase(unittest.TestCase):
  7. def assertContainsResults(self, geom, x, y):
  8. from shapely.vectorized import contains
  9. result = contains(geom, x, y)
  10. x = np.asanyarray(x)
  11. y = np.asanyarray(y)
  12. self.assertIsInstance(result, np.ndarray)
  13. self.assertEqual(result.dtype, bool)
  14. result_flat = result.flat
  15. x_flat, y_flat = x.flat, y.flat
  16. # Do the equivalent operation, only slowly, comparing the result
  17. # as we go.
  18. for idx in range(x.size):
  19. assert result_flat[idx] == geom.contains(Point(x_flat[idx], y_flat[idx]))
  20. return result
  21. def construct_torus(self):
  22. point = Point(0, 0)
  23. return point.buffer(5).symmetric_difference(point.buffer(2.5))
  24. def test_contains_poly(self):
  25. y, x = np.mgrid[-10:10:5j], np.mgrid[-5:15:5j]
  26. self.assertContainsResults(self.construct_torus(), x, y)
  27. def test_contains_point(self):
  28. y, x = np.mgrid[-10:10:5j], np.mgrid[-5:15:5j]
  29. self.assertContainsResults(Point(x[0], y[0]), x, y)
  30. def test_contains_linestring(self):
  31. y, x = np.mgrid[-10:10:5j], np.mgrid[-5:15:5j]
  32. self.assertContainsResults(Point(x[0], y[0]), x, y)
  33. def test_contains_multipoly(self):
  34. y, x = np.mgrid[-10:10:5j], np.mgrid[-5:15:5j]
  35. # Construct a geometry of the torus cut in half vertically.
  36. cut_poly = box(-1, -10, -2.5, 10)
  37. geom = self.construct_torus().difference(cut_poly)
  38. assert isinstance(geom, MultiPolygon)
  39. self.assertContainsResults(geom, x, y)
  40. def test_y_array_order(self):
  41. y, x = np.mgrid[-10:10:5j, -5:15:5j]
  42. y = y.copy("f")
  43. self.assertContainsResults(self.construct_torus(), x, y)
  44. def test_x_array_order(self):
  45. y, x = np.mgrid[-10:10:5j, -5:15:5j]
  46. x = x.copy("f")
  47. self.assertContainsResults(self.construct_torus(), x, y)
  48. def test_xy_array_order(self):
  49. y, x = np.mgrid[-10:10:5j, -5:15:5j]
  50. x = x.copy("f")
  51. y = y.copy("f")
  52. result = self.assertContainsResults(self.construct_torus(), x, y)
  53. # Preserve the order
  54. assert result.flags["F_CONTIGUOUS"]
  55. def test_array_dtype(self):
  56. y, x = np.mgrid[-10:10:5j], np.mgrid[-5:15:5j]
  57. x = x.astype(np.int16)
  58. self.assertContainsResults(self.construct_torus(), x, y)
  59. def test_array_2d(self):
  60. y, x = np.mgrid[-10:10:15j, -5:15:16j]
  61. result = self.assertContainsResults(self.construct_torus(), x, y)
  62. assert result.shape == x.shape
  63. def test_shapely_xy_attr_contains(self):
  64. g = Point(0, 0).buffer(10.0)
  65. self.assertContainsResults(self.construct_torus(), *g.exterior.xy)
  66. @pytest.mark.filterwarnings("ignore:The 'shapely.vectorized:")
  67. class VectorizedTouchesTestCase(unittest.TestCase):
  68. def test_touches(self):
  69. from shapely.vectorized import touches
  70. y, x = np.mgrid[-2:3:6j, -1:3:5j]
  71. geom = box(0, -1, 2, 2)
  72. result = touches(geom, x, y)
  73. expected = np.array(
  74. [
  75. [False, False, False, False, False],
  76. [False, True, True, True, False],
  77. [False, True, False, True, False],
  78. [False, True, False, True, False],
  79. [False, True, True, True, False],
  80. [False, False, False, False, False],
  81. ],
  82. dtype=bool,
  83. )
  84. from numpy.testing import assert_array_equal
  85. assert_array_equal(result, expected)