-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_original.js
133 lines (105 loc) · 3.95 KB
/
get_original.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
const request = require('request');
const WBK = require('wikibase-sdk');
var fs = require('fs');
var dir = './tmp';
const osmGeoJson = require('osm-geojson');
const { exit } = require('process');
const wdk = WBK({
instance: 'https://www.wikidata.org',
sparqlEndpoint: 'https://query.wikidata.org/sparql'
});
const sparql = `
SELECT ?item ?itemLabel ?countryRelation ?code
WHERE
{
?item wdt:P31 wd:Q3624078.
?item wdt:P402 ?countryRelation .
?item wdt:P297 ?code .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
`;
const url = wdk.sparqlQuery(sparql);
var options = {
headers: {'user-agent': 'node.js'}
};
request( url, options , async (err, response, body) => {
const res = JSON.parse(body);
let countries = [];
res.results.bindings.forEach((x) => {
countries.push({
wikidata: x.item.value.replace('http://www.wikidata.org/entity/', ''),
code: x.code.value,
relation: x.countryRelation.value,
name: x.itemLabel.value
});
});
for await (let country of countries) {
const dir = './data/' + country.code;
const fileName = './data/' + country.code + '.geojson';
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
const json = await osmGeoJson.get(country.relation);
json.properties = {
"name": country.name,
"wikidata": country.wikidata,
"osm": country.osm
};
fs.writeFileSync(fileName, JSON.stringify(json));
await new Promise((resolve, reject) => {
const sparql = `
SELECT ?admin ?adminLabel ?adminIso ?relation
WHERE
{
wd:${country.wikidata} wdt:P150 ?admin .
?admin wdt:P402 ?relation .
?admin wdt:P300 ?adminIso .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
`;
const url = wdk.sparqlQuery(sparql);
var options = {
headers: {'user-agent': 'node.js'}
};
request( url, options , async (err, response, body) => {
const res = JSON.parse(body);
const admins = [];
res.results.bindings.forEach((x) => {
if (x.relation.value) {
admins.push({
wikidata: x.admin.value.replace('http://www.wikidata.org/entity/', ''),
code: x.adminIso.value,
relation: x.relation.value,
name: x.adminLabel.value
});
}
});
for await (let admin of admins) {
const fileName = './data/' + country.code + '/' + admin.code + '.geojson';
if (fs.existsSync(fileName)) {
continue;
}
console.log('Getting ' + country.code + '/' + admin.code);
try {
console.log(admin.wikidata);
await new Promise((resolve, reject) => {
request('http://polygons.openstreetmap.fr/?id=' + admin.relation, {}, (e, r, b) => {
resolve();
});
});
const json = await osmGeoJson.get(admin.relation);
json.properties = {
"name": admin.name,
"wikidata": admin.wikidata,
"osm": admin.osm
};
fs.writeFileSync(fileName, JSON.stringify(json));
} catch (e) {
console.error(e);
}
}
resolve();
});
});
}
});