44 # 'Geometry' initialization implies an unknown geometry type. |
44 # 'Geometry' initialization implies an unknown geometry type. |
45 gt = OGRGeomType('Geometry') |
45 gt = OGRGeomType('Geometry') |
46 self.assertEqual(0, gt.num) |
46 self.assertEqual(0, gt.num) |
47 self.assertEqual('Unknown', gt.name) |
47 self.assertEqual('Unknown', gt.name) |
48 |
48 |
|
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 |
49 def test01a_wkt(self): |
56 def test01a_wkt(self): |
50 "Testing WKT output." |
57 "Testing WKT output." |
51 for g in wkt_out: |
58 for g in wkt_out: |
52 geom = OGRGeometry(g.wkt) |
59 geom = OGRGeometry(g.wkt) |
53 self.assertEqual(g.wkt, geom.wkt) |
60 self.assertEqual(g.wkt, geom.wkt) |
|
61 |
|
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) |
54 |
72 |
55 def test01b_gml(self): |
73 def test01b_gml(self): |
56 "Testing GML output." |
74 "Testing GML output." |
57 for g in wkt_out: |
75 for g in wkt_out: |
58 geom = OGRGeometry(g.wkt) |
76 geom = OGRGeometry(g.wkt) |
116 mgeom3.add(g.wkt) # should take WKT as well |
134 mgeom3.add(g.wkt) # should take WKT as well |
117 self.assertEqual(mgeom1, mgeom2) # they should equal |
135 self.assertEqual(mgeom1, mgeom2) # they should equal |
118 self.assertEqual(mgeom1, mgeom3) |
136 self.assertEqual(mgeom1, mgeom3) |
119 self.assertEqual(mp.points, mgeom2.tuple) |
137 self.assertEqual(mp.points, mgeom2.tuple) |
120 self.assertEqual(mp.n_p, mgeom2.point_count) |
138 self.assertEqual(mp.n_p, mgeom2.point_count) |
121 |
139 |
122 def test04_linestring(self): |
140 def test04_linestring(self): |
123 "Testing LineString objects." |
141 "Testing LineString objects." |
124 prev = OGRGeometry('POINT(0 0)') |
142 prev = OGRGeometry('POINT(0 0)') |
125 for ls in linestrings: |
143 for ls in linestrings: |
126 linestr = OGRGeometry(ls.wkt) |
144 linestr = OGRGeometry(ls.wkt) |
152 self.assertEqual(True, mlinestr != prev) |
170 self.assertEqual(True, mlinestr != prev) |
153 prev = mlinestr |
171 prev = mlinestr |
154 for ls in mlinestr: |
172 for ls in mlinestr: |
155 self.assertEqual(2, ls.geom_type) |
173 self.assertEqual(2, ls.geom_type) |
156 self.assertEqual('LINESTRING', ls.geom_name) |
174 self.assertEqual('LINESTRING', ls.geom_name) |
157 self.assertRaises(OGRIndexError, mlinestr.__getitem__, len(mlinestr)) |
175 self.assertRaises(OGRIndexError, mlinestr.__getitem__, len(mlinestr)) |
158 |
176 |
159 def test06_linearring(self): |
177 def test06_linearring(self): |
160 "Testing LinearRing objects." |
178 "Testing LinearRing objects." |
161 prev = OGRGeometry('POINT(0 0)') |
179 prev = OGRGeometry('POINT(0 0)') |
162 for rr in linearrings: |
180 for rr in linearrings: |
170 |
188 |
171 def test07a_polygons(self): |
189 def test07a_polygons(self): |
172 "Testing Polygon objects." |
190 "Testing Polygon objects." |
173 |
191 |
174 # Testing `from_bbox` class method |
192 # Testing `from_bbox` class method |
175 bbox = (-180,-90,180,90) |
193 bbox = (-180,-90,180,90) |
176 p = OGRGeometry.from_bbox( bbox ) |
194 p = OGRGeometry.from_bbox( bbox ) |
177 self.assertEqual(bbox, p.extent) |
195 self.assertEqual(bbox, p.extent) |
178 |
196 |
179 prev = OGRGeometry('POINT(0 0)') |
197 prev = OGRGeometry('POINT(0 0)') |
180 for p in polygons: |
198 for p in polygons: |
191 self.assertAlmostEqual(p.centroid[1], y, 9) |
209 self.assertAlmostEqual(p.centroid[1], y, 9) |
192 |
210 |
193 # Testing equivalence |
211 # Testing equivalence |
194 self.assertEqual(True, poly == OGRGeometry(p.wkt)) |
212 self.assertEqual(True, poly == OGRGeometry(p.wkt)) |
195 self.assertEqual(True, poly != prev) |
213 self.assertEqual(True, poly != prev) |
196 |
214 |
197 if p.ext_ring_cs: |
215 if p.ext_ring_cs: |
198 ring = poly[0] |
216 ring = poly[0] |
199 self.assertEqual(p.ext_ring_cs, ring.tuple) |
217 self.assertEqual(p.ext_ring_cs, ring.tuple) |
200 self.assertEqual(p.ext_ring_cs, poly[0].tuple) |
218 self.assertEqual(p.ext_ring_cs, poly[0].tuple) |
201 self.assertEqual(len(p.ext_ring_cs), ring.point_count) |
219 self.assertEqual(len(p.ext_ring_cs), ring.point_count) |
202 |
220 |
203 for r in poly: |
221 for r in poly: |
204 self.assertEqual('LINEARRING', r.geom_name) |
222 self.assertEqual('LINEARRING', r.geom_name) |
205 |
223 |
206 def test07b_closepolygons(self): |
224 def test07b_closepolygons(self): |
207 "Testing closing Polygon objects." |
225 "Testing closing Polygon objects." |
249 for mp in multipolygons: |
267 for mp in multipolygons: |
250 # Creating a geometry w/spatial reference |
268 # Creating a geometry w/spatial reference |
251 sr = SpatialReference('WGS84') |
269 sr = SpatialReference('WGS84') |
252 mpoly = OGRGeometry(mp.wkt, sr) |
270 mpoly = OGRGeometry(mp.wkt, sr) |
253 self.assertEqual(sr.wkt, mpoly.srs.wkt) |
271 self.assertEqual(sr.wkt, mpoly.srs.wkt) |
254 |
272 |
255 # Ensuring that SRS is propagated to clones. |
273 # Ensuring that SRS is propagated to clones. |
256 klone = mpoly.clone() |
274 klone = mpoly.clone() |
257 self.assertEqual(sr.wkt, klone.srs.wkt) |
275 self.assertEqual(sr.wkt, klone.srs.wkt) |
258 |
276 |
259 # Ensuring all children geometries (polygons and their rings) all |
277 # Ensuring all children geometries (polygons and their rings) all |
260 # return the assigned spatial reference as well. |
278 # return the assigned spatial reference as well. |
261 for poly in mpoly: |
279 for poly in mpoly: |
262 self.assertEqual(sr.wkt, poly.srs.wkt) |
280 self.assertEqual(sr.wkt, poly.srs.wkt) |
263 for ring in poly: |
281 for ring in poly: |
275 mpoly = OGRGeometry(mp.wkt, 4326) |
293 mpoly = OGRGeometry(mp.wkt, 4326) |
276 self.assertEqual(4326, mpoly.srid) |
294 self.assertEqual(4326, mpoly.srid) |
277 mpoly.srs = SpatialReference(4269) |
295 mpoly.srs = SpatialReference(4269) |
278 self.assertEqual(4269, mpoly.srid) |
296 self.assertEqual(4269, mpoly.srid) |
279 self.assertEqual('NAD83', mpoly.srs.name) |
297 self.assertEqual('NAD83', mpoly.srs.name) |
280 |
298 |
281 # Incrementing through the multipolyogn after the spatial reference |
299 # Incrementing through the multipolyogn after the spatial reference |
282 # has been re-assigned. |
300 # has been re-assigned. |
283 for poly in mpoly: |
301 for poly in mpoly: |
284 self.assertEqual(mpoly.srs.wkt, poly.srs.wkt) |
302 self.assertEqual(mpoly.srs.wkt, poly.srs.wkt) |
285 poly.srs = 32140 |
303 poly.srs = 32140 |
316 |
334 |
317 prec = 3 |
335 prec = 3 |
318 for p in (t1, t2, t3, k2): |
336 for p in (t1, t2, t3, k2): |
319 self.assertAlmostEqual(trans.x, p.x, prec) |
337 self.assertAlmostEqual(trans.x, p.x, prec) |
320 self.assertAlmostEqual(trans.y, p.y, prec) |
338 self.assertAlmostEqual(trans.y, p.y, prec) |
|
339 |
|
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) |
321 |
351 |
322 def test10_difference(self): |
352 def test10_difference(self): |
323 "Testing difference()." |
353 "Testing difference()." |
324 for i in xrange(len(topology_geoms)): |
354 for i in xrange(len(topology_geoms)): |
325 g_tup = topology_geoms[i] |
355 g_tup = topology_geoms[i] |
356 d2 = a.sym_difference(b) |
386 d2 = a.sym_difference(b) |
357 self.assertEqual(d1, d2) |
387 self.assertEqual(d1, d2) |
358 self.assertEqual(d1, a ^ b) # __xor__ is symmetric difference operator |
388 self.assertEqual(d1, a ^ b) # __xor__ is symmetric difference operator |
359 a ^= b # testing __ixor__ |
389 a ^= b # testing __ixor__ |
360 self.assertEqual(d1, a) |
390 self.assertEqual(d1, a) |
361 |
391 |
362 def test13_union(self): |
392 def test13_union(self): |
363 "Testing union()." |
393 "Testing union()." |
364 for i in xrange(len(topology_geoms)): |
394 for i in xrange(len(topology_geoms)): |
365 g_tup = topology_geoms[i] |
395 g_tup = topology_geoms[i] |
366 a = OGRGeometry(g_tup[0].wkt) |
396 a = OGRGeometry(g_tup[0].wkt) |
404 x, y = ring.x, ring.y |
434 x, y = ring.x, ring.y |
405 xmin, ymin = min(x), min(y) |
435 xmin, ymin = min(x), min(y) |
406 xmax, ymax = max(x), max(y) |
436 xmax, ymax = max(x), max(y) |
407 self.assertEqual((xmin, ymin, xmax, ymax), poly.extent) |
437 self.assertEqual((xmin, ymin, xmax, ymax), poly.extent) |
408 |
438 |
|
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 |
409 def suite(): |
489 def suite(): |
410 s = unittest.TestSuite() |
490 s = unittest.TestSuite() |
411 s.addTest(unittest.makeSuite(OGRGeomTest)) |
491 s.addTest(unittest.makeSuite(OGRGeomTest)) |
412 return s |
492 return s |
413 |
493 |