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