|
| 1 | +import io |
| 2 | +from itertools import chain |
| 3 | +from six.moves import range |
| 4 | +import struct |
| 5 | +from cassandra.cqltypes import CassandraType |
| 6 | +from cassandra.util import is_little_endian as _platform_is_le |
| 7 | +from dse.marshal import point_be, point_le, circle_be, circle_le |
| 8 | +from dse.util import Point, Circle, LineString, Polygon |
| 9 | + |
| 10 | +_endian_flag = 1 if _platform_is_le else 0 |
| 11 | + |
| 12 | + |
| 13 | +class WKBGeometryType(object): |
| 14 | + POINT = 1 |
| 15 | + LINESTRING = 2 |
| 16 | + POLYGON = 3 |
| 17 | + CIRCLE = 101 # DSE custom |
| 18 | + |
| 19 | + |
| 20 | +class PointType(CassandraType): |
| 21 | + typename = 'PointType' |
| 22 | + |
| 23 | + _platform_point = point_le if _platform_is_le else point_be |
| 24 | + _type = struct.pack('=BI', _endian_flag, WKBGeometryType.POINT) |
| 25 | + |
| 26 | + @staticmethod |
| 27 | + def serialize(val, protocol_version): |
| 28 | + return PointType._type + PointType._platform_point.pack(val.x, val.y) |
| 29 | + |
| 30 | + @staticmethod |
| 31 | + def deserialize(byts, protocol_version): |
| 32 | + is_little_endian = bool(byts[0]) |
| 33 | + point = point_le if is_little_endian else point_be |
| 34 | + return Point(*point.unpack_from(byts, 5)) # ofs = endian byte + int type |
| 35 | + |
| 36 | + |
| 37 | +class CircleType(CassandraType): |
| 38 | + typename = 'CircleType' |
| 39 | + |
| 40 | + _platform_circle = circle_le if _platform_is_le else circle_be |
| 41 | + _type = struct.pack('=BI', _endian_flag, WKBGeometryType.CIRCLE) |
| 42 | + |
| 43 | + @staticmethod |
| 44 | + def serialize(val, protocol_version): |
| 45 | + return CircleType._type + CircleType._platform_circle.pack(val.x, val.y, val.r) |
| 46 | + |
| 47 | + @staticmethod |
| 48 | + def deserialize(byts, protocol_version): |
| 49 | + is_little_endian = bool(byts[0]) |
| 50 | + circle = circle_le if is_little_endian else circle_be |
| 51 | + return Circle(*circle.unpack_from(byts, 5)) |
| 52 | + |
| 53 | + |
| 54 | +class LineStringType(CassandraType): |
| 55 | + typename = 'LineStringType' |
| 56 | + |
| 57 | + _type = struct.pack('=BI', _endian_flag, WKBGeometryType.LINESTRING) |
| 58 | + |
| 59 | + @staticmethod |
| 60 | + def serialize(val, protocol_version): |
| 61 | + num_points = len(val.coords) |
| 62 | + return LineStringType._type + struct.pack('=I' + 'dd' * num_points, num_points, *(d for coords in val.coords for d in coords)) |
| 63 | + |
| 64 | + @staticmethod |
| 65 | + def deserialize(byts, protocol_version): |
| 66 | + is_little_endian = bool(byts[0]) |
| 67 | + point = point_le if is_little_endian else point_be |
| 68 | + coords = ((point.unpack_from(byts, offset) for offset in range(1 + 4 + 4, len(byts), point.size))) # start = endian + int type + int count |
| 69 | + return LineString(coords) |
| 70 | + |
| 71 | + |
| 72 | +class PolygonType(CassandraType): |
| 73 | + typename = 'PolygonType' |
| 74 | + |
| 75 | + _type = struct.pack('=BI', _endian_flag, WKBGeometryType.POLYGON) |
| 76 | + _platform_ring_count = struct.Struct('=I').pack |
| 77 | + |
| 78 | + @staticmethod |
| 79 | + def serialize(val, protocol_version): |
| 80 | + buf = io.BytesIO(PolygonType._type) |
| 81 | + buf.seek(0, 2) |
| 82 | + |
| 83 | + num_rings = 1 + len(val.interiors) |
| 84 | + buf.write(PolygonType._platform_ring_count(num_rings)) |
| 85 | + for ring in chain((val.exterior,), val.interiors): |
| 86 | + num_points = len(ring.coords) |
| 87 | + buf.write(struct.pack('=I' + 'dd' * num_points, num_points, *(d for coord in ring.coords for d in coord))) |
| 88 | + return buf.getvalue() |
| 89 | + |
| 90 | + @staticmethod |
| 91 | + def deserialize(byts, protocol_version): |
| 92 | + is_little_endian = bool(byts[0]) |
| 93 | + if is_little_endian: |
| 94 | + int_fmt = '<i' |
| 95 | + point = point_le |
| 96 | + else: |
| 97 | + int_fmt = '>i' |
| 98 | + point = point_be |
| 99 | + p = 5 |
| 100 | + ring_count = struct.unpack_from(int_fmt, byts, p)[0] |
| 101 | + p += 4 |
| 102 | + rings = [] |
| 103 | + for _ in range(ring_count): |
| 104 | + point_count = struct.unpack_from(int_fmt, byts, p)[0] |
| 105 | + p += 4 |
| 106 | + end = p + point_count * point.size |
| 107 | + rings.append([point.unpack_from(byts, offset) for offset in range(p, end, point.size)]) |
| 108 | + p = end |
| 109 | + return Polygon(exterior=rings[0], interiors=rings[1:]) |
0 commit comments