test_geos_mutation.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # Copyright (c) 2008-2009 Aryeh Leib Taurog, all rights reserved.
  2. # Modified from original contribution by Aryeh Leib Taurog, which was
  3. # released under the New BSD license.
  4. import unittest
  5. from unittest import skipUnless
  6. from django.contrib.gis.geos import HAS_GEOS
  7. if HAS_GEOS:
  8. from django.contrib.gis.geos import (
  9. fromstr, LinearRing, LineString, MultiPoint, Point, Polygon,
  10. )
  11. from django.contrib.gis.geos.error import GEOSIndexError
  12. if HAS_GEOS:
  13. def api_get_distance(x):
  14. return x.distance(Point(-200, -200))
  15. def api_get_buffer(x):
  16. return x.buffer(10)
  17. def api_get_geom_typeid(x):
  18. return x.geom_typeid
  19. def api_get_num_coords(x):
  20. return x.num_coords
  21. def api_get_centroid(x):
  22. return x.centroid
  23. def api_get_empty(x):
  24. return x.empty
  25. def api_get_valid(x):
  26. return x.valid
  27. def api_get_simple(x):
  28. return x.simple
  29. def api_get_ring(x):
  30. return x.ring
  31. def api_get_boundary(x):
  32. return x.boundary
  33. def api_get_convex_hull(x):
  34. return x.convex_hull
  35. def api_get_extent(x):
  36. return x.extent
  37. def api_get_area(x):
  38. return x.area
  39. def api_get_length(x):
  40. return x.length
  41. geos_function_tests = [val for name, val in vars().items()
  42. if hasattr(val, '__call__')
  43. and name.startswith('api_get_')]
  44. @skipUnless(HAS_GEOS, "Geos is required.")
  45. class GEOSMutationTest(unittest.TestCase):
  46. """
  47. Tests Pythonic Mutability of Python GEOS geometry wrappers
  48. get/set/delitem on a slice, normal list methods
  49. """
  50. def test00_GEOSIndexException(self):
  51. 'Testing Geometry GEOSIndexError'
  52. p = Point(1, 2)
  53. for i in range(-2, 2):
  54. p._checkindex(i)
  55. self.assertRaises(GEOSIndexError, p._checkindex, 2)
  56. self.assertRaises(GEOSIndexError, p._checkindex, -3)
  57. def test01_PointMutations(self):
  58. 'Testing Point mutations'
  59. for p in (Point(1, 2, 3), fromstr('POINT (1 2 3)')):
  60. self.assertEqual(p._get_single_external(1), 2.0, 'Point _get_single_external')
  61. # _set_single
  62. p._set_single(0, 100)
  63. self.assertEqual(p.coords, (100.0, 2.0, 3.0), 'Point _set_single')
  64. # _set_list
  65. p._set_list(2, (50, 3141))
  66. self.assertEqual(p.coords, (50.0, 3141.0), 'Point _set_list')
  67. def test02_PointExceptions(self):
  68. 'Testing Point exceptions'
  69. self.assertRaises(TypeError, Point, range(1))
  70. self.assertRaises(TypeError, Point, range(4))
  71. def test03_PointApi(self):
  72. 'Testing Point API'
  73. q = Point(4, 5, 3)
  74. for p in (Point(1, 2, 3), fromstr('POINT (1 2 3)')):
  75. p[0:2] = [4, 5]
  76. for f in geos_function_tests:
  77. self.assertEqual(f(q), f(p), 'Point ' + f.__name__)
  78. def test04_LineStringMutations(self):
  79. 'Testing LineString mutations'
  80. for ls in (LineString((1, 0), (4, 1), (6, -1)),
  81. fromstr('LINESTRING (1 0,4 1,6 -1)')):
  82. self.assertEqual(ls._get_single_external(1), (4.0, 1.0), 'LineString _get_single_external')
  83. # _set_single
  84. ls._set_single(0, (-50, 25))
  85. self.assertEqual(ls.coords, ((-50.0, 25.0), (4.0, 1.0), (6.0, -1.0)), 'LineString _set_single')
  86. # _set_list
  87. ls._set_list(2, ((-50.0, 25.0), (6.0, -1.0)))
  88. self.assertEqual(ls.coords, ((-50.0, 25.0), (6.0, -1.0)), 'LineString _set_list')
  89. lsa = LineString(ls.coords)
  90. for f in geos_function_tests:
  91. self.assertEqual(f(lsa), f(ls), 'LineString ' + f.__name__)
  92. def test05_Polygon(self):
  93. 'Testing Polygon mutations'
  94. for pg in (Polygon(((1, 0), (4, 1), (6, -1), (8, 10), (1, 0)),
  95. ((5, 4), (6, 4), (6, 3), (5, 4))),
  96. fromstr('POLYGON ((1 0,4 1,6 -1,8 10,1 0),(5 4,6 4,6 3,5 4))')):
  97. self.assertEqual(pg._get_single_external(0),
  98. LinearRing((1, 0), (4, 1), (6, -1), (8, 10), (1, 0)),
  99. 'Polygon _get_single_external(0)')
  100. self.assertEqual(pg._get_single_external(1),
  101. LinearRing((5, 4), (6, 4), (6, 3), (5, 4)),
  102. 'Polygon _get_single_external(1)')
  103. # _set_list
  104. pg._set_list(2, (((1, 2), (10, 0), (12, 9), (-1, 15), (1, 2)),
  105. ((4, 2), (5, 2), (5, 3), (4, 2))))
  106. self.assertEqual(
  107. pg.coords,
  108. (((1.0, 2.0), (10.0, 0.0), (12.0, 9.0), (-1.0, 15.0), (1.0, 2.0)),
  109. ((4.0, 2.0), (5.0, 2.0), (5.0, 3.0), (4.0, 2.0))),
  110. 'Polygon _set_list')
  111. lsa = Polygon(*pg.coords)
  112. for f in geos_function_tests:
  113. self.assertEqual(f(lsa), f(pg), 'Polygon ' + f.__name__)
  114. def test06_Collection(self):
  115. 'Testing Collection mutations'
  116. for mp in (MultiPoint(*map(Point, ((3, 4), (-1, 2), (5, -4), (2, 8)))),
  117. fromstr('MULTIPOINT (3 4,-1 2,5 -4,2 8)')):
  118. self.assertEqual(mp._get_single_external(2), Point(5, -4), 'Collection _get_single_external')
  119. mp._set_list(3, map(Point, ((5, 5), (3, -2), (8, 1))))
  120. self.assertEqual(mp.coords, ((5.0, 5.0), (3.0, -2.0), (8.0, 1.0)), 'Collection _set_list')
  121. lsa = MultiPoint(*map(Point, ((5, 5), (3, -2), (8, 1))))
  122. for f in geos_function_tests:
  123. self.assertEqual(f(lsa), f(mp), 'MultiPoint ' + f.__name__)