forked from aaronlidman/osm-and-geojson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osm_geojson.js
466 lines (394 loc) · 15.5 KB
/
osm_geojson.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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
var osm_geojson = {};
osm_geojson.geojson2osm = function(geo, changeset, osmChange) {
function togeojson(geo, properties) {
var nodes = '',
ways = '',
relations = '';
properties = properties || {};
switch (geo.type) {
case 'Point':
var coord = roundCoords([geo.coordinates]);
nodes += '<node id="' + count + '" lat="' + coord[0][1] +
'" lon="' + coord[0][0] + '" changeset="' + changeset + '">';
nodes += propertiesToTags(properties);
nodes += '</node>';
count--;
break;
case 'MultiPoint':
break;
case 'LineString':
break;
case 'MultiLineString':
break;
case 'Polygon':
append(polygon(geo, properties));
break;
case 'MultiPolygon':
relations += '<relation id="' + count + '" changeset="' + changeset + '">';
properties.type = 'multipolygon';
count--;
for (var i = 0; i < geo.coordinates.length; i++){
poly = polygon({
'coordinates': geo.coordinates[i]
}, undefined, true);
nodes += poly.nodes;
ways += poly.ways;
relations += poly.relations;
}
relations += propertiesToTags(properties);
relations += '</relation>';
break;
}
function append(obj) {
nodes += obj.nodes;
ways += obj.ways;
relations += obj.relations;
}
osm = '<?xml version="1.0" encoding="UTF-8"?><osm version="0.6" generator="github.com/aaronlidman/osm-and-geojson">' +
nodes + ways + relations + '</osm>';
if (osmChange) {
osm = '<osmChange version="0.6" generator="github.com/aaronlidman/osm-and-geojson"><create>' +
nodes + ways + relations + '</create></osmChange>';
}
return {
nodes: nodes,
ways: ways,
relations: relations,
osm: osm
};
}
function polygon(geo, properties, multipolygon) {
var nodes = '',
ways = '',
relations = '',
role = '';
properties = properties || {};
multipolygon = multipolygon || false;
var coords = [];
if (geo.coordinates.length > 1) {
// polygon with holes -> multipolygon
if (!multipolygon) relations += '<relation id="' + count + '" changeset="' + changeset +'">';
count--;
properties.type = 'multipolygon';
for (var i = 0; i < geo.coordinates.length; i++) {
role = ((i === 0) ? 'outer' : 'inner');
relations += '<member type="way" ref="' + count + '" role="' + role + '"/>';
ways += '<way id="' + count + '" changeset="' + changeset + '">';
count--;
for (var a = 0; a < geo.coordinates[i].length-1; a++) {
coords.push([geo.coordinates[i][a][1], geo.coordinates[i][a][0]]);
}
coords = createNodes(coords, true);
nodes += coords.nodes;
ways += coords.nds;
ways += '</way>';
coords = [];
}
if (!multipolygon) {
relations += propertiesToTags(properties);
relations += '</relation>';
}
} else {
// polygon -> way
ways += '<way id="' + count + '" changeset="' + changeset + '">';
if (multipolygon) relations += '<member type="way" ref="' + count + '" role="outer"/>';
count--;
for (var j = 0; j < geo.coordinates[0].length-1; j++) {
coords.push([geo.coordinates[0][j][1], geo.coordinates[0][j][0]]);
}
coords = createNodes(coords, true);
nodes += coords.nodes;
ways += coords.nds;
ways += propertiesToTags(properties);
ways += '</way>';
}
return {
nodes: nodes,
ways: ways,
relations: relations
};
}
function propertiesToTags(properties) {
var tags = '';
for (var tag in properties) {
if (properties[tag] !== null) {
tags += '<tag k="' + tag + '" v="' + properties[tag] + '"/>';
}
}
return tags;
}
function roundCoords(coords){
for (var a = 0; a < coords.length; a++) {
coords[a][0] = Math.round(coords[a][0] * 1000000) / 1000000;
coords[a][1] = Math.round(coords[a][1] * 1000000) / 1000000;
}
return coords;
}
function createNodes(coords, repeatLastND) {
var nds = '',
nodes = '',
length = coords.length;
repeatLastND = repeatLastND || false;
// for polygons
coords = roundCoords(coords);
for (var a = 0; a < length; a++) {
if (repeatLastND && a === 0) repeatLastND = count;
nds += '<nd ref="' + count + '"/>';
nodes += '<node id="' + count + '" lat="' + coords[a][0] +'" lon="' + coords[a][1] +
'" changeset="' + changeset + '"/>';
if (repeatLastND && a === length-1) nds += '<nd ref="' + repeatLastND + '"/>';
count--;
}
return {'nds': nds, 'nodes': nodes};
}
if (typeof geo === 'string') geo = JSON.parse(geo);
var obj,
count = -1;
changeset = changeset || false;
switch (geo.type) {
case 'FeatureCollection':
var temp = {
nodes: '',
ways: '',
relations: ''
};
obj = [];
for (var i = 0; i < geo.features.length; i++){
obj.push(togeojson(geo.features[i].geometry, geo.features[i].properties));
}
temp.osm = '<?xml version="1.0" encoding="UTF-8"?><osm version="0.6" generator="github.com/aaronlidman/osm-and-geojson">';
if (osmChange) temp.osm = '<osmChange version="0.6" generator="github.com/aaronlidman/osm-and-geojson"><create>';
for (var n = 0; n < obj.length; n++) {
temp.nodes += obj[n].nodes;
temp.ways += obj[n].ways;
temp.relations += obj[n].relations;
}
temp.osm += temp.nodes + temp.ways + temp.relations;
if (osmChange) {
temp.osm += '</create></osmChange>';
} else {
temp.osm += '</osm>';
}
obj = temp.osm;
break;
case 'GeometryCollection':
obj = [];
for (var j = 0; j < geo.geometries.length; j++){
obj.push(togeojson(geo.geometries[j]));
}
break;
case 'Feature':
obj = togeojson(geo.geometry, geo.properties);
obj = obj.osm;
break;
case 'Point':
case 'MultiPoint':
case 'LineString':
case 'MultiLineString':
case 'Polygon':
case 'MultiPolygon':
obj = togeojson(geo);
obj = obj.osm;
break;
default:
if (console) console.log('Invalid GeoJSON object: GeoJSON object must be one of \"Point\", \"LineString\", ' +
'\"Polygon\", \"MultiPolygon\", \"Feature\", \"FeatureCollection\" or \"GeometryCollection\".');
}
return obj;
};
osm_geojson.osm2geojson = function(osm, metaProperties) {
var xml = parse(osm),
usedCoords = {},
nodeCache = cacheNodes(),
wayCache = cacheWays();
return Bounds({
type : 'FeatureCollection',
features : []
.concat(Ways(wayCache))
.concat(Ways(Relations))
.concat(Points(nodeCache))
}, xml);
function parse(xml) {
if (typeof xml !== 'string') return xml;
return (new DOMParser()).parseFromString(xml, 'text/xml');
}
function Bounds(geo, xml) {
var bounds = getBy(xml, 'bounds');
if (!bounds.length) return geo;
geo.bbox = [
attrf(bounds[0], 'minlon'),
attrf(bounds[0], 'minlat'),
attrf(bounds[0], 'maxlon'),
attrf(bounds[0], 'maxlat')
];
return geo;
}
function setProperties(element) {
if (!element) return {};
var props = {},
tags = element.getElementsByTagName('tag'),
tags_length = tags.length;
for (var t = 0; t < tags_length; t++) {
props[attr(tags[t], 'k')] = isNumber(attr(tags[t], 'v')) ?
parseFloat(attr(tags[t], 'v')) : attr(tags[t], 'v');
}
if (metaProperties) {
setIf(element, 'id', props, 'osm_id');
setIf(element, 'user', props, 'osm_lastEditor');
setIf(element, 'version', props, 'osm_version', true);
setIf(element, 'changeset', props, 'osm_lastChangeset', true);
setIf(element, 'timestamp', props, 'osm_lastEdited');
}
return sortObject(props);
}
function getFeature(element, type, coordinates) {
return {
geometry: {
type: type,
coordinates: coordinates || []
},
type: 'Feature',
properties: setProperties(element)
};
}
function cacheNodes() {
var nodes = getBy(xml, 'node'),
coords = {};
for (var n = 0; n < nodes.length; n++) {
coords[attr(nodes[n], 'id')] = nodes[n];
}
return coords;
}
function Points(nodeCache) {
var points = nodeCache,
features = [];
for (var node in nodeCache) {
var tags = getBy(nodeCache[node], 'tag');
if (!usedCoords[node] || tags.length)
features.push(getFeature(nodeCache[node], 'Point', lonLat(nodeCache[node])));
}
return features;
}
function cacheWays() {
var ways = getBy(xml, 'way'),
out = {};
for (var w = 0; w < ways.length; w++) {
var feature = {},
nds = getBy(ways[w], 'nd');
if (attr(nds[0], 'ref') === attr(nds[nds.length - 1], 'ref')) {
feature = getFeature(ways[w], 'Polygon', [[]]);
} else {
feature = getFeature(ways[w], 'LineString');
}
for (var n = 0; n < nds.length; n++) {
var node = nodeCache[attr(nds[n], 'ref')];
if (node) {
var cords = lonLat(node);
usedCoords[attr(nds[n], 'ref')] = true;
if (feature.geometry.type === 'Polygon') {
feature.geometry.coordinates[0].push(cords);
} else {
feature.geometry.coordinates.push(cords);
}
}
}
out[attr(ways[w], 'id')] = feature;
}
return out;
}
function Relations() {
var relations = getBy(xml, 'relation'),
relations_length = relations.length,
features = [];
for (var r = 0; r < relations_length; r++) {
var feature = getFeature(relations[r], 'MultiPolygon');
if (feature.properties.type == 'multipolygon') {
var members = getBy(relations[r], 'member');
// osm doesn't keep roles in order, so we do this twice
for (var m = 0; m < members.length; m++) {
if (attr(members[m], 'role') == 'outer') assignWay(members[m], feature);
}
for (var n = 0; n < members.length; n++) {
if (attr(members[n], 'role') == 'inner') assignWay(members[n], feature);
}
delete feature.properties.type;
} else {
// http://taginfo.openstreetmap.us/relations
}
if (feature.geometry.coordinates.length) features.push(feature);
}
return features;
function assignWay(member, feature) {
var ref = attr(member, 'ref'),
way = wayCache[ref];
if (way && way.geometry.type == 'Polygon') {
if (way && attr(member, 'role') == 'outer') {
feature.geometry.coordinates.push(way.geometry.coordinates);
if (way.properties) {
// exterior polygon properties can move to the multipolygon
// but multipolygon (relation) tags take precedence
for (var prop in way.properties) {
if (!feature.properties[prop]) {
feature.properties[prop] = prop;
}
}
}
} else if (way && attr(member, 'role') == 'inner'){
if (feature.geometry.coordinates.length > 1) {
// do a point in polygon against each outer
// this determines which outer the inner goes with
for (var a = 0; a < feature.geometry.coordinates.length; a++) {
if (pointInPolygon(
way.geometry.coordinates[0][0],
feature.geometry.coordinates[a][0])) {
feature.geometry.coordinates[a].push(way.geometry.coordinates[0]);
break;
}
}
} else {
if (feature.geometry.coordinates.length) {
feature.geometry.coordinates[0].push(way.geometry.coordinates[0]);
}
}
}
}
wayCache[ref] = false;
}
}
function Ways(wayCache) {
var features = [];
for (var w in wayCache) if (wayCache[w]) features.push(wayCache[w]);
return features;
}
// https://github.com/substack/point-in-polygon/blob/master/index.js
function pointInPolygon(point, vs) {
var x = point[0], y = point[1];
var inside = false;
for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
var xi = vs[i][0], yi = vs[i][1],
xj = vs[j][0], yj = vs[j][1],
intersect = ((yi > y) != (yj > y)) &&
(x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
}
// http://stackoverflow.com/a/1359808
function sortObject(o) {
var sorted = {}, key, a = [];
for (key in o) if (o.hasOwnProperty(key)) a.push(key);
a.sort();
for (key = 0; key < a.length; key++) sorted[a[key]] = o[a[key]];
return sorted;
}
// http://stackoverflow.com/a/1830844
function isNumber(n) { return !isNaN(parseFloat(n)) && isFinite(n); }
function attr(x, y) { return x.getAttribute(y); }
function attrf(x, y) { return parseFloat(x.getAttribute(y)); }
function getBy(x, y) { return x.getElementsByTagName(y); }
function lonLat(elem) { return [attrf(elem, 'lon'), attrf(elem, 'lat')]; }
function setIf(x, y, o, name, f) {
if (attr(x, y)) o[name] = f ? parseFloat(attr(x, y)) : attr(x, y);
}
};
if (typeof module !== 'undefined') module.exports = osm_geojson;