forked from paulmach/osm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osm.go
405 lines (328 loc) · 9.39 KB
/
osm.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package osm
import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"strconv"
"github.com/gogo/protobuf/proto"
"github.com/paulmach/osm/internal/osmpb"
)
// These values should be returned if the osm data is actual
// osm data to give some information about the source and license.
const (
Copyright = "OpenStreetMap and contributors"
Attribution = "http://www.openstreetmap.org/copyright"
License = "http://opendatacommons.org/licenses/odbl/1-0/"
)
// OSM represents the core osm data
// designed to parse http://wiki.openstreetmap.org/wiki/OSM_XML
type OSM struct {
Version float64 `xml:"version,attr,omitempty"`
Generator string `xml:"generator,attr,omitempty"`
// These three attributes are returned by the osm api.
// The Copyright, Attribution and License constants contain
// suggested values that match those returned by the official api.
Copyright string `xml:"copyright,attr,omitempty"`
Attribution string `xml:"attribution,attr,omitempty"`
License string `xml:"license,attr,omitempty"`
Bounds *Bounds `xml:"bounds,omitempty"`
Nodes Nodes `xml:"node"`
Ways Ways `xml:"way"`
Relations Relations `xml:"relation"`
// Changesets will typically not be included with actual data,
// but all this stuff is technically all under the osm xml
Changesets Changesets `xml:"changeset"`
Notes Notes `xml:"note"`
Users Users `xml:"user"`
}
// Marshal encodes the osm data using protocol buffers.
// Will only save the elements: nodes, ways and relations.
func (o *OSM) Marshal() ([]byte, error) {
ss := &stringSet{}
encoded := marshalOSM(o, ss, true)
encoded.Strings = ss.Strings()
return proto.Marshal(encoded)
}
// Append will add the given object to the OSM object.
func (o *OSM) Append(obj Object) {
switch obj.ObjectID().Type() {
case TypeNode:
o.Nodes = append(o.Nodes, obj.(*Node))
case TypeWay:
o.Ways = append(o.Ways, obj.(*Way))
case TypeRelation:
o.Relations = append(o.Relations, obj.(*Relation))
case TypeChangeset:
o.Changesets = append(o.Changesets, obj.(*Changeset))
case TypeNote:
o.Notes = append(o.Notes, obj.(*Note))
case TypeUser:
o.Users = append(o.Users, obj.(*User))
default:
panic(fmt.Sprintf("unsupported type: %[1]T: %[1]v", obj))
}
}
// Elements returns all the nodes, ways and relations
// as a single slice of Elements.
func (o *OSM) Elements() Elements {
if o == nil {
return nil
}
result := make(Elements, 0, len(o.Nodes)+len(o.Ways)+len(o.Relations))
for _, e := range o.Nodes {
result = append(result, e)
}
for _, e := range o.Ways {
result = append(result, e)
}
for _, e := range o.Relations {
result = append(result, e)
}
return result
}
// Objects returns an array of objects containing any nodes, ways, relations,
// changesets, notes and users.
func (o *OSM) Objects() Objects {
if o == nil {
return nil
}
result := make(Objects, 0, len(o.Nodes)+len(o.Ways)+len(o.Relations)+len(o.Changesets)+len(o.Notes)+len(o.Users))
for _, o := range o.Nodes {
result = append(result, o)
}
for _, o := range o.Ways {
result = append(result, o)
}
for _, o := range o.Relations {
result = append(result, o)
}
for _, o := range o.Changesets {
result = append(result, o)
}
for _, o := range o.Users {
result = append(result, o)
}
for _, o := range o.Notes {
result = append(result, o)
}
return result
}
// FeatureIDs returns the slice of feature ids for all the
// nodes, ways and relations.
func (o *OSM) FeatureIDs() FeatureIDs {
if o == nil {
return nil
}
result := make(FeatureIDs, 0, len(o.Nodes)+len(o.Ways)+len(o.Relations))
for _, e := range o.Nodes {
result = append(result, e.FeatureID())
}
for _, e := range o.Ways {
result = append(result, e.FeatureID())
}
for _, e := range o.Relations {
result = append(result, e.FeatureID())
}
return result
}
// ElementIDs returns the slice of element ids for all the
// nodes, ways and relations.
func (o *OSM) ElementIDs() ElementIDs {
if o == nil {
return nil
}
result := make(ElementIDs, 0, len(o.Nodes)+len(o.Ways)+len(o.Relations))
for _, e := range o.Nodes {
result = append(result, e.ElementID())
}
for _, e := range o.Ways {
result = append(result, e.ElementID())
}
for _, e := range o.Relations {
result = append(result, e.ElementID())
}
return result
}
// HistoryDatasource converts the osm object a datasource accessible
// by the feature id.
func (o *OSM) HistoryDatasource() *HistoryDatasource {
ds := &HistoryDatasource{}
ds.add(o)
return ds
}
// UnmarshalOSM will unmarshal the data into a OSM object.
func UnmarshalOSM(data []byte) (*OSM, error) {
pbf := &osmpb.OSM{}
err := proto.Unmarshal(data, pbf)
if err != nil {
return nil, err
}
return unmarshalOSM(pbf, pbf.GetStrings(), nil)
}
// includeChangeset can be set to false to not repeat the changeset
// info for every item, if this comes from osm change data.
func marshalOSM(o *OSM, ss *stringSet, includeChangeset bool) *osmpb.OSM {
encoded := &osmpb.OSM{}
if o == nil {
return nil
}
if len(o.Nodes) > 0 {
encoded.DenseNodes = marshalNodes(o.Nodes, ss, includeChangeset)
}
if len(o.Ways) > 0 {
encoded.Ways = make([]*osmpb.Way, len(o.Ways))
for i, w := range o.Ways {
encoded.Ways[i] = marshalWay(w, ss, includeChangeset)
}
}
if len(o.Relations) > 0 {
encoded.Relations = make([]*osmpb.Relation, len(o.Relations))
for i, r := range o.Relations {
encoded.Relations[i] = marshalRelation(r, ss, includeChangeset)
}
}
if o.Bounds != nil {
encoded.Bounds = &osmpb.Bounds{
MinLat: geoToInt64(o.Bounds.MinLat),
MaxLat: geoToInt64(o.Bounds.MaxLat),
MinLon: geoToInt64(o.Bounds.MinLon),
MaxLon: geoToInt64(o.Bounds.MaxLon),
}
}
return encoded
}
func unmarshalOSM(encoded *osmpb.OSM, ss []string, cs *Changeset) (*OSM, error) {
if encoded == nil {
return nil, nil
}
o := &OSM{}
if len(encoded.Nodes) != 0 && encoded.DenseNodes != nil {
return nil, errors.New("found both nodes and dense nodes")
}
if len(encoded.Nodes) != 0 {
o.Nodes = make([]*Node, len(encoded.Nodes))
for i, en := range encoded.Nodes {
n, err := unmarshalNode(en, ss, cs)
if err != nil {
return nil, err
}
o.Nodes[i] = n
}
}
if encoded.DenseNodes != nil {
var err error
o.Nodes, err = unmarshalNodes(encoded.DenseNodes, ss, cs)
if err != nil {
return nil, err
}
}
if len(encoded.Ways) != 0 {
o.Ways = make([]*Way, len(encoded.Ways))
for i, ew := range encoded.Ways {
w, err := unmarshalWay(ew, ss, cs)
if err != nil {
return nil, err
}
o.Ways[i] = w
}
}
if len(encoded.Relations) != 0 {
o.Relations = make([]*Relation, len(encoded.Relations))
for i, er := range encoded.Relations {
r, err := unmarshalRelation(er, ss, cs)
if err != nil {
return nil, err
}
o.Relations[i] = r
}
}
if encoded.Bounds != nil {
o.Bounds = &Bounds{
MinLat: float64(encoded.Bounds.GetMinLat()) / locMultiple,
MaxLat: float64(encoded.Bounds.GetMaxLat()) / locMultiple,
MinLon: float64(encoded.Bounds.GetMinLon()) / locMultiple,
MaxLon: float64(encoded.Bounds.GetMaxLon()) / locMultiple,
}
}
return o, nil
}
// MarshalJSON allows the tags to be marshalled as an object
// as defined by the overpass osmjson.
// http://overpass-api.de/output_formats.html#json
func (o OSM) MarshalJSON() ([]byte, error) {
s := struct {
Version float64 `json:"version,omitempty"`
Generator string `json:"generator,omitempty"`
Copyright string `json:"copyright,omitempty"`
Attribution string `json:"attribution,omitempty"`
License string `json:"license,omitempty"`
Elements Objects `json:"elements"`
}{o.Version, o.Generator, o.Copyright,
o.Attribution, o.License, o.Objects()}
return json.Marshal(s)
}
// MarshalXML implements the xml.Marshaller method to allow for the
// correct wrapper/start element case and attr data.
func (o OSM) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name.Local = "osm"
start.Attr = make([]xml.Attr, 0, 5)
if o.Version != 0 {
start.Attr = append(start.Attr, xml.Attr{
Name: xml.Name{Local: "version"},
Value: strconv.FormatFloat(o.Version, 'g', -1, 64),
})
}
if o.Generator != "" {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "generator"}, Value: o.Generator})
}
if o.Copyright != "" {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "copyright"}, Value: o.Copyright})
}
if o.Attribution != "" {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "attribution"}, Value: o.Attribution})
}
if o.License != "" {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "license"}, Value: o.License})
}
if err := e.EncodeToken(start); err != nil {
return err
}
if err := o.marshalInnerXML(e); err != nil {
return err
}
return e.EncodeToken(start.End())
}
func (o *OSM) marshalInnerXML(e *xml.Encoder) error {
if o == nil {
return nil
}
if err := e.Encode(o.Bounds); err != nil {
return err
}
if err := e.Encode(o.Nodes); err != nil {
return err
}
if err := e.Encode(o.Ways); err != nil {
return err
}
if err := e.Encode(o.Relations); err != nil {
return err
}
if err := e.Encode(o.Changesets); err != nil {
return err
}
if err := e.Encode(o.Notes); err != nil {
return err
}
return e.Encode(o.Users)
}
func (o *OSM) marshalInnerElementsXML(e *xml.Encoder) error {
if err := e.Encode(o.Nodes); err != nil {
return err
}
if err := e.Encode(o.Ways); err != nil {
return err
}
return e.Encode(o.Relations)
}