web/lib/django/contrib/gis/gdal/tests/test_geom.py
changeset 0 0d40e90630ef
child 29 cc9b7e14412b
equal deleted inserted replaced
-1:000000000000 0:0d40e90630ef
       
     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 
       
    49     def test01a_wkt(self):
       
    50         "Testing WKT output."
       
    51         for g in wkt_out:
       
    52             geom = OGRGeometry(g.wkt)
       
    53             self.assertEqual(g.wkt, geom.wkt)
       
    54 
       
    55     def test01b_gml(self):
       
    56         "Testing GML output."
       
    57         for g in wkt_out:
       
    58             geom = OGRGeometry(g.wkt)
       
    59             self.assertEqual(g.gml, geom.gml)
       
    60 
       
    61     def test01c_hex(self):
       
    62         "Testing HEX input/output."
       
    63         for g in hex_wkt:
       
    64             geom1 = OGRGeometry(g.wkt)
       
    65             self.assertEqual(g.hex, geom1.hex)
       
    66             # Constructing w/HEX
       
    67             geom2 = OGRGeometry(g.hex)
       
    68             self.assertEqual(geom1, geom2)
       
    69 
       
    70     def test01d_wkb(self):
       
    71         "Testing WKB input/output."
       
    72         from binascii import b2a_hex
       
    73         for g in hex_wkt:
       
    74             geom1 = OGRGeometry(g.wkt)
       
    75             wkb = geom1.wkb
       
    76             self.assertEqual(b2a_hex(wkb).upper(), g.hex)
       
    77             # Constructing w/WKB.
       
    78             geom2 = OGRGeometry(wkb)
       
    79             self.assertEqual(geom1, geom2)
       
    80 
       
    81     def test01e_json(self):
       
    82         "Testing GeoJSON input/output."
       
    83         from django.contrib.gis.gdal.prototypes.geom import GEOJSON
       
    84         if not GEOJSON: return
       
    85         for g in json_geoms:
       
    86             geom = OGRGeometry(g.wkt)
       
    87             if not hasattr(g, 'not_equal'):
       
    88                 self.assertEqual(g.json, geom.json)
       
    89                 self.assertEqual(g.json, geom.geojson)
       
    90             self.assertEqual(OGRGeometry(g.wkt), OGRGeometry(geom.json))
       
    91 
       
    92     def test02_points(self):
       
    93         "Testing Point objects."
       
    94 
       
    95         prev = OGRGeometry('POINT(0 0)')
       
    96         for p in points:
       
    97             if not hasattr(p, 'z'): # No 3D
       
    98                 pnt = OGRGeometry(p.wkt)
       
    99                 self.assertEqual(1, pnt.geom_type)
       
   100                 self.assertEqual('POINT', pnt.geom_name)
       
   101                 self.assertEqual(p.x, pnt.x)
       
   102                 self.assertEqual(p.y, pnt.y)
       
   103                 self.assertEqual((p.x, p.y), pnt.tuple)
       
   104 
       
   105     def test03_multipoints(self):
       
   106         "Testing MultiPoint objects."
       
   107 
       
   108         for mp in multipoints:
       
   109             mgeom1 = OGRGeometry(mp.wkt) # First one from WKT
       
   110             self.assertEqual(4, mgeom1.geom_type)
       
   111             self.assertEqual('MULTIPOINT', mgeom1.geom_name)
       
   112             mgeom2 = OGRGeometry('MULTIPOINT') # Creating empty multipoint
       
   113             mgeom3 = OGRGeometry('MULTIPOINT')
       
   114             for g in mgeom1:
       
   115                 mgeom2.add(g) # adding each point from the multipoints
       
   116                 mgeom3.add(g.wkt) # should take WKT as well
       
   117             self.assertEqual(mgeom1, mgeom2) # they should equal
       
   118             self.assertEqual(mgeom1, mgeom3)
       
   119             self.assertEqual(mp.points, mgeom2.tuple)
       
   120             self.assertEqual(mp.n_p, mgeom2.point_count)
       
   121                                                                             
       
   122     def test04_linestring(self):
       
   123         "Testing LineString objects."
       
   124         prev = OGRGeometry('POINT(0 0)')
       
   125         for ls in linestrings:
       
   126             linestr = OGRGeometry(ls.wkt)
       
   127             self.assertEqual(2, linestr.geom_type)
       
   128             self.assertEqual('LINESTRING', linestr.geom_name)
       
   129             self.assertEqual(ls.n_p, linestr.point_count)
       
   130             self.assertEqual(ls.tup, linestr.tuple)
       
   131             self.assertEqual(True, linestr == OGRGeometry(ls.wkt))
       
   132             self.assertEqual(True, linestr != prev)
       
   133             self.assertRaises(OGRIndexError, linestr.__getitem__, len(linestr))
       
   134             prev = linestr
       
   135 
       
   136             # Testing the x, y properties.
       
   137             x = [tmpx for tmpx, tmpy in ls.tup]
       
   138             y = [tmpy for tmpx, tmpy in ls.tup]
       
   139             self.assertEqual(x, linestr.x)
       
   140             self.assertEqual(y, linestr.y)
       
   141 
       
   142     def test05_multilinestring(self):
       
   143         "Testing MultiLineString objects."
       
   144         prev = OGRGeometry('POINT(0 0)')
       
   145         for mls in multilinestrings:
       
   146             mlinestr = OGRGeometry(mls.wkt)
       
   147             self.assertEqual(5, mlinestr.geom_type)
       
   148             self.assertEqual('MULTILINESTRING', mlinestr.geom_name)
       
   149             self.assertEqual(mls.n_p, mlinestr.point_count)
       
   150             self.assertEqual(mls.tup, mlinestr.tuple)
       
   151             self.assertEqual(True, mlinestr == OGRGeometry(mls.wkt))
       
   152             self.assertEqual(True, mlinestr != prev)
       
   153             prev = mlinestr
       
   154             for ls in mlinestr:
       
   155                 self.assertEqual(2, ls.geom_type)
       
   156                 self.assertEqual('LINESTRING', ls.geom_name)
       
   157             self.assertRaises(OGRIndexError, mlinestr.__getitem__, len(mlinestr)) 
       
   158 
       
   159     def test06_linearring(self):
       
   160         "Testing LinearRing objects."
       
   161         prev = OGRGeometry('POINT(0 0)')
       
   162         for rr in linearrings:
       
   163             lr = OGRGeometry(rr.wkt)
       
   164             #self.assertEqual(101, lr.geom_type.num)
       
   165             self.assertEqual('LINEARRING', lr.geom_name)
       
   166             self.assertEqual(rr.n_p, len(lr))
       
   167             self.assertEqual(True, lr == OGRGeometry(rr.wkt))
       
   168             self.assertEqual(True, lr != prev)
       
   169             prev = lr
       
   170 
       
   171     def test07a_polygons(self):
       
   172         "Testing Polygon objects."
       
   173 
       
   174         # Testing `from_bbox` class method
       
   175         bbox =  (-180,-90,180,90) 
       
   176         p = OGRGeometry.from_bbox( bbox )
       
   177         self.assertEqual(bbox, p.extent)
       
   178 
       
   179         prev = OGRGeometry('POINT(0 0)')
       
   180         for p in polygons:
       
   181             poly = OGRGeometry(p.wkt)
       
   182             self.assertEqual(3, poly.geom_type)
       
   183             self.assertEqual('POLYGON', poly.geom_name)
       
   184             self.assertEqual(p.n_p, poly.point_count)
       
   185             self.assertEqual(p.n_i + 1, len(poly))
       
   186 
       
   187             # Testing area & centroid.
       
   188             self.assertAlmostEqual(p.area, poly.area, 9)
       
   189             x, y = poly.centroid.tuple
       
   190             self.assertAlmostEqual(p.centroid[0], x, 9)
       
   191             self.assertAlmostEqual(p.centroid[1], y, 9)
       
   192 
       
   193             # Testing equivalence
       
   194             self.assertEqual(True, poly == OGRGeometry(p.wkt))
       
   195             self.assertEqual(True, poly != prev)
       
   196             
       
   197             if p.ext_ring_cs:
       
   198                 ring = poly[0]
       
   199                 self.assertEqual(p.ext_ring_cs, ring.tuple)
       
   200                 self.assertEqual(p.ext_ring_cs, poly[0].tuple)
       
   201                 self.assertEqual(len(p.ext_ring_cs), ring.point_count)
       
   202             
       
   203             for r in poly:
       
   204                 self.assertEqual('LINEARRING', r.geom_name)
       
   205 
       
   206     def test07b_closepolygons(self):
       
   207         "Testing closing Polygon objects."
       
   208         # Both rings in this geometry are not closed.
       
   209         poly = OGRGeometry('POLYGON((0 0, 5 0, 5 5, 0 5), (1 1, 2 1, 2 2, 2 1))')
       
   210         self.assertEqual(8, poly.point_count)
       
   211         print "\nBEGIN - expecting IllegalArgumentException; safe to ignore.\n"
       
   212         try:
       
   213             c = poly.centroid
       
   214         except OGRException:
       
   215             # Should raise an OGR exception, rings are not closed
       
   216             pass
       
   217         else:
       
   218             self.fail('Should have raised an OGRException!')
       
   219         print "\nEND - expecting IllegalArgumentException; safe to ignore.\n"
       
   220 
       
   221         # Closing the rings -- doesn't work on GDAL versions 1.4.1 and below:
       
   222         # http://trac.osgeo.org/gdal/ticket/1673
       
   223         major, minor1, minor2 = gdal_version().split('.')
       
   224         if major == '1':
       
   225             iminor1 = int(minor1)
       
   226             if iminor1 < 4 or (iminor1 == 4 and minor2.startswith('1')): return
       
   227         poly.close_rings()
       
   228         self.assertEqual(10, poly.point_count) # Two closing points should've been added
       
   229         self.assertEqual(OGRGeometry('POINT(2.5 2.5)'), poly.centroid)
       
   230 
       
   231     def test08_multipolygons(self):
       
   232         "Testing MultiPolygon objects."
       
   233         prev = OGRGeometry('POINT(0 0)')
       
   234         for mp in multipolygons:
       
   235             mpoly = OGRGeometry(mp.wkt)
       
   236             self.assertEqual(6, mpoly.geom_type)
       
   237             self.assertEqual('MULTIPOLYGON', mpoly.geom_name)
       
   238             if mp.valid:
       
   239                 self.assertEqual(mp.n_p, mpoly.point_count)
       
   240                 self.assertEqual(mp.num_geom, len(mpoly))
       
   241                 self.assertRaises(OGRIndexError, mpoly.__getitem__, len(mpoly))
       
   242                 for p in mpoly:
       
   243                     self.assertEqual('POLYGON', p.geom_name)
       
   244                     self.assertEqual(3, p.geom_type)
       
   245             self.assertEqual(mpoly.wkt, OGRGeometry(mp.wkt).wkt)
       
   246 
       
   247     def test09a_srs(self):
       
   248         "Testing OGR Geometries with Spatial Reference objects."
       
   249         for mp in multipolygons:
       
   250             # Creating a geometry w/spatial reference
       
   251             sr = SpatialReference('WGS84')
       
   252             mpoly = OGRGeometry(mp.wkt, sr)
       
   253             self.assertEqual(sr.wkt, mpoly.srs.wkt)
       
   254           
       
   255             # Ensuring that SRS is propagated to clones.
       
   256             klone = mpoly.clone()
       
   257             self.assertEqual(sr.wkt, klone.srs.wkt)
       
   258   
       
   259             # Ensuring all children geometries (polygons and their rings) all
       
   260             # return the assigned spatial reference as well.
       
   261             for poly in mpoly:
       
   262                 self.assertEqual(sr.wkt, poly.srs.wkt)
       
   263                 for ring in poly:
       
   264                     self.assertEqual(sr.wkt, ring.srs.wkt)
       
   265 
       
   266             # Ensuring SRS propagate in topological ops.
       
   267             a, b = topology_geoms[0]
       
   268             a, b = OGRGeometry(a.wkt, sr), OGRGeometry(b.wkt, sr)
       
   269             diff = a.difference(b)
       
   270             union = a.union(b)
       
   271             self.assertEqual(sr.wkt, diff.srs.wkt)
       
   272             self.assertEqual(sr.srid, union.srs.srid)
       
   273 
       
   274             # Instantiating w/an integer SRID
       
   275             mpoly = OGRGeometry(mp.wkt, 4326)
       
   276             self.assertEqual(4326, mpoly.srid)
       
   277             mpoly.srs = SpatialReference(4269)
       
   278             self.assertEqual(4269, mpoly.srid)
       
   279             self.assertEqual('NAD83', mpoly.srs.name)
       
   280           
       
   281             # Incrementing through the multipolyogn after the spatial reference
       
   282             # has been re-assigned.
       
   283             for poly in mpoly:
       
   284                 self.assertEqual(mpoly.srs.wkt, poly.srs.wkt)
       
   285                 poly.srs = 32140
       
   286                 for ring in poly:
       
   287                     # Changing each ring in the polygon
       
   288                     self.assertEqual(32140, ring.srs.srid)
       
   289                     self.assertEqual('NAD83 / Texas South Central', ring.srs.name)
       
   290                     ring.srs = str(SpatialReference(4326)) # back to WGS84
       
   291                     self.assertEqual(4326, ring.srs.srid)
       
   292 
       
   293                     # Using the `srid` property.
       
   294                     ring.srid = 4322
       
   295                     self.assertEqual('WGS 72', ring.srs.name)
       
   296                     self.assertEqual(4322, ring.srid)
       
   297 
       
   298     def test09b_srs_transform(self):
       
   299         "Testing transform()."
       
   300         orig = OGRGeometry('POINT (-104.609 38.255)', 4326)
       
   301         trans = OGRGeometry('POINT (992385.4472045 481455.4944650)', 2774)
       
   302 
       
   303         # Using an srid, a SpatialReference object, and a CoordTransform object
       
   304         # or transformations.
       
   305         t1, t2, t3 = orig.clone(), orig.clone(), orig.clone()
       
   306         t1.transform(trans.srid)
       
   307         t2.transform(SpatialReference('EPSG:2774'))
       
   308         ct = CoordTransform(SpatialReference('WGS84'), SpatialReference(2774))
       
   309         t3.transform(ct)
       
   310 
       
   311         # Testing use of the `clone` keyword.
       
   312         k1 = orig.clone()
       
   313         k2 = k1.transform(trans.srid, clone=True)
       
   314         self.assertEqual(k1, orig)
       
   315         self.assertNotEqual(k1, k2)
       
   316 
       
   317         prec = 3
       
   318         for p in (t1, t2, t3, k2):
       
   319             self.assertAlmostEqual(trans.x, p.x, prec)
       
   320             self.assertAlmostEqual(trans.y, p.y, prec)
       
   321 
       
   322     def test10_difference(self):
       
   323         "Testing difference()."
       
   324         for i in xrange(len(topology_geoms)):
       
   325             g_tup = topology_geoms[i]
       
   326             a = OGRGeometry(g_tup[0].wkt)
       
   327             b = OGRGeometry(g_tup[1].wkt)
       
   328             d1 = OGRGeometry(diff_geoms[i].wkt)
       
   329             d2 = a.difference(b)
       
   330             self.assertEqual(d1, d2)
       
   331             self.assertEqual(d1, a - b) # __sub__ is difference operator
       
   332             a -= b # testing __isub__
       
   333             self.assertEqual(d1, a)
       
   334 
       
   335     def test11_intersection(self):
       
   336         "Testing intersects() and intersection()."
       
   337         for i in xrange(len(topology_geoms)):
       
   338             g_tup = topology_geoms[i]
       
   339             a = OGRGeometry(g_tup[0].wkt)
       
   340             b = OGRGeometry(g_tup[1].wkt)
       
   341             i1 = OGRGeometry(intersect_geoms[i].wkt)
       
   342             self.assertEqual(True, a.intersects(b))
       
   343             i2 = a.intersection(b)
       
   344             self.assertEqual(i1, i2)
       
   345             self.assertEqual(i1, a & b) # __and__ is intersection operator
       
   346             a &= b # testing __iand__
       
   347             self.assertEqual(i1, a)
       
   348 
       
   349     def test12_symdifference(self):
       
   350         "Testing sym_difference()."
       
   351         for i in xrange(len(topology_geoms)):
       
   352             g_tup = topology_geoms[i]
       
   353             a = OGRGeometry(g_tup[0].wkt)
       
   354             b = OGRGeometry(g_tup[1].wkt)
       
   355             d1 = OGRGeometry(sdiff_geoms[i].wkt)
       
   356             d2 = a.sym_difference(b)
       
   357             self.assertEqual(d1, d2)
       
   358             self.assertEqual(d1, a ^ b) # __xor__ is symmetric difference operator
       
   359             a ^= b # testing __ixor__
       
   360             self.assertEqual(d1, a)
       
   361             
       
   362     def test13_union(self):
       
   363         "Testing union()."
       
   364         for i in xrange(len(topology_geoms)):
       
   365             g_tup = topology_geoms[i]
       
   366             a = OGRGeometry(g_tup[0].wkt)
       
   367             b = OGRGeometry(g_tup[1].wkt)
       
   368             u1 = OGRGeometry(union_geoms[i].wkt)
       
   369             u2 = a.union(b)
       
   370             self.assertEqual(u1, u2)
       
   371             self.assertEqual(u1, a | b) # __or__ is union operator
       
   372             a |= b # testing __ior__
       
   373             self.assertEqual(u1, a)
       
   374 
       
   375     def test14_add(self):
       
   376         "Testing GeometryCollection.add()."
       
   377         # Can't insert a Point into a MultiPolygon.
       
   378         mp = OGRGeometry('MultiPolygon')
       
   379         pnt = OGRGeometry('POINT(5 23)')
       
   380         self.assertRaises(OGRException, mp.add, pnt)
       
   381 
       
   382         # GeometryCollection.add may take an OGRGeometry (if another collection
       
   383         # of the same type all child geoms will be added individually) or WKT.
       
   384         for mp in multipolygons:
       
   385             mpoly = OGRGeometry(mp.wkt)
       
   386             mp1 = OGRGeometry('MultiPolygon')
       
   387             mp2 = OGRGeometry('MultiPolygon')
       
   388             mp3 = OGRGeometry('MultiPolygon')
       
   389 
       
   390             for poly in mpoly:
       
   391                 mp1.add(poly) # Adding a geometry at a time
       
   392                 mp2.add(poly.wkt) # Adding WKT
       
   393             mp3.add(mpoly) # Adding a MultiPolygon's entire contents at once.
       
   394             for tmp in (mp1, mp2, mp3): self.assertEqual(mpoly, tmp)
       
   395 
       
   396     def test15_extent(self):
       
   397         "Testing `extent` property."
       
   398         # The xmin, ymin, xmax, ymax of the MultiPoint should be returned.
       
   399         mp = OGRGeometry('MULTIPOINT(5 23, 0 0, 10 50)')
       
   400         self.assertEqual((0.0, 0.0, 10.0, 50.0), mp.extent)
       
   401         # Testing on the 'real world' Polygon.
       
   402         poly = OGRGeometry(polygons[3].wkt)
       
   403         ring = poly.shell
       
   404         x, y = ring.x, ring.y
       
   405         xmin, ymin = min(x), min(y)
       
   406         xmax, ymax = max(x), max(y)
       
   407         self.assertEqual((xmin, ymin, xmax, ymax), poly.extent)
       
   408 
       
   409 def suite():
       
   410     s = unittest.TestSuite()
       
   411     s.addTest(unittest.makeSuite(OGRGeomTest))
       
   412     return s
       
   413 
       
   414 def run(verbosity=2):
       
   415     unittest.TextTestRunner(verbosity=verbosity).run(suite())