PageRenderTime 181ms CodeModel.GetById 167ms app.highlight 11ms RepoModel.GetById 0ms app.codeStats 1ms

/django/contrib/gis/geos/tests/test_io.py

https://code.google.com/p/mango-py/
Python | 112 lines | 67 code | 25 blank | 20 comment | 12 complexity | dcb19a5a7b8c7a7fb1df39f0d8d4070a MD5 | raw file
  1import binascii, ctypes, unittest
  2from django.contrib.gis.geos import GEOSGeometry, WKTReader, WKTWriter, WKBReader, WKBWriter, geos_version_info
  3
  4class GEOSIOTest(unittest.TestCase):
  5
  6    def test01_wktreader(self):
  7        # Creating a WKTReader instance
  8        wkt_r = WKTReader()
  9        wkt = 'POINT (5 23)'
 10
 11        # read() should return a GEOSGeometry
 12        ref = GEOSGeometry(wkt)
 13        g1 = wkt_r.read(wkt)
 14        g2 = wkt_r.read(unicode(wkt))
 15
 16        for geom in (g1, g2):
 17            self.assertEqual(ref, geom)
 18
 19        # Should only accept basestring objects.
 20        self.assertRaises(TypeError, wkt_r.read, 1)
 21        self.assertRaises(TypeError, wkt_r.read, buffer('foo'))
 22
 23    def test02_wktwriter(self):
 24        # Creating a WKTWriter instance, testing its ptr property.
 25        wkt_w = WKTWriter()
 26        self.assertRaises(TypeError, wkt_w._set_ptr, WKTReader.ptr_type())
 27
 28        ref = GEOSGeometry('POINT (5 23)')
 29        ref_wkt = 'POINT (5.0000000000000000 23.0000000000000000)'
 30        self.assertEqual(ref_wkt, wkt_w.write(ref))
 31
 32    def test03_wkbreader(self):
 33        # Creating a WKBReader instance
 34        wkb_r = WKBReader()
 35
 36        hex = '000000000140140000000000004037000000000000'
 37        wkb = buffer(binascii.a2b_hex(hex))
 38        ref = GEOSGeometry(hex)
 39
 40        # read() should return a GEOSGeometry on either a hex string or
 41        # a WKB buffer.
 42        g1 = wkb_r.read(wkb)
 43        g2 = wkb_r.read(hex)
 44        for geom in (g1, g2):
 45            self.assertEqual(ref, geom)
 46
 47        bad_input = (1, 5.23, None, False)
 48        for bad_wkb in bad_input:
 49            self.assertRaises(TypeError, wkb_r.read, bad_wkb)
 50        
 51    def test04_wkbwriter(self):
 52        wkb_w = WKBWriter()
 53
 54        # Representations of 'POINT (5 23)' in hex -- one normal and
 55        # the other with the byte order changed.
 56        g = GEOSGeometry('POINT (5 23)')
 57        hex1 = '010100000000000000000014400000000000003740'
 58        wkb1 = buffer(binascii.a2b_hex(hex1))
 59        hex2 = '000000000140140000000000004037000000000000'
 60        wkb2 = buffer(binascii.a2b_hex(hex2))
 61
 62        self.assertEqual(hex1, wkb_w.write_hex(g))
 63        self.assertEqual(wkb1, wkb_w.write(g))
 64
 65        # Ensuring bad byteorders are not accepted.
 66        for bad_byteorder in (-1, 2, 523, 'foo', None):
 67            # Equivalent of `wkb_w.byteorder = bad_byteorder`
 68            self.assertRaises(ValueError, wkb_w._set_byteorder, bad_byteorder)
 69            
 70        # Setting the byteorder to 0 (for Big Endian)
 71        wkb_w.byteorder = 0
 72        self.assertEqual(hex2, wkb_w.write_hex(g))
 73        self.assertEqual(wkb2, wkb_w.write(g))
 74
 75        # Back to Little Endian
 76        wkb_w.byteorder = 1
 77
 78        # Now, trying out the 3D and SRID flags.
 79        g = GEOSGeometry('POINT (5 23 17)')
 80        g.srid = 4326
 81        
 82        hex3d = '0101000080000000000000144000000000000037400000000000003140'
 83        wkb3d = buffer(binascii.a2b_hex(hex3d))
 84        hex3d_srid = '01010000A0E6100000000000000000144000000000000037400000000000003140'
 85        wkb3d_srid = buffer(binascii.a2b_hex(hex3d_srid))
 86
 87        # Ensuring bad output dimensions are not accepted
 88        for bad_outdim in (-1, 0, 1, 4, 423, 'foo', None):
 89            # Equivalent of `wkb_w.outdim = bad_outdim`
 90            self.assertRaises(ValueError, wkb_w._set_outdim, bad_outdim)
 91
 92        # These tests will fail on 3.0.0 because of a bug that was fixed in 3.1:
 93        # http://trac.osgeo.org/geos/ticket/216
 94        if not geos_version_info()['version'].startswith('3.0.'):
 95            # Now setting the output dimensions to be 3
 96            wkb_w.outdim = 3
 97
 98            self.assertEqual(hex3d, wkb_w.write_hex(g))
 99            self.assertEqual(wkb3d, wkb_w.write(g))
100
101            # Telling the WKBWriter to inlcude the srid in the representation.
102            wkb_w.srid = True
103            self.assertEqual(hex3d_srid, wkb_w.write_hex(g))
104            self.assertEqual(wkb3d_srid, wkb_w.write(g))
105
106def suite():
107    s = unittest.TestSuite()
108    s.addTest(unittest.makeSuite(GEOSIOTest))
109    return s
110
111def run(verbosity=2):
112    unittest.TextTestRunner(verbosity=verbosity).run(suite())