test_persist.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """Persistence tests"""
  2. import pickle
  3. import struct
  4. import unittest
  5. from shapely import wkb, wkt
  6. from shapely.geometry import Point
  7. class PersistTestCase(unittest.TestCase):
  8. def test_pickle(self):
  9. p = Point(0.0, 0.0)
  10. data = pickle.dumps(p)
  11. q = pickle.loads(data)
  12. assert q.equals(p)
  13. def test_wkb(self):
  14. p = Point(0.0, 0.0)
  15. wkb_big_endian = wkb.dumps(p, big_endian=True)
  16. wkb_little_endian = wkb.dumps(p, big_endian=False)
  17. # Regardless of byte order, loads ought to correctly recover the
  18. # geometry
  19. assert p.equals(wkb.loads(wkb_big_endian))
  20. assert p.equals(wkb.loads(wkb_little_endian))
  21. def test_wkb_dumps_endianness(self):
  22. p = Point(0.5, 2.0)
  23. wkb_big_endian = wkb.dumps(p, big_endian=True)
  24. wkb_little_endian = wkb.dumps(p, big_endian=False)
  25. assert wkb_big_endian != wkb_little_endian
  26. # According to WKB specification in section 3.3 of OpenGIS
  27. # Simple Features Specification for SQL, revision 1.1, the
  28. # first byte of a WKB representation indicates byte order.
  29. # Big-endian is 0, little-endian is 1.
  30. assert wkb_big_endian[0] == 0
  31. assert wkb_little_endian[0] == 1
  32. # Check that the doubles (0.5, 2.0) are in correct byte order
  33. double_size = struct.calcsize("d")
  34. assert wkb_big_endian[(-2 * double_size) :] == struct.pack(">2d", p.x, p.y)
  35. assert wkb_little_endian[(-2 * double_size) :] == struct.pack("<2d", p.x, p.y)
  36. def test_wkt(self):
  37. p = Point(0.0, 0.0)
  38. text = wkt.dumps(p)
  39. assert text.startswith("POINT")
  40. pt = wkt.loads(text)
  41. assert pt.equals(p)