~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to django/contrib/gis/gdal/tests/test_geom.py

  • Committer: Package Import Robot
  • Author(s): Raphaël Hertzog
  • Date: 2014-09-17 14:15:11 UTC
  • mfrom: (1.3.17) (6.2.18 experimental)
  • Revision ID: package-import@ubuntu.com-20140917141511-icneokthe9ww5sk4
Tags: 1.7-2
* Release to unstable.
* Add a migrate-south sample script to help users apply their South
  migrations. Thanks to Brian May.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
    from django.utils.six.moves import cPickle as pickle
5
5
except ImportError:
6
6
    import pickle
 
7
import unittest
 
8
from unittest import skipUnless
7
9
 
8
10
from django.contrib.gis.gdal import HAS_GDAL
9
11
from django.contrib.gis.geometry.test_data import TestDataMixin
10
12
from django.utils.six.moves import xrange
11
 
from django.utils import unittest
12
 
from django.utils.unittest import skipUnless
13
13
 
14
14
if HAS_GDAL:
15
15
    from django.contrib.gis.gdal import (OGRGeometry, OGRGeomType,
25
25
        "Testing OGRGeomType object."
26
26
 
27
27
        # OGRGeomType should initialize on all these inputs.
28
 
        try:
29
 
            g = OGRGeomType(1)
30
 
            g = OGRGeomType(7)
31
 
            g = OGRGeomType('point')
32
 
            g = OGRGeomType('GeometrycollectioN')
33
 
            g = OGRGeomType('LINearrING')
34
 
            g = OGRGeomType('Unknown')
35
 
        except:
36
 
            self.fail('Could not create an OGRGeomType object!')
 
28
        OGRGeomType(1)
 
29
        OGRGeomType(7)
 
30
        OGRGeomType('point')
 
31
        OGRGeomType('GeometrycollectioN')
 
32
        OGRGeomType('LINearrING')
 
33
        OGRGeomType('Unknown')
37
34
 
38
35
        # Should throw TypeError on this input
39
36
        self.assertRaises(OGRException, OGRGeomType, 23)
41
38
        self.assertRaises(OGRException, OGRGeomType, 9)
42
39
 
43
40
        # Equivalence can take strings, ints, and other OGRGeomTypes
44
 
        self.assertEqual(True, OGRGeomType(1) == OGRGeomType(1))
45
 
        self.assertEqual(True, OGRGeomType(7) == 'GeometryCollection')
46
 
        self.assertEqual(True, OGRGeomType('point') == 'POINT')
47
 
        self.assertEqual(False, OGRGeomType('point') == 2)
48
 
        self.assertEqual(True, OGRGeomType('unknown') == 0)
49
 
        self.assertEqual(True, OGRGeomType(6) == 'MULtiPolyGON')
50
 
        self.assertEqual(False, OGRGeomType(1) != OGRGeomType('point'))
51
 
        self.assertEqual(True, OGRGeomType('POINT') != OGRGeomType(6))
 
41
        self.assertEqual(OGRGeomType(1), OGRGeomType(1))
 
42
        self.assertEqual(OGRGeomType(7), 'GeometryCollection')
 
43
        self.assertEqual(OGRGeomType('point'), 'POINT')
 
44
        self.assertNotEqual(OGRGeomType('point'), 2)
 
45
        self.assertEqual(OGRGeomType('unknown'), 0)
 
46
        self.assertEqual(OGRGeomType(6), 'MULtiPolyGON')
 
47
        self.assertEqual(OGRGeomType(1), OGRGeomType('point'))
 
48
        self.assertNotEqual(OGRGeomType('POINT'), OGRGeomType(6))
52
49
 
53
50
        # Testing the Django field name equivalent property.
54
51
        self.assertEqual('PointField', OGRGeomType('Point').django)
 
52
        self.assertEqual('GeometryField', OGRGeomType('Geometry').django)
55
53
        self.assertEqual('GeometryField', OGRGeomType('Unknown').django)
56
 
        self.assertEqual(None, OGRGeomType('none').django)
 
54
        self.assertIsNone(OGRGeomType('none').django)
57
55
 
58
56
        # 'Geometry' initialization implies an unknown geometry type.
59
57
        gt = OGRGeomType('Geometry')
63
61
    def test00b_geomtype_25d(self):
64
62
        "Testing OGRGeomType object with 25D types."
65
63
        wkb25bit = OGRGeomType.wkb25bit
66
 
        self.assertTrue(OGRGeomType(wkb25bit + 1) == 'Point25D')
67
 
        self.assertTrue(OGRGeomType('MultiLineString25D') == (5 + wkb25bit))
 
64
        self.assertEqual(OGRGeomType(wkb25bit + 1), 'Point25D')
 
65
        self.assertEqual(OGRGeomType('MultiLineString25D'), (5 + wkb25bit))
68
66
        self.assertEqual('GeometryCollectionField', OGRGeomType('GeometryCollection25D').django)
69
67
 
70
68
    def test01a_wkt(self):
123
121
                self.assertEqual(json.loads(g.json), json.loads(geom.json))
124
122
                self.assertEqual(json.loads(g.json), json.loads(geom.geojson))
125
123
            self.assertEqual(OGRGeometry(g.wkt), OGRGeometry(geom.json))
 
124
        # Test input with some garbage content (but valid json) (#15529)
 
125
        geom = OGRGeometry('{"type": "Point", "coordinates": [ 100.0, 0.0 ], "other": "<test>"}')
 
126
        self.assertIsInstance(geom, OGRGeometry)
126
127
 
127
128
    def test02_points(self):
128
129
        "Testing Point objects."
129
130
 
130
 
        prev = OGRGeometry('POINT(0 0)')
 
131
        OGRGeometry('POINT(0 0)')
131
132
        for p in self.geometries.points:
132
 
            if not hasattr(p, 'z'): # No 3D
 
133
            if not hasattr(p, 'z'):  # No 3D
133
134
                pnt = OGRGeometry(p.wkt)
134
135
                self.assertEqual(1, pnt.geom_type)
135
136
                self.assertEqual('POINT', pnt.geom_name)
140
141
    def test03_multipoints(self):
141
142
        "Testing MultiPoint objects."
142
143
        for mp in self.geometries.multipoints:
143
 
            mgeom1 = OGRGeometry(mp.wkt) # First one from WKT
 
144
            mgeom1 = OGRGeometry(mp.wkt)  # First one from WKT
144
145
            self.assertEqual(4, mgeom1.geom_type)
145
146
            self.assertEqual('MULTIPOINT', mgeom1.geom_name)
146
 
            mgeom2 = OGRGeometry('MULTIPOINT') # Creating empty multipoint
 
147
            mgeom2 = OGRGeometry('MULTIPOINT')  # Creating empty multipoint
147
148
            mgeom3 = OGRGeometry('MULTIPOINT')
148
149
            for g in mgeom1:
149
 
                mgeom2.add(g) # adding each point from the multipoints
150
 
                mgeom3.add(g.wkt) # should take WKT as well
151
 
            self.assertEqual(mgeom1, mgeom2) # they should equal
 
150
                mgeom2.add(g)  # adding each point from the multipoints
 
151
                mgeom3.add(g.wkt)  # should take WKT as well
 
152
            self.assertEqual(mgeom1, mgeom2)  # they should equal
152
153
            self.assertEqual(mgeom1, mgeom3)
153
154
            self.assertEqual(mp.coords, mgeom2.coords)
154
155
            self.assertEqual(mp.n_p, mgeom2.point_count)
162
163
            self.assertEqual('LINESTRING', linestr.geom_name)
163
164
            self.assertEqual(ls.n_p, linestr.point_count)
164
165
            self.assertEqual(ls.coords, linestr.tuple)
165
 
            self.assertEqual(True, linestr == OGRGeometry(ls.wkt))
166
 
            self.assertEqual(True, linestr != prev)
 
166
            self.assertEqual(linestr, OGRGeometry(ls.wkt))
 
167
            self.assertNotEqual(linestr, prev)
167
168
            self.assertRaises(OGRIndexError, linestr.__getitem__, len(linestr))
168
169
            prev = linestr
169
170
 
182
183
            self.assertEqual('MULTILINESTRING', mlinestr.geom_name)
183
184
            self.assertEqual(mls.n_p, mlinestr.point_count)
184
185
            self.assertEqual(mls.coords, mlinestr.tuple)
185
 
            self.assertEqual(True, mlinestr == OGRGeometry(mls.wkt))
186
 
            self.assertEqual(True, mlinestr != prev)
 
186
            self.assertEqual(mlinestr, OGRGeometry(mls.wkt))
 
187
            self.assertNotEqual(mlinestr, prev)
187
188
            prev = mlinestr
188
189
            for ls in mlinestr:
189
190
                self.assertEqual(2, ls.geom_type)
198
199
            #self.assertEqual(101, lr.geom_type.num)
199
200
            self.assertEqual('LINEARRING', lr.geom_name)
200
201
            self.assertEqual(rr.n_p, len(lr))
201
 
            self.assertEqual(True, lr == OGRGeometry(rr.wkt))
202
 
            self.assertEqual(True, lr != prev)
 
202
            self.assertEqual(lr, OGRGeometry(rr.wkt))
 
203
            self.assertNotEqual(lr, prev)
203
204
            prev = lr
204
205
 
205
206
    def test07a_polygons(self):
206
207
        "Testing Polygon objects."
207
208
 
208
209
        # Testing `from_bbox` class method
209
 
        bbox =  (-180,-90,180,90)
210
 
        p = OGRGeometry.from_bbox( bbox )
 
210
        bbox = (-180, -90, 180, 90)
 
211
        p = OGRGeometry.from_bbox(bbox)
211
212
        self.assertEqual(bbox, p.extent)
212
213
 
213
214
        prev = OGRGeometry('POINT(0 0)')
225
226
            self.assertAlmostEqual(p.centroid[1], y, 9)
226
227
 
227
228
            # Testing equivalence
228
 
            self.assertEqual(True, poly == OGRGeometry(p.wkt))
229
 
            self.assertEqual(True, poly != prev)
 
229
            self.assertEqual(poly, OGRGeometry(p.wkt))
 
230
            self.assertNotEqual(poly, prev)
230
231
 
231
232
            if p.ext_ring_cs:
232
233
                ring = poly[0]
243
244
        poly = OGRGeometry('POLYGON((0 0, 5 0, 5 5, 0 5), (1 1, 2 1, 2 2, 2 1))')
244
245
        self.assertEqual(8, poly.point_count)
245
246
        with self.assertRaises(OGRException):
246
 
            _ = poly.centroid
 
247
            poly.centroid
247
248
 
248
249
        poly.close_rings()
249
 
        self.assertEqual(10, poly.point_count) # Two closing points should've been added
 
250
        self.assertEqual(10, poly.point_count)  # Two closing points should've been added
250
251
        self.assertEqual(OGRGeometry('POINT(2.5 2.5)'), poly.centroid)
251
252
 
252
253
    def test08_multipolygons(self):
253
254
        "Testing MultiPolygon objects."
254
 
        prev = OGRGeometry('POINT(0 0)')
 
255
        OGRGeometry('POINT(0 0)')
255
256
        for mp in self.geometries.multipolygons:
256
257
            mpoly = OGRGeometry(mp.wkt)
257
258
            self.assertEqual(6, mpoly.geom_type)
299
300
            self.assertEqual(4269, mpoly.srid)
300
301
            self.assertEqual('NAD83', mpoly.srs.name)
301
302
 
302
 
            # Incrementing through the multipolyogn after the spatial reference
 
303
            # Incrementing through the multipolygon after the spatial reference
303
304
            # has been re-assigned.
304
305
            for poly in mpoly:
305
306
                self.assertEqual(mpoly.srs.wkt, poly.srs.wkt)
308
309
                    # Changing each ring in the polygon
309
310
                    self.assertEqual(32140, ring.srs.srid)
310
311
                    self.assertEqual('NAD83 / Texas South Central', ring.srs.name)
311
 
                    ring.srs = str(SpatialReference(4326)) # back to WGS84
 
312
                    ring.srs = str(SpatialReference(4326))  # back to WGS84
312
313
                    self.assertEqual(4326, ring.srs.srid)
313
314
 
314
315
                    # Using the `srid` property.
360
361
            d1 = OGRGeometry(self.geometries.diff_geoms[i].wkt)
361
362
            d2 = a.difference(b)
362
363
            self.assertEqual(d1, d2)
363
 
            self.assertEqual(d1, a - b) # __sub__ is difference operator
364
 
            a -= b # testing __isub__
 
364
            self.assertEqual(d1, a - b)  # __sub__ is difference operator
 
365
            a -= b  # testing __isub__
365
366
            self.assertEqual(d1, a)
366
367
 
367
368
    def test11_intersection(self):
370
371
            a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a)
371
372
            b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b)
372
373
            i1 = OGRGeometry(self.geometries.intersect_geoms[i].wkt)
373
 
            self.assertEqual(True, a.intersects(b))
 
374
            self.assertTrue(a.intersects(b))
374
375
            i2 = a.intersection(b)
375
376
            self.assertEqual(i1, i2)
376
 
            self.assertEqual(i1, a & b) # __and__ is intersection operator
377
 
            a &= b # testing __iand__
 
377
            self.assertEqual(i1, a & b)  # __and__ is intersection operator
 
378
            a &= b  # testing __iand__
378
379
            self.assertEqual(i1, a)
379
380
 
380
381
    def test12_symdifference(self):
385
386
            d1 = OGRGeometry(self.geometries.sdiff_geoms[i].wkt)
386
387
            d2 = a.sym_difference(b)
387
388
            self.assertEqual(d1, d2)
388
 
            self.assertEqual(d1, a ^ b) # __xor__ is symmetric difference operator
389
 
            a ^= b # testing __ixor__
 
389
            self.assertEqual(d1, a ^ b)  # __xor__ is symmetric difference operator
 
390
            a ^= b  # testing __ixor__
390
391
            self.assertEqual(d1, a)
391
392
 
392
393
    def test13_union(self):
397
398
            u1 = OGRGeometry(self.geometries.union_geoms[i].wkt)
398
399
            u2 = a.union(b)
399
400
            self.assertEqual(u1, u2)
400
 
            self.assertEqual(u1, a | b) # __or__ is union operator
401
 
            a |= b # testing __ior__
 
401
            self.assertEqual(u1, a | b)  # __or__ is union operator
 
402
            a |= b  # testing __ior__
402
403
            self.assertEqual(u1, a)
403
404
 
404
405
    def test14_add(self):
417
418
            mp3 = OGRGeometry('MultiPolygon')
418
419
 
419
420
            for poly in mpoly:
420
 
                mp1.add(poly) # Adding a geometry at a time
421
 
                mp2.add(poly.wkt) # Adding WKT
422
 
            mp3.add(mpoly) # Adding a MultiPolygon's entire contents at once.
423
 
            for tmp in (mp1, mp2, mp3): self.assertEqual(mpoly, tmp)
 
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):
 
425
                self.assertEqual(mpoly, tmp)
424
426
 
425
427
    def test15_extent(self):
426
428
        "Testing `extent` property."
481
483
 
482
484
    def test19_equivalence_regression(self):
483
485
        "Testing equivalence methods with non-OGRGeometry instances."
484
 
        self.assertNotEqual(None, OGRGeometry('POINT(0 0)'))
485
 
        self.assertEqual(False, OGRGeometry('LINESTRING(0 0, 1 1)') == 3)
 
486
        self.assertIsNotNone(OGRGeometry('POINT(0 0)'))
 
487
        self.assertNotEqual(OGRGeometry('LINESTRING(0 0, 1 1)'), 3)