forked from paulmach/osm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource.go
143 lines (114 loc) · 2.87 KB
/
datasource.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
package annotate
import (
"context"
"github.com/paulmach/osm"
"github.com/paulmach/osm/annotate/internal/core"
"github.com/paulmach/orb"
"github.com/paulmach/orb/planar"
)
type wayDatasource struct {
NodeHistoryDatasourcer
}
func (wds *wayDatasource) Get(ctx context.Context, id osm.FeatureID) (core.ChildList, error) {
if id.Type() != osm.TypeNode {
panic("only node types supported")
}
nodes, err := wds.NodeHistory(ctx, id.NodeID())
if err != nil {
return nil, err
}
return nodesToChildList(nodes), nil
}
type relationDatasource struct {
osm.HistoryDatasourcer
}
func (rds *relationDatasource) Get(ctx context.Context, id osm.FeatureID) (core.ChildList, error) {
switch id.Type() {
case osm.TypeNode:
nodes, err := rds.NodeHistory(ctx, id.NodeID())
if err != nil {
return nil, err
}
return nodesToChildList(nodes), nil
case osm.TypeWay:
ways, err := rds.WayHistory(ctx, id.WayID())
if err != nil {
return nil, err
}
return waysToChildList(ways), nil
case osm.TypeRelation:
relations, err := rds.RelationHistory(ctx, id.RelationID())
if err != nil {
return nil, err
}
return relationsToChildList(relations), nil
}
return nil, &UnsupportedMemberTypeError{
MemberType: id.Type(),
}
}
func nodesToChildList(nodes osm.Nodes) core.ChildList {
if len(nodes) == 0 {
return nil
}
list := make(core.ChildList, len(nodes))
nodes.SortByIDVersion()
for i, n := range nodes {
list[i] = &childNode{
Index: i,
Node: n,
}
}
return list
}
func waysToChildList(ways osm.Ways) core.ChildList {
if len(ways) == 0 {
return nil
}
list := make(core.ChildList, len(ways))
ways.SortByIDVersion()
for i, w := range ways {
c := &childWay{
Index: i,
Way: w,
}
if i != 0 {
c.ReverseOfPrevious = isReverse(w, ways[i-1])
}
list[i] = c
}
return list
}
// isReverse checks to see if this way update was a "reversal". It is very tricky
// to generally answer this question but easier for a relation minor update.
// Since the relation wasn't updated we assume things are still connected and
// can just check the endpoints.
func isReverse(w1, w2 *osm.Way) bool {
if len(w1.Nodes) < 2 || len(w2.Nodes) < 2 {
return false
}
// check if either is a ring
if w1.Nodes[0].ID == w1.Nodes[len(w1.Nodes)-1].ID ||
w2.Nodes[0].ID == w2.Nodes[len(w2.Nodes)-1].ID {
r1 := orb.Ring(w1.LineString())
r2 := orb.Ring(w2.LineString())
return planar.Area(r1)*planar.Area(r2) < 0
}
// not a ring so see if endpoint were flipped
return w1.Nodes[0].ID == w2.Nodes[len(w2.Nodes)-1].ID &&
w2.Nodes[0].ID == w1.Nodes[len(w1.Nodes)-1].ID
}
func relationsToChildList(relations osm.Relations) core.ChildList {
if len(relations) == 0 {
return nil
}
list := make(core.ChildList, len(relations))
relations.SortByIDVersion()
for i, r := range relations {
list[i] = &childRelation{
Index: i,
Relation: r,
}
}
return list
}