|
| 1 | +import binascii |
| 2 | +from io import BytesIO |
| 3 | +import struct |
| 4 | + |
| 5 | + |
| 6 | +class Typed(type): |
| 7 | + |
| 8 | + types = {} |
| 9 | + |
| 10 | + def __new__(mcs, name, bases, attrs, **kwargs): |
| 11 | + cls = super().__new__(mcs, name, bases, attrs, **kwargs) |
| 12 | + if hasattr(cls, 'TYPE'): |
| 13 | + Typed.types[cls.TYPE] = cls |
| 14 | + return cls |
| 15 | + |
| 16 | + def __call__(cls, *args, **kwargs): |
| 17 | + # Allow to pass an instance as first argument, for blind casting. |
| 18 | + if args and isinstance(args[0], cls): |
| 19 | + return args[0] |
| 20 | + return super().__call__(*args, **kwargs) |
| 21 | + |
| 22 | + |
| 23 | +class Reader: |
| 24 | + |
| 25 | + __slots__ = ['stream', 'endianness', 'has_z', 'has_m'] |
| 26 | + |
| 27 | + def __init__(self, stream): |
| 28 | + self.stream = stream |
| 29 | + |
| 30 | + def clone(self): |
| 31 | + return type(self)(self.stream) |
| 32 | + |
| 33 | + def read(self): |
| 34 | + # https://en.wikipedia.org/wiki/Well-known_text#Well-known_binary |
| 35 | + byte_order = self.stream.read(1) |
| 36 | + if byte_order == b'\x00': |
| 37 | + self.endianness = b'>' |
| 38 | + elif byte_order == b'\x01': |
| 39 | + self.endianness = b'<' |
| 40 | + else: |
| 41 | + raise Exception('invalid encoding') |
| 42 | + |
| 43 | + type_ = self.read_int() |
| 44 | + self.has_z = bool(type_ & 0x80000000) |
| 45 | + self.has_m = bool(type_ & 0x40000000) |
| 46 | + srid = self.read_int() if bool(type_ & 0x20000000) else None |
| 47 | + type_ &= 0x1fffffff |
| 48 | + |
| 49 | + try: |
| 50 | + class_ = Typed.types[type_] |
| 51 | + except KeyError: |
| 52 | + raise ValueError('unsupported geometry type {0}'.format(type_)) |
| 53 | + else: |
| 54 | + return class_.from_ewkb_body(self, srid) |
| 55 | + |
| 56 | + def read_int(self): |
| 57 | + return struct.unpack(self.endianness + b'I', self.stream.read(4))[0] |
| 58 | + |
| 59 | + def read_double(self): |
| 60 | + return struct.unpack(self.endianness + b'd', self.stream.read(8))[0] |
| 61 | + |
| 62 | + @classmethod |
| 63 | + def from_hex(cls, value): |
| 64 | + return cls(BytesIO(binascii.a2b_hex(value))).read() |
| 65 | + |
| 66 | + |
| 67 | +class Writer: |
| 68 | + |
| 69 | + __slots__ = ['stream'] |
| 70 | + |
| 71 | + def __init__(self, geometry, stream=None): |
| 72 | + self.stream = stream or BytesIO() |
| 73 | + try: |
| 74 | + type_ = geometry.TYPE |
| 75 | + except AttributeError: |
| 76 | + raise ValueError('Unknown geometry {}'.format(geometry.__class__)) |
| 77 | + |
| 78 | + # Little endian. |
| 79 | + self.stream.write(b'\x01') |
| 80 | + self.write_int( |
| 81 | + type_ | |
| 82 | + (0x80000000 if geometry.has_z else 0) | |
| 83 | + (0x40000000 if geometry.has_m else 0) | |
| 84 | + (0x20000000 if geometry.has_srid else 0)) |
| 85 | + if geometry.has_srid: |
| 86 | + self.write_int(geometry.srid) |
| 87 | + |
| 88 | + def write_int(self, value): |
| 89 | + self.stream.write(struct.pack(b'<I', value)) |
| 90 | + |
| 91 | + def write_double(self, value): |
| 92 | + self.stream.write(struct.pack(b'<d', value)) |
| 93 | + |
| 94 | + def clone(self, geometry): |
| 95 | + return type(self)(geometry, self.stream) |
| 96 | + |
| 97 | + @classmethod |
| 98 | + def to_hex(cls, value): |
| 99 | + writer = cls(value) |
| 100 | + value.write_ewkb_body(writer) |
| 101 | + return binascii.b2a_hex(writer.stream.getvalue()).upper() |
0 commit comments