forked from omniscale/imposm3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
279 lines (260 loc) · 5.78 KB
/
parser.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package diff
import (
"compress/gzip"
"encoding/xml"
"io"
"os"
"strconv"
"time"
"github.com/omniscale/imposm3/element"
"github.com/omniscale/imposm3/logging"
)
var log = logging.NewLogger("osc parser")
type Element struct {
Add bool
Mod bool
Del bool
Node *element.Node
Way *element.Way
Rel *element.Relation
}
// Parser is a stream based parser for OSM diff files (.osc).
// Parsing is handled in a background goroutine.
type Parser struct {
reader io.Reader
elems chan Element
errc chan error
metadata bool
running bool
onClose func() error
}
// SetWithMetadata enables parsing of metadata
func (p *Parser) SetWithMetadata(metadata bool) {
p.metadata = metadata
}
// Next returns the next Element of the .osc file.
// Returns io.EOF and an empty Element if the parser
// reached the end of the .osc file.
func (p *Parser) Next() (Element, error) {
if !p.running {
p.running = true
go parse(p.reader, p.elems, p.errc, p.metadata)
}
select {
case elem, ok := <-p.elems:
if !ok {
p.elems = nil
} else {
return elem, nil
}
case err, ok := <-p.errc:
if !ok {
p.errc = nil
} else {
if p.onClose != nil {
p.onClose()
p.onClose = nil
}
return Element{}, err
}
}
if p.onClose != nil {
err := p.onClose()
p.onClose = nil
return Element{}, err
}
return Element{}, nil
}
// NewParser returns a parser from an io.Reader
func NewParser(r io.Reader) *Parser {
elems := make(chan Element)
errc := make(chan error)
return &Parser{reader: r, elems: elems, errc: errc}
}
// NewOscGzParser returns a parser from a .osc.gz file
func NewOscGzParser(fname string) (*Parser, error) {
file, err := os.Open(fname)
if err != nil {
return nil, err
}
reader, err := gzip.NewReader(file)
if err != nil {
file.Close()
return nil, err
}
elems := make(chan Element)
errc := make(chan error)
return &Parser{reader: reader, elems: elems, errc: errc, onClose: file.Close}, nil
}
func parse(reader io.Reader, elems chan Element, errc chan error, metadata bool) {
defer close(elems)
defer close(errc)
decoder := xml.NewDecoder(reader)
add := false
mod := false
del := false
tags := make(map[string]string)
newElem := false
node := &element.Node{}
way := &element.Way{}
rel := &element.Relation{}
NextToken:
for {
token, err := decoder.Token()
if err != nil {
errc <- err
return
}
switch tok := token.(type) {
case xml.StartElement:
switch tok.Name.Local {
case "create":
add = true
mod = false
del = false
case "modify":
add = false
mod = true
del = false
case "delete":
add = false
mod = false
del = true
case "node":
for _, attr := range tok.Attr {
switch attr.Name.Local {
case "id":
node.Id, _ = strconv.ParseInt(attr.Value, 10, 64)
case "lat":
node.Lat, _ = strconv.ParseFloat(attr.Value, 64)
case "lon":
node.Long, _ = strconv.ParseFloat(attr.Value, 64)
}
}
if metadata {
setElemMetadata(tok.Attr, &node.OSMElem)
}
case "way":
for _, attr := range tok.Attr {
if attr.Name.Local == "id" {
way.Id, _ = strconv.ParseInt(attr.Value, 10, 64)
}
}
if metadata {
setElemMetadata(tok.Attr, &way.OSMElem)
}
case "relation":
for _, attr := range tok.Attr {
if attr.Name.Local == "id" {
rel.Id, _ = strconv.ParseInt(attr.Value, 10, 64)
}
}
if metadata {
setElemMetadata(tok.Attr, &rel.OSMElem)
}
case "nd":
for _, attr := range tok.Attr {
if attr.Name.Local == "ref" {
ref, _ := strconv.ParseInt(attr.Value, 10, 64)
way.Refs = append(way.Refs, ref)
}
}
case "member":
member := element.Member{}
for _, attr := range tok.Attr {
switch attr.Name.Local {
case "type":
var ok bool
member.Type, ok = element.MemberTypeValues[attr.Value]
if !ok {
// ignore unknown member types
continue NextToken
}
case "role":
member.Role = attr.Value
case "ref":
var err error
member.Id, err = strconv.ParseInt(attr.Value, 10, 64)
if err != nil {
// ignore invalid ref
continue NextToken
}
}
}
rel.Members = append(rel.Members, member)
case "tag":
var k, v string
for _, attr := range tok.Attr {
if attr.Name.Local == "k" {
k = attr.Value
} else if attr.Name.Local == "v" {
v = attr.Value
}
}
tags[k] = v
case "osmChange":
// pass
default:
log.Warn("unhandled XML tag ", tok.Name.Local, " in OSC")
}
case xml.EndElement:
var e Element
switch tok.Name.Local {
case "node":
if len(tags) > 0 {
node.Tags = tags
}
e.Node = node
node = &element.Node{}
newElem = true
case "way":
if len(tags) > 0 {
way.Tags = tags
}
e.Way = way
way = &element.Way{}
newElem = true
case "relation":
if len(tags) > 0 {
rel.Tags = tags
}
e.Rel = rel
rel = &element.Relation{}
newElem = true
case "osmChange":
errc <- io.EOF
return
}
if newElem {
e.Add = add
e.Del = del
e.Mod = mod
if len(tags) > 0 {
tags = make(map[string]string)
}
newElem = false
elems <- e
}
}
}
}
func setElemMetadata(attrs []xml.Attr, elem *element.OSMElem) {
elem.Metadata = &element.Metadata{}
for _, attr := range attrs {
switch attr.Name.Local {
case "version":
v, _ := strconv.ParseInt(attr.Value, 10, 64)
elem.Metadata.Version = int(v)
case "uid":
v, _ := strconv.ParseInt(attr.Value, 10, 64)
elem.Metadata.UserId = int(v)
case "user":
elem.Metadata.UserName = attr.Value
case "changeset":
v, _ := strconv.ParseInt(attr.Value, 10, 64)
elem.Metadata.Changeset = int(v)
case "timestamp":
elem.Metadata.Timestamp, _ = time.Parse(time.RFC3339, attr.Value)
}
}
}