How to use the geoalchemy2.compat.str function in GeoAlchemy2

To help you get started, we’ve selected a few GeoAlchemy2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github geoalchemy / geoalchemy2 / geoalchemy2 / elements.py View on Github external
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]
            if not PY3:
                header = bytearray(header)
            byte_order, srid = header[0], header[5:]
            srid = struct.unpack('I', srid)[0]
        _SpatialElement.__init__(self, data, srid, extended)
github geoalchemy / geoalchemy2 / tests / test_shape.py View on Github external
def test_from_shape():
    # Standard case: POINT(1 2)
    expected = WKBElement(str('0101000000000000000000f03f0000000000000040'))
    p = Point(1, 2)
    e = from_shape(p)
    assert isinstance(e, WKBElement)
    assert isinstance(e.data, buffer)
    assert e == expected

    s = shapely.wkb.loads(bytes(e.data))
    assert isinstance(s, Point)
    assert s.equals(p)

    # Standard case with SRID: SRID=2145;POINT(1 2)
    expected2 = WKBElement(str('0101000000000000000000f03f0000000000000040'), srid=2154)
    p = Point(1, 2)
    e2 = from_shape(p, srid=2154)
    assert isinstance(e2, WKBElement)
    assert isinstance(e2.data, buffer)
    assert e2 == expected2

    s2 = shapely.wkb.loads(bytes(e2.data))
    assert isinstance(s2, Point)
    assert s2.equals(p)

    # Extended case: SRID=2145;POINT(1 2)
    expected3 = WKBElement(
        str('01010000206a080000000000000000f03f0000000000000040'),
        extended=True)
    e3 = from_shape(p, srid=2154, extended=True)
    assert isinstance(e3, WKBElement)
github geoalchemy / geoalchemy2 / tests / test_functional_spatialite.py View on Github external
def test_to_shape(self):
        lake = Lake(WKTElement('LINESTRING(0 0,1 1)', srid=4326))
        session.add(lake)
        session.flush()
        session.expire(lake)
        lake = session.query(Lake).one()
        assert isinstance(lake.geom, WKBElement)
        assert isinstance(lake.geom.data, str_)
        assert lake.geom.srid == 4326
        s = to_shape(lake.geom)
        assert isinstance(s, LineString)
        assert s.wkt == 'LINESTRING (0 0, 1 1)'
        lake = Lake(lake.geom)
        session.add(lake)
        session.flush()
        session.expire(lake)
        assert isinstance(lake.geom, WKBElement)
        assert isinstance(lake.geom.data, str_)
        assert lake.geom.srid == 4326
github geoalchemy / geoalchemy2 / tests / test_shape.py View on Github external
def test_to_shape_WKBElement_str():
    # POINT(1 2)
    e = WKBElement(str('0101000000000000000000f03f0000000000000040'))
    s = to_shape(e)
    assert isinstance(s, Point)
    assert s.x == 1
    assert s.y == 2
github geoalchemy / geoalchemy2 / tests / test_functional_spatialite.py View on Github external
session.add(lake)
        session.flush()
        session.expire(lake)
        lake = session.query(Lake).one()
        assert isinstance(lake.geom, WKBElement)
        assert isinstance(lake.geom.data, str_)
        assert lake.geom.srid == 4326
        s = to_shape(lake.geom)
        assert isinstance(s, LineString)
        assert s.wkt == 'LINESTRING (0 0, 1 1)'
        lake = Lake(lake.geom)
        session.add(lake)
        session.flush()
        session.expire(lake)
        assert isinstance(lake.geom, WKBElement)
        assert isinstance(lake.geom.data, str_)
        assert lake.geom.srid == 4326
github geoalchemy / geoalchemy2 / tests / test_elements.py View on Github external
assert isinstance(str(e), str)

    def test_eq(self):
        a = WKBElement(b'\x01\x02')
        b = WKBElement(b'\x01\x02')
        assert a == b


class TestNotEqualSpatialElement():

    # _bin/_hex computed by following query:
    # SELECT ST_GeomFromEWKT('SRID=3;POINT(1 2)');
    _ewkb = buffer_(b'\x01\x01\x00\x00 \x03\x00\x00\x00\x00\x00\x00'
                    b'\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@')
    _wkb = wkb.loads(bytes_(_ewkb)).wkb
    _hex = str_('010100002003000000000000000000f03f0000000000000040')
    _srid = 3
    _wkt = 'POINT (1 2)'
    _ewkt = 'SRID=3;POINT (1 2)'

    def test_eq(self):
        a = WKBElement(self._ewkb, extended=True)
        b = WKBElement(self._wkb, srid=self._srid)
        c = WKTElement(self._wkt, srid=self._srid)
        d = WKTElement(self._ewkt, extended=True)
        e = WKBElement(self._hex, extended=True)
        assert a == a
        assert b == b
        assert c == c
        assert d == d
        assert e == e
        assert a == e and e == a
github geoalchemy / geoalchemy2 / tests / test_shape.py View on Github external
# Standard case with SRID: SRID=2145;POINT(1 2)
    expected2 = WKBElement(str('0101000000000000000000f03f0000000000000040'), srid=2154)
    p = Point(1, 2)
    e2 = from_shape(p, srid=2154)
    assert isinstance(e2, WKBElement)
    assert isinstance(e2.data, buffer)
    assert e2 == expected2

    s2 = shapely.wkb.loads(bytes(e2.data))
    assert isinstance(s2, Point)
    assert s2.equals(p)

    # Extended case: SRID=2145;POINT(1 2)
    expected3 = WKBElement(
        str('01010000206a080000000000000000f03f0000000000000040'),
        extended=True)
    e3 = from_shape(p, srid=2154, extended=True)
    assert isinstance(e3, WKBElement)
    assert isinstance(e3.data, buffer)
    assert e3 == expected3

    s3 = shapely.wkb.loads(bytes(e3.data))
    assert isinstance(s, Point)
    assert s3.equals(p)
github geoalchemy / geoalchemy2 / geoalchemy2 / shape.py View on Github external
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)
github geoalchemy / geoalchemy2 / geoalchemy2 / elements.py View on Github external
def desc(self):
        """
        This element's description string.
        """
        if isinstance(self.data, str_):
            # SpatiaLite case
            return self.data
        desc = binascii.hexlify(self.data)
        if PY3:
            # hexlify returns a bytes object on py3
            desc = str(desc, encoding="utf-8")
        return desc