-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathconvert.go
416 lines (339 loc) · 8.65 KB
/
convert.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
406
407
408
409
410
411
412
413
414
415
416
package osmgeojson
import (
"fmt"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
"github.com/paulmach/osm"
"github.com/paulmach/osm/internal/mputil"
)
type context struct {
noID bool
noMeta bool
noRelationMembership bool
includeInvalidPolygons bool
osm *osm.OSM
skippable map[osm.WayID]struct{}
relationMember map[osm.FeatureID][]*relationSummary
wayMember map[osm.NodeID]struct{}
nodeMap map[osm.NodeID]*osm.Node
wayMap map[osm.WayID]*osm.Way
}
type relationSummary struct {
ID osm.RelationID `json:"id"`
Role string `json:"role"`
Tags map[string]string `json:"tags"`
}
// Convert takes a set of osm elements and converts them
// to a geojson feature collection.
func Convert(o *osm.OSM, opts ...Option) (*geojson.FeatureCollection, error) {
ctx := &context{
osm: o,
skippable: make(map[osm.WayID]struct{}),
}
for _, opt := range opts {
if err := opt(ctx); err != nil {
return nil, err
}
}
ctx.wayMap = make(map[osm.WayID]*osm.Way, len(o.Ways))
for _, w := range ctx.osm.Ways {
ctx.wayMap[w.ID] = w
}
ctx.wayMember = make(map[osm.NodeID]struct{}, len(ctx.osm.Nodes))
for _, w := range ctx.osm.Ways {
for i := range w.Nodes {
ctx.wayMember[w.Nodes[i].ID] = struct{}{}
}
}
// figure out relation membership map
ctx.relationMember = make(map[osm.FeatureID][]*relationSummary)
for _, relation := range ctx.osm.Relations {
var tags map[string]string
for _, m := range relation.Members {
if ctx.noRelationMembership && m.Type != osm.TypeNode {
// If we don't need to do relation membership we only
// need this for nodes to check if they're interesting.
continue
}
if m.Type == osm.TypeWay {
// We only need to store the way membership for ways that are
// present. eg. relations could have thousands of members but only
// a few in set of osm.
if _, ok := ctx.wayMap[osm.WayID(m.Ref)]; !ok {
continue
}
}
if tags == nil {
tags = relation.Tags.Map()
}
fid := m.FeatureID()
ctx.relationMember[fid] = append(ctx.relationMember[fid], &relationSummary{
ID: relation.ID,
Role: m.Role,
Tags: tags,
})
}
}
features := make([]*geojson.Feature, 0, len(ctx.osm.Relations)+len(ctx.osm.Ways))
// relations
for _, relation := range ctx.osm.Relations {
tt := relation.Tags.Find("type")
if tt == "route" {
feature := ctx.buildRouteLineString(relation)
if feature != nil {
features = append(features, feature)
}
} else if tt == "multipolygon" || tt == "boundary" {
feature := ctx.buildPolygon(relation)
if feature != nil {
features = append(features, feature)
}
}
// NOTE: we skip/ignore relation that aren't multipolygons, boundaries or routes
}
for _, way := range ctx.osm.Ways {
// should skip only skippable relation members
if _, skip := ctx.skippable[way.ID]; skip {
continue
}
feature := ctx.wayToFeature(way)
if feature != nil {
features = append(features, feature)
}
}
for _, node := range ctx.osm.Nodes {
// should NOT skip if any are true:
// not a member of a way.
// a member of a relation member
// has any interesting tags
// should skip if all are true:
// a member of a way.
// not a member of a relation member
// does not have any interesting tags
if _, ok := ctx.wayMember[node.ID]; ok &&
len(ctx.relationMember[node.FeatureID()]) == 0 &&
!hasInterestingTags(node.Tags, nil) {
continue
}
feature := ctx.nodeToFeature(node)
if feature != nil {
features = append(features, feature)
}
}
fc := geojson.NewFeatureCollection()
fc.Features = features
return fc, nil
}
// getNode will find the node in the set.
// This allows to lazily create the node map only if
// the nodes+ways aren't augmented (ie. include the lat/lon on them).
func (ctx *context) getNode(id osm.NodeID) *osm.Node {
if ctx.nodeMap == nil {
ctx.nodeMap = make(map[osm.NodeID]*osm.Node, len(ctx.osm.Nodes))
for _, n := range ctx.osm.Nodes {
ctx.nodeMap[n.ID] = n
}
}
return ctx.nodeMap[id]
}
func (ctx *context) nodeToFeature(n *osm.Node) *geojson.Feature {
// our definition of empty, ill defined
if n.Lon == 0 && n.Lat == 0 && n.Version == 0 {
return nil
}
f := geojson.NewFeature(orb.Point{n.Lon, n.Lat})
if !ctx.noID {
f.ID = fmt.Sprintf("node/%d", n.ID)
}
f.Properties["id"] = int(n.ID)
f.Properties["type"] = "node"
f.Properties["tags"] = n.Tags.Map()
ctx.addMetaProperties(f.Properties, n)
return f
}
func (ctx *context) wayToLineString(w *osm.Way) (orb.LineString, bool) {
ls := make(orb.LineString, 0, len(w.Nodes))
tainted := false
for _, wn := range w.Nodes {
if wn.Lon != 0 || wn.Lat != 0 {
ls = append(ls, orb.Point{wn.Lon, wn.Lat})
} else if n := ctx.getNode(wn.ID); n != nil {
ls = append(ls, orb.Point{n.Lon, n.Lat})
} else {
tainted = true
}
}
return ls, tainted
}
func (ctx *context) wayToFeature(w *osm.Way) *geojson.Feature {
ls, tainted := ctx.wayToLineString(w)
if len(ls) <= 1 {
// one node ways are ignored.
return nil
}
var f *geojson.Feature
if w.Polygon() {
p := orb.Polygon{toRing(ls)}
reorient(p)
f = geojson.NewFeature(p)
} else {
f = geojson.NewFeature(ls)
}
if !ctx.noID {
f.ID = fmt.Sprintf("way/%d", w.ID)
}
f.Properties["id"] = int(w.ID)
f.Properties["type"] = "way"
f.Properties["tags"] = w.Tags.Map()
if tainted {
f.Properties["tainted"] = true
}
ctx.addMetaProperties(f.Properties, w)
return f
}
func (ctx *context) buildRouteLineString(relation *osm.Relation) *geojson.Feature {
lines := make([]mputil.Segment, 0, 10)
tainted := false
for _, m := range relation.Members {
if m.Type != osm.TypeWay {
continue
}
way := ctx.wayMap[osm.WayID(m.Ref)]
if way == nil {
tainted = true
continue
}
if !hasInterestingTags(way.Tags, nil) {
ctx.skippable[way.ID] = struct{}{}
}
ls, t := ctx.wayToLineString(way)
if t {
tainted = true
}
if len(ls) == 0 {
continue
}
lines = append(lines, mputil.Segment{
Orientation: m.Orientation,
Line: ls,
})
}
if len(lines) == 0 {
// route relation is here, but we don't have any of the way members?
// TODO: what to do about this?
return nil
}
lineSections := mputil.Join(lines)
var geometry orb.Geometry
if len(lineSections) == 1 {
geometry = lineSections[0].LineString()
} else {
mls := make(orb.MultiLineString, 0, len(lines))
for _, ls := range lineSections {
mls = append(mls, ls.LineString())
}
geometry = mls
}
f := geojson.NewFeature(geometry)
if !ctx.noID {
f.ID = fmt.Sprintf("relation/%d", relation.ID)
}
f.Properties["id"] = int(relation.ID)
f.Properties["type"] = "relation"
if tainted {
f.Properties["tainted"] = true
}
f.Properties["tags"] = relation.Tags.Map()
ctx.addMetaProperties(f.Properties, relation)
return f
}
func (ctx *context) addMetaProperties(props geojson.Properties, e osm.Element) {
if !ctx.noRelationMembership {
relations := ctx.relationMember[e.FeatureID()]
if len(relations) != 0 {
props["relations"] = relations
} else {
props["relations"] = []*relationSummary{}
}
}
if ctx.noMeta {
return
}
meta := make(map[string]interface{}, 5)
switch e := e.(type) {
case *osm.Node:
if !e.Timestamp.IsZero() {
meta["timestamp"] = e.Timestamp
}
if e.Version != 0 {
meta["version"] = e.Version
}
if e.ChangesetID != 0 {
meta["changeset"] = e.ChangesetID
}
if e.User != "" {
meta["user"] = e.User
}
if e.UserID != 0 {
meta["uid"] = e.UserID
}
case *osm.Way:
if !e.Timestamp.IsZero() {
meta["timestamp"] = e.Timestamp
}
if e.Version != 0 {
meta["version"] = e.Version
}
if e.ChangesetID != 0 {
meta["changeset"] = e.ChangesetID
}
if e.User != "" {
meta["user"] = e.User
}
if e.UserID != 0 {
meta["uid"] = e.UserID
}
case *osm.Relation:
if !e.Timestamp.IsZero() {
meta["timestamp"] = e.Timestamp
}
if e.Version != 0 {
meta["version"] = e.Version
}
if e.ChangesetID != 0 {
meta["changeset"] = e.ChangesetID
}
if e.User != "" {
meta["user"] = e.User
}
if e.UserID != 0 {
meta["uid"] = e.UserID
}
default:
panic("unsupported type")
}
props["meta"] = meta
}
func hasInterestingTags(tags osm.Tags, ignore map[string]string) bool {
if len(tags) == 0 {
return false
}
for _, tag := range tags {
k, v := tag.Key, tag.Value
if !osm.UninterestingTags[k] &&
(ignore == nil || !(ignore[k] == "true" || ignore[k] == v)) {
return true
}
}
return false
}
func toRing(ls orb.LineString) orb.Ring {
if len(ls) < 2 {
return orb.Ring(ls)
}
// duplicate last point
if ls[0] != ls[len(ls)-1] {
return orb.Ring(append(ls, ls[0]))
}
return orb.Ring(ls)
}