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