test_ndarrays.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # Tests of support for Numpy ndarrays. See
  2. # https://github.com/sgillies/shapely/issues/26 for discussion.
  3. import unittest
  4. from functools import reduce
  5. import numpy as np
  6. from shapely import geometry
  7. class TransposeTestCase(unittest.TestCase):
  8. def test_multipoint(self):
  9. arr = np.array([[1.0, 1.0, 2.0, 2.0, 1.0], [3.0, 4.0, 4.0, 3.0, 3.0]])
  10. tarr = arr.T
  11. shape = geometry.MultiPoint(tarr)
  12. coords = reduce(lambda x, y: x + y, [list(g.coords) for g in shape.geoms])
  13. assert coords == [(1.0, 3.0), (1.0, 4.0), (2.0, 4.0), (2.0, 3.0), (1.0, 3.0)]
  14. def test_linestring(self):
  15. a = np.array([[1.0, 1.0, 2.0, 2.0, 1.0], [3.0, 4.0, 4.0, 3.0, 3.0]])
  16. t = a.T
  17. s = geometry.LineString(t)
  18. assert list(s.coords) == [
  19. (1.0, 3.0),
  20. (1.0, 4.0),
  21. (2.0, 4.0),
  22. (2.0, 3.0),
  23. (1.0, 3.0),
  24. ]
  25. def test_polygon(self):
  26. a = np.array([[1.0, 1.0, 2.0, 2.0, 1.0], [3.0, 4.0, 4.0, 3.0, 3.0]])
  27. t = a.T
  28. s = geometry.Polygon(t)
  29. assert list(s.exterior.coords) == [
  30. (1.0, 3.0),
  31. (1.0, 4.0),
  32. (2.0, 4.0),
  33. (2.0, 3.0),
  34. (1.0, 3.0),
  35. ]