scripts/osm_conversion/app.js

95 lines
2.5 KiB
JavaScript
Raw Normal View History

2022-08-03 11:10:08 +02:00
/**
* converter of overpass results to CSV
*/
const exportFileName = "bdd-vitesses-fr-osm_ways_zone-saint-mande_date-made-2022-08-03_full.csv"
2022-08-03 11:54:32 +02:00
let header_csv = ["national_ref", "way_id", "osm_link", "name", "highway_tag","speed_limit"]
2022-08-03 11:10:08 +02:00
let lines_csv = []
const sourceFilePath = "./overpass_results/export_saint_mande.json"
const overpassDataJson = require(sourceFilePath)
2022-08-03 11:54:32 +02:00
const reference_prefix = "V94160_";
let counter_no_speedlimit = 0;
let counter_highways = overpassDataJson['elements'].length;
2022-08-03 11:10:08 +02:00
let turn_ii = 0;
2022-08-03 11:54:32 +02:00
let turn_ii_limit = 2000;
2022-08-03 11:10:08 +02:00
const fs = require('fs')
console.log("overpassDataJson elements", overpassDataJson['elements'].length)
overpassDataJson['elements'].forEach((elem) => {
2022-08-03 11:26:43 +02:00
// limit turns for dev time
turn_ii++
if (turn_ii >= turn_ii_limit) {
return
}
let line_properties = {
2022-08-03 11:54:32 +02:00
"ref": ""+reference_prefix + turn_ii,
2022-08-03 11:26:43 +02:00
"way_id": elem.id,
2022-08-03 11:54:32 +02:00
"osm_link": `"https://www.openstreetmap.org/way/${elem.id}"`,
2022-08-03 11:26:43 +02:00
"name": null,
2022-08-03 11:54:32 +02:00
"highway": null,
"speedlimit": null,
2022-08-03 11:26:43 +02:00
}
if (elem.tags) {
console.log("elem.tags.highway", elem.tags.highway)
if (elem.tags.highway) {
line_properties.highway = elem.tags.highway
2022-08-03 11:10:08 +02:00
}
2022-08-03 11:26:43 +02:00
if (elem.tags.ref) {
2022-08-03 11:10:08 +02:00
2022-08-03 11:26:43 +02:00
line_properties.ref += '_' + elem.tags.ref.replace(' ', '-')
2022-08-03 11:10:08 +02:00
}
2022-08-03 11:26:43 +02:00
if (elem.tags.name) {
line_properties['name'] = elem.tags.name
}
if (elem.tags.maxspeed) {
line_properties['speedlimit'] = elem.tags.maxspeed
2022-08-03 11:54:32 +02:00
}else{
counter_no_speedlimit++
2022-08-03 11:26:43 +02:00
}
}
lines_csv.push(line_properties)
})
2022-08-03 11:30:28 +02:00
let lines_out = lines_csv.map(elem => {
2022-08-03 11:10:08 +02:00
let keys = Object.keys(elem)
let csv_line = '';
2022-08-03 11:54:32 +02:00
keys.forEach(keyName => {
2022-08-03 11:26:43 +02:00
csv_line += elem[keyName]
2022-08-03 11:30:28 +02:00
csv_line += ';'
2022-08-03 11:26:43 +02:00
2022-08-03 11:10:08 +02:00
})
return csv_line + "\n"
});
2022-08-03 11:54:32 +02:00
2022-08-03 11:26:43 +02:00
writeCSVOutput();
2022-08-03 11:10:08 +02:00
function writeCSVOutput() {
2022-08-03 11:30:28 +02:00
let content = header_csv.join(';') + ';\n' + lines_out;
console.log(" ")
2022-08-03 11:54:32 +02:00
// console.log("content", content.replace(','+reference_prefix, reference_prefix))
2022-08-03 11:26:43 +02:00
fs.writeFile('output/' + exportFileName, content, function (err, data) {
2022-08-03 11:10:08 +02:00
if (err) {
return console.log(err);
}
2022-08-03 11:54:32 +02:00
console.log("counter_no_speedlimit", counter_no_speedlimit)
console.log("on", counter_highways)
console.log("missing data : ", Math.floor( counter_no_speedlimit * 100 /counter_highways ) + "%" )
console.log(" ")
2022-08-03 11:10:08 +02:00
console.log('wrote output file', exportFileName);
});
}