test_transform.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import unittest
  2. import pytest
  3. from shapely import geometry
  4. from shapely.ops import transform
  5. class IdentityTestCase(unittest.TestCase):
  6. """New geometry/coordseq method 'xy' makes numpy interop easier"""
  7. def func(self, x, y, z=None):
  8. return tuple(c for c in [x, y, z] if c)
  9. def test_empty(self):
  10. g = geometry.Point()
  11. h = transform(self.func, g)
  12. assert h.is_empty
  13. def test_point(self):
  14. g = geometry.Point(0, 1)
  15. h = transform(self.func, g)
  16. assert h.geom_type == "Point"
  17. assert list(h.coords) == [(0, 1)]
  18. def test_line(self):
  19. g = geometry.LineString([(0, 1), (2, 3)])
  20. h = transform(self.func, g)
  21. assert h.geom_type == "LineString"
  22. assert list(h.coords) == [(0, 1), (2, 3)]
  23. def test_linearring(self):
  24. g = geometry.LinearRing([(0, 1), (2, 3), (2, 2), (0, 1)])
  25. h = transform(self.func, g)
  26. assert h.geom_type == "LinearRing"
  27. assert list(h.coords) == [(0, 1), (2, 3), (2, 2), (0, 1)]
  28. def test_polygon(self):
  29. g = geometry.Point(0, 1).buffer(1.0)
  30. h = transform(self.func, g)
  31. assert h.geom_type == "Polygon"
  32. assert g.area == pytest.approx(h.area)
  33. def test_multipolygon(self):
  34. g = geometry.MultiPoint([(0, 1), (0, 4)]).buffer(1.0)
  35. h = transform(self.func, g)
  36. assert h.geom_type == "MultiPolygon"
  37. assert g.area == pytest.approx(h.area)
  38. class LambdaTestCase(unittest.TestCase):
  39. """New geometry/coordseq method 'xy' makes numpy interop easier"""
  40. def test_point(self):
  41. g = geometry.Point(0, 1)
  42. h = transform(lambda x, y, z=None: (x + 1.0, y + 1.0), g)
  43. assert h.geom_type == "Point"
  44. assert list(h.coords) == [(1.0, 2.0)]
  45. def test_line(self):
  46. g = geometry.LineString([(0, 1), (2, 3)])
  47. h = transform(lambda x, y, z=None: (x + 1.0, y + 1.0), g)
  48. assert h.geom_type == "LineString"
  49. assert list(h.coords) == [(1.0, 2.0), (3.0, 4.0)]
  50. def test_polygon(self):
  51. g = geometry.Point(0, 1).buffer(1.0)
  52. h = transform(lambda x, y, z=None: (x + 1.0, y + 1.0), g)
  53. assert h.geom_type == "Polygon"
  54. assert g.area == pytest.approx(h.area)
  55. assert h.centroid.x == pytest.approx(1.0)
  56. assert h.centroid.y == pytest.approx(2.0)
  57. def test_multipolygon(self):
  58. g = geometry.MultiPoint([(0, 1), (0, 4)]).buffer(1.0)
  59. h = transform(lambda x, y, z=None: (x + 1.0, y + 1.0), g)
  60. assert h.geom_type == "MultiPolygon"
  61. assert g.area == pytest.approx(h.area)
  62. assert h.centroid.x == pytest.approx(1.0)
  63. assert h.centroid.y == pytest.approx(3.5)