django/contrib/gis/geos/tests/test_geos.py
author Justin Bronn <jbronn@geodjango.org>
Sat Jan 31 15:18:50 2009 -0600 (3 years ago)
branchtrunk
changeset 138 466bece04a15
parent 125 d95f64667c2a
child 140 57acbf59090e
permissions -rw-r--r--
Merged in patch from Aryeh Leib Taurog for #9877, adapting as necessary.
jbronn@1
     1
import random, unittest, sys
jbronn@1
     2
from ctypes import ArgumentError
jbronn@1
     3
from django.contrib.gis.geos import *
jbronn@1
     4
from django.contrib.gis.geos.base import gdal, numpy
jbronn@10
     5
from django.contrib.gis.tests.geometries import *
jbronn@1
     6
    
jbronn@1
     7
class GEOSTest(unittest.TestCase):
jbronn@1
     8
jbronn@1
     9
    @property
jbronn@1
    10
    def null_srid(self):
jbronn@1
    11
        """
jbronn@1
    12
        Returns the proper null SRID depending on the GEOS version.
jbronn@1
    13
        See the comments in `test15_srid` for more details. 
jbronn@1
    14
        """
jbronn@1
    15
        info = geos_version_info()
jbronn@1
    16
        if info['version'] == '3.0.0' and info['release_candidate']:
jbronn@1
    17
            return -1
jbronn@1
    18
        else:
jbronn@1
    19
            return None
jbronn@1
    20
jbronn@1
    21
    def test01a_wkt(self):
jbronn@1
    22
        "Testing WKT output."
jbronn@1
    23
        for g in wkt_out:
jbronn@1
    24
            geom = fromstr(g.wkt)
jbronn@1
    25
            self.assertEqual(g.ewkt, geom.wkt)
jbronn@1
    26
jbronn@1
    27
    def test01b_hex(self):
jbronn@1
    28
        "Testing HEX output."
jbronn@1
    29
        for g in hex_wkt:
jbronn@1
    30
            geom = fromstr(g.wkt)
jbronn@1
    31
            self.assertEqual(g.hex, geom.hex)
jbronn@1
    32
jbronn@1
    33
    def test01c_kml(self):
jbronn@1
    34
        "Testing KML output."
jbronn@1
    35
        for tg in wkt_out:
jbronn@1
    36
            geom = fromstr(tg.wkt)
jbronn@1
    37
            kml = getattr(tg, 'kml', False)
jbronn@1
    38
            if kml: self.assertEqual(kml, geom.kml)
jbronn@1
    39
jbronn@1
    40
    def test01d_errors(self):
jbronn@1
    41
        "Testing the Error handlers."
jbronn@1
    42
        # string-based
jbronn@1
    43
        print "\nBEGIN - expecting GEOS_ERROR; safe to ignore.\n"
jbronn@1
    44
        for err in errors:
jbronn@1
    45
            try:
jbronn@1
    46
                g = fromstr(err.wkt)
jbronn@1
    47
            except (GEOSException, ValueError):
jbronn@1
    48
                pass
jbronn@1
    49
jbronn@1
    50
        # Bad WKB
jbronn@1
    51
        self.assertRaises(GEOSException, GEOSGeometry, buffer('0'))
jbronn@1
    52
jbronn@1
    53
        print "\nEND - expecting GEOS_ERROR; safe to ignore.\n"
jbronn@1
    54
        
jbronn@1
    55
        class NotAGeometry(object):
jbronn@1
    56
            pass
jbronn@1
    57
        
jbronn@1
    58
        # Some other object
jbronn@1
    59
        self.assertRaises(TypeError, GEOSGeometry, NotAGeometry())
jbronn@1
    60
        # None
jbronn@1
    61
        self.assertRaises(TypeError, GEOSGeometry, None)
jbronn@1
    62
                
jbronn@1
    63
    def test01e_wkb(self):
jbronn@1
    64
        "Testing WKB output."
jbronn@1
    65
        from binascii import b2a_hex
jbronn@1
    66
        for g in hex_wkt:
jbronn@1
    67
            geom = fromstr(g.wkt)
jbronn@1
    68
            wkb = geom.wkb
jbronn@1
    69
            self.assertEqual(b2a_hex(wkb).upper(), g.hex)
jbronn@1
    70
jbronn@1
    71
    def test01f_create_hex(self):
jbronn@1
    72
        "Testing creation from HEX."
jbronn@1
    73
        for g in hex_wkt:
jbronn@1
    74
            geom_h = GEOSGeometry(g.hex)
jbronn@1
    75
            # we need to do this so decimal places get normalised
jbronn@1
    76
            geom_t = fromstr(g.wkt)
jbronn@1
    77
            self.assertEqual(geom_t.wkt, geom_h.wkt)
jbronn@1
    78
jbronn@1
    79
    def test01g_create_wkb(self):
jbronn@1
    80
        "Testing creation from WKB."
jbronn@1
    81
        from binascii import a2b_hex
jbronn@1
    82
        for g in hex_wkt:
jbronn@1
    83
            wkb = buffer(a2b_hex(g.hex))
jbronn@1
    84
            geom_h = GEOSGeometry(wkb)
jbronn@1
    85
            # we need to do this so decimal places get normalised
jbronn@1
    86
            geom_t = fromstr(g.wkt)
jbronn@1
    87
            self.assertEqual(geom_t.wkt, geom_h.wkt)
jbronn@1
    88
jbronn@1
    89
    def test01h_ewkt(self):
jbronn@1
    90
        "Testing EWKT."
jbronn@1
    91
        srid = 32140
jbronn@1
    92
        for p in polygons:
jbronn@1
    93
            ewkt = 'SRID=%d;%s' % (srid, p.wkt)
jbronn@1
    94
            poly = fromstr(ewkt)
jbronn@1
    95
            self.assertEqual(srid, poly.srid)
jbronn@1
    96
            self.assertEqual(srid, poly.shell.srid)
jbronn@1
    97
            self.assertEqual(srid, fromstr(poly.ewkt).srid) # Checking export
jbronn@1
    98
    
jbronn@1
    99
    def test01i_json(self):
jbronn@1
   100
        "Testing GeoJSON input/output (via GDAL)."
jbronn@1
   101
        if not gdal or not gdal.GEOJSON: return
jbronn@1
   102
        for g in json_geoms:
jbronn@1
   103
            geom = GEOSGeometry(g.wkt)
jbronn@1
   104
            if not hasattr(g, 'not_equal'):
jbronn@1
   105
                self.assertEqual(g.json, geom.json)
jbronn@1
   106
                self.assertEqual(g.json, geom.geojson)
jbronn@1
   107
            self.assertEqual(GEOSGeometry(g.wkt), GEOSGeometry(geom.json))
jbronn@1
   108
jbronn@1
   109
    def test01j_eq(self):
jbronn@1
   110
        "Testing equivalence."
jbronn@1
   111
        p = fromstr('POINT(5 23)')
jbronn@1
   112
        self.assertEqual(p, p.wkt)
jbronn@1
   113
        self.assertNotEqual(p, 'foo')
jbronn@1
   114
        ls = fromstr('LINESTRING(0 0, 1 1, 5 5)')
jbronn@1
   115
        self.assertEqual(ls, ls.wkt)
jbronn@1
   116
        self.assertNotEqual(p, 'bar')
jbronn@1
   117
        # Error shouldn't be raise on equivalence testing with 
jbronn@1
   118
        # an invalid type.
jbronn@1
   119
        for g in (p, ls):
jbronn@1
   120
            self.assertNotEqual(g, None)
jbronn@1
   121
            self.assertNotEqual(g, {'foo' : 'bar'})
jbronn@1
   122
            self.assertNotEqual(g, False)
jbronn@1
   123
jbronn@1
   124
    def test02a_points(self):
jbronn@1
   125
        "Testing Point objects."
jbronn@1
   126
        prev = fromstr('POINT(0 0)')
jbronn@1
   127
        for p in points:
jbronn@1
   128
            # Creating the point from the WKT
jbronn@1
   129
            pnt = fromstr(p.wkt)
jbronn@1
   130
            self.assertEqual(pnt.geom_type, 'Point')
jbronn@1
   131
            self.assertEqual(pnt.geom_typeid, 0)
jbronn@1
   132
            self.assertEqual(p.x, pnt.x)
jbronn@1
   133
            self.assertEqual(p.y, pnt.y)
jbronn@1
   134
            self.assertEqual(True, pnt == fromstr(p.wkt))
jbronn@1
   135
            self.assertEqual(False, pnt == prev)
jbronn@1
   136
jbronn@1
   137
            # Making sure that the point's X, Y components are what we expect
jbronn@1
   138
            self.assertAlmostEqual(p.x, pnt.tuple[0], 9)
jbronn@1
   139
            self.assertAlmostEqual(p.y, pnt.tuple[1], 9)
jbronn@1
   140
jbronn@1
   141
            # Testing the third dimension, and getting the tuple arguments
jbronn@1
   142
            if hasattr(p, 'z'):
jbronn@1
   143
                self.assertEqual(True, pnt.hasz)
jbronn@1
   144
                self.assertEqual(p.z, pnt.z)
jbronn@1
   145
                self.assertEqual(p.z, pnt.tuple[2], 9)
jbronn@1
   146
                tup_args = (p.x, p.y, p.z)
jbronn@1
   147
                set_tup1 = (2.71, 3.14, 5.23)
jbronn@1
   148
                set_tup2 = (5.23, 2.71, 3.14)
jbronn@1
   149
            else:
jbronn@1
   150
                self.assertEqual(False, pnt.hasz)
jbronn@1
   151
                self.assertEqual(None, pnt.z)
jbronn@1
   152
                tup_args = (p.x, p.y)
jbronn@1
   153
                set_tup1 = (2.71, 3.14)
jbronn@1
   154
                set_tup2 = (3.14, 2.71)
jbronn@1
   155
jbronn@1
   156
            # Centroid operation on point should be point itself
jbronn@1
   157
            self.assertEqual(p.centroid, pnt.centroid.tuple)
jbronn@1
   158
jbronn@1
   159
            # Now testing the different constructors
jbronn@1
   160
            pnt2 = Point(tup_args)  # e.g., Point((1, 2))
jbronn@1
   161
            pnt3 = Point(*tup_args) # e.g., Point(1, 2)
jbronn@1
   162
            self.assertEqual(True, pnt == pnt2)
jbronn@1
   163
            self.assertEqual(True, pnt == pnt3)
jbronn@1
   164
jbronn@1
   165
            # Now testing setting the x and y
jbronn@1
   166
            pnt.y = 3.14
jbronn@1
   167
            pnt.x = 2.71
jbronn@1
   168
            self.assertEqual(3.14, pnt.y)
jbronn@1
   169
            self.assertEqual(2.71, pnt.x)
jbronn@1
   170
jbronn@1
   171
            # Setting via the tuple/coords property
jbronn@1
   172
            pnt.tuple = set_tup1
jbronn@1
   173
            self.assertEqual(set_tup1, pnt.tuple)
jbronn@1
   174
            pnt.coords = set_tup2
jbronn@1
   175
            self.assertEqual(set_tup2, pnt.coords)
jbronn@1
   176
            
jbronn@1
   177
            prev = pnt # setting the previous geometry
jbronn@1
   178
jbronn@1
   179
    def test02b_multipoints(self):
jbronn@1
   180
        "Testing MultiPoint objects."
jbronn@1
   181
        for mp in multipoints:
jbronn@1
   182
            mpnt = fromstr(mp.wkt)
jbronn@1
   183
            self.assertEqual(mpnt.geom_type, 'MultiPoint')
jbronn@1
   184
            self.assertEqual(mpnt.geom_typeid, 4)
jbronn@1
   185
jbronn@1
   186
            self.assertAlmostEqual(mp.centroid[0], mpnt.centroid.tuple[0], 9)
jbronn@1
   187
            self.assertAlmostEqual(mp.centroid[1], mpnt.centroid.tuple[1], 9)
jbronn@1
   188
jbronn@1
   189
            self.assertRaises(GEOSIndexError, mpnt.__getitem__, len(mpnt))
jbronn@1
   190
            self.assertEqual(mp.centroid, mpnt.centroid.tuple)
jbronn@1
   191
            self.assertEqual(mp.points, tuple(m.tuple for m in mpnt))
jbronn@1
   192
            for p in mpnt:
jbronn@1
   193
                self.assertEqual(p.geom_type, 'Point')
jbronn@1
   194
                self.assertEqual(p.geom_typeid, 0)
jbronn@1
   195
                self.assertEqual(p.empty, False)
jbronn@1
   196
                self.assertEqual(p.valid, True)
jbronn@1
   197
jbronn@1
   198
    def test03a_linestring(self):
jbronn@1
   199
        "Testing LineString objects."
jbronn@1
   200
        prev = fromstr('POINT(0 0)')
jbronn@1
   201
        for l in linestrings:
jbronn@1
   202
            ls = fromstr(l.wkt)
jbronn@1
   203
            self.assertEqual(ls.geom_type, 'LineString')
jbronn@1
   204
            self.assertEqual(ls.geom_typeid, 1)
jbronn@1
   205
            self.assertEqual(ls.empty, False)
jbronn@1
   206
            self.assertEqual(ls.ring, False)
jbronn@1
   207
            if hasattr(l, 'centroid'):
jbronn@1
   208
                self.assertEqual(l.centroid, ls.centroid.tuple)
jbronn@1
   209
            if hasattr(l, 'tup'):
jbronn@1
   210
                self.assertEqual(l.tup, ls.tuple)
jbronn@1
   211
                
jbronn@1
   212
            self.assertEqual(True, ls == fromstr(l.wkt))
jbronn@1
   213
            self.assertEqual(False, ls == prev)
jbronn@1
   214
            self.assertRaises(GEOSIndexError, ls.__getitem__, len(ls))
jbronn@1
   215
            prev = ls
jbronn@1
   216
jbronn@1
   217
            # Creating a LineString from a tuple, list, and numpy array
jbronn@1
   218
            self.assertEqual(ls, LineString(ls.tuple))  # tuple
jbronn@1
   219
            self.assertEqual(ls, LineString(*ls.tuple)) # as individual arguments
jbronn@1
   220
            self.assertEqual(ls, LineString([list(tup) for tup in ls.tuple])) # as list
jbronn@1
   221
            self.assertEqual(ls.wkt, LineString(*tuple(Point(tup) for tup in ls.tuple)).wkt) # Point individual arguments
jbronn@1
   222
            if numpy: self.assertEqual(ls, LineString(numpy.array(ls.tuple))) # as numpy array
jbronn@1
   223
jbronn@1
   224
    def test03b_multilinestring(self):
jbronn@1
   225
        "Testing MultiLineString objects."
jbronn@1
   226
        prev = fromstr('POINT(0 0)')
jbronn@1
   227
        for l in multilinestrings:
jbronn@1
   228
            ml = fromstr(l.wkt)
jbronn@1
   229
            self.assertEqual(ml.geom_type, 'MultiLineString')
jbronn@1
   230
            self.assertEqual(ml.geom_typeid, 5)
jbronn@1
   231
jbronn@1
   232
            self.assertAlmostEqual(l.centroid[0], ml.centroid.x, 9)
jbronn@1
   233
            self.assertAlmostEqual(l.centroid[1], ml.centroid.y, 9)
jbronn@1
   234
jbronn@1
   235
            self.assertEqual(True, ml == fromstr(l.wkt))
jbronn@1
   236
            self.assertEqual(False, ml == prev)
jbronn@1
   237
            prev = ml
jbronn@1
   238
jbronn@1
   239
            for ls in ml:
jbronn@1
   240
                self.assertEqual(ls.geom_type, 'LineString')
jbronn@1
   241
                self.assertEqual(ls.geom_typeid, 1)
jbronn@1
   242
                self.assertEqual(ls.empty, False)
jbronn@1
   243
jbronn@1
   244
            self.assertRaises(GEOSIndexError, ml.__getitem__, len(ml))
jbronn@1
   245
            self.assertEqual(ml.wkt, MultiLineString(*tuple(s.clone() for s in ml)).wkt)
jbronn@1
   246
            self.assertEqual(ml, MultiLineString(*tuple(LineString(s.tuple) for s in ml)))
jbronn@1
   247
jbronn@1
   248
    def test04_linearring(self):
jbronn@1
   249
        "Testing LinearRing objects."
jbronn@1
   250
        for rr in linearrings:
jbronn@1
   251
            lr = fromstr(rr.wkt)
jbronn@1
   252
            self.assertEqual(lr.geom_type, 'LinearRing')
jbronn@1
   253
            self.assertEqual(lr.geom_typeid, 2)
jbronn@1
   254
            self.assertEqual(rr.n_p, len(lr))
jbronn@1
   255
            self.assertEqual(True, lr.valid)
jbronn@1
   256
            self.assertEqual(False, lr.empty)
jbronn@1
   257
jbronn@1
   258
            # Creating a LinearRing from a tuple, list, and numpy array
jbronn@1
   259
            self.assertEqual(lr, LinearRing(lr.tuple))
jbronn@1
   260
            self.assertEqual(lr, LinearRing(*lr.tuple))
jbronn@1
   261
            self.assertEqual(lr, LinearRing([list(tup) for tup in lr.tuple]))
jbronn@1
   262
            if numpy: self.assertEqual(lr, LinearRing(numpy.array(lr.tuple)))
jbronn@1
   263
    
jbronn@1
   264
    def test05a_polygons(self):
jbronn@1
   265
        "Testing Polygon objects."
jbronn@13
   266
jbronn@13
   267
        # Testing `from_bbox` class method
jbronn@13
   268
        bbox = (-180, -90, 180, 90)
jbronn@13
   269
        p = Polygon.from_bbox( bbox )
jbronn@13
   270
        self.assertEqual(bbox, p.extent)
jbronn@13
   271
jbronn@1
   272
        prev = fromstr('POINT(0 0)')
jbronn@1
   273
        for p in polygons:
jbronn@1
   274
            # Creating the Polygon, testing its properties.
jbronn@1
   275
            poly = fromstr(p.wkt)
jbronn@1
   276
            self.assertEqual(poly.geom_type, 'Polygon')
jbronn@1
   277
            self.assertEqual(poly.geom_typeid, 3)
jbronn@1
   278
            self.assertEqual(poly.empty, False)
jbronn@1
   279
            self.assertEqual(poly.ring, False)
jbronn@1
   280
            self.assertEqual(p.n_i, poly.num_interior_rings)
jbronn@1
   281
            self.assertEqual(p.n_i + 1, len(poly)) # Testing __len__
jbronn@1
   282
            self.assertEqual(p.n_p, poly.num_points)
jbronn@1
   283
jbronn@1
   284
            # Area & Centroid
jbronn@1
   285
            self.assertAlmostEqual(p.area, poly.area, 9)
jbronn@1
   286
            self.assertAlmostEqual(p.centroid[0], poly.centroid.tuple[0], 9)
jbronn@1
   287
            self.assertAlmostEqual(p.centroid[1], poly.centroid.tuple[1], 9)
jbronn@1
   288
jbronn@1
   289
            # Testing the geometry equivalence
jbronn@1
   290
            self.assertEqual(True, poly == fromstr(p.wkt))
jbronn@1
   291
            self.assertEqual(False, poly == prev) # Should not be equal to previous geometry
jbronn@1
   292
            self.assertEqual(True, poly != prev)
jbronn@1
   293
jbronn@1
   294
            # Testing the exterior ring
jbronn@1
   295
            ring = poly.exterior_ring
jbronn@1
   296
            self.assertEqual(ring.geom_type, 'LinearRing')
jbronn@1
   297
            self.assertEqual(ring.geom_typeid, 2)
jbronn@1
   298
            if p.ext_ring_cs:
jbronn@1
   299
                self.assertEqual(p.ext_ring_cs, ring.tuple)
jbronn@1
   300
                self.assertEqual(p.ext_ring_cs, poly[0].tuple) # Testing __getitem__
jbronn@1
   301
jbronn@1
   302
            # Testing __getitem__ and __setitem__ on invalid indices
jbronn@1
   303
            self.assertRaises(GEOSIndexError, poly.__getitem__, len(poly))
jbronn@1
   304
            self.assertRaises(GEOSIndexError, poly.__setitem__, len(poly), False)
jbronn@1
   305
            self.assertRaises(GEOSIndexError, poly.__getitem__, -1)
jbronn@1
   306
jbronn@1
   307
            # Testing __iter__ 
jbronn@1
   308
            for r in poly:
jbronn@1
   309
                self.assertEqual(r.geom_type, 'LinearRing')
jbronn@1
   310
                self.assertEqual(r.geom_typeid, 2)
jbronn@1
   311
jbronn@1
   312
            # Testing polygon construction.
jbronn@1
   313
            self.assertRaises(TypeError, Polygon.__init__, 0, [1, 2, 3])
jbronn@1
   314
            self.assertRaises(TypeError, Polygon.__init__, 'foo')
jbronn@1
   315
            
jbronn@1
   316
            # Polygon(shell, (hole1, ... holeN))
jbronn@1
   317
            rings = tuple(r for r in poly)
jbronn@1
   318
            self.assertEqual(poly, Polygon(rings[0], rings[1:]))
jbronn@1
   319
            
jbronn@1
   320
            # Polygon(shell_tuple, hole_tuple1, ... , hole_tupleN)
jbronn@1
   321
            ring_tuples = tuple(r.tuple for r in poly)
jbronn@1
   322
            self.assertEqual(poly, Polygon(*ring_tuples))
jbronn@1
   323
jbronn@1
   324
            # Constructing with tuples of LinearRings.
jbronn@1
   325
            self.assertEqual(poly.wkt, Polygon(*tuple(r for r in poly)).wkt)
jbronn@1
   326
            self.assertEqual(poly.wkt, Polygon(*tuple(LinearRing(r.tuple) for r in poly)).wkt)
jbronn@1
   327
jbronn@1
   328
    def test05b_multipolygons(self):
jbronn@1
   329
        "Testing MultiPolygon objects."
jbronn@1
   330
        print "\nBEGIN - expecting GEOS_NOTICE; safe to ignore.\n"
jbronn@1
   331
        prev = fromstr('POINT (0 0)')
jbronn@1
   332
        for mp in multipolygons:
jbronn@1
   333
            mpoly = fromstr(mp.wkt)
jbronn@1
   334
            self.assertEqual(mpoly.geom_type, 'MultiPolygon')
jbronn@1
   335
            self.assertEqual(mpoly.geom_typeid, 6)
jbronn@1
   336
            self.assertEqual(mp.valid, mpoly.valid)
jbronn@1
   337
jbronn@1
   338
            if mp.valid:
jbronn@1
   339
                self.assertEqual(mp.num_geom, mpoly.num_geom)
jbronn@1
   340
                self.assertEqual(mp.n_p, mpoly.num_coords)
jbronn@1
   341
                self.assertEqual(mp.num_geom, len(mpoly))
jbronn@1
   342
                self.assertRaises(GEOSIndexError, mpoly.__getitem__, len(mpoly))
jbronn@1
   343
                for p in mpoly:
jbronn@1
   344
                    self.assertEqual(p.geom_type, 'Polygon')
jbronn@1
   345
                    self.assertEqual(p.geom_typeid, 3)
jbronn@1
   346
                    self.assertEqual(p.valid, True)
jbronn@1
   347
                self.assertEqual(mpoly.wkt, MultiPolygon(*tuple(poly.clone() for poly in mpoly)).wkt)
jbronn@1
   348
jbronn@1
   349
        print "\nEND - expecting GEOS_NOTICE; safe to ignore.\n"  
jbronn@1
   350
jbronn@1
   351
    def test06a_memory_hijinks(self):
jbronn@1
   352
        "Testing Geometry __del__() on rings and polygons."
jbronn@1
   353
        #### Memory issues with rings and polygons
jbronn@1
   354
jbronn@1
   355
        # These tests are needed to ensure sanity with writable geometries.
jbronn@1
   356
jbronn@1
   357
        # Getting a polygon with interior rings, and pulling out the interior rings
jbronn@1
   358
        poly = fromstr(polygons[1].wkt)
jbronn@1
   359
        ring1 = poly[0]
jbronn@1
   360
        ring2 = poly[1]
jbronn@1
   361
jbronn@1
   362
        # These deletes should be 'harmless' since they are done on child geometries
jbronn@1
   363
        del ring1 
jbronn@1
   364
        del ring2
jbronn@1
   365
        ring1 = poly[0]
jbronn@1
   366
        ring2 = poly[1]
jbronn@1
   367
jbronn@1
   368
        # Deleting the polygon
jbronn@1
   369
        del poly
jbronn@1
   370
jbronn@1
   371
        # Access to these rings is OK since they are clones.
jbronn@1
   372
        s1, s2 = str(ring1), str(ring2)
jbronn@1
   373
jbronn@1
   374
        # The previous hijinks tests are now moot because only clones are 
jbronn@1
   375
        # now used =)
jbronn@1
   376
jbronn@1
   377
    def test08_coord_seq(self):
jbronn@1
   378
        "Testing Coordinate Sequence objects."
jbronn@1
   379
        for p in polygons:
jbronn@1
   380
            if p.ext_ring_cs:
jbronn@1
   381
                # Constructing the polygon and getting the coordinate sequence
jbronn@1
   382
                poly = fromstr(p.wkt)
jbronn@1
   383
                cs = poly.exterior_ring.coord_seq
jbronn@1
   384
jbronn@1
   385
                self.assertEqual(p.ext_ring_cs, cs.tuple) # done in the Polygon test too.
jbronn@1
   386
                self.assertEqual(len(p.ext_ring_cs), len(cs)) # Making sure __len__ works
jbronn@1
   387
jbronn@1
   388
                # Checks __getitem__ and __setitem__
jbronn@1
   389
                for i in xrange(len(p.ext_ring_cs)):
jbronn@1
   390
                    c1 = p.ext_ring_cs[i] # Expected value
jbronn@1
   391
                    c2 = cs[i] # Value from coordseq
jbronn@1
   392
                    self.assertEqual(c1, c2)
jbronn@1
   393
jbronn@1
   394
                    # Constructing the test value to set the coordinate sequence with
jbronn@1
   395
                    if len(c1) == 2: tset = (5, 23)
jbronn@1
   396
                    else: tset = (5, 23, 8)
jbronn@1
   397
                    cs[i] = tset
jbronn@1
   398
                    
jbronn@1
   399
                    # Making sure every set point matches what we expect
jbronn@1
   400
                    for j in range(len(tset)):
jbronn@1
   401
                        cs[i] = tset
jbronn@1
   402
                        self.assertEqual(tset[j], cs[i][j])
jbronn@1
   403
jbronn@1
   404
    def test09_relate_pattern(self):
jbronn@1
   405
        "Testing relate() and relate_pattern()."
jbronn@1
   406
        g = fromstr('POINT (0 0)')
jbronn@1
   407
        self.assertRaises(GEOSException, g.relate_pattern, 0, 'invalid pattern, yo')
jbronn@1
   408
        for i in xrange(len(relate_geoms)):
jbronn@1
   409
            g_tup = relate_geoms[i]
jbronn@1
   410
            a = fromstr(g_tup[0].wkt)
jbronn@1
   411
            b = fromstr(g_tup[1].wkt)
jbronn@1
   412
            pat = g_tup[2]
jbronn@1
   413
            result = g_tup[3]
jbronn@1
   414
            self.assertEqual(result, a.relate_pattern(b, pat))
jbronn@1
   415
            self.assertEqual(pat, a.relate(b))
jbronn@1
   416
jbronn@1
   417
    def test10_intersection(self):
jbronn@1
   418
        "Testing intersects() and intersection()."
jbronn@1
   419
        for i in xrange(len(topology_geoms)):
jbronn@1
   420
            g_tup = topology_geoms[i]
jbronn@1
   421
            a = fromstr(g_tup[0].wkt)
jbronn@1
   422
            b = fromstr(g_tup[1].wkt)
jbronn@1
   423
            i1 = fromstr(intersect_geoms[i].wkt) 
jbronn@1
   424
            self.assertEqual(True, a.intersects(b))
jbronn@1
   425
            i2 = a.intersection(b)
jbronn@1
   426
            self.assertEqual(i1, i2)
jbronn@1
   427
            self.assertEqual(i1, a & b) # __and__ is intersection operator
jbronn@1
   428
            a &= b # testing __iand__
jbronn@1
   429
            self.assertEqual(i1, a)
jbronn@1
   430
jbronn@1
   431
    def test11_union(self):
jbronn@1
   432
        "Testing union()."
jbronn@1
   433
        for i in xrange(len(topology_geoms)):
jbronn@1
   434
            g_tup = topology_geoms[i]
jbronn@1
   435
            a = fromstr(g_tup[0].wkt)
jbronn@1
   436
            b = fromstr(g_tup[1].wkt)
jbronn@1
   437
            u1 = fromstr(union_geoms[i].wkt)
jbronn@1
   438
            u2 = a.union(b)
jbronn@1
   439
            self.assertEqual(u1, u2)
jbronn@1
   440
            self.assertEqual(u1, a | b) # __or__ is union operator
jbronn@1
   441
            a |= b # testing __ior__
jbronn@1
   442
            self.assertEqual(u1, a) 
jbronn@1
   443
jbronn@1
   444
    def test12_difference(self):
jbronn@1
   445
        "Testing difference()."
jbronn@1
   446
        for i in xrange(len(topology_geoms)):
jbronn@1
   447
            g_tup = topology_geoms[i]
jbronn@1
   448
            a = fromstr(g_tup[0].wkt)
jbronn@1
   449
            b = fromstr(g_tup[1].wkt)
jbronn@1
   450
            d1 = fromstr(diff_geoms[i].wkt)
jbronn@1
   451
            d2 = a.difference(b)
jbronn@1
   452
            self.assertEqual(d1, d2)
jbronn@1
   453
            self.assertEqual(d1, a - b) # __sub__ is difference operator
jbronn@1
   454
            a -= b # testing __isub__
jbronn@1
   455
            self.assertEqual(d1, a)
jbronn@1
   456
jbronn@1
   457
    def test13_symdifference(self):
jbronn@1
   458
        "Testing sym_difference()."
jbronn@1
   459
        for i in xrange(len(topology_geoms)):
jbronn@1
   460
            g_tup = topology_geoms[i]
jbronn@1
   461
            a = fromstr(g_tup[0].wkt)
jbronn@1
   462
            b = fromstr(g_tup[1].wkt)
jbronn@1
   463
            d1 = fromstr(sdiff_geoms[i].wkt)
jbronn@1
   464
            d2 = a.sym_difference(b)
jbronn@1
   465
            self.assertEqual(d1, d2)
jbronn@1
   466
            self.assertEqual(d1, a ^ b) # __xor__ is symmetric difference operator
jbronn@1
   467
            a ^= b # testing __ixor__
jbronn@1
   468
            self.assertEqual(d1, a)
jbronn@1
   469
jbronn@1
   470
    def test14_buffer(self):
jbronn@1
   471
        "Testing buffer()."
jbronn@1
   472
        for i in xrange(len(buffer_geoms)):
jbronn@1
   473
            g_tup = buffer_geoms[i]
jbronn@1
   474
            g = fromstr(g_tup[0].wkt)
jbronn@1
   475
jbronn@1
   476
            # The buffer we expect
jbronn@1
   477
            exp_buf = fromstr(g_tup[1].wkt)
jbronn@1
   478
jbronn@1
   479
            # Can't use a floating-point for the number of quadsegs.
jbronn@1
   480
            self.assertRaises(ArgumentError, g.buffer, g_tup[2], float(g_tup[3]))
jbronn@1
   481
jbronn@1
   482
            # Constructing our buffer
jbronn@1
   483
            buf = g.buffer(g_tup[2], g_tup[3])
jbronn@1
   484
            self.assertEqual(exp_buf.num_coords, buf.num_coords)
jbronn@1
   485
            self.assertEqual(len(exp_buf), len(buf))
jbronn@1
   486
jbronn@1
   487
            # Now assuring that each point in the buffer is almost equal
jbronn@1
   488
            for j in xrange(len(exp_buf)):
jbronn@1
   489
                exp_ring = exp_buf[j]
jbronn@1
   490
                buf_ring = buf[j]
jbronn@1
   491
                self.assertEqual(len(exp_ring), len(buf_ring))
jbronn@1
   492
                for k in xrange(len(exp_ring)):
jbronn@1
   493
                    # Asserting the X, Y of each point are almost equal (due to floating point imprecision)
jbronn@1
   494
                    self.assertAlmostEqual(exp_ring[k][0], buf_ring[k][0], 9)
jbronn@1
   495
                    self.assertAlmostEqual(exp_ring[k][1], buf_ring[k][1], 9)
jbronn@1
   496
jbronn@1
   497
    def test15_srid(self):
jbronn@1
   498
        "Testing the SRID property and keyword."
jbronn@1
   499
        # Testing SRID keyword on Point
jbronn@1
   500
        pnt = Point(5, 23, srid=4326)
jbronn@1
   501
        self.assertEqual(4326, pnt.srid)
jbronn@1
   502
        pnt.srid = 3084
jbronn@1
   503
        self.assertEqual(3084, pnt.srid)
jbronn@1
   504
        self.assertRaises(ArgumentError, pnt.set_srid, '4326')
jbronn@1
   505
jbronn@1
   506
        # Testing SRID keyword on fromstr(), and on Polygon rings.
jbronn@1
   507
        poly = fromstr(polygons[1].wkt, srid=4269)
jbronn@1
   508
        self.assertEqual(4269, poly.srid)
jbronn@1
   509
        for ring in poly: self.assertEqual(4269, ring.srid)
jbronn@1
   510
        poly.srid = 4326
jbronn@1
   511
        self.assertEqual(4326, poly.shell.srid)
jbronn@1
   512
jbronn@1
   513
        # Testing SRID keyword on GeometryCollection
jbronn@1
   514
        gc = GeometryCollection(Point(5, 23), LineString((0, 0), (1.5, 1.5), (3, 3)), srid=32021)
jbronn@1
   515
        self.assertEqual(32021, gc.srid)
jbronn@1
   516
        for i in range(len(gc)): self.assertEqual(32021, gc[i].srid)
jbronn@1
   517
jbronn@1
   518
        # GEOS may get the SRID from HEXEWKB
jbronn@1
   519
        # 'POINT(5 23)' at SRID=4326 in hex form -- obtained from PostGIS
jbronn@1
   520
        # using `SELECT GeomFromText('POINT (5 23)', 4326);`.
jbronn@1
   521
        hex = '0101000020E610000000000000000014400000000000003740'
jbronn@1
   522
        p1 = fromstr(hex)
jbronn@1
   523
        self.assertEqual(4326, p1.srid)
jbronn@1
   524
jbronn@1
   525
        # In GEOS 3.0.0rc1-4  when the EWKB and/or HEXEWKB is exported,
jbronn@1
   526
        # the SRID information is lost and set to -1 -- this is not a
jbronn@1
   527
        # problem on the 3.0.0 version (another reason to upgrade).  
jbronn@1
   528
        exp_srid = self.null_srid
jbronn@1
   529
jbronn@1
   530
        p2 = fromstr(p1.hex)
jbronn@1
   531
        self.assertEqual(exp_srid, p2.srid)
jbronn@1
   532
        p3 = fromstr(p1.hex, srid=-1) # -1 is intended.
jbronn@1
   533
        self.assertEqual(-1, p3.srid)
jbronn@1
   534
jbronn@1
   535
    def test16_mutable_geometries(self):
jbronn@1
   536
        "Testing the mutability of Polygons and Geometry Collections."
jbronn@1
   537
        ### Testing the mutability of Polygons ###
jbronn@1
   538
        for p in polygons:
jbronn@1
   539
            poly = fromstr(p.wkt)
jbronn@1
   540
jbronn@1
   541
            # Should only be able to use __setitem__ with LinearRing geometries.
jbronn@1
   542
            self.assertRaises(TypeError, poly.__setitem__, 0, LineString((1, 1), (2, 2)))
jbronn@1
   543
jbronn@1
   544
            # Constructing the new shell by adding 500 to every point in the old shell.
jbronn@1
   545
            shell_tup = poly.shell.tuple
jbronn@1
   546
            new_coords = []
jbronn@1
   547
            for point in shell_tup: new_coords.append((point[0] + 500., point[1] + 500.))
jbronn@1
   548
            new_shell = LinearRing(*tuple(new_coords))
jbronn@1
   549
jbronn@1
   550
            # Assigning polygon's exterior ring w/the new shell
jbronn@1
   551
            poly.exterior_ring = new_shell
jbronn@1
   552
            s = str(new_shell) # new shell is still accessible
jbronn@1
   553
            self.assertEqual(poly.exterior_ring, new_shell)
jbronn@1
   554
            self.assertEqual(poly[0], new_shell)
jbronn@1
   555
jbronn@1
   556
        ### Testing the mutability of Geometry Collections
jbronn@1
   557
        for tg in multipoints:
jbronn@1
   558
            mp = fromstr(tg.wkt)
jbronn@1
   559
            for i in range(len(mp)):
jbronn@1
   560
                # Creating a random point.
jbronn@1
   561
                pnt = mp[i]
jbronn@1
   562
                new = Point(random.randint(1, 100), random.randint(1, 100))
jbronn@1
   563
                # Testing the assignment
jbronn@1
   564
                mp[i] = new
jbronn@1
   565
                s = str(new) # what was used for the assignment is still accessible
jbronn@1
   566
                self.assertEqual(mp[i], new)
jbronn@1
   567
                self.assertEqual(mp[i].wkt, new.wkt)
jbronn@1
   568
                self.assertNotEqual(pnt, mp[i])
jbronn@1
   569
jbronn@1
   570
        # MultiPolygons involve much more memory management because each
jbronn@1
   571
        # Polygon w/in the collection has its own rings.
jbronn@1
   572
        for tg in multipolygons:
jbronn@1
   573
            mpoly = fromstr(tg.wkt)
jbronn@1
   574
            for i in xrange(len(mpoly)):
jbronn@1
   575
                poly = mpoly[i]
jbronn@1
   576
                old_poly = mpoly[i]
jbronn@1
   577
                # Offsetting the each ring in the polygon by 500.
jbronn@1
   578
                for j in xrange(len(poly)):
jbronn@1
   579
                    r = poly[j]
jbronn@1
   580
                    for k in xrange(len(r)): r[k] = (r[k][0] + 500., r[k][1] + 500.)
jbronn@1
   581
                    poly[j] = r
jbronn@1
   582
                
jbronn@1
   583
                self.assertNotEqual(mpoly[i], poly)
jbronn@1
   584
                # Testing the assignment
jbronn@1
   585
                mpoly[i] = poly
jbronn@1
   586
                s = str(poly) # Still accessible
jbronn@1
   587
                self.assertEqual(mpoly[i], poly)
jbronn@1
   588
                self.assertNotEqual(mpoly[i], old_poly)
jbronn@1
   589
jbronn@1
   590
        # Extreme (!!) __setitem__ -- no longer works, have to detect
jbronn@1
   591
        # in the first object that __setitem__ is called in the subsequent
jbronn@1
   592
        # objects -- maybe mpoly[0, 0, 0] = (3.14, 2.71)?
jbronn@1
   593
        #mpoly[0][0][0] = (3.14, 2.71)
jbronn@1
   594
        #self.assertEqual((3.14, 2.71), mpoly[0][0][0])
jbronn@1
   595
        # Doing it more slowly..
jbronn@1
   596
        #self.assertEqual((3.14, 2.71), mpoly[0].shell[0])
jbronn@1
   597
        #del mpoly
jbronn@1
   598
    
jbronn@1
   599
    def test17_threed(self):
jbronn@1
   600
        "Testing three-dimensional geometries."
jbronn@1
   601
        # Testing a 3D Point
jbronn@1
   602
        pnt = Point(2, 3, 8)
jbronn@1
   603
        self.assertEqual((2.,3.,8.), pnt.coords)
jbronn@1
   604
        self.assertRaises(TypeError, pnt.set_coords, (1.,2.))
jbronn@1
   605
        pnt.coords = (1.,2.,3.)
jbronn@1
   606
        self.assertEqual((1.,2.,3.), pnt.coords)
jbronn@1
   607
jbronn@1
   608
        # Testing a 3D LineString
jbronn@1
   609
        ls = LineString((2., 3., 8.), (50., 250., -117.))
jbronn@1
   610
        self.assertEqual(((2.,3.,8.), (50.,250.,-117.)), ls.tuple)
jbronn@1
   611
        self.assertRaises(TypeError, ls.__setitem__, 0, (1.,2.))
jbronn@1
   612
        ls[0] = (1.,2.,3.)
jbronn@1
   613
        self.assertEqual((1.,2.,3.), ls[0])
jbronn@1
   614
            
jbronn@1
   615
    def test18_distance(self):
jbronn@1
   616
        "Testing the distance() function."
jbronn@1
   617
        # Distance to self should be 0. 
jbronn@1
   618
        pnt = Point(0, 0)
jbronn@1
   619
        self.assertEqual(0.0, pnt.distance(Point(0, 0)))
jbronn@1
   620
    
jbronn@1
   621
        # Distance should be 1
jbronn@1
   622
        self.assertEqual(1.0, pnt.distance(Point(0, 1)))
jbronn@1
   623
jbronn@1
   624
        # Distance should be ~ sqrt(2)
jbronn@1
   625
        self.assertAlmostEqual(1.41421356237, pnt.distance(Point(1, 1)), 11)
jbronn@1
   626
jbronn@1
   627
        # Distances are from the closest vertex in each geometry --
jbronn@1
   628
        #  should be 3 (distance from (2, 2) to (5, 2)).
jbronn@1
   629
        ls1 = LineString((0, 0), (1, 1), (2, 2))
jbronn@1
   630
        ls2 = LineString((5, 2), (6, 1), (7, 0))
jbronn@1
   631
        self.assertEqual(3, ls1.distance(ls2))
jbronn@1
   632
jbronn@1
   633
    def test19_length(self):
jbronn@1
   634
        "Testing the length property."
jbronn@1
   635
        # Points have 0 length.
jbronn@1
   636
        pnt = Point(0, 0)
jbronn@1
   637
        self.assertEqual(0.0, pnt.length)
jbronn@1
   638
        
jbronn@1
   639
        # Should be ~ sqrt(2)
jbronn@1
   640
        ls = LineString((0, 0), (1, 1))
jbronn@1
   641
        self.assertAlmostEqual(1.41421356237, ls.length, 11)
jbronn@1
   642
jbronn@1
   643
        # Should be circumfrence of Polygon
jbronn@1
   644
        poly = Polygon(LinearRing((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
jbronn@1
   645
        self.assertEqual(4.0, poly.length)
jbronn@1
   646
jbronn@1
   647
        # Should be sum of each element's length in collection.
jbronn@1
   648
        mpoly = MultiPolygon(poly.clone(), poly)
jbronn@1
   649
        self.assertEqual(8.0, mpoly.length)
jbronn@1
   650
jbronn@94
   651
    def test20a_emptyCollections(self):
jbronn@1
   652
        "Testing empty geometries and collections."
jbronn@1
   653
        gc1 = GeometryCollection([])
jbronn@1
   654
        gc2 = fromstr('GEOMETRYCOLLECTION EMPTY')
jbronn@1
   655
        pnt = fromstr('POINT EMPTY')
jbronn@1
   656
        ls = fromstr('LINESTRING EMPTY')
jbronn@1
   657
        poly = fromstr('POLYGON EMPTY')
jbronn@1
   658
        mls = fromstr('MULTILINESTRING EMPTY')
jbronn@1
   659
        mpoly1 = fromstr('MULTIPOLYGON EMPTY')
jbronn@1
   660
        mpoly2 = MultiPolygon(())
jbronn@1
   661
jbronn@1
   662
        for g in [gc1, gc2, pnt, ls, poly, mls, mpoly1, mpoly2]:
jbronn@1
   663
            self.assertEqual(True, g.empty)
jbronn@1
   664
jbronn@1
   665
            # Testing len() and num_geom.
jbronn@1
   666
            if isinstance(g, Polygon):
jbronn@1
   667
                self.assertEqual(1, len(g)) # Has one empty linear ring
jbronn@1
   668
                self.assertEqual(1, g.num_geom)
jbronn@1
   669
                self.assertEqual(0, len(g[0]))
jbronn@1
   670
            elif isinstance(g, (Point, LineString)):
jbronn@1
   671
                self.assertEqual(1, g.num_geom)
jbronn@1
   672
                self.assertEqual(0, len(g))
jbronn@1
   673
            else:
jbronn@1
   674
                self.assertEqual(0, g.num_geom)
jbronn@1
   675
                self.assertEqual(0, len(g))
jbronn@1
   676
jbronn@1
   677
            # Testing __getitem__ (doesn't work on Point or Polygon)
jbronn@1
   678
            if isinstance(g, Point):
jbronn@1
   679
                self.assertRaises(GEOSIndexError, g.get_x)
jbronn@1
   680
            elif isinstance(g, Polygon):
jbronn@1
   681
                lr = g.shell
jbronn@1
   682
                self.assertEqual('LINEARRING EMPTY', lr.wkt)
jbronn@1
   683
                self.assertEqual(0, len(lr))
jbronn@1
   684
                self.assertEqual(True, lr.empty)
jbronn@1
   685
                self.assertRaises(GEOSIndexError, lr.__getitem__, 0)
jbronn@1
   686
            else:
jbronn@1
   687
                self.assertRaises(GEOSIndexError, g.__getitem__, 0)
jbronn@1
   688
jbronn@94
   689
    def test20b_collections_of_collections(self):
jbronn@94
   690
        "Testing GeometryCollection handling of other collections."
jbronn@94
   691
        # Creating a GeometryCollection WKT string composed of other 
jbronn@94
   692
        # collections and polygons.
jbronn@94
   693
        coll = [mp.wkt for mp in multipolygons if mp.valid]
jbronn@94
   694
        coll.extend([mls.wkt for mls in multilinestrings])
jbronn@94
   695
        coll.extend([p.wkt for p in polygons])
jbronn@94
   696
        coll.extend([mp.wkt for mp in multipoints])
jbronn@94
   697
        gc_wkt = 'GEOMETRYCOLLECTION(%s)' % ','.join(coll)
jbronn@94
   698
        
jbronn@94
   699
        # Should construct ok from WKT
jbronn@94
   700
        gc1 = GEOSGeometry(gc_wkt)
jbronn@94
   701
jbronn@94
   702
        # Should also construct ok from individual geometry arguments.
jbronn@94
   703
        gc2 = GeometryCollection(*tuple(g for g in gc1))
jbronn@94
   704
jbronn@94
   705
        # And, they should be equal.
jbronn@94
   706
        self.assertEqual(gc1, gc2)
jbronn@94
   707
jbronn@1
   708
    def test21_test_gdal(self):
jbronn@1
   709
        "Testing `ogr` and `srs` properties."
jbronn@1
   710
        if not gdal.HAS_GDAL: return
jbronn@1
   711
        g1 = fromstr('POINT(5 23)')
jbronn@1
   712
        self.assertEqual(True, isinstance(g1.ogr, gdal.OGRGeometry))
jbronn@1
   713
        self.assertEqual(g1.srs, None)
jbronn@1
   714
        
jbronn@1
   715
        g2 = fromstr('LINESTRING(0 0, 5 5, 23 23)', srid=4326)
jbronn@1
   716
        self.assertEqual(True, isinstance(g2.ogr, gdal.OGRGeometry))
jbronn@1
   717
        self.assertEqual(True, isinstance(g2.srs, gdal.SpatialReference))
jbronn@1
   718
        self.assertEqual(g2.hex, g2.ogr.hex)
jbronn@1
   719
        self.assertEqual('WGS 84', g2.srs.name)
jbronn@1
   720
jbronn@1
   721
    def test22_copy(self):
jbronn@1
   722
        "Testing use with the Python `copy` module."
jbronn@1
   723
        import copy
jbronn@1
   724
        poly = GEOSGeometry('POLYGON((0 0, 0 23, 23 23, 23 0, 0 0), (5 5, 5 10, 10 10, 10 5, 5 5))')
jbronn@1
   725
        cpy1 = copy.copy(poly)
jbronn@1
   726
        cpy2 = copy.deepcopy(poly)
jbronn@1
   727
        self.assertNotEqual(poly._ptr, cpy1._ptr)
jbronn@1
   728
        self.assertNotEqual(poly._ptr, cpy2._ptr)
jbronn@1
   729
jbronn@1
   730
    def test23_transform(self):
jbronn@1
   731
        "Testing `transform` method."
jbronn@1
   732
        if not gdal.HAS_GDAL: return
jbronn@1
   733
        orig = GEOSGeometry('POINT (-104.609 38.255)', 4326)
jbronn@1
   734
        trans = GEOSGeometry('POINT (992385.4472045 481455.4944650)', 2774)
jbronn@1
   735
jbronn@1
   736
        # Using a srid, a SpatialReference object, and a CoordTransform object
jbronn@1
   737
        # for transformations.
jbronn@1
   738
        t1, t2, t3 = orig.clone(), orig.clone(), orig.clone()
jbronn@1
   739
        t1.transform(trans.srid)
jbronn@1
   740
        t2.transform(gdal.SpatialReference('EPSG:2774'))
jbronn@1
   741
        ct = gdal.CoordTransform(gdal.SpatialReference('WGS84'), gdal.SpatialReference(2774))
jbronn@1
   742
        t3.transform(ct)
jbronn@1
   743
jbronn@1
   744
        # Testing use of the `clone` keyword.
jbronn@1
   745
        k1 = orig.clone()
jbronn@1
   746
        k2 = k1.transform(trans.srid, clone=True)
jbronn@1
   747
        self.assertEqual(k1, orig)
jbronn@1
   748
        self.assertNotEqual(k1, k2)
jbronn@1
   749
jbronn@1
   750
        prec = 3
jbronn@1
   751
        for p in (t1, t2, t3, k2):
jbronn@1
   752
            self.assertAlmostEqual(trans.x, p.x, prec)
jbronn@1
   753
            self.assertAlmostEqual(trans.y, p.y, prec)
jbronn@1
   754
jbronn@1
   755
    def test24_extent(self):
jbronn@1
   756
        "Testing `extent` method."
jbronn@1
   757
        # The xmin, ymin, xmax, ymax of the MultiPoint should be returned.
jbronn@1
   758
        mp = MultiPoint(Point(5, 23), Point(0, 0), Point(10, 50))
jbronn@1
   759
        self.assertEqual((0.0, 0.0, 10.0, 50.0), mp.extent)
jbronn@1
   760
        pnt = Point(5.23, 17.8)
jbronn@1
   761
        # Extent of points is just the point itself repeated.
jbronn@1
   762
        self.assertEqual((5.23, 17.8, 5.23, 17.8), pnt.extent)
jbronn@1
   763
        # Testing on the 'real world' Polygon.
jbronn@1
   764
        poly = fromstr(polygons[3].wkt)
jbronn@1
   765
        ring = poly.shell
jbronn@1
   766
        x, y = ring.x, ring.y
jbronn@1
   767
        xmin, ymin = min(x), min(y)
jbronn@1
   768
        xmax, ymax = max(x), max(y)
jbronn@1
   769
        self.assertEqual((xmin, ymin, xmax, ymax), poly.extent)
jbronn@1
   770
jbronn@1
   771
    def test25_pickle(self):
jbronn@1
   772
        "Testing pickling and unpickling support."
jbronn@1
   773
        # Using both pickle and cPickle -- just 'cause.
jbronn@1
   774
        import pickle, cPickle
jbronn@1
   775
jbronn@1
   776
        # Creating a list of test geometries for pickling, 
jbronn@1
   777
        # and setting the SRID on some of them.
jbronn@1
   778
        def get_geoms(lst, srid=None):
jbronn@1
   779
            return [GEOSGeometry(tg.wkt, srid) for tg in lst]
jbronn@1
   780
        tgeoms = get_geoms(points)
jbronn@1
   781
        tgeoms.extend(get_geoms(multilinestrings, 4326))
jbronn@1
   782
        tgeoms.extend(get_geoms(polygons, 3084))
jbronn@1
   783
        tgeoms.extend(get_geoms(multipolygons, 900913))
jbronn@1
   784
jbronn@1
   785
        # The SRID won't be exported in GEOS 3.0 release candidates.
jbronn@1
   786
        no_srid = self.null_srid == -1 
jbronn@1
   787
        for geom in tgeoms:
jbronn@1
   788
            s1, s2 = cPickle.dumps(geom), pickle.dumps(geom)
jbronn@1
   789
            g1, g2 = cPickle.loads(s1), pickle.loads(s2)
jbronn@1
   790
            for tmpg in (g1, g2):
jbronn@1
   791
                self.assertEqual(geom, tmpg)
jbronn@1
   792
                if not no_srid: self.assertEqual(geom.srid, tmpg.srid)
jbronn@1
   793
jbronn@125
   794
    def test26_prepared(self):
jbronn@125
   795
        "Testing PreparedGeometry support."
jbronn@138
   796
        if GEOS_PREPARE: return
jbronn@125
   797
        # Creating a simple multipolygon and getting a prepared version.
jbronn@125
   798
        mpoly = GEOSGeometry('MULTIPOLYGON(((0 0,0 5,5 5,5 0,0 0)),((5 5,5 10,10 10,10 5,5 5)))')
jbronn@125
   799
        prep = mpoly.prepared
jbronn@125
   800
jbronn@125
   801
        # A set of test points.
jbronn@125
   802
        pnts = [Point(5, 5), Point(7.5, 7.5), Point(2.5, 7.5)]
jbronn@125
   803
        covers = [True, True, False] # No `covers` op for regular GEOS geoms.
jbronn@125
   804
        for pnt, c in zip(pnts, covers):
jbronn@125
   805
            # Results should be the same (but faster)
jbronn@125
   806
            self.assertEqual(mpoly.contains(pnt), prep.contains(pnt))
jbronn@125
   807
            self.assertEqual(mpoly.intersects(pnt), prep.intersects(pnt))
jbronn@125
   808
            self.assertEqual(c, prep.covers(pnt))
jbronn@125
   809
jbronn@1
   810
def suite():
jbronn@1
   811
    s = unittest.TestSuite()
jbronn@1
   812
    s.addTest(unittest.makeSuite(GEOSTest))
jbronn@1
   813
    return s
jbronn@1
   814
jbronn@1
   815
def run(verbosity=2):
jbronn@1
   816
    unittest.TextTestRunner(verbosity=verbosity).run(suite())