-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (43 loc) · 1.69 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
const fs = require('fs');
const fetch = require('node-fetch');
const delay = (ms) => new Promise((result) => setTimeout(() => result(), ms));
// Take city OSM ID from nominatim.openstreetmap
const findCityOSMid = async (city) => {
const cityUrl = encodeURI(`https://nominatim.openstreetmap.org/search?q=${city}&format=json`);
const response = await fetch(cityUrl);
const data = await response.json();
const cityOSMid = data.find(({ class: klazz, type, display_name }) => {
return klazz === 'place' && ['town', 'city'].includes(type) && display_name.includes('Россия');
});
return cityOSMid ? cityOSMid.osm_id : null;
};
//Take city border
const takeBorder = async (city) => {
await delay(2000);
const cityOSMid = await findCityOSMid(city);
const borderUrl = encodeURI(`https://polygons.openstreetmap.fr/get_geojson.py?id=${cityOSMid}¶ms=0`);
const response = await fetch(borderUrl);
if (!response.headers.get('content-encoding')) {
console.error(`Не найдены геоданные для города ${city}, OSM OD = ${cityOSMid}`);
return null;
}
return await response.json();
};
const makeGeoJson = async (filePath) => {
let boundaries = {};
//make Array of cties from txt file
const cities = await fs
.readFileSync(filePath, 'utf-8', (data) => data)
.trim()
.split('\n');
for (let i in cities) {
const city = cities[i];
const boundary = await takeBorder(city);
boundaries = boundary ? { ...boundaries, [city]: boundary } : boundaries;
}
await fs.writeFile(`boundaries.json`, JSON.stringify(boundaries), (err) => {
if (err) throw err;
console.log('The file has been saved!');
});
};
makeGeoJson('cities.txt');