|
1 import random, unittest, sys |
|
2 from ctypes import ArgumentError |
|
3 from django.contrib.gis.geos import * |
|
4 from django.contrib.gis.geos.base import HAS_GDAL |
|
5 from django.contrib.gis.tests.geometries import * |
|
6 |
|
7 if HAS_NUMPY: from numpy import array |
|
8 if HAS_GDAL: from django.contrib.gis.gdal import OGRGeometry, SpatialReference, CoordTransform, GEOJSON |
|
9 |
|
10 class GEOSTest(unittest.TestCase): |
|
11 |
|
12 @property |
|
13 def null_srid(self): |
|
14 """ |
|
15 Returns the proper null SRID depending on the GEOS version. |
|
16 See the comments in `test15_srid` for more details. |
|
17 """ |
|
18 info = geos_version_info() |
|
19 if info['version'] == '3.0.0' and info['release_candidate']: |
|
20 return -1 |
|
21 else: |
|
22 return None |
|
23 |
|
24 def test01a_wkt(self): |
|
25 "Testing WKT output." |
|
26 for g in wkt_out: |
|
27 geom = fromstr(g.wkt) |
|
28 self.assertEqual(g.ewkt, geom.wkt) |
|
29 |
|
30 def test01b_hex(self): |
|
31 "Testing HEX output." |
|
32 for g in hex_wkt: |
|
33 geom = fromstr(g.wkt) |
|
34 self.assertEqual(g.hex, geom.hex) |
|
35 |
|
36 def test01c_kml(self): |
|
37 "Testing KML output." |
|
38 for tg in wkt_out: |
|
39 geom = fromstr(tg.wkt) |
|
40 kml = getattr(tg, 'kml', False) |
|
41 if kml: self.assertEqual(kml, geom.kml) |
|
42 |
|
43 def test01d_errors(self): |
|
44 "Testing the Error handlers." |
|
45 # string-based |
|
46 print "\nBEGIN - expecting GEOS_ERROR; safe to ignore.\n" |
|
47 for err in errors: |
|
48 try: |
|
49 g = fromstr(err.wkt) |
|
50 except (GEOSException, ValueError): |
|
51 pass |
|
52 print "\nEND - expecting GEOS_ERROR; safe to ignore.\n" |
|
53 |
|
54 class NotAGeometry(object): |
|
55 pass |
|
56 |
|
57 # Some other object |
|
58 self.assertRaises(TypeError, GEOSGeometry, NotAGeometry()) |
|
59 # None |
|
60 self.assertRaises(TypeError, GEOSGeometry, None) |
|
61 # Bad WKB |
|
62 self.assertRaises(GEOSException, GEOSGeometry, buffer('0')) |
|
63 |
|
64 def test01e_wkb(self): |
|
65 "Testing WKB output." |
|
66 from binascii import b2a_hex |
|
67 for g in hex_wkt: |
|
68 geom = fromstr(g.wkt) |
|
69 wkb = geom.wkb |
|
70 self.assertEqual(b2a_hex(wkb).upper(), g.hex) |
|
71 |
|
72 def test01f_create_hex(self): |
|
73 "Testing creation from HEX." |
|
74 for g in hex_wkt: |
|
75 geom_h = GEOSGeometry(g.hex) |
|
76 # we need to do this so decimal places get normalised |
|
77 geom_t = fromstr(g.wkt) |
|
78 self.assertEqual(geom_t.wkt, geom_h.wkt) |
|
79 |
|
80 def test01g_create_wkb(self): |
|
81 "Testing creation from WKB." |
|
82 from binascii import a2b_hex |
|
83 for g in hex_wkt: |
|
84 wkb = buffer(a2b_hex(g.hex)) |
|
85 geom_h = GEOSGeometry(wkb) |
|
86 # we need to do this so decimal places get normalised |
|
87 geom_t = fromstr(g.wkt) |
|
88 self.assertEqual(geom_t.wkt, geom_h.wkt) |
|
89 |
|
90 def test01h_ewkt(self): |
|
91 "Testing EWKT." |
|
92 srid = 32140 |
|
93 for p in polygons: |
|
94 ewkt = 'SRID=%d;%s' % (srid, p.wkt) |
|
95 poly = fromstr(ewkt) |
|
96 self.assertEqual(srid, poly.srid) |
|
97 self.assertEqual(srid, poly.shell.srid) |
|
98 self.assertEqual(srid, fromstr(poly.ewkt).srid) # Checking export |
|
99 |
|
100 def test01i_json(self): |
|
101 "Testing GeoJSON input/output (via GDAL)." |
|
102 if not HAS_GDAL or not GEOJSON: return |
|
103 for g in json_geoms: |
|
104 geom = GEOSGeometry(g.wkt) |
|
105 self.assertEqual(g.json, geom.json) |
|
106 self.assertEqual(g.json, geom.geojson) |
|
107 self.assertEqual(GEOSGeometry(g.wkt), GEOSGeometry(geom.json)) |
|
108 |
|
109 def test01j_eq(self): |
|
110 "Testing equivalence." |
|
111 p = fromstr('POINT(5 23)') |
|
112 self.assertEqual(p, p.wkt) |
|
113 self.assertNotEqual(p, 'foo') |
|
114 ls = fromstr('LINESTRING(0 0, 1 1, 5 5)') |
|
115 self.assertEqual(ls, ls.wkt) |
|
116 self.assertNotEqual(p, 'bar') |
|
117 # Error shouldn't be raise on equivalence testing with |
|
118 # an invalid type. |
|
119 for g in (p, ls): |
|
120 self.assertNotEqual(g, None) |
|
121 self.assertNotEqual(g, {'foo' : 'bar'}) |
|
122 self.assertNotEqual(g, False) |
|
123 |
|
124 def test02a_points(self): |
|
125 "Testing Point objects." |
|
126 prev = fromstr('POINT(0 0)') |
|
127 for p in points: |
|
128 # Creating the point from the WKT |
|
129 pnt = fromstr(p.wkt) |
|
130 self.assertEqual(pnt.geom_type, 'Point') |
|
131 self.assertEqual(pnt.geom_typeid, 0) |
|
132 self.assertEqual(p.x, pnt.x) |
|
133 self.assertEqual(p.y, pnt.y) |
|
134 self.assertEqual(True, pnt == fromstr(p.wkt)) |
|
135 self.assertEqual(False, pnt == prev) |
|
136 |
|
137 # Making sure that the point's X, Y components are what we expect |
|
138 self.assertAlmostEqual(p.x, pnt.tuple[0], 9) |
|
139 self.assertAlmostEqual(p.y, pnt.tuple[1], 9) |
|
140 |
|
141 # Testing the third dimension, and getting the tuple arguments |
|
142 if hasattr(p, 'z'): |
|
143 self.assertEqual(True, pnt.hasz) |
|
144 self.assertEqual(p.z, pnt.z) |
|
145 self.assertEqual(p.z, pnt.tuple[2], 9) |
|
146 tup_args = (p.x, p.y, p.z) |
|
147 set_tup1 = (2.71, 3.14, 5.23) |
|
148 set_tup2 = (5.23, 2.71, 3.14) |
|
149 else: |
|
150 self.assertEqual(False, pnt.hasz) |
|
151 self.assertEqual(None, pnt.z) |
|
152 tup_args = (p.x, p.y) |
|
153 set_tup1 = (2.71, 3.14) |
|
154 set_tup2 = (3.14, 2.71) |
|
155 |
|
156 # Centroid operation on point should be point itself |
|
157 self.assertEqual(p.centroid, pnt.centroid.tuple) |
|
158 |
|
159 # Now testing the different constructors |
|
160 pnt2 = Point(tup_args) # e.g., Point((1, 2)) |
|
161 pnt3 = Point(*tup_args) # e.g., Point(1, 2) |
|
162 self.assertEqual(True, pnt == pnt2) |
|
163 self.assertEqual(True, pnt == pnt3) |
|
164 |
|
165 # Now testing setting the x and y |
|
166 pnt.y = 3.14 |
|
167 pnt.x = 2.71 |
|
168 self.assertEqual(3.14, pnt.y) |
|
169 self.assertEqual(2.71, pnt.x) |
|
170 |
|
171 # Setting via the tuple/coords property |
|
172 pnt.tuple = set_tup1 |
|
173 self.assertEqual(set_tup1, pnt.tuple) |
|
174 pnt.coords = set_tup2 |
|
175 self.assertEqual(set_tup2, pnt.coords) |
|
176 |
|
177 prev = pnt # setting the previous geometry |
|
178 |
|
179 def test02b_multipoints(self): |
|
180 "Testing MultiPoint objects." |
|
181 for mp in multipoints: |
|
182 mpnt = fromstr(mp.wkt) |
|
183 self.assertEqual(mpnt.geom_type, 'MultiPoint') |
|
184 self.assertEqual(mpnt.geom_typeid, 4) |
|
185 |
|
186 self.assertAlmostEqual(mp.centroid[0], mpnt.centroid.tuple[0], 9) |
|
187 self.assertAlmostEqual(mp.centroid[1], mpnt.centroid.tuple[1], 9) |
|
188 |
|
189 self.assertRaises(GEOSIndexError, mpnt.__getitem__, len(mpnt)) |
|
190 self.assertEqual(mp.centroid, mpnt.centroid.tuple) |
|
191 self.assertEqual(mp.points, tuple(m.tuple for m in mpnt)) |
|
192 for p in mpnt: |
|
193 self.assertEqual(p.geom_type, 'Point') |
|
194 self.assertEqual(p.geom_typeid, 0) |
|
195 self.assertEqual(p.empty, False) |
|
196 self.assertEqual(p.valid, True) |
|
197 |
|
198 def test03a_linestring(self): |
|
199 "Testing LineString objects." |
|
200 prev = fromstr('POINT(0 0)') |
|
201 for l in linestrings: |
|
202 ls = fromstr(l.wkt) |
|
203 self.assertEqual(ls.geom_type, 'LineString') |
|
204 self.assertEqual(ls.geom_typeid, 1) |
|
205 self.assertEqual(ls.empty, False) |
|
206 self.assertEqual(ls.ring, False) |
|
207 if hasattr(l, 'centroid'): |
|
208 self.assertEqual(l.centroid, ls.centroid.tuple) |
|
209 if hasattr(l, 'tup'): |
|
210 self.assertEqual(l.tup, ls.tuple) |
|
211 |
|
212 self.assertEqual(True, ls == fromstr(l.wkt)) |
|
213 self.assertEqual(False, ls == prev) |
|
214 self.assertRaises(GEOSIndexError, ls.__getitem__, len(ls)) |
|
215 prev = ls |
|
216 |
|
217 # Creating a LineString from a tuple, list, and numpy array |
|
218 self.assertEqual(ls, LineString(ls.tuple)) # tuple |
|
219 self.assertEqual(ls, LineString(*ls.tuple)) # as individual arguments |
|
220 self.assertEqual(ls, LineString([list(tup) for tup in ls.tuple])) # as list |
|
221 self.assertEqual(ls.wkt, LineString(*tuple(Point(tup) for tup in ls.tuple)).wkt) # Point individual arguments |
|
222 if HAS_NUMPY: self.assertEqual(ls, LineString(array(ls.tuple))) # as numpy array |
|
223 |
|
224 def test03b_multilinestring(self): |
|
225 "Testing MultiLineString objects." |
|
226 prev = fromstr('POINT(0 0)') |
|
227 for l in multilinestrings: |
|
228 ml = fromstr(l.wkt) |
|
229 self.assertEqual(ml.geom_type, 'MultiLineString') |
|
230 self.assertEqual(ml.geom_typeid, 5) |
|
231 |
|
232 self.assertAlmostEqual(l.centroid[0], ml.centroid.x, 9) |
|
233 self.assertAlmostEqual(l.centroid[1], ml.centroid.y, 9) |
|
234 |
|
235 self.assertEqual(True, ml == fromstr(l.wkt)) |
|
236 self.assertEqual(False, ml == prev) |
|
237 prev = ml |
|
238 |
|
239 for ls in ml: |
|
240 self.assertEqual(ls.geom_type, 'LineString') |
|
241 self.assertEqual(ls.geom_typeid, 1) |
|
242 self.assertEqual(ls.empty, False) |
|
243 |
|
244 self.assertRaises(GEOSIndexError, ml.__getitem__, len(ml)) |
|
245 self.assertEqual(ml.wkt, MultiLineString(*tuple(s.clone() for s in ml)).wkt) |
|
246 self.assertEqual(ml, MultiLineString(*tuple(LineString(s.tuple) for s in ml))) |
|
247 |
|
248 def test04_linearring(self): |
|
249 "Testing LinearRing objects." |
|
250 for rr in linearrings: |
|
251 lr = fromstr(rr.wkt) |
|
252 self.assertEqual(lr.geom_type, 'LinearRing') |
|
253 self.assertEqual(lr.geom_typeid, 2) |
|
254 self.assertEqual(rr.n_p, len(lr)) |
|
255 self.assertEqual(True, lr.valid) |
|
256 self.assertEqual(False, lr.empty) |
|
257 |
|
258 # Creating a LinearRing from a tuple, list, and numpy array |
|
259 self.assertEqual(lr, LinearRing(lr.tuple)) |
|
260 self.assertEqual(lr, LinearRing(*lr.tuple)) |
|
261 self.assertEqual(lr, LinearRing([list(tup) for tup in lr.tuple])) |
|
262 if HAS_NUMPY: self.assertEqual(lr, LinearRing(array(lr.tuple))) |
|
263 |
|
264 def test05a_polygons(self): |
|
265 "Testing Polygon objects." |
|
266 prev = fromstr('POINT(0 0)') |
|
267 for p in polygons: |
|
268 # Creating the Polygon, testing its properties. |
|
269 poly = fromstr(p.wkt) |
|
270 self.assertEqual(poly.geom_type, 'Polygon') |
|
271 self.assertEqual(poly.geom_typeid, 3) |
|
272 self.assertEqual(poly.empty, False) |
|
273 self.assertEqual(poly.ring, False) |
|
274 self.assertEqual(p.n_i, poly.num_interior_rings) |
|
275 self.assertEqual(p.n_i + 1, len(poly)) # Testing __len__ |
|
276 self.assertEqual(p.n_p, poly.num_points) |
|
277 |
|
278 # Area & Centroid |
|
279 self.assertAlmostEqual(p.area, poly.area, 9) |
|
280 self.assertAlmostEqual(p.centroid[0], poly.centroid.tuple[0], 9) |
|
281 self.assertAlmostEqual(p.centroid[1], poly.centroid.tuple[1], 9) |
|
282 |
|
283 # Testing the geometry equivalence |
|
284 self.assertEqual(True, poly == fromstr(p.wkt)) |
|
285 self.assertEqual(False, poly == prev) # Should not be equal to previous geometry |
|
286 self.assertEqual(True, poly != prev) |
|
287 |
|
288 # Testing the exterior ring |
|
289 ring = poly.exterior_ring |
|
290 self.assertEqual(ring.geom_type, 'LinearRing') |
|
291 self.assertEqual(ring.geom_typeid, 2) |
|
292 if p.ext_ring_cs: |
|
293 self.assertEqual(p.ext_ring_cs, ring.tuple) |
|
294 self.assertEqual(p.ext_ring_cs, poly[0].tuple) # Testing __getitem__ |
|
295 |
|
296 # Testing __getitem__ and __setitem__ on invalid indices |
|
297 self.assertRaises(GEOSIndexError, poly.__getitem__, len(poly)) |
|
298 self.assertRaises(GEOSIndexError, poly.__setitem__, len(poly), False) |
|
299 self.assertRaises(GEOSIndexError, poly.__getitem__, -1) |
|
300 |
|
301 # Testing __iter__ |
|
302 for r in poly: |
|
303 self.assertEqual(r.geom_type, 'LinearRing') |
|
304 self.assertEqual(r.geom_typeid, 2) |
|
305 |
|
306 # Testing polygon construction. |
|
307 self.assertRaises(TypeError, Polygon.__init__, 0, [1, 2, 3]) |
|
308 self.assertRaises(TypeError, Polygon.__init__, 'foo') |
|
309 |
|
310 # Polygon(shell, (hole1, ... holeN)) |
|
311 rings = tuple(r for r in poly) |
|
312 self.assertEqual(poly, Polygon(rings[0], rings[1:])) |
|
313 |
|
314 # Polygon(shell_tuple, hole_tuple1, ... , hole_tupleN) |
|
315 ring_tuples = tuple(r.tuple for r in poly) |
|
316 self.assertEqual(poly, Polygon(*ring_tuples)) |
|
317 |
|
318 # Constructing with tuples of LinearRings. |
|
319 self.assertEqual(poly.wkt, Polygon(*tuple(r for r in poly)).wkt) |
|
320 self.assertEqual(poly.wkt, Polygon(*tuple(LinearRing(r.tuple) for r in poly)).wkt) |
|
321 |
|
322 def test05b_multipolygons(self): |
|
323 "Testing MultiPolygon objects." |
|
324 print "\nBEGIN - expecting GEOS_NOTICE; safe to ignore.\n" |
|
325 prev = fromstr('POINT (0 0)') |
|
326 for mp in multipolygons: |
|
327 mpoly = fromstr(mp.wkt) |
|
328 self.assertEqual(mpoly.geom_type, 'MultiPolygon') |
|
329 self.assertEqual(mpoly.geom_typeid, 6) |
|
330 self.assertEqual(mp.valid, mpoly.valid) |
|
331 |
|
332 if mp.valid: |
|
333 self.assertEqual(mp.num_geom, mpoly.num_geom) |
|
334 self.assertEqual(mp.n_p, mpoly.num_coords) |
|
335 self.assertEqual(mp.num_geom, len(mpoly)) |
|
336 self.assertRaises(GEOSIndexError, mpoly.__getitem__, len(mpoly)) |
|
337 for p in mpoly: |
|
338 self.assertEqual(p.geom_type, 'Polygon') |
|
339 self.assertEqual(p.geom_typeid, 3) |
|
340 self.assertEqual(p.valid, True) |
|
341 self.assertEqual(mpoly.wkt, MultiPolygon(*tuple(poly.clone() for poly in mpoly)).wkt) |
|
342 |
|
343 print "\nEND - expecting GEOS_NOTICE; safe to ignore.\n" |
|
344 |
|
345 def test06a_memory_hijinks(self): |
|
346 "Testing Geometry __del__() on rings and polygons." |
|
347 #### Memory issues with rings and polygons |
|
348 |
|
349 # These tests are needed to ensure sanity with writable geometries. |
|
350 |
|
351 # Getting a polygon with interior rings, and pulling out the interior rings |
|
352 poly = fromstr(polygons[1].wkt) |
|
353 ring1 = poly[0] |
|
354 ring2 = poly[1] |
|
355 |
|
356 # These deletes should be 'harmless' since they are done on child geometries |
|
357 del ring1 |
|
358 del ring2 |
|
359 ring1 = poly[0] |
|
360 ring2 = poly[1] |
|
361 |
|
362 # Deleting the polygon |
|
363 del poly |
|
364 |
|
365 # Access to these rings is OK since they are clones. |
|
366 s1, s2 = str(ring1), str(ring2) |
|
367 |
|
368 # The previous hijinks tests are now moot because only clones are |
|
369 # now used =) |
|
370 |
|
371 def test08_coord_seq(self): |
|
372 "Testing Coordinate Sequence objects." |
|
373 for p in polygons: |
|
374 if p.ext_ring_cs: |
|
375 # Constructing the polygon and getting the coordinate sequence |
|
376 poly = fromstr(p.wkt) |
|
377 cs = poly.exterior_ring.coord_seq |
|
378 |
|
379 self.assertEqual(p.ext_ring_cs, cs.tuple) # done in the Polygon test too. |
|
380 self.assertEqual(len(p.ext_ring_cs), len(cs)) # Making sure __len__ works |
|
381 |
|
382 # Checks __getitem__ and __setitem__ |
|
383 for i in xrange(len(p.ext_ring_cs)): |
|
384 c1 = p.ext_ring_cs[i] # Expected value |
|
385 c2 = cs[i] # Value from coordseq |
|
386 self.assertEqual(c1, c2) |
|
387 |
|
388 # Constructing the test value to set the coordinate sequence with |
|
389 if len(c1) == 2: tset = (5, 23) |
|
390 else: tset = (5, 23, 8) |
|
391 cs[i] = tset |
|
392 |
|
393 # Making sure every set point matches what we expect |
|
394 for j in range(len(tset)): |
|
395 cs[i] = tset |
|
396 self.assertEqual(tset[j], cs[i][j]) |
|
397 |
|
398 def test09_relate_pattern(self): |
|
399 "Testing relate() and relate_pattern()." |
|
400 g = fromstr('POINT (0 0)') |
|
401 self.assertRaises(GEOSException, g.relate_pattern, 0, 'invalid pattern, yo') |
|
402 for i in xrange(len(relate_geoms)): |
|
403 g_tup = relate_geoms[i] |
|
404 a = fromstr(g_tup[0].wkt) |
|
405 b = fromstr(g_tup[1].wkt) |
|
406 pat = g_tup[2] |
|
407 result = g_tup[3] |
|
408 self.assertEqual(result, a.relate_pattern(b, pat)) |
|
409 self.assertEqual(pat, a.relate(b)) |
|
410 |
|
411 def test10_intersection(self): |
|
412 "Testing intersects() and intersection()." |
|
413 for i in xrange(len(topology_geoms)): |
|
414 g_tup = topology_geoms[i] |
|
415 a = fromstr(g_tup[0].wkt) |
|
416 b = fromstr(g_tup[1].wkt) |
|
417 i1 = fromstr(intersect_geoms[i].wkt) |
|
418 self.assertEqual(True, a.intersects(b)) |
|
419 i2 = a.intersection(b) |
|
420 self.assertEqual(i1, i2) |
|
421 self.assertEqual(i1, a & b) # __and__ is intersection operator |
|
422 a &= b # testing __iand__ |
|
423 self.assertEqual(i1, a) |
|
424 |
|
425 def test11_union(self): |
|
426 "Testing union()." |
|
427 for i in xrange(len(topology_geoms)): |
|
428 g_tup = topology_geoms[i] |
|
429 a = fromstr(g_tup[0].wkt) |
|
430 b = fromstr(g_tup[1].wkt) |
|
431 u1 = fromstr(union_geoms[i].wkt) |
|
432 u2 = a.union(b) |
|
433 self.assertEqual(u1, u2) |
|
434 self.assertEqual(u1, a | b) # __or__ is union operator |
|
435 a |= b # testing __ior__ |
|
436 self.assertEqual(u1, a) |
|
437 |
|
438 def test12_difference(self): |
|
439 "Testing difference()." |
|
440 for i in xrange(len(topology_geoms)): |
|
441 g_tup = topology_geoms[i] |
|
442 a = fromstr(g_tup[0].wkt) |
|
443 b = fromstr(g_tup[1].wkt) |
|
444 d1 = fromstr(diff_geoms[i].wkt) |
|
445 d2 = a.difference(b) |
|
446 self.assertEqual(d1, d2) |
|
447 self.assertEqual(d1, a - b) # __sub__ is difference operator |
|
448 a -= b # testing __isub__ |
|
449 self.assertEqual(d1, a) |
|
450 |
|
451 def test13_symdifference(self): |
|
452 "Testing sym_difference()." |
|
453 for i in xrange(len(topology_geoms)): |
|
454 g_tup = topology_geoms[i] |
|
455 a = fromstr(g_tup[0].wkt) |
|
456 b = fromstr(g_tup[1].wkt) |
|
457 d1 = fromstr(sdiff_geoms[i].wkt) |
|
458 d2 = a.sym_difference(b) |
|
459 self.assertEqual(d1, d2) |
|
460 self.assertEqual(d1, a ^ b) # __xor__ is symmetric difference operator |
|
461 a ^= b # testing __ixor__ |
|
462 self.assertEqual(d1, a) |
|
463 |
|
464 def test14_buffer(self): |
|
465 "Testing buffer()." |
|
466 for i in xrange(len(buffer_geoms)): |
|
467 g_tup = buffer_geoms[i] |
|
468 g = fromstr(g_tup[0].wkt) |
|
469 |
|
470 # The buffer we expect |
|
471 exp_buf = fromstr(g_tup[1].wkt) |
|
472 |
|
473 # Can't use a floating-point for the number of quadsegs. |
|
474 self.assertRaises(ArgumentError, g.buffer, g_tup[2], float(g_tup[3])) |
|
475 |
|
476 # Constructing our buffer |
|
477 buf = g.buffer(g_tup[2], g_tup[3]) |
|
478 self.assertEqual(exp_buf.num_coords, buf.num_coords) |
|
479 self.assertEqual(len(exp_buf), len(buf)) |
|
480 |
|
481 # Now assuring that each point in the buffer is almost equal |
|
482 for j in xrange(len(exp_buf)): |
|
483 exp_ring = exp_buf[j] |
|
484 buf_ring = buf[j] |
|
485 self.assertEqual(len(exp_ring), len(buf_ring)) |
|
486 for k in xrange(len(exp_ring)): |
|
487 # Asserting the X, Y of each point are almost equal (due to floating point imprecision) |
|
488 self.assertAlmostEqual(exp_ring[k][0], buf_ring[k][0], 9) |
|
489 self.assertAlmostEqual(exp_ring[k][1], buf_ring[k][1], 9) |
|
490 |
|
491 def test15_srid(self): |
|
492 "Testing the SRID property and keyword." |
|
493 # Testing SRID keyword on Point |
|
494 pnt = Point(5, 23, srid=4326) |
|
495 self.assertEqual(4326, pnt.srid) |
|
496 pnt.srid = 3084 |
|
497 self.assertEqual(3084, pnt.srid) |
|
498 self.assertRaises(ArgumentError, pnt.set_srid, '4326') |
|
499 |
|
500 # Testing SRID keyword on fromstr(), and on Polygon rings. |
|
501 poly = fromstr(polygons[1].wkt, srid=4269) |
|
502 self.assertEqual(4269, poly.srid) |
|
503 for ring in poly: self.assertEqual(4269, ring.srid) |
|
504 poly.srid = 4326 |
|
505 self.assertEqual(4326, poly.shell.srid) |
|
506 |
|
507 # Testing SRID keyword on GeometryCollection |
|
508 gc = GeometryCollection(Point(5, 23), LineString((0, 0), (1.5, 1.5), (3, 3)), srid=32021) |
|
509 self.assertEqual(32021, gc.srid) |
|
510 for i in range(len(gc)): self.assertEqual(32021, gc[i].srid) |
|
511 |
|
512 # GEOS may get the SRID from HEXEWKB |
|
513 # 'POINT(5 23)' at SRID=4326 in hex form -- obtained from PostGIS |
|
514 # using `SELECT GeomFromText('POINT (5 23)', 4326);`. |
|
515 hex = '0101000020E610000000000000000014400000000000003740' |
|
516 p1 = fromstr(hex) |
|
517 self.assertEqual(4326, p1.srid) |
|
518 |
|
519 # In GEOS 3.0.0rc1-4 when the EWKB and/or HEXEWKB is exported, |
|
520 # the SRID information is lost and set to -1 -- this is not a |
|
521 # problem on the 3.0.0 version (another reason to upgrade). |
|
522 exp_srid = self.null_srid |
|
523 |
|
524 p2 = fromstr(p1.hex) |
|
525 self.assertEqual(exp_srid, p2.srid) |
|
526 p3 = fromstr(p1.hex, srid=-1) # -1 is intended. |
|
527 self.assertEqual(-1, p3.srid) |
|
528 |
|
529 def test16_mutable_geometries(self): |
|
530 "Testing the mutability of Polygons and Geometry Collections." |
|
531 ### Testing the mutability of Polygons ### |
|
532 for p in polygons: |
|
533 poly = fromstr(p.wkt) |
|
534 |
|
535 # Should only be able to use __setitem__ with LinearRing geometries. |
|
536 self.assertRaises(TypeError, poly.__setitem__, 0, LineString((1, 1), (2, 2))) |
|
537 |
|
538 # Constructing the new shell by adding 500 to every point in the old shell. |
|
539 shell_tup = poly.shell.tuple |
|
540 new_coords = [] |
|
541 for point in shell_tup: new_coords.append((point[0] + 500., point[1] + 500.)) |
|
542 new_shell = LinearRing(*tuple(new_coords)) |
|
543 |
|
544 # Assigning polygon's exterior ring w/the new shell |
|
545 poly.exterior_ring = new_shell |
|
546 s = str(new_shell) # new shell is still accessible |
|
547 self.assertEqual(poly.exterior_ring, new_shell) |
|
548 self.assertEqual(poly[0], new_shell) |
|
549 |
|
550 ### Testing the mutability of Geometry Collections |
|
551 for tg in multipoints: |
|
552 mp = fromstr(tg.wkt) |
|
553 for i in range(len(mp)): |
|
554 # Creating a random point. |
|
555 pnt = mp[i] |
|
556 new = Point(random.randint(1, 100), random.randint(1, 100)) |
|
557 # Testing the assignment |
|
558 mp[i] = new |
|
559 s = str(new) # what was used for the assignment is still accessible |
|
560 self.assertEqual(mp[i], new) |
|
561 self.assertEqual(mp[i].wkt, new.wkt) |
|
562 self.assertNotEqual(pnt, mp[i]) |
|
563 |
|
564 # MultiPolygons involve much more memory management because each |
|
565 # Polygon w/in the collection has its own rings. |
|
566 for tg in multipolygons: |
|
567 mpoly = fromstr(tg.wkt) |
|
568 for i in xrange(len(mpoly)): |
|
569 poly = mpoly[i] |
|
570 old_poly = mpoly[i] |
|
571 # Offsetting the each ring in the polygon by 500. |
|
572 for j in xrange(len(poly)): |
|
573 r = poly[j] |
|
574 for k in xrange(len(r)): r[k] = (r[k][0] + 500., r[k][1] + 500.) |
|
575 poly[j] = r |
|
576 |
|
577 self.assertNotEqual(mpoly[i], poly) |
|
578 # Testing the assignment |
|
579 mpoly[i] = poly |
|
580 s = str(poly) # Still accessible |
|
581 self.assertEqual(mpoly[i], poly) |
|
582 self.assertNotEqual(mpoly[i], old_poly) |
|
583 |
|
584 # Extreme (!!) __setitem__ -- no longer works, have to detect |
|
585 # in the first object that __setitem__ is called in the subsequent |
|
586 # objects -- maybe mpoly[0, 0, 0] = (3.14, 2.71)? |
|
587 #mpoly[0][0][0] = (3.14, 2.71) |
|
588 #self.assertEqual((3.14, 2.71), mpoly[0][0][0]) |
|
589 # Doing it more slowly.. |
|
590 #self.assertEqual((3.14, 2.71), mpoly[0].shell[0]) |
|
591 #del mpoly |
|
592 |
|
593 def test17_threed(self): |
|
594 "Testing three-dimensional geometries." |
|
595 # Testing a 3D Point |
|
596 pnt = Point(2, 3, 8) |
|
597 self.assertEqual((2.,3.,8.), pnt.coords) |
|
598 self.assertRaises(TypeError, pnt.set_coords, (1.,2.)) |
|
599 pnt.coords = (1.,2.,3.) |
|
600 self.assertEqual((1.,2.,3.), pnt.coords) |
|
601 |
|
602 # Testing a 3D LineString |
|
603 ls = LineString((2., 3., 8.), (50., 250., -117.)) |
|
604 self.assertEqual(((2.,3.,8.), (50.,250.,-117.)), ls.tuple) |
|
605 self.assertRaises(TypeError, ls.__setitem__, 0, (1.,2.)) |
|
606 ls[0] = (1.,2.,3.) |
|
607 self.assertEqual((1.,2.,3.), ls[0]) |
|
608 |
|
609 def test18_distance(self): |
|
610 "Testing the distance() function." |
|
611 # Distance to self should be 0. |
|
612 pnt = Point(0, 0) |
|
613 self.assertEqual(0.0, pnt.distance(Point(0, 0))) |
|
614 |
|
615 # Distance should be 1 |
|
616 self.assertEqual(1.0, pnt.distance(Point(0, 1))) |
|
617 |
|
618 # Distance should be ~ sqrt(2) |
|
619 self.assertAlmostEqual(1.41421356237, pnt.distance(Point(1, 1)), 11) |
|
620 |
|
621 # Distances are from the closest vertex in each geometry -- |
|
622 # should be 3 (distance from (2, 2) to (5, 2)). |
|
623 ls1 = LineString((0, 0), (1, 1), (2, 2)) |
|
624 ls2 = LineString((5, 2), (6, 1), (7, 0)) |
|
625 self.assertEqual(3, ls1.distance(ls2)) |
|
626 |
|
627 def test19_length(self): |
|
628 "Testing the length property." |
|
629 # Points have 0 length. |
|
630 pnt = Point(0, 0) |
|
631 self.assertEqual(0.0, pnt.length) |
|
632 |
|
633 # Should be ~ sqrt(2) |
|
634 ls = LineString((0, 0), (1, 1)) |
|
635 self.assertAlmostEqual(1.41421356237, ls.length, 11) |
|
636 |
|
637 # Should be circumfrence of Polygon |
|
638 poly = Polygon(LinearRing((0, 0), (0, 1), (1, 1), (1, 0), (0, 0))) |
|
639 self.assertEqual(4.0, poly.length) |
|
640 |
|
641 # Should be sum of each element's length in collection. |
|
642 mpoly = MultiPolygon(poly.clone(), poly) |
|
643 self.assertEqual(8.0, mpoly.length) |
|
644 |
|
645 def test20_emptyCollections(self): |
|
646 "Testing empty geometries and collections." |
|
647 gc1 = GeometryCollection([]) |
|
648 gc2 = fromstr('GEOMETRYCOLLECTION EMPTY') |
|
649 pnt = fromstr('POINT EMPTY') |
|
650 ls = fromstr('LINESTRING EMPTY') |
|
651 poly = fromstr('POLYGON EMPTY') |
|
652 mls = fromstr('MULTILINESTRING EMPTY') |
|
653 mpoly1 = fromstr('MULTIPOLYGON EMPTY') |
|
654 mpoly2 = MultiPolygon(()) |
|
655 |
|
656 for g in [gc1, gc2, pnt, ls, poly, mls, mpoly1, mpoly2]: |
|
657 self.assertEqual(True, g.empty) |
|
658 |
|
659 # Testing len() and num_geom. |
|
660 if isinstance(g, Polygon): |
|
661 self.assertEqual(1, len(g)) # Has one empty linear ring |
|
662 self.assertEqual(1, g.num_geom) |
|
663 self.assertEqual(0, len(g[0])) |
|
664 elif isinstance(g, (Point, LineString)): |
|
665 self.assertEqual(1, g.num_geom) |
|
666 self.assertEqual(0, len(g)) |
|
667 else: |
|
668 self.assertEqual(0, g.num_geom) |
|
669 self.assertEqual(0, len(g)) |
|
670 |
|
671 # Testing __getitem__ (doesn't work on Point or Polygon) |
|
672 if isinstance(g, Point): |
|
673 self.assertRaises(GEOSIndexError, g.get_x) |
|
674 elif isinstance(g, Polygon): |
|
675 lr = g.shell |
|
676 self.assertEqual('LINEARRING EMPTY', lr.wkt) |
|
677 self.assertEqual(0, len(lr)) |
|
678 self.assertEqual(True, lr.empty) |
|
679 self.assertRaises(GEOSIndexError, lr.__getitem__, 0) |
|
680 else: |
|
681 self.assertRaises(GEOSIndexError, g.__getitem__, 0) |
|
682 |
|
683 def test21_test_gdal(self): |
|
684 "Testing `ogr` and `srs` properties." |
|
685 if not HAS_GDAL: return |
|
686 g1 = fromstr('POINT(5 23)') |
|
687 self.assertEqual(True, isinstance(g1.ogr, OGRGeometry)) |
|
688 self.assertEqual(g1.srs, None) |
|
689 |
|
690 g2 = fromstr('LINESTRING(0 0, 5 5, 23 23)', srid=4326) |
|
691 self.assertEqual(True, isinstance(g2.ogr, OGRGeometry)) |
|
692 self.assertEqual(True, isinstance(g2.srs, SpatialReference)) |
|
693 self.assertEqual(g2.hex, g2.ogr.hex) |
|
694 self.assertEqual('WGS 84', g2.srs.name) |
|
695 |
|
696 def test22_copy(self): |
|
697 "Testing use with the Python `copy` module." |
|
698 import copy |
|
699 poly = GEOSGeometry('POLYGON((0 0, 0 23, 23 23, 23 0, 0 0), (5 5, 5 10, 10 10, 10 5, 5 5))') |
|
700 cpy1 = copy.copy(poly) |
|
701 cpy2 = copy.deepcopy(poly) |
|
702 self.assertNotEqual(poly._ptr, cpy1._ptr) |
|
703 self.assertNotEqual(poly._ptr, cpy2._ptr) |
|
704 |
|
705 def test23_transform(self): |
|
706 "Testing `transform` method." |
|
707 if not HAS_GDAL: return |
|
708 orig = GEOSGeometry('POINT (-104.609 38.255)', 4326) |
|
709 trans = GEOSGeometry('POINT (992385.4472045 481455.4944650)', 2774) |
|
710 |
|
711 # Using a srid, a SpatialReference object, and a CoordTransform object |
|
712 # for transformations. |
|
713 t1, t2, t3 = orig.clone(), orig.clone(), orig.clone() |
|
714 t1.transform(trans.srid) |
|
715 t2.transform(SpatialReference('EPSG:2774')) |
|
716 ct = CoordTransform(SpatialReference('WGS84'), SpatialReference(2774)) |
|
717 t3.transform(ct) |
|
718 |
|
719 # Testing use of the `clone` keyword. |
|
720 k1 = orig.clone() |
|
721 k2 = k1.transform(trans.srid, clone=True) |
|
722 self.assertEqual(k1, orig) |
|
723 self.assertNotEqual(k1, k2) |
|
724 |
|
725 prec = 3 |
|
726 for p in (t1, t2, t3, k2): |
|
727 self.assertAlmostEqual(trans.x, p.x, prec) |
|
728 self.assertAlmostEqual(trans.y, p.y, prec) |
|
729 |
|
730 def test24_extent(self): |
|
731 "Testing `extent` method." |
|
732 # The xmin, ymin, xmax, ymax of the MultiPoint should be returned. |
|
733 mp = MultiPoint(Point(5, 23), Point(0, 0), Point(10, 50)) |
|
734 self.assertEqual((0.0, 0.0, 10.0, 50.0), mp.extent) |
|
735 pnt = Point(5.23, 17.8) |
|
736 # Extent of points is just the point itself repeated. |
|
737 self.assertEqual((5.23, 17.8, 5.23, 17.8), pnt.extent) |
|
738 # Testing on the 'real world' Polygon. |
|
739 poly = fromstr(polygons[3].wkt) |
|
740 ring = poly.shell |
|
741 x, y = ring.x, ring.y |
|
742 xmin, ymin = min(x), min(y) |
|
743 xmax, ymax = max(x), max(y) |
|
744 self.assertEqual((xmin, ymin, xmax, ymax), poly.extent) |
|
745 |
|
746 def test25_pickle(self): |
|
747 "Testing pickling and unpickling support." |
|
748 # Using both pickle and cPickle -- just 'cause. |
|
749 import pickle, cPickle |
|
750 |
|
751 # Creating a list of test geometries for pickling, |
|
752 # and setting the SRID on some of them. |
|
753 def get_geoms(lst, srid=None): |
|
754 return [GEOSGeometry(tg.wkt, srid) for tg in lst] |
|
755 tgeoms = get_geoms(points) |
|
756 tgeoms.extend(get_geoms(multilinestrings, 4326)) |
|
757 tgeoms.extend(get_geoms(polygons, 3084)) |
|
758 tgeoms.extend(get_geoms(multipolygons, 900913)) |
|
759 |
|
760 # The SRID won't be exported in GEOS 3.0 release candidates. |
|
761 no_srid = self.null_srid == -1 |
|
762 for geom in tgeoms: |
|
763 s1, s2 = cPickle.dumps(geom), pickle.dumps(geom) |
|
764 g1, g2 = cPickle.loads(s1), pickle.loads(s2) |
|
765 for tmpg in (g1, g2): |
|
766 self.assertEqual(geom, tmpg) |
|
767 if not no_srid: self.assertEqual(geom.srid, tmpg.srid) |
|
768 |
|
769 def suite(): |
|
770 s = unittest.TestSuite() |
|
771 s.addTest(unittest.makeSuite(GEOSTest)) |
|
772 return s |
|
773 |
|
774 def run(verbosity=2): |
|
775 unittest.TextTestRunner(verbosity=verbosity).run(suite()) |