PageRenderTime 35ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/gis/geos/tests/test_geos_mutation.py

https://code.google.com/p/mango-py/
Python | 137 lines | 100 code | 25 blank | 12 comment | 14 complexity | 715d240b2b20783c29940bd5af0aa160 MD5 | raw file
Possible License(s): BSD-3-Clause
  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 django.utils.copycompat as copy
  5. from django.contrib.gis.geos import *
  6. from django.contrib.gis.geos.error import GEOSIndexError
  7. from django.utils import unittest
  8. def getItem(o,i): return o[i]
  9. def delItem(o,i): del o[i]
  10. def setItem(o,i,v): o[i] = v
  11. def api_get_distance(x): return x.distance(Point(-200,-200))
  12. def api_get_buffer(x): return x.buffer(10)
  13. def api_get_geom_typeid(x): return x.geom_typeid
  14. def api_get_num_coords(x): return x.num_coords
  15. def api_get_centroid(x): return x.centroid
  16. def api_get_empty(x): return x.empty
  17. def api_get_valid(x): return x.valid
  18. def api_get_simple(x): return x.simple
  19. def api_get_ring(x): return x.ring
  20. def api_get_boundary(x): return x.boundary
  21. def api_get_convex_hull(x): return x.convex_hull
  22. def api_get_extent(x): return x.extent
  23. def api_get_area(x): return x.area
  24. def api_get_length(x): return x.length
  25. geos_function_tests = [ val for name, val in vars().items()
  26. if hasattr(val, '__call__')
  27. and name.startswith('api_get_') ]
  28. class GEOSMutationTest(unittest.TestCase):
  29. """
  30. Tests Pythonic Mutability of Python GEOS geometry wrappers
  31. get/set/delitem on a slice, normal list methods
  32. """
  33. def test00_GEOSIndexException(self):
  34. 'Testing Geometry GEOSIndexError'
  35. p = Point(1,2)
  36. for i in range(-2,2): p._checkindex(i)
  37. self.assertRaises(GEOSIndexError, p._checkindex, 2)
  38. self.assertRaises(GEOSIndexError, p._checkindex, -3)
  39. def test01_PointMutations(self):
  40. 'Testing Point mutations'
  41. for p in (Point(1,2,3), fromstr('POINT (1 2 3)')):
  42. self.assertEqual(p._get_single_external(1), 2.0, 'Point _get_single_external')
  43. # _set_single
  44. p._set_single(0,100)
  45. self.assertEqual(p.coords, (100.0,2.0,3.0), 'Point _set_single')
  46. # _set_list
  47. p._set_list(2,(50,3141))
  48. self.assertEqual(p.coords, (50.0,3141.0), 'Point _set_list')
  49. def test02_PointExceptions(self):
  50. 'Testing Point exceptions'
  51. self.assertRaises(TypeError, Point, range(1))
  52. self.assertRaises(TypeError, Point, range(4))
  53. def test03_PointApi(self):
  54. 'Testing Point API'
  55. q = Point(4,5,3)
  56. for p in (Point(1,2,3), fromstr('POINT (1 2 3)')):
  57. p[0:2] = [4,5]
  58. for f in geos_function_tests:
  59. self.assertEqual(f(q), f(p), 'Point ' + f.__name__)
  60. def test04_LineStringMutations(self):
  61. 'Testing LineString mutations'
  62. for ls in (LineString((1,0),(4,1),(6,-1)),
  63. fromstr('LINESTRING (1 0,4 1,6 -1)')):
  64. self.assertEqual(ls._get_single_external(1), (4.0,1.0), 'LineString _get_single_external')
  65. # _set_single
  66. ls._set_single(0,(-50,25))
  67. self.assertEqual(ls.coords, ((-50.0,25.0),(4.0,1.0),(6.0,-1.0)), 'LineString _set_single')
  68. # _set_list
  69. ls._set_list(2, ((-50.0,25.0),(6.0,-1.0)))
  70. self.assertEqual(ls.coords, ((-50.0,25.0),(6.0,-1.0)), 'LineString _set_list')
  71. lsa = LineString(ls.coords)
  72. for f in geos_function_tests:
  73. self.assertEqual(f(lsa), f(ls), 'LineString ' + f.__name__)
  74. def test05_Polygon(self):
  75. 'Testing Polygon mutations'
  76. for pg in (Polygon(((1,0),(4,1),(6,-1),(8,10),(1,0)),
  77. ((5,4),(6,4),(6,3),(5,4))),
  78. fromstr('POLYGON ((1 0,4 1,6 -1,8 10,1 0),(5 4,6 4,6 3,5 4))')):
  79. self.assertEqual(pg._get_single_external(0),
  80. LinearRing((1,0),(4,1),(6,-1),(8,10),(1,0)),
  81. 'Polygon _get_single_external(0)')
  82. self.assertEqual(pg._get_single_external(1),
  83. LinearRing((5,4),(6,4),(6,3),(5,4)),
  84. 'Polygon _get_single_external(1)')
  85. # _set_list
  86. pg._set_list(2, (((1,2),(10,0),(12,9),(-1,15),(1,2)),
  87. ((4,2),(5,2),(5,3),(4,2))))
  88. self.assertEqual(pg.coords,
  89. (((1.0,2.0),(10.0,0.0),(12.0,9.0),(-1.0,15.0),(1.0,2.0)),
  90. ((4.0,2.0),(5.0,2.0),(5.0,3.0),(4.0,2.0))),
  91. 'Polygon _set_list')
  92. lsa = Polygon(*pg.coords)
  93. for f in geos_function_tests:
  94. self.assertEqual(f(lsa), f(pg), 'Polygon ' + f.__name__)
  95. def test06_Collection(self):
  96. 'Testing Collection mutations'
  97. for mp in (MultiPoint(*map(Point,((3,4),(-1,2),(5,-4),(2,8)))),
  98. fromstr('MULTIPOINT (3 4,-1 2,5 -4,2 8)')):
  99. self.assertEqual(mp._get_single_external(2), Point(5,-4), 'Collection _get_single_external')
  100. mp._set_list(3, map(Point,((5,5),(3,-2),(8,1))))
  101. self.assertEqual(mp.coords, ((5.0,5.0),(3.0,-2.0),(8.0,1.0)), 'Collection _set_list')
  102. lsa = MultiPoint(*map(Point,((5,5),(3,-2),(8,1))))
  103. for f in geos_function_tests:
  104. self.assertEqual(f(lsa), f(mp), 'MultiPoint ' + f.__name__)
  105. def suite():
  106. s = unittest.TestSuite()
  107. s.addTest(unittest.makeSuite(GEOSMutationTest))
  108. return s
  109. def run(verbosity=2):
  110. unittest.TextTestRunner(verbosity=verbosity).run(suite())
  111. if __name__ == '__main__':
  112. run()