PageRenderTime 24ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

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