-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeocode2.js
66 lines (54 loc) · 1.65 KB
/
geocode2.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
const fs = require("fs");
const https = require("https");
const placesStr = fs.readFileSync("places.txt", {encoding: "utf-8"});
const places = placesStr.split("\n").slice(0, -1);
const API_KEY = "";
/**
* Sleep for some time
* @param {string} millis
* @returns {Promise<void>}
*/
function sleep(millis) {
return new Promise(resolve => setTimeout(resolve, millis));
}
async function main() {
for (let i = 0; i < places.length; ++i) {
const placeName = places[i];
const iStr = ("0000" + i.toString()).substr(-5);
const destFilename = `places2/${iStr}.json`;
console.log(`${iStr} / ${places.length}`);
if (fs.existsSync(destFilename)) {
console.log(` Destination file ${destFilename} exists, skipping`);
continue;
}
const options = {
host: "eu1.locationiq.com",
path: `/v1/search.php?key=${API_KEY}&q=${encodeURIComponent(placeName)}&format=json&polygon_geojson=1&limit=1`,
};
console.log(` URL: ${options.host}${options.path}`);
console.log(` Destination: ${destFilename}`);
/** @type {Promise<string>} */
const result = await new Promise(resolve => {
https.request(options, (res) => {
let str = "";
res.on("data", chunk => str += chunk);
res.on("end", () => resolve(str));
}).end();
});
try {
const json = JSON.parse(result);
if (json.hasOwnProperty("error") && json.error !== "Unable to geocode" && json.error !== "Unknown error - Please try again after some time")
{
console.log(`ERROR: ${json.error}`);
break;
}
}
catch (e) {
console.log("ERROR: Can't parse as JSON:");
console.log(result);
}
fs.writeFileSync(destFilename, result);
await sleep(1100);
}
}
main();