-
Notifications
You must be signed in to change notification settings - Fork 2
/
prepare-osm.js
96 lines (75 loc) · 2.74 KB
/
prepare-osm.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
const reader = require('geojson-writer').reader
const bigXml = require('big-xml');
const fs = require('fs');
const args = require('minimist')(process.argv.slice(2));
let inOsmPath;
let inLTS=-1;
if (args.help) {
const usage = `
Usage: node prepare-osm.js [osm_file] [lts-level]
where [osm_file] - path to osm extract, [lts-level] - lts level to generate data for (1-4)
`;
process.stdout.write(`${usage}\n`);
process.exit(0);
}
try {
inOsmPath = args._[0]
inLTS = parseInt(args._[1])
fs.accessSync(inOsmPath, fs.F_OK)
if(isNaN(inLTS) || inLTS<1 || inLTS>4) throw 'bad LTS'
} catch (error) {
process.stderr.write("Wrong parameters. See --help for details\n"/*,`${error}\n`*/);
process.exit(-1);
}
inLTS--;
const badWays = [] //badWays contains all ways that are not suitable for the chosen LTS, i.e. for lts-1 - all that are in lts2-4
const levels = [{name: "LTS-1", inLtsJsonFile: "data/level_1.json", outLtsOsmile: "data/lts1/data.osm"},
{name: "LTS-2", inLtsJsonFile: "data/level_2.json", outLtsOsmile: "data/lts2/data.osm"},
{name: "LTS-3", inLtsJsonFile: "data/level_3.json", outLtsOsmile: "data/lts3/data.osm"},
{name: "LTS-4", inLtsJsonFile: "data/level_4.json", outLtsOsmile: "data/lts4/data.osm"}]
let totalTagged = 0
for (let i=levels.length-1; i>inLTS; i--) {
let level = levels[i]
let ltsFile = reader(level.inLtsJsonFile)
for(let feature of ltsFile.features) {
if(feature.properties.id){
const id = parseInt(feature.properties.id.split('/')[1])
if(!isNaN(id) && id >= 0){
badWays.push(id)
}
}
}
}
console.log("<?xml version='1.0' encoding='UTF-8'?>")
console.log('<osm version="0.6" generator="osmfilter 1.4.3">')
var xml = bigXml.createReader(inOsmPath, /^(node|way|bounds|relation)$/, { gzip: false });
xml.on('record', function(record) {
var node = '<'+record.tag
let id=0
for (var prop in record.attrs) {
node += ' '+prop + '="'+record.attrs[prop].replace(/</g,'<').replace(/>/g,'>').replace(/'/g,''').replace(/&/g,'&').replace(/\"/g, '')+'"'
if(prop=='id'){
id = parseInt(record.attrs[prop])
}
}
if(record.tag=='way' && !isNaN(id) && badWays.includes(id))
return;
if(record.children && record.children.length){
node+='>\n'
for (var child of record.children) {
node += '\t<'+child.tag
for (var prop in child.attrs) {
node += ' '+prop + '="'+child.attrs[prop].replace(/</g,'<').replace(/>/g,'>').replace(/'/g,''').replace(/&/g,'&').replace(/\"/g, '')+'"'
}
node+='/>\n'
}
node+='</'+record.tag+'>'
}
else{
node+='/>'
}
console.log(node);
});
xml.on('end', function() {
console.log('</osm>')
});