-
Notifications
You must be signed in to change notification settings - Fork 48
/
relation.go
129 lines (105 loc) · 3.08 KB
/
relation.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
package annotate
import (
"context"
"time"
"github.com/paulmach/osm"
"github.com/paulmach/osm/annotate/internal/core"
"github.com/paulmach/osm/annotate/shared"
)
// HistoryAsChildrenDatasourcer is an advanced data source that
// returns the needed elements as children directly.
type HistoryAsChildrenDatasourcer interface {
osm.HistoryDatasourcer
NodeHistoryAsChildren(context.Context, osm.NodeID) ([]*shared.Child, error)
WayHistoryAsChildren(context.Context, osm.WayID) ([]*shared.Child, error)
RelationHistoryAsChildren(context.Context, osm.RelationID) ([]*shared.Child, error)
}
// Relations computes the updates for the given relations
// and annotate members with stuff like changeset and lon/lat data.
// The input relations are modified to include this information.
func Relations(
ctx context.Context,
relations osm.Relations,
datasource osm.HistoryDatasourcer,
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(relations))
for i, r := range relations {
parents[i] = &parentRelation{Relation: r}
}
rds := newRelationDatasourcer(datasource)
updatesForParents, err := core.Compute(ctx, parents, rds, computeOpts)
if err != nil {
return mapErrors(err)
}
for _, p := range parents {
r := p.(*parentRelation)
if r.Relation.Polygon() {
orientation(r.Relation.Members, r.ways, r.Relation.CommittedAt())
}
}
for i, updates := range updatesForParents {
relations[i].Updates = updates
}
return nil
}
// A parentRelation wraps a osm.Relation into the core.Parent interface
// so that updates can be computed.
type parentRelation struct {
Relation *osm.Relation
ways map[osm.WayID]*osm.Way
}
func (r *parentRelation) ID() osm.FeatureID {
return r.Relation.FeatureID()
}
func (r *parentRelation) ChangesetID() osm.ChangesetID {
return r.Relation.ChangesetID
}
func (r *parentRelation) Version() int {
return r.Relation.Version
}
func (r *parentRelation) Visible() bool {
return r.Relation.Visible
}
func (r *parentRelation) Timestamp() time.Time {
return r.Relation.Timestamp
}
func (r *parentRelation) Committed() time.Time {
if r.Relation.Committed == nil {
return time.Time{}
}
return *r.Relation.Committed
}
func (r *parentRelation) Refs() (osm.FeatureIDs, []bool) {
ids := make(osm.FeatureIDs, len(r.Relation.Members))
annotated := make([]bool, len(r.Relation.Members))
for i := range r.Relation.Members {
ids[i] = r.Relation.Members[i].FeatureID()
annotated[i] = r.Relation.Members[i].Version != 0
}
return ids, annotated
}
func (r *parentRelation) SetChild(idx int, child *shared.Child) {
if r.Relation.Polygon() && r.ways == nil {
r.ways = make(map[osm.WayID]*osm.Way, len(r.Relation.Members))
}
if child == nil {
return
}
r.Relation.Members[idx].Version = child.Version
r.Relation.Members[idx].ChangesetID = child.ChangesetID
r.Relation.Members[idx].Lat = child.Lat
r.Relation.Members[idx].Lon = child.Lon
if r.ways != nil && child.Way != nil {
r.ways[child.Way.ID] = child.Way
}
}