-
Notifications
You must be signed in to change notification settings - Fork 1
/
to-georender.js
162 lines (155 loc) · 4.34 KB
/
to-georender.js
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
const encode = require('georender-pack/encode')
const setEdges = require('./lib/set-edges.js')
module.exports = encodeDynamic
module.exports.collection = encodeCollection
module.exports.feature = encodeFeature
function encodeDynamic(obj, opts) {
if (!obj) return []
else if (obj.type === 'FeatureCollection') {
return encodeCollection(obj, opts)
} else {
return encodeFeature(obj, opts, 0)
}
}
function encodeCollection(fc, opts) {
if (!fc || fc.type !== 'FeatureCollection') return []
return (fc.features || []).flatMap((feature,i) => encodeFeature(feature, opts, i))
}
function encodeFeature(feature, opts, i) {
var propertyMap = opts && opts.propertyMap || identity
if (!feature || feature.type !== 'Feature') return []
var g = feature.geometry
if (!g || !Array.isArray(g.coordinates)) return []
var props = propertyMap(feature.properties || {}, feature)
if (g.type === 'Point') {
return [ encode({
type: 'node',
tags: props,
id: props.id || i,
lon: g.coordinates[0],
lat: g.coordinates[1],
}) ]
} else if (g.type === 'MultiPoint') {
return g.coordinates.map(p => encode({
type: 'node',
tags: props,
id: props.id || i,
lon: p[0],
lat: p[1],
}))
} else if (g.type === 'LineString') {
var cs = g.coordinates
var deps = new Array(cs.length)
var refs = new Array(cs.length)
for (var j = 0; j < cs.length; j++) {
deps[j] = { lon: cs[j][0], lat: cs[j][1] }
refs[j] = j
}
return [ encode({
type: 'way',
tags: props,
id: props.id || i,
refs,
}, deps) ]
} else if (g.type === 'MultiLineString') {
return g.coordinates.map(cs => {
var deps = new Array(cs.length)
var refs = new Array(cs.length)
for (var j = 0; j < cs.length; j++) {
deps[j] = { lon: cs[j][0], lat: cs[j][1] }
refs[j] = j
}
return encode({
type: 'way',
tags: props,
id: props.id || i,
refs,
}, deps)
})
} else if (g.type === 'Polygon') {
var rings = g.coordinates
var deps = {}
var members = []
var id = 1
for (var j = 0; j < rings.length; j++) {
var cs = removeDuplicates(rings[j])
if (cs.length === 0) continue
var wayId = id++
members.push({
type: 'way',
role: j === 0 ? 'outer' : 'inner',
id: wayId,
})
var refs = []
var firstId = id
for (var k = 0; k < cs.length; k++) {
if (k === cs.length-1 && approxEq(cs[0], cs[k])) continue
refs.push(id)
deps[id] = { lon: cs[k][0], lat: cs[k][1] }
id++
}
refs.push(firstId)
deps[wayId] = { type: 'way', id: wayId, refs }
}
var buf = encode({
type: 'relation',
tags: Object.assign({}, props, { type: 'multipolygon' }),
id: props.id || i,
members,
}, deps)
if (g.edges) setEdges(buf, g.edges)
return [buf]
} else if (g.type === 'MultiPolygon') {
var mrings = g.coordinates
var deps = []
var members = []
var id = 1
for (var k = 0; k < mrings.length; k++) {
var rings = mrings[k]
for (var n = 0; n < rings.length; n++) {
var cs = removeDuplicates(rings[n])
if (cs.length === 0) continue
var wayId = id++
members.push({
type: 'way',
role: n === 0 ? 'outer' : 'inner',
id: wayId,
})
var refs = []
var firstId = id
for (var j = 0; j < cs.length; j++) {
if (j === cs.length-1 && approxEq(cs[0], cs[j])) continue
refs.push(id)
deps[id] = { lon: cs[j][0], lat: cs[j][1] }
id++
}
refs.push(firstId)
deps[wayId] = { type: 'way', id: wayId, refs }
}
}
var buf = encode({
type: 'relation',
tags: Object.assign({}, props, { type: 'multipolygon' }),
id: props.id || i,
members,
}, deps)
if (g.edges) setEdges(buf, g.edges)
return [buf]
} else {
return []
}
}
function approxEq(a,b) {
var epsilon = 1e-7
return Math.abs(a[0]-b[0]) < epsilon && Math.abs(a[1]-b[1]) < epsilon
}
function removeDuplicates(ring) {
var res = []
for (var i = 0; i < ring.length; i++) {
var p0 = ring[i]
var p1 = ring[(i+ring.length-1)%ring.length]
if (i === 0 || !approxEq(p0,p1)) res.push(p0)
}
return res
}
function identity(x) { return x }