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