forked from paulmach/osm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
85 lines (67 loc) · 2.64 KB
/
update.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
package osm
import (
"fmt"
"sort"
"time"
)
// CommitInfoStart is the start time when we know committed at information.
// Any update.Timestamp >= this date is a committed at time. Anything before
// this date is the element timestamp.
var CommitInfoStart = time.Date(2012, 9, 12, 9, 30, 3, 0, time.UTC)
// An Update is a change to children of a way or relation.
// The child type, id, ref and/or role are the same as the child
// at the given index. Lon/Lat are only updated for nodes.
type Update struct {
Index int `xml:"index,attr" json:"index"`
Version int `xml:"version,attr" json:"version"`
// Timestamp is the committed at time if time > CommitInfoStart or the
// element timestamp if before that date.
Timestamp time.Time `xml:"timestamp,attr" json:"timestamp"`
ChangesetID ChangesetID `xml:"changeset,attr,omitempty" json:"changeset,omitempty"`
Lat float64 `xml:"lat,attr,omitempty" json:"lat,omitempty"`
Lon float64 `xml:"lon,attr,omitempty" json:"lon,omitempty"`
Reverse bool `xml:"reverse,attr,omitempty" json:"reverse,omitempty"`
}
// Updates are collections of updates.
type Updates []Update
// UpTo will return the subset of updates taking place upto and on
// the given time.
func (us Updates) UpTo(t time.Time) Updates {
var result Updates
for _, u := range us {
if u.Timestamp.After(t) {
continue
}
result = append(result, u)
}
return result
}
// UpdateIndexOutOfRangeError is return when applying an update to an object
// and the update index is out of range.
type UpdateIndexOutOfRangeError struct {
Index int
}
var _ error = &UpdateIndexOutOfRangeError{}
// Error returns a string representation of the error.
func (e *UpdateIndexOutOfRangeError) Error() string {
return fmt.Sprintf("osm: index %d is out of range", e.Index)
}
type updatesSortTS Updates
// SortByTimestamp will sort the updates by timestamp in ascending order.
func (us Updates) SortByTimestamp() { sort.Sort(updatesSortTS(us)) }
func (us updatesSortTS) Len() int { return len(us) }
func (us updatesSortTS) Swap(i, j int) { us[i], us[j] = us[j], us[i] }
func (us updatesSortTS) Less(i, j int) bool {
return us[i].Timestamp.Before(us[j].Timestamp)
}
type updatesSortIndex Updates
// SortByIndex will sort the updates by index in ascending order.
func (us Updates) SortByIndex() { sort.Sort(updatesSortIndex(us)) }
func (us updatesSortIndex) Len() int { return len(us) }
func (us updatesSortIndex) Swap(i, j int) { us[i], us[j] = us[j], us[i] }
func (us updatesSortIndex) Less(i, j int) bool {
if us[i].Index != us[j].Index {
return us[i].Index < us[j].Index
}
return us[i].Timestamp.Before(us[j].Timestamp)
}