-
Notifications
You must be signed in to change notification settings - Fork 29
/
decode_data.go
231 lines (180 loc) · 5.82 KB
/
decode_data.go
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package osmpbf
import (
"time"
"github.com/qedus/osmpbf/OSMPBF"
"google.golang.org/protobuf/proto"
)
// Decoder for Blob with OSMData (PrimitiveBlock)
type dataDecoder struct {
q []interface{}
}
func (dec *dataDecoder) Decode(blob *OSMPBF.Blob) ([]interface{}, error) {
dec.q = make([]interface{}, 0, 8000) // typical PrimitiveBlock contains 8k OSM entities
data, err := getData(blob)
if err != nil {
return nil, err
}
primitiveBlock := &OSMPBF.PrimitiveBlock{}
if err := proto.Unmarshal(data, primitiveBlock); err != nil {
return nil, err
}
dec.parsePrimitiveBlock(primitiveBlock)
return dec.q, nil
}
func (dec *dataDecoder) parsePrimitiveBlock(pb *OSMPBF.PrimitiveBlock) {
for _, pg := range pb.GetPrimitivegroup() {
dec.parsePrimitiveGroup(pb, pg)
}
}
func (dec *dataDecoder) parsePrimitiveGroup(pb *OSMPBF.PrimitiveBlock, pg *OSMPBF.PrimitiveGroup) {
dec.parseNodes(pb, pg.GetNodes())
dec.parseDenseNodes(pb, pg.GetDense())
dec.parseWays(pb, pg.GetWays())
dec.parseRelations(pb, pg.GetRelations())
}
func (dec *dataDecoder) parseNodes(pb *OSMPBF.PrimitiveBlock, nodes []*OSMPBF.Node) {
st := pb.GetStringtable().GetS()
granularity := int64(pb.GetGranularity())
dateGranularity := int64(pb.GetDateGranularity())
latOffset := pb.GetLatOffset()
lonOffset := pb.GetLonOffset()
for _, node := range nodes {
id := node.GetId()
lat := node.GetLat()
lon := node.GetLon()
latitude := 1e-9 * float64((latOffset + (granularity * lat)))
longitude := 1e-9 * float64((lonOffset + (granularity * lon)))
tags := extractTags(st, node.GetKeys(), node.GetVals())
info := extractInfo(st, node.GetInfo(), dateGranularity)
dec.q = append(dec.q, &Node{id, latitude, longitude, tags, info})
}
}
func (dec *dataDecoder) parseDenseNodes(pb *OSMPBF.PrimitiveBlock, dn *OSMPBF.DenseNodes) {
st := pb.GetStringtable().GetS()
granularity := int64(pb.GetGranularity())
latOffset := pb.GetLatOffset()
lonOffset := pb.GetLonOffset()
dateGranularity := int64(pb.GetDateGranularity())
ids := dn.GetId()
lats := dn.GetLat()
lons := dn.GetLon()
di := dn.GetDenseinfo()
tu := tagUnpacker{st, dn.GetKeysVals(), 0}
var id, lat, lon int64
var state denseInfoState
for index := range ids {
id = ids[index] + id
lat = lats[index] + lat
lon = lons[index] + lon
latitude := 1e-9 * float64((latOffset + (granularity * lat)))
longitude := 1e-9 * float64((lonOffset + (granularity * lon)))
tags := tu.next()
info := extractDenseInfo(st, &state, di, index, dateGranularity)
dec.q = append(dec.q, &Node{id, latitude, longitude, tags, info})
}
}
func (dec *dataDecoder) parseWays(pb *OSMPBF.PrimitiveBlock, ways []*OSMPBF.Way) {
st := pb.GetStringtable().GetS()
dateGranularity := int64(pb.GetDateGranularity())
for _, way := range ways {
id := way.GetId()
tags := extractTags(st, way.GetKeys(), way.GetVals())
refs := way.GetRefs()
var nodeID int64
nodeIDs := make([]int64, len(refs))
for index := range refs {
nodeID = refs[index] + nodeID // delta encoding
nodeIDs[index] = nodeID
}
info := extractInfo(st, way.GetInfo(), dateGranularity)
dec.q = append(dec.q, &Way{id, tags, nodeIDs, info})
}
}
// Make relation members from stringtable and three parallel arrays of IDs.
func extractMembers(stringTable []string, rel *OSMPBF.Relation) []Member {
memIDs := rel.GetMemids()
types := rel.GetTypes()
roleIDs := rel.GetRolesSid()
var memID int64
members := make([]Member, len(memIDs))
for index := range memIDs {
memID = memIDs[index] + memID // delta encoding
var memType MemberType
switch types[index] {
case OSMPBF.Relation_NODE:
memType = NodeType
case OSMPBF.Relation_WAY:
memType = WayType
case OSMPBF.Relation_RELATION:
memType = RelationType
}
role := stringTable[roleIDs[index]]
members[index] = Member{memID, memType, role}
}
return members
}
func (dec *dataDecoder) parseRelations(pb *OSMPBF.PrimitiveBlock, relations []*OSMPBF.Relation) {
st := pb.GetStringtable().GetS()
dateGranularity := int64(pb.GetDateGranularity())
for _, rel := range relations {
id := rel.GetId()
tags := extractTags(st, rel.GetKeys(), rel.GetVals())
members := extractMembers(st, rel)
info := extractInfo(st, rel.GetInfo(), dateGranularity)
dec.q = append(dec.q, &Relation{id, tags, members, info})
}
}
func extractInfo(stringTable []string, i *OSMPBF.Info, dateGranularity int64) Info {
info := Info{Visible: true}
if i != nil {
info.Version = i.GetVersion()
millisec := time.Duration(i.GetTimestamp()*dateGranularity) * time.Millisecond
info.Timestamp = time.Unix(0, millisec.Nanoseconds()).UTC()
info.Changeset = i.GetChangeset()
info.Uid = i.GetUid()
info.User = stringTable[i.GetUserSid()]
if i.Visible != nil {
info.Visible = i.GetVisible()
}
}
return info
}
type denseInfoState struct {
timestamp int64
changeset int64
uid int32
userSid int32
}
func extractDenseInfo(stringTable []string, state *denseInfoState, di *OSMPBF.DenseInfo, index int, dateGranularity int64) Info {
info := Info{Visible: true}
versions := di.GetVersion()
if len(versions) > 0 {
info.Version = versions[index]
}
timestamps := di.GetTimestamp()
if len(timestamps) > 0 {
state.timestamp = timestamps[index] + state.timestamp
millisec := time.Duration(state.timestamp*dateGranularity) * time.Millisecond
info.Timestamp = time.Unix(0, millisec.Nanoseconds()).UTC()
}
changesets := di.GetChangeset()
if len(changesets) > 0 {
state.changeset = changesets[index] + state.changeset
info.Changeset = state.changeset
}
uids := di.GetUid()
if len(uids) > 0 {
state.uid = uids[index] + state.uid
info.Uid = state.uid
}
usersids := di.GetUserSid()
if len(usersids) > 0 {
state.userSid = usersids[index] + state.userSid
info.User = stringTable[state.userSid]
}
visibleArray := di.GetVisible()
if len(visibleArray) > 0 {
info.Visible = visibleArray[index]
}
return info
}