-
Notifications
You must be signed in to change notification settings - Fork 2
/
geocoder.js
63 lines (56 loc) · 1.84 KB
/
geocoder.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
const http = require('http');
const fs = require('fs');
function geocode(name) {
console.log(name)
let data = '';
return new Promise((resolve, reject) => {
http.get('http://nominatim.openstreetmap.org/search?q=' + encodeURIComponent(name) + '&format=json', (res) => {
if(res.statusCode !== 200) {
reject('STATUS CODE ' + res.statusCode + ' for ' + name)
}
res.on('data', (d) => {
data +=d;
});
res.on('end', () => {
resolve(data);
})
}).on('error', (e) => {
reject(e);
});
})
}
module.exports = function (cities) {
return Promise.all(cities
// .filter((city, index) => index < 1)
.map(city => {
// страна область город
return geocode(city[1])
.then(function(data) {
let json;
try {
json = JSON.parse(data);
} catch(e) {
console.log(e)
}
if(!json.length) {
console.log(city)
return;
}
return [
parseInt(city[0]),
city[1],
city[2],
city[3],
parseInt(city[4]),
parseInt(city[5]),
parseFloat(json[0].lat),
parseFloat(json[0].lon),
parseInt(json[0].place_id),
parseInt(json[0].osm_id)
]
})
.catch(err => {
console.log(err);
});
}))
}