forked from paulmach/osm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
child.go
111 lines (89 loc) · 2.22 KB
/
child.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
// Package shared is used by annotate and the internal core.
// External usage of this package is for advanced use only.
package shared
import (
"time"
"github.com/cessien/osm"
)
// A Child represents a node, way or relation that is a dependent for
// annotating ways or relations.
type Child struct {
ID osm.FeatureID
Version int
ChangesetID osm.ChangesetID
// VersionIndex is the index of the version if sorted from lowest to highest.
// This is necessary since version don't have to start at 1 or be sequential.
VersionIndex int
Timestamp time.Time
Committed time.Time
// for nodes
Lon, Lat float64
// for ways
Way *osm.Way
ReverseOfPrevious bool
// moving the visible bool here decreases the struct size from
// size 120 (size class 128) to 112 (size class 112).
Visible bool
}
// Update generates an update from this child.
func (c *Child) Update() osm.Update {
return osm.Update{
Version: c.Version,
Timestamp: updateTimestamp(c.Timestamp, c.Committed),
ChangesetID: c.ChangesetID,
Lat: c.Lat,
Lon: c.Lon,
Reverse: c.ReverseOfPrevious,
}
}
// FromNode converts a node to a child.
func FromNode(n *osm.Node) *Child {
c := &Child{
ID: n.FeatureID(),
Version: n.Version,
ChangesetID: n.ChangesetID,
Visible: n.Visible,
Timestamp: n.Timestamp,
Lon: n.Lon,
Lat: n.Lat,
}
if n.Committed != nil {
c.Committed = *n.Committed
}
return c
}
// FromWay converts a way to a child.
func FromWay(w *osm.Way) *Child {
c := &Child{
ID: w.FeatureID(),
Version: w.Version,
ChangesetID: w.ChangesetID,
Visible: w.Visible,
Timestamp: w.Timestamp,
Way: w,
}
if w.Committed != nil {
c.Committed = *w.Committed
}
return c
}
// FromRelation converts a way to a child.
func FromRelation(r *osm.Relation) *Child {
c := &Child{
ID: r.FeatureID(),
Version: r.Version,
ChangesetID: r.ChangesetID,
Visible: r.Visible,
Timestamp: r.Timestamp,
}
if r.Committed != nil {
c.Committed = *r.Committed
}
return c
}
func updateTimestamp(timestamp, committed time.Time) time.Time {
if timestamp.Before(osm.CommitInfoStart) || committed.IsZero() {
return timestamp
}
return committed
}