-
Notifications
You must be signed in to change notification settings - Fork 1
/
CityBufReader.py
45 lines (35 loc) · 1.23 KB
/
CityBufReader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import struct
from CityBuf_ import \
Header, \
CityFeature
class CityBufReader:
MAGIC_NUMBER_SIZE = 8
def __init__(self, file):
self.f = file
self.f.seek(0)
self.read_magic_number()
self.pos_header = self.MAGIC_NUMBER_SIZE
self.read_header()
self.pos_data = self.f.tell()
def read_magic_number(self):
self.f.seek(0)
magic_number = self.f.read(self.MAGIC_NUMBER_SIZE)
if magic_number[:3] == b'FCB' and magic_number[4:7] == b'FCB':
self.version_major = magic_number[3]
self.version_minor = magic_number[7]
else:
raise Exception('Invalid magic number')
def read_header(self):
self.f.seek(self.pos_header)
self.size_header = struct.unpack('<I', self.f.read(4))[0]
header_buf = self.f.read(self.size_header)
# Get a pointer to the root object inside the FlatBuffer
self.header = Header.Header.GetRootAsHeader(header_buf, 0)
# iterator over the features
def features(self):
self.f.seek(self.pos_data)
for i in range(self.header.FeaturesCount()):
feature_length = struct.unpack('<I', self.f.read(4))[0]
feature_buf = self.f.read(feature_length)
feature = CityFeature.CityFeature.GetRootAsCityFeature(feature_buf, 0)
yield feature