-
Notifications
You must be signed in to change notification settings - Fork 48
/
way.go
120 lines (97 loc) · 2.62 KB
/
way.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
package annotate
import (
"context"
"time"
"github.com/paulmach/osm"
"github.com/paulmach/osm/annotate/internal/core"
"github.com/paulmach/osm/annotate/shared"
)
// NodeHistoryDatasourcer is an more strict interface for when we only need node history.
type NodeHistoryDatasourcer interface {
NodeHistory(context.Context, osm.NodeID) (osm.Nodes, error)
NotFound(error) bool
}
// NodeHistoryAsChildrenDatasourcer is an advanced data source that
// returns the needed nodes as children directly.
type NodeHistoryAsChildrenDatasourcer interface {
NodeHistoryDatasourcer
NodeHistoryAsChildren(context.Context, osm.NodeID) ([]*shared.Child, error)
}
var _ NodeHistoryDatasourcer = &osm.HistoryDatasource{}
// Ways computes the updates for the given ways
// and annotate the way nodes with changeset and lon/lat data.
// The input ways are modified to include this information.
func Ways(
ctx context.Context,
ways osm.Ways,
datasource NodeHistoryDatasourcer,
opts ...Option,
) error {
computeOpts := &core.Options{
Threshold: defaultThreshold,
}
for _, o := range opts {
err := o(computeOpts)
if err != nil {
return err
}
}
parents := make([]core.Parent, len(ways))
for i, w := range ways {
parents[i] = &parentWay{Way: w}
}
wds := newWayDatasourcer(datasource)
updatesForParents, err := core.Compute(ctx, parents, wds, computeOpts)
if err != nil {
return mapErrors(err)
}
// fill in updates
for i, updates := range updatesForParents {
ways[i].Updates = updates
}
return nil
}
// A parentWay wraps a osm.Way into the core.Parent interface
// so that updates can be computed.
type parentWay struct {
Way *osm.Way
}
func (w *parentWay) ID() osm.FeatureID {
return w.Way.FeatureID()
}
func (w *parentWay) ChangesetID() osm.ChangesetID {
return w.Way.ChangesetID
}
func (w *parentWay) Version() int {
return w.Way.Version
}
func (w *parentWay) Visible() bool {
return w.Way.Visible
}
func (w *parentWay) Timestamp() time.Time {
return w.Way.Timestamp
}
func (w *parentWay) Committed() time.Time {
if w.Way.Committed == nil {
return time.Time{}
}
return *w.Way.Committed
}
func (w *parentWay) Refs() (osm.FeatureIDs, []bool) {
ids := make(osm.FeatureIDs, len(w.Way.Nodes))
annotated := make([]bool, len(w.Way.Nodes))
for i := range w.Way.Nodes {
ids[i] = w.Way.Nodes[i].FeatureID()
annotated[i] = w.Way.Nodes[i].Version != 0
}
return ids, annotated
}
func (w *parentWay) SetChild(idx int, child *shared.Child) {
if child == nil {
return
}
w.Way.Nodes[idx].Version = child.Version
w.Way.Nodes[idx].ChangesetID = child.ChangesetID
w.Way.Nodes[idx].Lat = child.Lat
w.Way.Nodes[idx].Lon = child.Lon
}