forked from paulmach/osm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrelation.go
155 lines (126 loc) · 5.1 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
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
package osmapi
import (
"context"
"fmt"
"strconv"
"github.com/onXmaps/osm"
)
// Relation returns the latest version of the relation from the osm rest api.
// Delegates to the DefaultDatasource and uses its http.Client to make the request.
func Relation(ctx context.Context, id osm.RelationID, opts ...FeatureOption) (*osm.Relation, error) {
return DefaultDatasource.Relation(ctx, id, opts...)
}
// Relation returns the latest version of the relation from the osm rest api.
func (ds *Datasource) Relation(ctx context.Context, id osm.RelationID, opts ...FeatureOption) (*osm.Relation, error) {
params, err := featureOptions(opts)
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s/relation/%d?%s", ds.baseURL(), id, params)
o := &osm.OSM{}
if err := ds.getFromAPI(ctx, url, &o); err != nil {
return nil, err
}
if l := len(o.Relations); l != 1 {
return nil, fmt.Errorf("wrong number of relations, expected 1, got %v", l)
}
return o.Relations[0], nil
}
// Relations returns the latest version of the relations from the osm rest api.
// Delegates to the DefaultDatasource and uses its http.Client to make the request.
func Relations(ctx context.Context, ids []osm.RelationID, opts ...FeatureOption) (osm.Relations, error) {
return DefaultDatasource.Relations(ctx, ids, opts...)
}
// Relations returns the latest version of the relations from the osm rest api.
// Will return 404 if any node is missing.
func (ds *Datasource) Relations(ctx context.Context, ids []osm.RelationID, opts ...FeatureOption) (osm.Relations, error) {
params, err := featureOptions(opts)
if err != nil {
return nil, err
}
data := make([]byte, 0, 11*len(ids))
for i, id := range ids {
if i != 0 {
data = append(data, byte(','))
}
data = strconv.AppendInt(data, int64(id), 10)
}
url := ds.baseURL() + "/relations?relations=" + string(data)
if len(params) > 0 {
url += "&" + params
}
o := &osm.OSM{}
if err := ds.getFromAPI(ctx, url, &o); err != nil {
return nil, err
}
return o.Relations, nil
}
// RelationVersion returns the specific version of the relation from the osm rest api.
// Delegates to the DefaultDatasource and uses its http.Client to make the request.
func RelationVersion(ctx context.Context, id osm.RelationID, v int) (*osm.Relation, error) {
return DefaultDatasource.RelationVersion(ctx, id, v)
}
// RelationVersion returns the specific version of the relation from the osm rest api.
func (ds *Datasource) RelationVersion(ctx context.Context, id osm.RelationID, v int) (*osm.Relation, error) {
url := fmt.Sprintf("%s/relation/%d/%d", ds.baseURL(), id, v)
o := &osm.OSM{}
if err := ds.getFromAPI(ctx, url, &o); err != nil {
return nil, err
}
if l := len(o.Relations); l != 1 {
return nil, fmt.Errorf("wrong number of relations, expected 1, got %v", l)
}
return o.Relations[0], nil
}
// RelationRelations returns all relations a relation is part of.
// There is no error if the element does not exist.
// Delegates to the DefaultDatasource and uses its http.Client to make the request.
func RelationRelations(ctx context.Context, id osm.RelationID, opts ...FeatureOption) (osm.Relations, error) {
return DefaultDatasource.RelationRelations(ctx, id, opts...)
}
// RelationRelations returns all relations a relation is part of.
// There is no error if the element does not exist.
func (ds *Datasource) RelationRelations(ctx context.Context, id osm.RelationID, opts ...FeatureOption) (osm.Relations, error) {
params, err := featureOptions(opts)
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s/relation/%d/relations?%s", ds.baseURL(), id, params)
o := &osm.OSM{}
if err := ds.getFromAPI(ctx, url, &o); err != nil {
return nil, err
}
return o.Relations, nil
}
// RelationHistory returns all the versions of the relation from the osm rest api.
// Delegates to the DefaultDatasource and uses its http.Client to make the request.
func RelationHistory(ctx context.Context, id osm.RelationID) (osm.Relations, error) {
return DefaultDatasource.RelationHistory(ctx, id)
}
// RelationHistory returns all the versions of the relation from the osm rest api.
func (ds *Datasource) RelationHistory(ctx context.Context, id osm.RelationID) (osm.Relations, error) {
url := fmt.Sprintf("%s/relation/%d/history", ds.baseURL(), id)
o := &osm.OSM{}
if err := ds.getFromAPI(ctx, url, &o); err != nil {
return nil, err
}
return o.Relations, nil
}
// RelationFull returns the relation and its nodes for the latest version the relation.
// Delegates to the DefaultDatasource and uses its http.Client to make the request.
func RelationFull(ctx context.Context, id osm.RelationID, opts ...FeatureOption) (*osm.OSM, error) {
return DefaultDatasource.RelationFull(ctx, id, opts...)
}
// RelationFull returns the relation and its nodes for the latest version the relation.
func (ds *Datasource) RelationFull(ctx context.Context, id osm.RelationID, opts ...FeatureOption) (*osm.OSM, error) {
params, err := featureOptions(opts)
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s/relation/%d/full?%s", ds.baseURL(), id, params)
o := &osm.OSM{}
if err := ds.getFromAPI(ctx, url, &o); err != nil {
return nil, err
}
return o, nil
}