|
0
|
1 |
import unittest |
|
|
2 |
from django.contrib.gis.gdal import OGRGeometry, OGRGeomType, \ |
|
|
3 |
OGRException, OGRIndexError, SpatialReference, CoordTransform, \ |
|
|
4 |
gdal_version |
|
|
5 |
from django.contrib.gis.tests.geometries import * |
|
|
6 |
|
|
|
7 |
class OGRGeomTest(unittest.TestCase): |
|
|
8 |
"This tests the OGR Geometry." |
|
|
9 |
|
|
|
10 |
def test00a_geomtype(self): |
|
|
11 |
"Testing OGRGeomType object." |
|
|
12 |
|
|
|
13 |
# OGRGeomType should initialize on all these inputs. |
|
|
14 |
try: |
|
|
15 |
g = OGRGeomType(1) |
|
|
16 |
g = OGRGeomType(7) |
|
|
17 |
g = OGRGeomType('point') |
|
|
18 |
g = OGRGeomType('GeometrycollectioN') |
|
|
19 |
g = OGRGeomType('LINearrING') |
|
|
20 |
g = OGRGeomType('Unknown') |
|
|
21 |
except: |
|
|
22 |
self.fail('Could not create an OGRGeomType object!') |
|
|
23 |
|
|
|
24 |
# Should throw TypeError on this input |
|
|
25 |
self.assertRaises(OGRException, OGRGeomType, 23) |
|
|
26 |
self.assertRaises(OGRException, OGRGeomType, 'fooD') |
|
|
27 |
self.assertRaises(OGRException, OGRGeomType, 9) |
|
|
28 |
|
|
|
29 |
# Equivalence can take strings, ints, and other OGRGeomTypes |
|
|
30 |
self.assertEqual(True, OGRGeomType(1) == OGRGeomType(1)) |
|
|
31 |
self.assertEqual(True, OGRGeomType(7) == 'GeometryCollection') |
|
|
32 |
self.assertEqual(True, OGRGeomType('point') == 'POINT') |
|
|
33 |
self.assertEqual(False, OGRGeomType('point') == 2) |
|
|
34 |
self.assertEqual(True, OGRGeomType('unknown') == 0) |
|
|
35 |
self.assertEqual(True, OGRGeomType(6) == 'MULtiPolyGON') |
|
|
36 |
self.assertEqual(False, OGRGeomType(1) != OGRGeomType('point')) |
|
|
37 |
self.assertEqual(True, OGRGeomType('POINT') != OGRGeomType(6)) |
|
|
38 |
|
|
|
39 |
# Testing the Django field name equivalent property. |
|
|
40 |
self.assertEqual('PointField', OGRGeomType('Point').django) |
|
|
41 |
self.assertEqual('GeometryField', OGRGeomType('Unknown').django) |
|
|
42 |
self.assertEqual(None, OGRGeomType('none').django) |
|
|
43 |
|
|
|
44 |
# 'Geometry' initialization implies an unknown geometry type. |
|
|
45 |
gt = OGRGeomType('Geometry') |
|
|
46 |
self.assertEqual(0, gt.num) |
|
|
47 |
self.assertEqual('Unknown', gt.name) |
|
|
48 |
|
|
29
|
49 |
def test00b_geomtype_25d(self): |
|
|
50 |
"Testing OGRGeomType object with 25D types." |
|
|
51 |
wkb25bit = OGRGeomType.wkb25bit |
|
|
52 |
self.failUnless(OGRGeomType(wkb25bit + 1) == 'Point25D') |
|
|
53 |
self.failUnless(OGRGeomType('MultiLineString25D') == (5 + wkb25bit)) |
|
|
54 |
self.assertEqual('GeometryCollectionField', OGRGeomType('GeometryCollection25D').django) |
|
|
55 |
|
|
0
|
56 |
def test01a_wkt(self): |
|
|
57 |
"Testing WKT output." |
|
|
58 |
for g in wkt_out: |
|
|
59 |
geom = OGRGeometry(g.wkt) |
|
|
60 |
self.assertEqual(g.wkt, geom.wkt) |
|
|
61 |
|
|
29
|
62 |
def test01a_ewkt(self): |
|
|
63 |
"Testing EWKT input/output." |
|
|
64 |
for ewkt_val in ('POINT (1 2 3)', 'LINEARRING (0 0,1 1,2 1,0 0)'): |
|
|
65 |
# First with ewkt output when no SRID in EWKT |
|
|
66 |
self.assertEqual(ewkt_val, OGRGeometry(ewkt_val).ewkt) |
|
|
67 |
# No test consumption with an SRID specified. |
|
|
68 |
ewkt_val = 'SRID=4326;%s' % ewkt_val |
|
|
69 |
geom = OGRGeometry(ewkt_val) |
|
|
70 |
self.assertEqual(ewkt_val, geom.ewkt) |
|
|
71 |
self.assertEqual(4326, geom.srs.srid) |
|
|
72 |
|
|
0
|
73 |
def test01b_gml(self): |
|
|
74 |
"Testing GML output." |
|
|
75 |
for g in wkt_out: |
|
|
76 |
geom = OGRGeometry(g.wkt) |
|
|
77 |
self.assertEqual(g.gml, geom.gml) |
|
|
78 |
|
|
|
79 |
def test01c_hex(self): |
|
|
80 |
"Testing HEX input/output." |
|
|
81 |
for g in hex_wkt: |
|
|
82 |
geom1 = OGRGeometry(g.wkt) |
|
|
83 |
self.assertEqual(g.hex, geom1.hex) |
|
|
84 |
# Constructing w/HEX |
|
|
85 |
geom2 = OGRGeometry(g.hex) |
|
|
86 |
self.assertEqual(geom1, geom2) |
|
|
87 |
|
|
|
88 |
def test01d_wkb(self): |
|
|
89 |
"Testing WKB input/output." |
|
|
90 |
from binascii import b2a_hex |
|
|
91 |
for g in hex_wkt: |
|
|
92 |
geom1 = OGRGeometry(g.wkt) |
|
|
93 |
wkb = geom1.wkb |
|
|
94 |
self.assertEqual(b2a_hex(wkb).upper(), g.hex) |
|
|
95 |
# Constructing w/WKB. |
|
|
96 |
geom2 = OGRGeometry(wkb) |
|
|
97 |
self.assertEqual(geom1, geom2) |
|
|
98 |
|
|
|
99 |
def test01e_json(self): |
|
|
100 |
"Testing GeoJSON input/output." |
|
|
101 |
from django.contrib.gis.gdal.prototypes.geom import GEOJSON |
|
|
102 |
if not GEOJSON: return |
|
|
103 |
for g in json_geoms: |
|
|
104 |
geom = OGRGeometry(g.wkt) |
|
|
105 |
if not hasattr(g, 'not_equal'): |
|
|
106 |
self.assertEqual(g.json, geom.json) |
|
|
107 |
self.assertEqual(g.json, geom.geojson) |
|
|
108 |
self.assertEqual(OGRGeometry(g.wkt), OGRGeometry(geom.json)) |
|
|
109 |
|
|
|
110 |
def test02_points(self): |
|
|
111 |
"Testing Point objects." |
|
|
112 |
|
|
|
113 |
prev = OGRGeometry('POINT(0 0)') |
|
|
114 |
for p in points: |
|
|
115 |
if not hasattr(p, 'z'): # No 3D |
|
|
116 |
pnt = OGRGeometry(p.wkt) |
|
|
117 |
self.assertEqual(1, pnt.geom_type) |
|
|
118 |
self.assertEqual('POINT', pnt.geom_name) |
|
|
119 |
self.assertEqual(p.x, pnt.x) |
|
|
120 |
self.assertEqual(p.y, pnt.y) |
|
|
121 |
self.assertEqual((p.x, p.y), pnt.tuple) |
|
|
122 |
|
|
|
123 |
def test03_multipoints(self): |
|
|
124 |
"Testing MultiPoint objects." |
|
|
125 |
|
|
|
126 |
for mp in multipoints: |
|
|
127 |
mgeom1 = OGRGeometry(mp.wkt) # First one from WKT |
|
|
128 |
self.assertEqual(4, mgeom1.geom_type) |
|
|
129 |
self.assertEqual('MULTIPOINT', mgeom1.geom_name) |
|
|
130 |
mgeom2 = OGRGeometry('MULTIPOINT') # Creating empty multipoint |
|
|
131 |
mgeom3 = OGRGeometry('MULTIPOINT') |
|
|
132 |
for g in mgeom1: |
|
|
133 |
mgeom2.add(g) # adding each point from the multipoints |
|
|
134 |
mgeom3.add(g.wkt) # should take WKT as well |
|
|
135 |
self.assertEqual(mgeom1, mgeom2) # they should equal |
|
|
136 |
self.assertEqual(mgeom1, mgeom3) |
|
|
137 |
self.assertEqual(mp.points, mgeom2.tuple) |
|
|
138 |
self.assertEqual(mp.n_p, mgeom2.point_count) |
|
29
|
139 |
|
|
0
|
140 |
def test04_linestring(self): |
|
|
141 |
"Testing LineString objects." |
|
|
142 |
prev = OGRGeometry('POINT(0 0)') |
|
|
143 |
for ls in linestrings: |
|
|
144 |
linestr = OGRGeometry(ls.wkt) |
|
|
145 |
self.assertEqual(2, linestr.geom_type) |
|
|
146 |
self.assertEqual('LINESTRING', linestr.geom_name) |
|
|
147 |
self.assertEqual(ls.n_p, linestr.point_count) |
|
|
148 |
self.assertEqual(ls.tup, linestr.tuple) |
|
|
149 |
self.assertEqual(True, linestr == OGRGeometry(ls.wkt)) |
|
|
150 |
self.assertEqual(True, linestr != prev) |
|
|
151 |
self.assertRaises(OGRIndexError, linestr.__getitem__, len(linestr)) |
|
|
152 |
prev = linestr |
|
|
153 |
|
|
|
154 |
# Testing the x, y properties. |
|
|
155 |
x = [tmpx for tmpx, tmpy in ls.tup] |
|
|
156 |
y = [tmpy for tmpx, tmpy in ls.tup] |
|
|
157 |
self.assertEqual(x, linestr.x) |
|
|
158 |
self.assertEqual(y, linestr.y) |
|
|
159 |
|
|
|
160 |
def test05_multilinestring(self): |
|
|
161 |
"Testing MultiLineString objects." |
|
|
162 |
prev = OGRGeometry('POINT(0 0)') |
|
|
163 |
for mls in multilinestrings: |
|
|
164 |
mlinestr = OGRGeometry(mls.wkt) |
|
|
165 |
self.assertEqual(5, mlinestr.geom_type) |
|
|
166 |
self.assertEqual('MULTILINESTRING', mlinestr.geom_name) |
|
|
167 |
self.assertEqual(mls.n_p, mlinestr.point_count) |
|
|
168 |
self.assertEqual(mls.tup, mlinestr.tuple) |
|
|
169 |
self.assertEqual(True, mlinestr == OGRGeometry(mls.wkt)) |
|
|
170 |
self.assertEqual(True, mlinestr != prev) |
|
|
171 |
prev = mlinestr |
|
|
172 |
for ls in mlinestr: |
|
|
173 |
self.assertEqual(2, ls.geom_type) |
|
|
174 |
self.assertEqual('LINESTRING', ls.geom_name) |
|
29
|
175 |
self.assertRaises(OGRIndexError, mlinestr.__getitem__, len(mlinestr)) |
|
0
|
176 |
|
|
|
177 |
def test06_linearring(self): |
|
|
178 |
"Testing LinearRing objects." |
|
|
179 |
prev = OGRGeometry('POINT(0 0)') |
|
|
180 |
for rr in linearrings: |
|
|
181 |
lr = OGRGeometry(rr.wkt) |
|
|
182 |
#self.assertEqual(101, lr.geom_type.num) |
|
|
183 |
self.assertEqual('LINEARRING', lr.geom_name) |
|
|
184 |
self.assertEqual(rr.n_p, len(lr)) |
|
|
185 |
self.assertEqual(True, lr == OGRGeometry(rr.wkt)) |
|
|
186 |
self.assertEqual(True, lr != prev) |
|
|
187 |
prev = lr |
|
|
188 |
|
|
|
189 |
def test07a_polygons(self): |
|
|
190 |
"Testing Polygon objects." |
|
|
191 |
|
|
|
192 |
# Testing `from_bbox` class method |
|
29
|
193 |
bbox = (-180,-90,180,90) |
|
0
|
194 |
p = OGRGeometry.from_bbox( bbox ) |
|
|
195 |
self.assertEqual(bbox, p.extent) |
|
|
196 |
|
|
|
197 |
prev = OGRGeometry('POINT(0 0)') |
|
|
198 |
for p in polygons: |
|
|
199 |
poly = OGRGeometry(p.wkt) |
|
|
200 |
self.assertEqual(3, poly.geom_type) |
|
|
201 |
self.assertEqual('POLYGON', poly.geom_name) |
|
|
202 |
self.assertEqual(p.n_p, poly.point_count) |
|
|
203 |
self.assertEqual(p.n_i + 1, len(poly)) |
|
|
204 |
|
|
|
205 |
# Testing area & centroid. |
|
|
206 |
self.assertAlmostEqual(p.area, poly.area, 9) |
|
|
207 |
x, y = poly.centroid.tuple |
|
|
208 |
self.assertAlmostEqual(p.centroid[0], x, 9) |
|
|
209 |
self.assertAlmostEqual(p.centroid[1], y, 9) |
|
|
210 |
|
|
|
211 |
# Testing equivalence |
|
|
212 |
self.assertEqual(True, poly == OGRGeometry(p.wkt)) |
|
|
213 |
self.assertEqual(True, poly != prev) |
|
29
|
214 |
|
|
0
|
215 |
if p.ext_ring_cs: |
|
|
216 |
ring = poly[0] |
|
|
217 |
self.assertEqual(p.ext_ring_cs, ring.tuple) |
|
|
218 |
self.assertEqual(p.ext_ring_cs, poly[0].tuple) |
|
|
219 |
self.assertEqual(len(p.ext_ring_cs), ring.point_count) |
|
29
|
220 |
|
|
0
|
221 |
for r in poly: |
|
|
222 |
self.assertEqual('LINEARRING', r.geom_name) |
|
|
223 |
|
|
|
224 |
def test07b_closepolygons(self): |
|
|
225 |
"Testing closing Polygon objects." |
|
|
226 |
# Both rings in this geometry are not closed. |
|
|
227 |
poly = OGRGeometry('POLYGON((0 0, 5 0, 5 5, 0 5), (1 1, 2 1, 2 2, 2 1))') |
|
|
228 |
self.assertEqual(8, poly.point_count) |
|
|
229 |
print "\nBEGIN - expecting IllegalArgumentException; safe to ignore.\n" |
|
|
230 |
try: |
|
|
231 |
c = poly.centroid |
|
|
232 |
except OGRException: |
|
|
233 |
# Should raise an OGR exception, rings are not closed |
|
|
234 |
pass |
|
|
235 |
else: |
|
|
236 |
self.fail('Should have raised an OGRException!') |
|
|
237 |
print "\nEND - expecting IllegalArgumentException; safe to ignore.\n" |
|
|
238 |
|
|
|
239 |
# Closing the rings -- doesn't work on GDAL versions 1.4.1 and below: |
|
|
240 |
# http://trac.osgeo.org/gdal/ticket/1673 |
|
|
241 |
major, minor1, minor2 = gdal_version().split('.') |
|
|
242 |
if major == '1': |
|
|
243 |
iminor1 = int(minor1) |
|
|
244 |
if iminor1 < 4 or (iminor1 == 4 and minor2.startswith('1')): return |
|
|
245 |
poly.close_rings() |
|
|
246 |
self.assertEqual(10, poly.point_count) # Two closing points should've been added |
|
|
247 |
self.assertEqual(OGRGeometry('POINT(2.5 2.5)'), poly.centroid) |
|
|
248 |
|
|
|
249 |
def test08_multipolygons(self): |
|
|
250 |
"Testing MultiPolygon objects." |
|
|
251 |
prev = OGRGeometry('POINT(0 0)') |
|
|
252 |
for mp in multipolygons: |
|
|
253 |
mpoly = OGRGeometry(mp.wkt) |
|
|
254 |
self.assertEqual(6, mpoly.geom_type) |
|
|
255 |
self.assertEqual('MULTIPOLYGON', mpoly.geom_name) |
|
|
256 |
if mp.valid: |
|
|
257 |
self.assertEqual(mp.n_p, mpoly.point_count) |
|
|
258 |
self.assertEqual(mp.num_geom, len(mpoly)) |
|
|
259 |
self.assertRaises(OGRIndexError, mpoly.__getitem__, len(mpoly)) |
|
|
260 |
for p in mpoly: |
|
|
261 |
self.assertEqual('POLYGON', p.geom_name) |
|
|
262 |
self.assertEqual(3, p.geom_type) |
|
|
263 |
self.assertEqual(mpoly.wkt, OGRGeometry(mp.wkt).wkt) |
|
|
264 |
|
|
|
265 |
def test09a_srs(self): |
|
|
266 |
"Testing OGR Geometries with Spatial Reference objects." |
|
|
267 |
for mp in multipolygons: |
|
|
268 |
# Creating a geometry w/spatial reference |
|
|
269 |
sr = SpatialReference('WGS84') |
|
|
270 |
mpoly = OGRGeometry(mp.wkt, sr) |
|
|
271 |
self.assertEqual(sr.wkt, mpoly.srs.wkt) |
|
29
|
272 |
|
|
0
|
273 |
# Ensuring that SRS is propagated to clones. |
|
|
274 |
klone = mpoly.clone() |
|
|
275 |
self.assertEqual(sr.wkt, klone.srs.wkt) |
|
29
|
276 |
|
|
0
|
277 |
# Ensuring all children geometries (polygons and their rings) all |
|
|
278 |
# return the assigned spatial reference as well. |
|
|
279 |
for poly in mpoly: |
|
|
280 |
self.assertEqual(sr.wkt, poly.srs.wkt) |
|
|
281 |
for ring in poly: |
|
|
282 |
self.assertEqual(sr.wkt, ring.srs.wkt) |
|
|
283 |
|
|
|
284 |
# Ensuring SRS propagate in topological ops. |
|
|
285 |
a, b = topology_geoms[0] |
|
|
286 |
a, b = OGRGeometry(a.wkt, sr), OGRGeometry(b.wkt, sr) |
|
|
287 |
diff = a.difference(b) |
|
|
288 |
union = a.union(b) |
|
|
289 |
self.assertEqual(sr.wkt, diff.srs.wkt) |
|
|
290 |
self.assertEqual(sr.srid, union.srs.srid) |
|
|
291 |
|
|
|
292 |
# Instantiating w/an integer SRID |
|
|
293 |
mpoly = OGRGeometry(mp.wkt, 4326) |
|
|
294 |
self.assertEqual(4326, mpoly.srid) |
|
|
295 |
mpoly.srs = SpatialReference(4269) |
|
|
296 |
self.assertEqual(4269, mpoly.srid) |
|
|
297 |
self.assertEqual('NAD83', mpoly.srs.name) |
|
29
|
298 |
|
|
0
|
299 |
# Incrementing through the multipolyogn after the spatial reference |
|
|
300 |
# has been re-assigned. |
|
|
301 |
for poly in mpoly: |
|
|
302 |
self.assertEqual(mpoly.srs.wkt, poly.srs.wkt) |
|
|
303 |
poly.srs = 32140 |
|
|
304 |
for ring in poly: |
|
|
305 |
# Changing each ring in the polygon |
|
|
306 |
self.assertEqual(32140, ring.srs.srid) |
|
|
307 |
self.assertEqual('NAD83 / Texas South Central', ring.srs.name) |
|
|
308 |
ring.srs = str(SpatialReference(4326)) # back to WGS84 |
|
|
309 |
self.assertEqual(4326, ring.srs.srid) |
|
|
310 |
|
|
|
311 |
# Using the `srid` property. |
|
|
312 |
ring.srid = 4322 |
|
|
313 |
self.assertEqual('WGS 72', ring.srs.name) |
|
|
314 |
self.assertEqual(4322, ring.srid) |
|
|
315 |
|
|
|
316 |
def test09b_srs_transform(self): |
|
|
317 |
"Testing transform()." |
|
|
318 |
orig = OGRGeometry('POINT (-104.609 38.255)', 4326) |
|
|
319 |
trans = OGRGeometry('POINT (992385.4472045 481455.4944650)', 2774) |
|
|
320 |
|
|
|
321 |
# Using an srid, a SpatialReference object, and a CoordTransform object |
|
|
322 |
# or transformations. |
|
|
323 |
t1, t2, t3 = orig.clone(), orig.clone(), orig.clone() |
|
|
324 |
t1.transform(trans.srid) |
|
|
325 |
t2.transform(SpatialReference('EPSG:2774')) |
|
|
326 |
ct = CoordTransform(SpatialReference('WGS84'), SpatialReference(2774)) |
|
|
327 |
t3.transform(ct) |
|
|
328 |
|
|
|
329 |
# Testing use of the `clone` keyword. |
|
|
330 |
k1 = orig.clone() |
|
|
331 |
k2 = k1.transform(trans.srid, clone=True) |
|
|
332 |
self.assertEqual(k1, orig) |
|
|
333 |
self.assertNotEqual(k1, k2) |
|
|
334 |
|
|
|
335 |
prec = 3 |
|
|
336 |
for p in (t1, t2, t3, k2): |
|
|
337 |
self.assertAlmostEqual(trans.x, p.x, prec) |
|
|
338 |
self.assertAlmostEqual(trans.y, p.y, prec) |
|
|
339 |
|
|
29
|
340 |
def test09c_transform_dim(self): |
|
|
341 |
"Testing coordinate dimension is the same on transformed geometries." |
|
|
342 |
ls_orig = OGRGeometry('LINESTRING(-104.609 38.255)', 4326) |
|
|
343 |
ls_trans = OGRGeometry('LINESTRING(992385.4472045 481455.4944650)', 2774) |
|
|
344 |
|
|
|
345 |
prec = 3 |
|
|
346 |
ls_orig.transform(ls_trans.srs) |
|
|
347 |
# Making sure the coordinate dimension is still 2D. |
|
|
348 |
self.assertEqual(2, ls_orig.coord_dim) |
|
|
349 |
self.assertAlmostEqual(ls_trans.x[0], ls_orig.x[0], prec) |
|
|
350 |
self.assertAlmostEqual(ls_trans.y[0], ls_orig.y[0], prec) |
|
|
351 |
|
|
0
|
352 |
def test10_difference(self): |
|
|
353 |
"Testing difference()." |
|
|
354 |
for i in xrange(len(topology_geoms)): |
|
|
355 |
g_tup = topology_geoms[i] |
|
|
356 |
a = OGRGeometry(g_tup[0].wkt) |
|
|
357 |
b = OGRGeometry(g_tup[1].wkt) |
|
|
358 |
d1 = OGRGeometry(diff_geoms[i].wkt) |
|
|
359 |
d2 = a.difference(b) |
|
|
360 |
self.assertEqual(d1, d2) |
|
|
361 |
self.assertEqual(d1, a - b) # __sub__ is difference operator |
|
|
362 |
a -= b # testing __isub__ |
|
|
363 |
self.assertEqual(d1, a) |
|
|
364 |
|
|
|
365 |
def test11_intersection(self): |
|
|
366 |
"Testing intersects() and intersection()." |
|
|
367 |
for i in xrange(len(topology_geoms)): |
|
|
368 |
g_tup = topology_geoms[i] |
|
|
369 |
a = OGRGeometry(g_tup[0].wkt) |
|
|
370 |
b = OGRGeometry(g_tup[1].wkt) |
|
|
371 |
i1 = OGRGeometry(intersect_geoms[i].wkt) |
|
|
372 |
self.assertEqual(True, a.intersects(b)) |
|
|
373 |
i2 = a.intersection(b) |
|
|
374 |
self.assertEqual(i1, i2) |
|
|
375 |
self.assertEqual(i1, a & b) # __and__ is intersection operator |
|
|
376 |
a &= b # testing __iand__ |
|
|
377 |
self.assertEqual(i1, a) |
|
|
378 |
|
|
|
379 |
def test12_symdifference(self): |
|
|
380 |
"Testing sym_difference()." |
|
|
381 |
for i in xrange(len(topology_geoms)): |
|
|
382 |
g_tup = topology_geoms[i] |
|
|
383 |
a = OGRGeometry(g_tup[0].wkt) |
|
|
384 |
b = OGRGeometry(g_tup[1].wkt) |
|
|
385 |
d1 = OGRGeometry(sdiff_geoms[i].wkt) |
|
|
386 |
d2 = a.sym_difference(b) |
|
|
387 |
self.assertEqual(d1, d2) |
|
|
388 |
self.assertEqual(d1, a ^ b) # __xor__ is symmetric difference operator |
|
|
389 |
a ^= b # testing __ixor__ |
|
|
390 |
self.assertEqual(d1, a) |
|
29
|
391 |
|
|
0
|
392 |
def test13_union(self): |
|
|
393 |
"Testing union()." |
|
|
394 |
for i in xrange(len(topology_geoms)): |
|
|
395 |
g_tup = topology_geoms[i] |
|
|
396 |
a = OGRGeometry(g_tup[0].wkt) |
|
|
397 |
b = OGRGeometry(g_tup[1].wkt) |
|
|
398 |
u1 = OGRGeometry(union_geoms[i].wkt) |
|
|
399 |
u2 = a.union(b) |
|
|
400 |
self.assertEqual(u1, u2) |
|
|
401 |
self.assertEqual(u1, a | b) # __or__ is union operator |
|
|
402 |
a |= b # testing __ior__ |
|
|
403 |
self.assertEqual(u1, a) |
|
|
404 |
|
|
|
405 |
def test14_add(self): |
|
|
406 |
"Testing GeometryCollection.add()." |
|
|
407 |
# Can't insert a Point into a MultiPolygon. |
|
|
408 |
mp = OGRGeometry('MultiPolygon') |
|
|
409 |
pnt = OGRGeometry('POINT(5 23)') |
|
|
410 |
self.assertRaises(OGRException, mp.add, pnt) |
|
|
411 |
|
|
|
412 |
# GeometryCollection.add may take an OGRGeometry (if another collection |
|
|
413 |
# of the same type all child geoms will be added individually) or WKT. |
|
|
414 |
for mp in multipolygons: |
|
|
415 |
mpoly = OGRGeometry(mp.wkt) |
|
|
416 |
mp1 = OGRGeometry('MultiPolygon') |
|
|
417 |
mp2 = OGRGeometry('MultiPolygon') |
|
|
418 |
mp3 = OGRGeometry('MultiPolygon') |
|
|
419 |
|
|
|
420 |
for poly in mpoly: |
|
|
421 |
mp1.add(poly) # Adding a geometry at a time |
|
|
422 |
mp2.add(poly.wkt) # Adding WKT |
|
|
423 |
mp3.add(mpoly) # Adding a MultiPolygon's entire contents at once. |
|
|
424 |
for tmp in (mp1, mp2, mp3): self.assertEqual(mpoly, tmp) |
|
|
425 |
|
|
|
426 |
def test15_extent(self): |
|
|
427 |
"Testing `extent` property." |
|
|
428 |
# The xmin, ymin, xmax, ymax of the MultiPoint should be returned. |
|
|
429 |
mp = OGRGeometry('MULTIPOINT(5 23, 0 0, 10 50)') |
|
|
430 |
self.assertEqual((0.0, 0.0, 10.0, 50.0), mp.extent) |
|
|
431 |
# Testing on the 'real world' Polygon. |
|
|
432 |
poly = OGRGeometry(polygons[3].wkt) |
|
|
433 |
ring = poly.shell |
|
|
434 |
x, y = ring.x, ring.y |
|
|
435 |
xmin, ymin = min(x), min(y) |
|
|
436 |
xmax, ymax = max(x), max(y) |
|
|
437 |
self.assertEqual((xmin, ymin, xmax, ymax), poly.extent) |
|
|
438 |
|
|
29
|
439 |
def test16_25D(self): |
|
|
440 |
"Testing 2.5D geometries." |
|
|
441 |
pnt_25d = OGRGeometry('POINT(1 2 3)') |
|
|
442 |
self.assertEqual('Point25D', pnt_25d.geom_type.name) |
|
|
443 |
self.assertEqual(3.0, pnt_25d.z) |
|
|
444 |
self.assertEqual(3, pnt_25d.coord_dim) |
|
|
445 |
ls_25d = OGRGeometry('LINESTRING(1 1 1,2 2 2,3 3 3)') |
|
|
446 |
self.assertEqual('LineString25D', ls_25d.geom_type.name) |
|
|
447 |
self.assertEqual([1.0, 2.0, 3.0], ls_25d.z) |
|
|
448 |
self.assertEqual(3, ls_25d.coord_dim) |
|
|
449 |
|
|
|
450 |
def test17_pickle(self): |
|
|
451 |
"Testing pickle support." |
|
|
452 |
import cPickle |
|
|
453 |
g1 = OGRGeometry('LINESTRING(1 1 1,2 2 2,3 3 3)', 'WGS84') |
|
|
454 |
g2 = cPickle.loads(cPickle.dumps(g1)) |
|
|
455 |
self.assertEqual(g1, g2) |
|
|
456 |
self.assertEqual(4326, g2.srs.srid) |
|
|
457 |
self.assertEqual(g1.srs.wkt, g2.srs.wkt) |
|
|
458 |
|
|
|
459 |
def test18_ogrgeometry_transform_workaround(self): |
|
|
460 |
"Testing coordinate dimensions on geometries after transformation." |
|
|
461 |
# A bug in GDAL versions prior to 1.7 changes the coordinate |
|
|
462 |
# dimension of a geometry after it has been transformed. |
|
|
463 |
# This test ensures that the bug workarounds employed within |
|
|
464 |
# `OGRGeometry.transform` indeed work. |
|
|
465 |
wkt_2d = "MULTILINESTRING ((0 0,1 1,2 2))" |
|
|
466 |
wkt_3d = "MULTILINESTRING ((0 0 0,1 1 1,2 2 2))" |
|
|
467 |
srid = 4326 |
|
|
468 |
|
|
|
469 |
# For both the 2D and 3D MultiLineString, ensure _both_ the dimension |
|
|
470 |
# of the collection and the component LineString have the expected |
|
|
471 |
# coordinate dimension after transform. |
|
|
472 |
geom = OGRGeometry(wkt_2d, srid) |
|
|
473 |
geom.transform(srid) |
|
|
474 |
self.assertEqual(2, geom.coord_dim) |
|
|
475 |
self.assertEqual(2, geom[0].coord_dim) |
|
|
476 |
self.assertEqual(wkt_2d, geom.wkt) |
|
|
477 |
|
|
|
478 |
geom = OGRGeometry(wkt_3d, srid) |
|
|
479 |
geom.transform(srid) |
|
|
480 |
self.assertEqual(3, geom.coord_dim) |
|
|
481 |
self.assertEqual(3, geom[0].coord_dim) |
|
|
482 |
self.assertEqual(wkt_3d, geom.wkt) |
|
|
483 |
|
|
|
484 |
def test19_equivalence_regression(self): |
|
|
485 |
"Testing equivalence methods with non-OGRGeometry instances." |
|
|
486 |
self.assertNotEqual(None, OGRGeometry('POINT(0 0)')) |
|
|
487 |
self.assertEqual(False, OGRGeometry('LINESTRING(0 0, 1 1)') == 3) |
|
|
488 |
|
|
0
|
489 |
def suite(): |
|
|
490 |
s = unittest.TestSuite() |
|
|
491 |
s.addTest(unittest.makeSuite(OGRGeomTest)) |
|
|
492 |
return s |
|
|
493 |
|
|
|
494 |
def run(verbosity=2): |
|
|
495 |
unittest.TextTestRunner(verbosity=verbosity).run(suite()) |