-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathvector_tile.py
executable file
·198 lines (178 loc) · 6.48 KB
/
vector_tile.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
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python
import sys
def signed(val):
if val & ~0x7f == 0x80 :
return val|~0x7f
else:
return val&0x7f
class vector_tile:
TAGS_MAX = 627
TAGS_LIMIT = 1024
def __init__(self, buf) :
self.buf = buf
self.pos = 0
self.tags = {}
self.ways = []
def decodeSize (self):
self.pos += 4
return self.buf[0] << 24 | self.buf[1] << 16 | self.buf[2] << 8 | self.buf[3]
def decodeVarint32 (self):
pos = self.pos
#print>>sys.stderr,pos #"BUFFER=>",self.buf[pos],self.buf[pos+1],self.buf[pos+2],self.buf[pos+3],self.buf[pos+4]
if signed(self.buf[pos]) >= 0:
self.pos +=1
return self.buf[pos]
elif signed(self.buf[pos+1]) >= 0:
self.pos +=2
return (self.buf[pos] & 0x7f) | (self.buf[pos+1] << 7)
elif signed(self.buf[pos+2]) >= 0:
self.pos +=3
return (self.buf[pos] & 0x7f) | (self.buf[pos + 1] & 0x7f) << 7 | (self.buf[pos + 2]) << 14
elif signed(self.buf[pos+3]) >= 0:
self.pos +=4
return (self.buf[pos] & 0x7f) | (self.buf[pos + 1] & 0x7f) << 7 | (self.buf[pos + 2] & 0x7f) << 14 | (self.buf[pos + 3]) << 21
result = (self.buf[pos] & 0x7f) | (self.buf[pos + 1] & 0x7f) << 7 | (self.buf[pos + 2] & 0x7f) << 14 | (self.buf[pos + 3] & 0x7f) << 21 | (self.buf[pos + 4]) << 28
print "--------------------------------------OOOPS"
read = 5
pos += 4
while buf[pos]<0 and read < 10:
pos+=1
read+=1
self.pos += read
return result
def decodeShortArray(self, numTags):
bytes = self.decodeVarint32()
print>>sys.stderr, "BYTES=", bytes
end = self.pos + bytes
index = []
while self.pos < end :
val = self.decodeVarint32()
index.append(val)
return index
def decodeTileTags(self, tag, index):
val = self.decodeString()
print "key=",tag ," val=",val
self.tags[tag]=val
def decodeString(self):
size = self.decodeVarint32()
pos = self.pos
self.pos += size
return self.buf[pos:pos + size]
def decodeTileElement(self, type, index):
bytes = self.decodeVarint32()
end = self.pos + bytes
indexCount = 1
coordCount = 0
if (type == 13):
coordCount = 2
while self.pos < end:
val = self.decodeVarint32()
#print "VAL=",val
if val==0: break
tag = val >> 3;
#print>>sys.stderr, "POS=", self.pos, "VAL=", val, "TAG=", tag
if tag == 11 : #TAG_ELEM_TAGS
print "TAG_ELEM_TAGS"
self.decodeWayTags(index)
elif tag == 1: # TAG_ELEM_NUM_INDICES
print "TAG_ELEM_NUM_INDICES"
indexCount = self.decodeVarint32();
print "INDEX Count = ",indexCount
#sys.exit(1)
elif tag == 12: # TAG_ELEM_INDEX
print "TAG_ELEM_INDEX"
print "indexCount=", indexCount
index = self.decodeShortArray(indexCount)
print "INDEX:",index
for i in range(0,indexCount):
length = index[i]*2
coordCount += length
index[i] = length
if indexCount < len(index):
index[indexCount] = -1
elif tag == 13: # TAG_ELEM_COORDS
print "TAG_ELEM_COORDS"
count = self.decodeWayCoordinates(coordCount)
print "COUNT=",count, " --- " , coordCount
elif tag == 21: # TAG_ELEM_LAYER
print "TAG_ELEM_LAYER"
sys.exit(1)
else:
print>>sys.stderr,"Invalid type for element:", tag
sys.exit(1)
def decodeWayTags(self,index):
bytes = self.decodeVarint32()
end = self.pos + bytes
while self.pos < end:
tagNum = self.decodeVarint32()
if tagNum < self.TAGS_MAX:
print "TagNum=", tagNum
else :
tagNum -= self.TAGS_LIMIT
print "TagNum(Local)=", index[tagNum], "val=", self.tags[tagNum]
def decodeWayCoordinates(self, nodes ):
bytes = self.decodeVarint32()
print>>sys.stderr,"BYTES=",bytes," pos=",self.pos
end = self.pos + bytes
last_x = 0
last_y = 0
count = 0
even = True
scale = 4096/400.0
way = []
while self.pos < end:
val = self.decodeVarint32()
val = ((val >> 1) ^ -(val & 1)) #zigzag
count +=1
if even :
last_x += val
even = False
way.insert(count,last_y / scale);
print>>sys.stderr, "X=", last_x
else :
last_y += val
even = True
way.insert(count,last_y / scale);
print>>sys.stderr, "Y=", last_y
print "TILE pos=",self.pos," end=", end
self.ways.append(way);
return count
def parse(filename):
f = open(filename)
s = f.read()
tile = vector_tile(bytearray(s))
print "content length: ", tile.decodeSize()
val = tile.decodeVarint32()
numTags = 0
curTag = 0
index = []
while tile.pos < len(s):
tag = val >> 3
print>>sys.stderr, "POS=", tile.pos, "VAL=", val, "TAG=", tag
if tag == 1 : # TAG_TILE_NUM_TAGS
numTags = tile.decodeVarint32()
elif tag == 2 : # TAG_TILE_TAG_KEYS
index = tile.decodeShortArray(numTags)
print "INDEX:",index
elif tag == 3 : # TAG_TILE_TAG_VALUES:
tile.decodeTileTags(curTag,index);
curTag += 1
elif tag == 11 : # TAG_TILE_LINE
print "======== TAG_TILE_LINE"
tile.decodeTileElement(tag, index);
elif tag == 12 : # TAG_TILE_POLY
print "======== TAG_TILE_POLY"
tile.decodeTileElement(tag, index);
elif tag == 13 : # TAG_TILE_POINT
print "======== TAG_TILE_POINT"
tile.decodeTileElement(tag, index);
else:
print>>sys.stderr,"Invalid type for tile:", tag
break
if (tile.pos < len(s)):
val = tile.decodeVarint32()
if __name__ == "__main__":
if len(sys.argv) != 2 :
print>>sys.stderr, "Usage:", sys.argv[0], "<osmtile>"
sys.exit(1)
parse(sys.argv[1])