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