-
Notifications
You must be signed in to change notification settings - Fork 2
/
spatialindex.py
187 lines (170 loc) · 6.67 KB
/
spatialindex.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import struct
import os
class Bin:
def __init__(self):
self.id = 1
self.features = []
class Feature:
def __init__(self):
self.id = None
self.xmin = None
self.ymin = None
self.xmax = None
self.ymax = None
class SBN:
def __init__(self, sbn=None):
self.bins = []
self.numFeat = 0
self.sbnName = sbn
self.load(self.sbnName)
def load(self, sbnName=None):
self.sbn = None
self.fileCode = 9994
self.unknownByte4 = -400
self.unused1 = None
self.unused2 = None
self.unused3 = None
self.unused4 = None
self.fileLen = 0
self.numRecords = 0
self.xmin = None
self.ymin = None
self.xmax = None
self.ymax = None
self.zmin = None
self.zmax = None
self.zmax = None
self.mmin = None
self.mmax = None
self.unknownByte96 = 0
if sbnName:
try:
self.sbn = open(sbnName, "rb")
except IOError:
raise Exception("Unable to open sbn file or sbn file not specified")
# Read sbn header
self.fileCode = struct.unpack(">i", self.sbn.read(4))[0]
self.unknownByte4 = struct.unpack(">i", self.sbn.read(4))[0]
self.unused1 = struct.unpack(">i", self.sbn.read(4))[0]
self.unused2 = struct.unpack(">i", self.sbn.read(4))[0]
self.unused3 = struct.unpack(">i", self.sbn.read(4))[0]
self.unused4 = struct.unpack(">i", self.sbn.read(4))[0]
self.fileLen = struct.unpack(">i", self.sbn.read(4))[0] * 2
# NOTE: numRecords is records in shp/dbf file not # of bins!
self.numRecords = struct.unpack(">i", self.sbn.read(4))[0]
self.xmin = struct.unpack(">d", self.sbn.read(8))[0]
self.ymin = struct.unpack(">d", self.sbn.read(8))[0]
self.xmax = struct.unpack(">d", self.sbn.read(8))[0]
self.ymax = struct.unpack(">d", self.sbn.read(8))[0]
self.zmin = struct.unpack(">d", self.sbn.read(8))[0]
self.zmax = struct.unpack(">d", self.sbn.read(8))[0]
self.mmin = struct.unpack(">d", self.sbn.read(8))[0]
self.mmax = struct.unpack(">d", self.sbn.read(8))[0]
self.unknownByte96 = struct.unpack(">i", self.sbn.read(4))[0]
# Read BIN descriptors
recNum = struct.unpack(">i", self.sbn.read(4))[0]
recLen = struct.unpack(">i", self.sbn.read(4))[0] * 2
# Append a blank convienience bin as a placeholder for bin 1
b = Bin()
b.id = 1
self.bins.append(b)
for i in range(recLen/8):
b = Bin()
b.id = struct.unpack(">i", self.sbn.read(4))[0]
b.numFeat = struct.unpack(">i", self.sbn.read(4))[0]
self.bins.append(b)
# Read Bin contents
while self.sbn.tell() < self.fileLen:
binId = struct.unpack(">i", self.sbn.read(4))[0]
recLen = struct.unpack(">i", self.sbn.read(4))[0] * 2
for i in range(recLen/8):
f = Feature()
f.xmin = struct.unpack(">B",self.sbn.read(1))[0]
f.ymin = struct.unpack(">B",self.sbn.read(1))[0]
f.xmax = struct.unpack(">B",self.sbn.read(1))[0]
f.ymax = struct.unpack(">B",self.sbn.read(1))[0]
f.id = struct.unpack(">i", self.sbn.read(4))[0]
b = self.bin(binId)
b.features.append(f)
self.numFeat += 1
self.sbn.close()
def bin(self, bid):
for b in self.bins:
if b.id == bid:
return b
def save(self, sbnName=None):
sbnName = sbnName or self.sbnName
sbn = open(sbnName, "wb")
sbxName = os.path.splitext(sbnName)[0] + ".sbx"
sbx = open(sbxName, "wb")
# Write headers
sbn.write(struct.pack(">i", self.fileCode))
sbx.write(struct.pack(">i", self.fileCode))
sbn.write(struct.pack(">i", self.unknownByte4))
sbx.write(struct.pack(">i", self.unknownByte4))
sbn.write(struct.pack(">i", self.unused1))
sbx.write(struct.pack(">i", self.unused1))
sbn.write(struct.pack(">i", self.unused2))
sbx.write(struct.pack(">i", self.unused2))
sbn.write(struct.pack(">i", self.unused3))
sbx.write(struct.pack(">i", self.unused3))
sbn.write(struct.pack(">i", self.unused4))
sbx.write(struct.pack(">i", self.unused4))
# Calculate File Length fields
# first bin descriptors
totalBinSize = len(self.bins[1:]) * 8
# then bins with features
usedBinSize = len([b for b in self.bins if b.id > 0]) * 8
sbxSize = 100 + usedBinSize
sbnSize = 100 + totalBinSize + usedBinSize
sbxSize //= 2
sbnSize += self.numFeat * 8
sbnSize //= 2
sbn.write(struct.pack(">i", sbnSize))
sbx.write(struct.pack(">i", sbxSize))
sbn.write(struct.pack(">i", self.numRecords))
sbx.write(struct.pack(">i", self.numRecords))
sbn.write(struct.pack(">d", self.xmin))
sbx.write(struct.pack(">d", self.xmin))
sbn.write(struct.pack(">d", self.ymin))
sbx.write(struct.pack(">d", self.ymin))
sbn.write(struct.pack(">d", self.xmax))
sbx.write(struct.pack(">d", self.xmax))
sbn.write(struct.pack(">d", self.ymax))
sbx.write(struct.pack(">d", self.ymax))
sbn.write(struct.pack(">d", self.zmin))
sbx.write(struct.pack(">d", self.zmin))
sbn.write(struct.pack(">d", self.zmax))
sbx.write(struct.pack(">d", self.zmax))
sbn.write(struct.pack(">d", self.mmin))
sbx.write(struct.pack(">d", self.mmin))
sbn.write(struct.pack(">d", self.mmax))
sbx.write(struct.pack(">d", self.mmax))
sbn.write(struct.pack(">i", self.unknownByte96))
sbx.write(struct.pack(">i", self.unknownByte96))
# sbn and sbx records
# first create bin descriptors record
recLen = (len(self.bins[1:]) * 4)
sbn.write(struct.pack(">i", 1))
sbn.write(struct.pack(">i", recLen))
sbx.write(struct.pack(">i", 100))
sbx.write(struct.pack(">i", recLen + 2))
for b in self.bins[1:]:
sbn.write(struct.pack(">i", b.id))
sbn.write(struct.pack(">i", len(b.features)))
# write actual bins
for b in self.bins[1:]:
if b.id <= 0:
continue
sbx.write(struct.pack(">i", sbn.tell()))
sbx.write(struct.pack(">i", (len(b.features) * 2)))
sbn.write(struct.pack(">i", b.id))
sbn.write(struct.pack(">i", len(b.features) * 4))
for f in b.features:
sbn.write(struct.pack(">B", f.xmin))
sbn.write(struct.pack(">B", f.ymin))
sbn.write(struct.pack(">B", f.xmax))
sbn.write(struct.pack(">B", f.ymax))
sbn.write(struct.pack(">i", f.id))
sbn.close()
sbx.close()