PageRenderTime 259ms CodeModel.GetById 226ms app.highlight 29ms RepoModel.GetById 0ms app.codeStats 1ms

/django/contrib/gis/gdal/tests/test_geom.py

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