-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
141 lines (119 loc) · 3.48 KB
/
index.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
let fs = require('fs');
let fetch = require('node-fetch')
let forEachLine = require('for-each-line');
let randomAPI = require('ngraph.random');
let toProtobuf = require('./lib/toProtobuf');
let path = require('path')
let outgoing;
let outErrors;
let queue = [];
let executeOSMQuery = require('./lib/executeOSMQuery')
let outFolderName = 'data';
let outFileName = path.join(__dirname, outFolderName, 'processed.json');
let outErrorsFileName = path.join(__dirname, outFolderName, 'errors.json');
var JSONStream = require('JSONStream')
var es = require('event-stream')
// readErroredFile(outFileName, crawl);
readProcessedFile(outFileName, crawl);
function crawl(seen) {
forEachLine(process.argv[2] || 'cities.txt', line => {
let cityName = sanitize(line)
if (!seen.has(cityName)) queue.push(cityName)
}).then(downloadAll);
}
function downloadAll() {
let last = 0;
randomAPI.randomIterator(queue).shuffle();
downloadNext();
function downloadNext() {
let city = queue[last];
if (!city) {
console.warn('All done');
return;
}
last += 1;
let url = `https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(city)}`;
console.log('Fetching area for: ' + url);
fetch(url)
.then(x => x.json())
.then(x => x.filter(row => row.osm_type === 'relation').map(row => ({
key: city,
name: row.display_name,
type: row.type,
areaId: row.osm_id + 36e8
})))
.then(data => {
if (!data.length) {
console.warn('Could not find matches for ' + city + '; Skipping');
downloadNext();
return;
}
return downloadOSM(data[0]);
}).catch(err => {
console.error('Error when fetching ' + url)
console.error(err);
setTimeout(downloadNext, 2000);
});
}
function downloadOSM(place) {
let query = getQuery(place.areaId);
return executeOSMQuery(query)
.then(data => saveResults(data.elements, place))
.then(downloadNext)
}
function saveResults(elements, place) {
let buffer = toProtobuf(elements, place.name, place.areaId);
let fileName = path.join(__dirname, outFolderName, place.areaId + '.pbf');
fs.writeFileSync(fileName, buffer);
markProcessed(place);
}
function getQuery(areaId) {
// way[highway~"^(motorway|primary|secondary|tertiary)|residential"](area.area);
// way["highway"](area.area);
return `[timeout:900][out:json];
area(${areaId});
(._; )->.area;
(
way[highway~"^(((motorway|trunk|primary|secondary|tertiary)(_link)?)|unclassified|residential|living_street|service|track)$"](area.area);
node(w);
);
out skel;`;
}
}
function sanitize(name) {
return name.replace(/\t/g, ',');
}
function markProcessed(page) {
if(!outgoing) {
createOutStream();
}
outgoing.write(page);
}
function createOutStream() {
outgoing = JSONStream.stringify(false);
var fileStream = fs.createWriteStream(outFileName, {
encoding: 'utf8',
flags: 'a'
});
outgoing.pipe(fileStream);
}
function readProcessedFile(fileName, done) {
var seen = new Set();
if (!fs.existsSync(fileName)) {
done(seen);
return;
}
console.log('Parsing processed list...');
var jsonStreamParser = JSONStream.parse();
fs.createReadStream(fileName)
.pipe(jsonStreamParser)
.pipe(es.mapSync(markProcessed))
.on('end', fileInitialized);
function markProcessed(place) {
seen.add(place.key);
}
function fileInitialized() {
console.log('Processed: ' + seen.size);
done(seen);
}
}