-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate.js
85 lines (65 loc) · 3.21 KB
/
generate.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
/*
--------------------------------------------------------------------------------
Here, we run the whole library and generate the OSM script, the OSM data and the
Graph.
--------------------------------------------------------------------------------
*/
// -----------------------------------------------------------------------------
// Imports ---------------------------------------------------------------------
const fs = require('fs'); // "fs" for "files system"
const { getOsmData, osmDataToGraph } = require('./index.js');
// -----------------------------------------------------------------------------
// Read and write functions ----------------------------------------------------
// Write text file
const write = (directory, string) => {
fs.writeFileSync(directory, string);
};
// Write JSON file
const writeJSON = (directory, json) => {
fs.writeFileSync(directory, JSON.stringify(json));
};
// Read JSON file
const readJSON = (fileName) => {
return JSON.parse(fs.readFileSync(fileName));
};
// -----------------------------------------------------------------------------
// Main function ---------------------------------------------------------------
const generateData = async (settings) => {
const t0 = new Date();
console.log("\n-------------------------------------------------------------")
console.log('Graph-From-OSM is generating data ...\n')
const dataDirectory = './data/';
const scriptDir = dataDirectory + 'osm-script.txt';
const rawDataDir = dataDirectory + 'raw-osm-data.json';
const graphDir = dataDirectory + 'graph.json';
// 1) OSM query --------------------------------------------------------------
console.log(' - OSM query sended ...')
let t1 = new Date();
const data = await getOsmData(settings);
write(scriptDir, data.generatingScript);
writeJSON(rawDataDir, data);
let t2 = new Date();
console.log(' - OSM data recieved (' + ((t2-t1)/1000) +' s).')
// 2) OSM to graph -----------------------------------------------------------
console.log(' - Processing of OSM data to graph ...')
let t3 = new Date();
const graph = osmDataToGraph(data);
writeJSON(graphDir, graph)
let t4 = new Date();
console.log(' - Processing done (' + ((t4-t3)/1000) +' s).')
console.log('\nGraph-From-OSM Run Done in ' + ((t4-t0)/1000) + ' seconds.')
// Print information ---------------------------------------------------------
console.log('\n\nData source: https://overpass-api.de/api/interpreter')
console.log('Copyright: The data included in this document is from')
console.log('www.openstreetmap.org. The data is made available under ODbL.\n')
console.log('You can change the query by modifying the file "./settings.json". \n\n')
console.log("You can find your data in: ")
console.log(" - OSM script: " + scriptDir)
console.log(" - OSM raw data: " + rawDataDir)
console.log(" - Graph in geoJSON: " + graphDir)
console.log("-------------------------------------------------------------\n")
}
// -----------------------------------------------------------------------------
// Execution -------------------------------------------------------------------
const settings = readJSON('./settings.json');
generateData(settings);