11import binascii
22from io import BytesIO
33import struct
4+ from cpython cimport bool
45
56
67class Typed (type ):
@@ -20,17 +21,23 @@ def __call__(cls, *args, **kwargs):
2021 return super ().__call__(* args, ** kwargs)
2122
2223
23- class Reader :
24+ cdef class Reader:
2425
25- __slots__ = ['stream' , 'endianness' , 'has_z' , 'has_m' ]
26+ cdef object stream
27+ cdef bytes endianness
28+ cdef public bool has_z
29+ cdef public bool has_m
2630
27- def __init__ (self , stream ):
31+ def __cinit__ (self , object stream ):
2832 self .stream = stream
2933
30- def clone (self ):
34+ cpdef clone(self ):
3135 return type (self )(self .stream)
3236
33- def read (self ):
37+ cpdef read(self ):
38+ return self ._read()
39+
40+ cdef _read(self ):
3441 # https://en.wikipedia.org/wiki/Well-known_text#Well-known_binary
3542 byte_order = self .stream.read(1 )
3643 if byte_order == b' \x00 ' :
@@ -53,22 +60,22 @@ def read(self):
5360 else :
5461 return class_.from_ewkb_body(self , srid)
5562
56- def read_int (self ):
63+ cpdef read_int(self ):
5764 return struct .unpack(self .endianness + b' I' , self .stream.read(4 ))[0 ]
5865
59- def read_double (self ):
66+ cpdef read_double(self ):
6067 return struct .unpack(self .endianness + b' d' , self .stream.read(8 ))[0 ]
6168
62- @ classmethod
63- def from_hex ( cls , value ):
64- return cls (BytesIO (binascii .a2b_hex (value ))).read ()
69+
70+ cpdef read( str value):
71+ return Reader (BytesIO(binascii.a2b_hex(value))).read()
6572
6673
67- class Writer :
74+ cdef class Writer:
6875
69- __slots__ = [ ' stream' ]
76+ cdef object stream
7077
71- def __init__ (self , geometry , stream = None ):
78+ def __cinit__ (self , object geometry , object stream = None ):
7279 self .stream = stream or BytesIO()
7380 try :
7481 type_ = geometry.TYPE
@@ -85,17 +92,17 @@ def __init__(self, geometry, stream=None):
8592 if geometry.has_srid:
8693 self .write_int(geometry.srid)
8794
88- def write_int (self , value ):
95+ cpdef write_int(self , value):
8996 self .stream.write(struct .pack(b' <I' , value))
9097
91- def write_double (self , value ):
98+ cpdef write_double(self , value):
9299 self .stream.write(struct .pack(b' <d' , value))
93100
94- def clone (self , geometry ):
101+ cpdef clone(self , object geometry):
95102 return type (self )(geometry, self .stream)
96103
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 ()
104+
105+ cpdef bytes write( object value):
106+ writer = Writer (value)
107+ value.write_ewkb_body(writer)
108+ return binascii.b2a_hex(writer.stream.getvalue()).upper()
0 commit comments