test_coords.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import numpy as np
  2. import pytest
  3. from shapely import LineString, geos_version
  4. from shapely.tests.common import (
  5. line_string,
  6. line_string_m,
  7. line_string_z,
  8. line_string_zm,
  9. point,
  10. point_m,
  11. point_z,
  12. point_zm,
  13. )
  14. class TestCoords:
  15. """
  16. Shapely assumes contiguous C-order float64 data for internal ops.
  17. Data should be converted to contiguous float64 if numpy exists.
  18. c9a0707 broke this a little bit.
  19. """
  20. def test_data_promotion(self):
  21. coords = np.array([[12, 34], [56, 78]], dtype=np.float32)
  22. processed_coords = np.array(LineString(coords).coords)
  23. assert coords.tolist() == processed_coords.tolist()
  24. def test_data_destriding(self):
  25. coords = np.array([[12, 34], [56, 78]], dtype=np.float32)
  26. # Easy way to introduce striding: reverse list order
  27. processed_coords = np.array(LineString(coords[::-1]).coords)
  28. assert coords[::-1].tolist() == processed_coords.tolist()
  29. class TestCoordsGetItem:
  30. def test_index_coords(self):
  31. c = [(float(x), float(-x)) for x in range(4)]
  32. g = LineString(c)
  33. for i in range(-4, 4):
  34. assert g.coords[i] == c[i]
  35. with pytest.raises(IndexError):
  36. g.coords[4]
  37. with pytest.raises(IndexError):
  38. g.coords[-5]
  39. def test_index_coords_z(self):
  40. c = [(float(x), float(-x), float(x * 2)) for x in range(4)]
  41. g = LineString(c)
  42. for i in range(-4, 4):
  43. assert g.coords[i] == c[i]
  44. with pytest.raises(IndexError):
  45. g.coords[4]
  46. with pytest.raises(IndexError):
  47. g.coords[-5]
  48. def test_index_coords_misc(self):
  49. g = LineString() # empty
  50. with pytest.raises(IndexError):
  51. g.coords[0]
  52. with pytest.raises(TypeError):
  53. g.coords[0.0]
  54. def test_slice_coords(self):
  55. c = [(float(x), float(-x)) for x in range(4)]
  56. g = LineString(c)
  57. assert g.coords[1:] == c[1:]
  58. assert g.coords[:-1] == c[:-1]
  59. assert g.coords[::-1] == c[::-1]
  60. assert g.coords[::2] == c[::2]
  61. assert g.coords[:4] == c[:4]
  62. assert g.coords[4:] == c[4:] == []
  63. def test_slice_coords_z(self):
  64. c = [(float(x), float(-x), float(x * 2)) for x in range(4)]
  65. g = LineString(c)
  66. assert g.coords[1:] == c[1:]
  67. assert g.coords[:-1] == c[:-1]
  68. assert g.coords[::-1] == c[::-1]
  69. assert g.coords[::2] == c[::2]
  70. assert g.coords[:4] == c[:4]
  71. assert g.coords[4:] == c[4:] == []
  72. class TestXY:
  73. """New geometry/coordseq method 'xy' makes numpy interop easier"""
  74. def test_arrays(self):
  75. x, y = LineString([(0, 0), (1, 1)]).xy
  76. assert len(x) == 2
  77. assert list(x) == [0.0, 1.0]
  78. assert len(y) == 2
  79. assert list(y) == [0.0, 1.0]
  80. @pytest.mark.parametrize("geom", [point, point_z, line_string, line_string_z])
  81. def test_coords_array_copy(geom):
  82. """Test CoordinateSequence.__array__ method."""
  83. coord_seq = geom.coords
  84. assert np.array(coord_seq) is not np.array(coord_seq)
  85. assert np.array(coord_seq, copy=True) is not np.array(coord_seq, copy=True)
  86. # Behaviour of copy=False is different between NumPy 1.x and 2.x
  87. if int(np.version.short_version.split(".", 1)[0]) >= 2:
  88. with pytest.raises(ValueError, match="A copy is always created"):
  89. np.array(coord_seq, copy=False)
  90. else:
  91. assert np.array(coord_seq, copy=False) is np.array(coord_seq, copy=False)
  92. @pytest.mark.skipif(geos_version < (3, 12, 0), reason="GEOS < 3.12")
  93. def test_coords_with_m():
  94. assert point_m.coords[:] == [(2.0, 3.0, 5.0)]
  95. assert point_zm.coords[:] == [(2.0, 3.0, 4.0, 5.0)]
  96. assert line_string_m.coords[:] == [
  97. (0.0, 0.0, 1.0),
  98. (1.0, 0.0, 2.0),
  99. (1.0, 1.0, 3.0),
  100. ]
  101. assert line_string_zm.coords[:] == [
  102. (0.0, 0.0, 4.0, 1.0),
  103. (1.0, 0.0, 4.0, 2.0),
  104. (1.0, 1.0, 4.0, 3.0),
  105. ]