-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathscrape.js
63 lines (56 loc) · 1.69 KB
/
scrape.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
/** Data scraped from https://sthaniya.gov.np/ for High res files */
const fs = require('fs');
const request = require('xhr-request');
const districts = [];
const writeFile = (filename, content) => {
if (!fs.existsSync('highres-geojson')) {
fs.mkdirSync('highres-geojson');
}
fs.writeFileSync(`./highres-geojson/${filename}.geojson`, JSON.stringify(content));
};
const fetchJSON = (url, params = { json: true }) => new Promise((resolve, reject) => {
request(
url,
params,
(err, data) => {
if (err) {
reject(err);
return;
}
resolve(data);
},
);
});
const fetchDistrict = async (district) => {
const districtName = district[0].toUpperCase() + district.substring(1);
const provinceData = await fetchJSON(
`https://sthaniya.gov.np/gis/data/Dist_GaPa/${districtName}.json`,
);
writeFile(`${districtName}-District`, provinceData);
};
const fetchProvince = async (num) => {
const provinceData = await fetchJSON(
`https://sthaniya.gov.np/gis/data/Province${num}.json`,
);
districts.push(...provinceData.features.map(dist => dist.properties.TARGET.toLowerCase()));
writeFile(`Province${num}`, provinceData);
};
const fetchProvinces = async () => Promise.all([
fetchProvince(1),
fetchProvince(2),
fetchProvince(3),
fetchProvince(4),
fetchProvince(5),
fetchProvince(6),
fetchProvince(7),
]);
(async () => {
const countryData = await fetchJSON(
'https://sthaniya.gov.np/gis/data/countries.json',
{},
);
const countryWithProvinces = JSON.parse(countryData.slice(16));
writeFile('Nepal-provinces', countryWithProvinces);
await fetchProvinces();
await Promise.all(districts.map(dist => fetchDistrict(dist)));
})();