forked from paulmach/osm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasource.go
188 lines (148 loc) · 4.1 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package annotate
import (
"context"
"github.com/ich5003/small-osm"
"github.com/ich5003/small-osm/annotate/internal/core"
"github.com/ich5003/small-osm/annotate/shared"
"github.com/paulmach/orb"
"github.com/paulmach/orb/planar"
)
type wayDatasource struct {
NodeHistoryDatasourcer
}
type wayChildDatasource struct {
NodeHistoryAsChildrenDatasourcer
}
func newWayDatasourcer(ds NodeHistoryDatasourcer) core.Datasourcer {
if d, ok := ds.(NodeHistoryAsChildrenDatasourcer); ok {
return &wayChildDatasource{d}
}
return &wayDatasource{ds}
}
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
}
func (wds *wayChildDatasource) Get(ctx context.Context, id osm.FeatureID) (core.ChildList, error) {
if id.Type() != osm.TypeNode {
panic("only node types supported")
}
return wds.NodeHistoryAsChildren(ctx, id.NodeID())
}
type relationDatasource struct {
osm.HistoryDatasourcer
}
type relationChildDatasource struct {
HistoryAsChildrenDatasourcer
}
func newRelationDatasourcer(ds osm.HistoryDatasourcer) core.Datasourcer {
if d, ok := ds.(HistoryAsChildrenDatasourcer); ok {
return &relationChildDatasource{d}
}
return &relationDatasource{ds}
}
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 (rds *relationChildDatasource) Get(ctx context.Context, id osm.FeatureID) (core.ChildList, error) {
switch id.Type() {
case osm.TypeNode:
return rds.NodeHistoryAsChildren(ctx, id.NodeID())
case osm.TypeWay:
return rds.WayHistoryAsChildren(ctx, id.WayID())
case osm.TypeRelation:
return rds.RelationHistoryAsChildren(ctx, id.RelationID())
}
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 {
c := shared.FromNode(n)
c.VersionIndex = i
list[i] = c
}
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 := shared.FromWay(w)
c.VersionIndex = i
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 {
c := shared.FromRelation(r)
c.VersionIndex = i
list[i] = c
}
return list
}