# Path: geoalchemy2/types.py
# class Geometry(_GISType):
#     """
#     The Geometry type.
# 
#     Creating a geometry column is done like this::
# 
#         Column(Geometry(geometry_type='POINT', srid=4326))
# 
#     See :class:`geoalchemy2.types._GISType` for the list of arguments that can
#     be passed to the constructor.
# 
#     If ``srid`` is set then the ``WKBElement`` objects resulting from queries will
#     have that SRID, and, when constructing the ``WKBElement`` objects, the SRID
#     won't be read from the data returned by the database. If ``srid`` is not set
#     (meaning it's ``-1``) then the SRID set in ``WKBElement`` objects will be read
#     from the data returned by the database.
#     """
# 
#     name = 'geometry'
#     """ Type name used for defining geometry columns in ``CREATE TABLE``. """
# 
#     from_text = 'ST_GeomFromEWKT'
#     """ The "from text" geometry constructor. Used by the parent class'
#         ``bind_expression`` method. """
# 
#     as_binary = 'ST_AsEWKB'
#     """ The "as binary" function to use. Used by the parent class'
#         ``column_expression`` method. """
# 
#     ElementType = WKBElement
#     """ The element class to use. Used by the parent class'
#         ``result_processor`` method. """
# 
#     cache_ok = False
#     """ Disable cache for this type. """
# 
# Path: geoalchemy2/elements.py
# class WKBElement(_SpatialElement):
#     """
#     Instances of this class wrap a WKB or EWKB value.
# 
#     Geometry values read from the database are converted to instances of this
#     type. In most cases you won't need to create ``WKBElement`` instances
#     yourself.
# 
#     If ``extended`` is ``True`` and ``srid`` is ``-1`` at construction time
#     then the SRID will be read from the EWKB data.
# 
#     Note: you can create ``WKBElement`` objects from Shapely geometries
#     using the :func:`geoalchemy2.shape.from_shape` function.
#     """
# 
#     geom_from = 'ST_GeomFromWKB'
#     geom_from_extended_version = 'ST_GeomFromEWKB'
# 
#     def __init__(self, data, srid=-1, extended=False):
#         if extended and srid == -1:
#             # read srid from the EWKB
#             #
#             # WKB struct {
#             #    byte    byteOrder;
#             #    uint32  wkbType;
#             #    uint32  SRID;
#             #    struct  geometry;
#             # }
#             # byteOrder enum {
#             #     WKB_XDR = 0,  // Most Significant Byte First
#             #     WKB_NDR = 1,  // Least Significant Byte First
#             # }
#             if isinstance(data, str):
#                 # SpatiaLite case
#                 # assume that the string is an hex value
#                 header = binascii.unhexlify(data[:18])
#             else:
#                 header = data[:9]
#             byte_order, srid = header[0], header[5:]
#             srid = struct.unpack('<I' if byte_order else '>I', srid)[0]
#         _SpatialElement.__init__(self, data, srid, extended)
# 
#     @property
#     def desc(self):
#         """
#         This element's description string.
#         """
#         if isinstance(self.data, str):
#             # SpatiaLite case
#             return self.data.lower()
#         desc = str(binascii.hexlify(self.data), encoding="utf-8").lower()
#         return desc
# 
#     @staticmethod
#     def _data_from_desc(desc):
#         desc = desc.encode(encoding="utf-8")
#         return binascii.unhexlify(desc)
# 
# Path: geoalchemy2/elements.py
# class WKTElement(_SpatialElement):
#     """
#     Instances of this class wrap a WKT or EWKT value.
# 
#     Usage examples::
# 
#         wkt_element_1 = WKTElement('POINT(5 45)')
#         wkt_element_2 = WKTElement('POINT(5 45)', srid=4326)
#         wkt_element_3 = WKTElement('SRID=4326;POINT(5 45)', extended=True)
#     """
# 
#     geom_from = 'ST_GeomFromText'
#     geom_from_extended_version = 'ST_GeomFromEWKT'
# 
#     def __init__(self, data, srid=-1, extended=False):
#         if extended and srid == -1:
#             # read srid from EWKT
#             if not data.startswith('SRID='):
#                 raise ArgumentError('invalid EWKT string {}'.format(data))
#             data_s = data.split(';', 1)
#             if len(data_s) != 2:
#                 raise ArgumentError('invalid EWKT string {}'.format(data))
#             header = data_s[0]
#             try:
#                 srid = int(header[5:])
#             except ValueError:
#                 raise ArgumentError('invalid EWKT string {}'.format(data))
#         _SpatialElement.__init__(self, data, srid, extended)
# 
#     @property
#     def desc(self):
#         """
#         This element's description string.
#         """
#         return self.data
# 
#     @staticmethod
#     def _data_from_desc(desc):
#         return desc


import pytest
        import pickle
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy.ext.declarative import declarative_base
from geoalchemy2 import Geometry
from geoalchemy2 import WKBElement
from geoalchemy2 import WKTElement


metadata = MetaData()
Base = declarative_base(metadata=metadata)


class PickledLake(Base):
    __tablename__ = 'pickled_lake'
    id = Column(Integer, primary_key=True)
    geom = Column(Geometry(geometry_type='LINESTRING', srid=4326))

    def __init__(self, geom):
        self.geom = geom


class TestPickle():

    @pytest.fixture
    def setup_one_lake(self, session):
        conn = session.bind
        metadata.drop_all(conn, checkfirst=True)
        metadata.create_all(conn)


# Path: geoalchemy2/types.py
# class Geometry(_GISType):
#     """
#     The Geometry type.
# 
#     Creating a geometry column is done like this::
# 
#         Column(Geometry(geometry_type='POINT', srid=4326))
# 
#     See :class:`geoalchemy2.types._GISType` for the list of arguments that can
#     be passed to the constructor.
# 
#     If ``srid`` is set then the ``WKBElement`` objects resulting from queries will
#     have that SRID, and, when constructing the ``WKBElement`` objects, the SRID
#     won't be read from the data returned by the database. If ``srid`` is not set
#     (meaning it's ``-1``) then the SRID set in ``WKBElement`` objects will be read
#     from the data returned by the database.
#     """
# 
#     name = 'geometry'
#     """ Type name used for defining geometry columns in ``CREATE TABLE``. """
# 
#     from_text = 'ST_GeomFromEWKT'
#     """ The "from text" geometry constructor. Used by the parent class'
#         ``bind_expression`` method. """
# 
#     as_binary = 'ST_AsEWKB'
#     """ The "as binary" function to use. Used by the parent class'
#         ``column_expression`` method. """
# 
#     ElementType = WKBElement
#     """ The element class to use. Used by the parent class'
#         ``result_processor`` method. """
# 
#     cache_ok = False
#     """ Disable cache for this type. """
# 
# Path: geoalchemy2/elements.py
# class WKBElement(_SpatialElement):
#     """
#     Instances of this class wrap a WKB or EWKB value.
# 
#     Geometry values read from the database are converted to instances of this
#     type. In most cases you won't need to create ``WKBElement`` instances
#     yourself.
# 
#     If ``extended`` is ``True`` and ``srid`` is ``-1`` at construction time
#     then the SRID will be read from the EWKB data.
# 
#     Note: you can create ``WKBElement`` objects from Shapely geometries
#     using the :func:`geoalchemy2.shape.from_shape` function.
#     """
# 
#     geom_from = 'ST_GeomFromWKB'
#     geom_from_extended_version = 'ST_GeomFromEWKB'
# 
#     def __init__(self, data, srid=-1, extended=False):
#         if extended and srid == -1:
#             # read srid from the EWKB
#             #
#             # WKB struct {
#             #    byte    byteOrder;
#             #    uint32  wkbType;
#             #    uint32  SRID;
#             #    struct  geometry;
#             # }
#             # byteOrder enum {
#             #     WKB_XDR = 0,  // Most Significant Byte First
#             #     WKB_NDR = 1,  // Least Significant Byte First
#             # }
#             if isinstance(data, str):
#                 # SpatiaLite case
#                 # assume that the string is an hex value
#                 header = binascii.unhexlify(data[:18])
#             else:
#                 header = data[:9]
#             byte_order, srid = header[0], header[5:]
#             srid = struct.unpack('<I' if byte_order else '>I', srid)[0]
#         _SpatialElement.__init__(self, data, srid, extended)
# 
#     @property
#     def desc(self):
#         """
#         This element's description string.
#         """
#         if isinstance(self.data, str):
#             # SpatiaLite case
#             return self.data.lower()
#         desc = str(binascii.hexlify(self.data), encoding="utf-8").lower()
#         return desc
# 
#     @staticmethod
#     def _data_from_desc(desc):
#         desc = desc.encode(encoding="utf-8")
#         return binascii.unhexlify(desc)
# 
# Path: geoalchemy2/elements.py
# class WKTElement(_SpatialElement):
#     """
#     Instances of this class wrap a WKT or EWKT value.
# 
#     Usage examples::
# 
#         wkt_element_1 = WKTElement('POINT(5 45)')
#         wkt_element_2 = WKTElement('POINT(5 45)', srid=4326)
#         wkt_element_3 = WKTElement('SRID=4326;POINT(5 45)', extended=True)
#     """
# 
#     geom_from = 'ST_GeomFromText'
#     geom_from_extended_version = 'ST_GeomFromEWKT'
# 
#     def __init__(self, data, srid=-1, extended=False):
#         if extended and srid == -1:
#             # read srid from EWKT
#             if not data.startswith('SRID='):
#                 raise ArgumentError('invalid EWKT string {}'.format(data))
#             data_s = data.split(';', 1)
#             if len(data_s) != 2:
#                 raise ArgumentError('invalid EWKT string {}'.format(data))
#             header = data_s[0]
#             try:
#                 srid = int(header[5:])
#             except ValueError:
#                 raise ArgumentError('invalid EWKT string {}'.format(data))
#         _SpatialElement.__init__(self, data, srid, extended)
# 
#     @property
#     def desc(self):
#         """
#         This element's description string.
#         """
#         return self.data
# 
#     @staticmethod
#     def _data_from_desc(desc):
#         return desc


# Path: tests/test_pickle.py
import pytest
        import pickle
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy.ext.declarative import declarative_base
from geoalchemy2 import Geometry
from geoalchemy2 import WKBElement
from geoalchemy2 import WKTElement



metadata = MetaData()
Base = declarative_base(metadata=metadata)


class PickledLake(Base):
    __tablename__ = 'pickled_lake'
    id = Column(Integer, primary_key=True)
    geom = Column(Geometry(geometry_type='LINESTRING', srid=4326))

    def __init__(self, geom):
        self.geom = geom


class TestPickle():

    @pytest.fixture
    def setup_one_lake(self, session):
        conn = session.bind
        metadata.drop_all(conn, checkfirst=True)
        metadata.create_all(conn)


        lake = PickledLake(WKTElement('LINESTRING(0 0,1 1)', srid=4326))