# 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: geoalchemy2/shape.py
# def from_shape(shape, srid=-1, extended=False):
#     """
#     Function to convert a Shapely geometry to a
#     :class:`geoalchemy2.types.WKBElement`.
# 
#     Additional arguments:
# 
#     ``srid``
# 
#         An integer representing the spatial reference system. E.g. 4326.
#         Default value is -1, which means no/unknown reference system.
# 
#     ``extended``
# 
#         A boolean to switch between WKB and EWKB.
#         Default value is False.
# 
#     Example::
# 
#         from shapely.geometry import Point
#         wkb_element = from_shape(Point(5, 45), srid=4326)
#         ewkb_element = from_shape(Point(5, 45), srid=4326, extended=True)
#     """
#     return WKBElement(
#         memoryview(dumps(shape, srid=srid if extended else None)),
#         srid=srid,
#         extended=extended)
# 
# Path: geoalchemy2/shape.py
# def to_shape(element):
#     """
#     Function to convert a :class:`geoalchemy2.types.SpatialElement`
#     to a Shapely geometry.
# 
#     Example::
# 
#         lake = Session.query(Lake).get(1)
#         polygon = to_shape(lake.geom)
#     """
#     assert isinstance(element, (WKBElement, WKTElement))
#     if isinstance(element, WKBElement):
#         data, hex = (element.data, True) if isinstance(element.data, str) else \
#                     (bytes(element.data), False)
#         return shapely.wkb.loads(data, hex=hex)
#     elif isinstance(element, WKTElement):
#         if element.extended:
#             return shapely.wkt.loads(element.data.split(';', 1)[1])
#         else:
#             return shapely.wkt.loads(element.data)


import shapely.wkb
from shapely.geometry import Point
from geoalchemy2.elements import WKBElement
from geoalchemy2.elements import WKTElement
from geoalchemy2.shape import from_shape
from geoalchemy2.shape import to_shape



def test_to_shape_WKBElement():
    # POINT(1 2)
    e = WKBElement(b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00'
                   b'\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@')

# 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: geoalchemy2/shape.py
# def from_shape(shape, srid=-1, extended=False):
#     """
#     Function to convert a Shapely geometry to a
#     :class:`geoalchemy2.types.WKBElement`.
# 
#     Additional arguments:
# 
#     ``srid``
# 
#         An integer representing the spatial reference system. E.g. 4326.
#         Default value is -1, which means no/unknown reference system.
# 
#     ``extended``
# 
#         A boolean to switch between WKB and EWKB.
#         Default value is False.
# 
#     Example::
# 
#         from shapely.geometry import Point
#         wkb_element = from_shape(Point(5, 45), srid=4326)
#         ewkb_element = from_shape(Point(5, 45), srid=4326, extended=True)
#     """
#     return WKBElement(
#         memoryview(dumps(shape, srid=srid if extended else None)),
#         srid=srid,
#         extended=extended)
# 
# Path: geoalchemy2/shape.py
# def to_shape(element):
#     """
#     Function to convert a :class:`geoalchemy2.types.SpatialElement`
#     to a Shapely geometry.
# 
#     Example::
# 
#         lake = Session.query(Lake).get(1)
#         polygon = to_shape(lake.geom)
#     """
#     assert isinstance(element, (WKBElement, WKTElement))
#     if isinstance(element, WKBElement):
#         data, hex = (element.data, True) if isinstance(element.data, str) else \
#                     (bytes(element.data), False)
#         return shapely.wkb.loads(data, hex=hex)
#     elif isinstance(element, WKTElement):
#         if element.extended:
#             return shapely.wkt.loads(element.data.split(';', 1)[1])
#         else:
#             return shapely.wkt.loads(element.data)


# Path: tests/test_shape.py
import shapely.wkb
from shapely.geometry import Point
from geoalchemy2.elements import WKBElement
from geoalchemy2.elements import WKTElement
from geoalchemy2.shape import from_shape
from geoalchemy2.shape import to_shape




def test_to_shape_WKBElement():
    # POINT(1 2)
    e = WKBElement(b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00'
                   b'\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@')

    s = to_shape(e)