-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnmarshalWay.go
132 lines (124 loc) · 2.94 KB
/
UnmarshalWay.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
package osm
import (
"encoding/xml"
"strconv"
"time"
)
import (
"github.com/pkg/errors"
)
// UnmarshalWay unmarshals a Way from an XML stream.
// Returns the way's tags as a separate slice, so they can be cached
func UnmarshalWay(decoder *xml.Decoder, e xml.StartElement, input *Input) (*Way, uint64, string, []Tag, error) {
w := NewWay()
user_id := uint64(0)
user_name := ""
tags := make([]Tag, 0)
for _, attr := range e.Attr {
switch attr.Name.Local {
case "id":
id, err := strconv.ParseUint(attr.Value, 10, 64)
if err != nil {
return w, user_id, user_name, tags, errors.Wrap(err, "Error parsing way id")
}
w.Id = id
case "version":
if !input.DropVersion {
version, err := strconv.ParseUint(attr.Value, 10, 16)
if err != nil {
return w, user_id, user_name, tags, errors.Wrap(err, "Error parsing way version")
}
w.Version = uint16(version)
}
case "changeset":
if !input.DropChangeset {
changeset, err := strconv.ParseUint(attr.Value, 10, 64)
if err != nil {
return w, user_id, user_name, tags, errors.Wrap(err, "Error parsing way changeset")
}
w.Changeset = changeset
}
case "timestamp":
if !input.DropTimestamp {
ts, err := time.Parse(time.RFC3339, attr.Value)
if err != nil {
return w, user_id, user_name, tags, errors.Wrap(err, "Error parsing way timestamp")
}
w.Timestamp = &ts
}
case "uid":
if !input.DropUserId {
uid, err := strconv.ParseUint(attr.Value, 10, 64)
if err != nil {
return w, user_id, user_name, tags, errors.Wrap(err, "Error parsing way uid")
}
user_id = uid
}
case "user":
if !input.DropUserName {
user_name = attr.Value
}
}
}
NodesAndTags:
for {
token, _ := decoder.Token()
if token == nil {
break
}
switch e := token.(type) {
case xml.StartElement:
switch e.Name.Local {
case "tag":
tag := Tag{}
for _, attr := range e.Attr {
switch attr.Name.Local {
case "k":
tag.Key = attr.Value
case "v":
tag.Value = attr.Value
}
}
keep := true
if len(input.KeysToKeep) > 0 {
keep = false
for _, k := range input.KeysToKeep {
if tag.Key == k {
keep = true
break
}
}
} else if len(input.KeysToDrop) > 0 {
for _, k := range input.KeysToDrop {
if tag.Key == k {
keep = false
break
}
}
}
if keep {
tags = append(tags, tag)
}
case "nd":
nr := NodeReference{}
for _, attr := range e.Attr {
switch attr.Name.Local {
case "ref":
ref, err := strconv.ParseUint(attr.Value, 10, 64)
if err != nil {
return w, user_id, user_name, tags, errors.Wrap(err, "Error parsing node reference for way.")
}
nr.Reference = ref
}
}
w.NodeReferences = append(w.NodeReferences, nr)
}
case xml.EndElement:
switch e.Name.Local {
case "way":
break NodesAndTags
}
}
}
return w, user_id, user_name, tags, nil
}