forked from paulmach/osm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangeset.go
238 lines (196 loc) · 6.7 KB
/
changeset.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
package osm
import (
"encoding/xml"
"time"
"github.com/paulmach/osm/internal/osmpb"
"github.com/gogo/protobuf/proto"
)
// ChangesetID is the primary key for an osm changeset.
type ChangesetID int64
// ObjectID is a helper returning the object id for this changeset id.
func (id ChangesetID) ObjectID() ObjectID {
return ObjectID(changesetMask | (id << versionBits))
}
// Changesets is a collection with some helper functions attached.
type Changesets []*Changeset
// A Changeset is a set of metadata around a set of osm changes.
type Changeset struct {
XMLName xmlNameJSONTypeCS `xml:"changeset" json:"type"`
ID ChangesetID `xml:"id,attr" json:"id"`
User string `xml:"user,attr" json:"user,omitempty"`
UserID UserID `xml:"uid,attr" json:"uid,omitempty"`
CreatedAt time.Time `xml:"created_at,attr" json:"created_at"`
ClosedAt time.Time `xml:"closed_at,attr" json:"closed_at"`
Open bool `xml:"open,attr" json:"open"`
ChangesCount int `xml:"num_changes,attr,omitempty" json:"num_changes,omitempty"`
MinLat float64 `xml:"min_lat,attr" json:"min_lat,omitempty"`
MaxLat float64 `xml:"max_lat,attr" json:"max_lat,omitempty"`
MinLon float64 `xml:"min_lon,attr" json:"min_lon,omitempty"`
MaxLon float64 `xml:"max_lon,attr" json:"max_lon,omitempty"`
CommentsCount int `xml:"comments_count,attr,omitempty" json:"comments_count,omitempty"`
Tags Tags `xml:"tag" json:"tags,omitempty"`
Discussion *ChangesetDiscussion `xml:"discussion,omitempty" json:"discussion,omitempty"`
Change *Change `xml:"-" json:"change,omitempty"`
}
// ObjectID returns the object id of the changeset.
func (c *Changeset) ObjectID() ObjectID {
return c.ID.ObjectID()
}
// Bounds returns the bounds of the changeset as a bounds object.
func (c *Changeset) Bounds() *Bounds {
return &Bounds{
MinLat: c.MinLat,
MaxLat: c.MaxLat,
MinLon: c.MinLon,
MaxLon: c.MaxLon,
}
}
// Comment is a helper and returns the changeset comment from the tag.
func (c *Changeset) Comment() string {
return c.Tags.Find("comment")
}
// CreatedBy is a helper and returns the changeset created by from the tag.
func (c *Changeset) CreatedBy() string {
return c.Tags.Find("created_by")
}
// Locale is a helper and returns the changeset locale from the tag.
func (c *Changeset) Locale() string {
return c.Tags.Find("locale")
}
// Host is a helper and returns the changeset host from the tag.
func (c *Changeset) Host() string {
return c.Tags.Find("host")
}
// ImageryUsed is a helper and returns imagery used for the changeset from the tag.
func (c *Changeset) ImageryUsed() string {
return c.Tags.Find("imagery_used")
}
// Source is a helper and returns source for the changeset from the tag.
func (c *Changeset) Source() string {
return c.Tags.Find("source")
}
// Bot is a helper and returns true if the bot tag is a yes.
func (c *Changeset) Bot() bool {
// As of July 5, 2015: 300k yes, 123 no, 8 other
return c.Tags.Find("bot") == "yes"
}
// Marshal encodes the changeset data using protocol buffers.
// Does not encode the changeset discussion.
//
// Deprecated: encoding could be improved, should be versioned separately.
func (c *Changeset) Marshal() ([]byte, error) {
ss := &stringSet{}
var userSid *uint32
if c.User != "" {
v := ss.Add(c.User)
userSid = &v
}
keys, vals := c.Tags.keyValues(ss)
encoded := &osmpb.Changeset{
Keys: keys,
Vals: vals,
UserSid: userSid,
CreatedAt: timeToUnixPointer(c.CreatedAt),
ClosedAt: timeToUnixPointer(c.ClosedAt),
}
// only set these values if they make any sense.
if c.ID != 0 {
encoded.Id = proto.Int64(int64(c.ID))
}
if c.Open {
encoded.Open = proto.Bool(c.Open)
}
if c.UserID != 0 {
encoded.UserId = proto.Int32(int32(c.UserID))
}
if c.MinLat != 0 || c.MaxLat != 0 || c.MinLon != 0 || c.MaxLon != 0 {
encoded.Bounds = &osmpb.Bounds{
MinLat: geoToInt64(c.MinLat),
MaxLat: geoToInt64(c.MaxLat),
MinLon: geoToInt64(c.MinLon),
MaxLon: geoToInt64(c.MaxLon),
}
}
if c.Change != nil &&
(c.Change.Create != nil || c.Change.Modify != nil || c.Change.Delete != nil) {
encoded.Change = marshalChange(c.Change, ss, false)
}
encoded.Strings = ss.Strings()
return proto.Marshal(encoded)
}
// UnmarshalChangeset will unmarshal the data into an OSM object.
//
// Deprecated: encoding could be improved, should be versioned separately.
func UnmarshalChangeset(data []byte) (*Changeset, error) {
encoded := &osmpb.Changeset{}
err := proto.Unmarshal(data, encoded)
if err != nil {
return nil, err
}
ss := encoded.GetStrings()
tags, err := tagsFromStrings(ss, encoded.GetKeys(), encoded.GetVals())
if err != nil {
return nil, err
}
cs := &Changeset{
ID: ChangesetID(encoded.GetId()),
UserID: UserID(encoded.GetUserId()),
CreatedAt: unixToTime(encoded.GetCreatedAt()),
ClosedAt: unixToTime(encoded.GetClosedAt()),
Open: encoded.GetOpen(),
Tags: tags,
}
if encoded.UserSid != nil {
cs.User = ss[encoded.GetUserSid()]
}
if encoded.Bounds != nil {
cs.MinLat = float64(encoded.Bounds.GetMinLat()) / locMultiple
cs.MaxLat = float64(encoded.Bounds.GetMaxLat()) / locMultiple
cs.MinLon = float64(encoded.Bounds.GetMinLon()) / locMultiple
cs.MaxLon = float64(encoded.Bounds.GetMaxLon()) / locMultiple
}
if encoded.Change != nil {
cs.Change, err = unmarshalChange(encoded.Change, ss, cs)
if err != nil {
return nil, err
}
}
return cs, nil
}
// IDs returns the ids of the changesets in the slice.
func (cs Changesets) IDs() []ChangesetID {
if len(cs) == 0 {
return nil
}
r := make([]ChangesetID, 0, len(cs))
for _, c := range cs {
r = append(r, c.ID)
}
return r
}
// ChangesetDiscussion is a conversation about a changeset.
type ChangesetDiscussion struct {
Comments []*ChangesetComment `xml:"comment" json:"comments"`
}
// ChangesetComment is a specific comment in a changeset discussion.
type ChangesetComment struct {
User string `xml:"user,attr" json:"user"`
UserID UserID `xml:"uid,attr" json:"uid"`
Timestamp time.Time `xml:"date,attr" json:"date"`
Text string `xml:"text" json:"text"`
}
// MarshalXML implements the xml.Marshaller method to exclude this
// whole element if the comments are empty.
func (csd ChangesetDiscussion) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if len(csd.Comments) == 0 {
return nil
}
if err := e.EncodeToken(start); err != nil {
return err
}
t := xml.StartElement{Name: xml.Name{Local: "comment"}}
if err := e.EncodeElement(csd.Comments, t); err != nil {
return err
}
return e.EncodeToken(start.End())
}