-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathjoin_data.js
235 lines (208 loc) · 10 KB
/
join_data.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
const fs = require("fs/promises");
// original topojson file
const topoFile = 'temp/belgium.topo.json';
// NIS codes
const nisFile = "source_data/REFNIS_2019.csv";
// population data
const populationFile = "source_data/bevolking_per_gemeente.csv"
// output file
const outputFile = "belgium.json";
(async function () {
// read files
const topojson = JSON.parse(await fs.readFile(topoFile));
const nisData = (await fs.readFile(nisFile, "utf8")).split("\n").map(line => line.split(";"));
const populationData = (await fs.readFile(populationFile, "utf8")).split("\n").map(line => line.trim().split(","));
// rename gemeentes to municipalities
topojson.objects.municipalities = topojson.objects.Gemeenten;
topojson.objects.arrondissements = topojson.objects.ARR;
topojson.objects.provinces = topojson.objects.Prov;
delete topojson.objects.Gemeenten;
delete topojson.objects.ARR;
delete topojson.objects.Prov;
// fix municipalities
topojson.objects.municipalities.geometries.map(muni => {
// manually fix the spelling in the original topojson file
if (muni.properties?.NAME_4 === "Sint-Joost-ten-Noode") muni.properties.NAME_4 = "Sint-Joost-ten-Node";
if (muni.properties?.NAME_4 === "MOERBEKE-WAAS") muni.properties.NAME_4 = "Moerbeke";
if (muni.properties?.NAME_4 === "Écaussinnes") muni.properties.NAME_4 = "Ecaussinnes";
if (muni.properties?.NAME_4 === "Celles-lez-Tournai") muni.properties.NAME_4 = "Celles";
if (muni.properties?.NAME_4 === "Blegny") muni.properties.NAME_4 = "Blégny";
if (muni.properties?.NAME_4 === "Fernlemont") muni.properties.NAME_4 = "Fernelmont";
if (muni.properties?.NAME_4 === "Gingelom") muni.properties.NAME_1 = "Vlaanderen";
if (muni.properties?.NAME_2 === "Vlaams Brabant") muni.properties.NAME_2 = "Vlaams-Brabant";
if (muni.properties?.NAME_2 === "Brabant Wallon") muni.properties.NAME_2 = "Waals-Brabant";
if (muni.properties?.NAME_2 === "Hainaut") muni.properties.NAME_2 = "Henegouwen";
if (muni.properties?.NAME_2 === "Luxembourg") muni.properties.NAME_2 = "Luxemburg";
if (muni.properties?.NAME_3 === "Moeskroen") muni.properties.NAME_3 = "Tournai-Mouscron";
if (muni.properties?.NAME_3 === "Tournai") muni.properties.NAME_3 = "Tournai-Mouscron";
if (muni.properties?.NAME_3 === "Brussel") muni.properties.NAME_3 = "Brussel Hoofdstad";
// set the region
if (muni.properties?.NAME_1 === "Bruxelles") {
muni.properties.reg_nis = "04000";
muni.properties.reg_nl = "Brussels Hoofdstedelijk Gewest"
muni.properties.reg_fr = "Région de Bruxelles-Capitale"
} else if (muni.properties?.NAME_1 === "Wallonie") {
muni.properties.reg_nis = "03000";
muni.properties.reg_nl = "Waals Gewest"
muni.properties.reg_fr = "Région Walonne"
} else if (muni.properties?.NAME_1 === "Vlaanderen") {
muni.properties.reg_nis = "02000";
muni.properties.reg_nl = "Vlaams Gewest"
muni.properties.reg_fr = "Région Flamande"
}
// merge the official names and nis code
let name_nl = muni.properties?.NAME_4?.toLowerCase();
let name_fr = muni.properties?.NAME_4?.toLowerCase();
let row = nisData.find(row => row[4].toLowerCase() == name_nl) || nisData.find(row => row[1].toLowerCase() == name_fr);
if (row) {
muni.properties.nis = row[0];
muni.properties.name_fr = row[1];
muni.properties.name_nl = row[4];
}
// merge the provinces
name_nl = "provincie " + muni.properties?.NAME_2?.toLowerCase();
name_fr = "province de " + muni.properties?.NAME_2?.toLowerCase();
row = nisData.find(row => row[4].toLowerCase() == name_nl) || nisData.find(row => row[1].toLowerCase() == name_fr);
if (row) {
muni.properties.prov_nis = row[0];
muni.properties.prov_fr = row[1];
muni.properties.prov_nl = row[4];
}
// merge the arrondissements
name_nl = "arrondissement " + muni.properties?.NAME_3?.toLowerCase();
name_fr = "arrondissement de " + muni.properties?.NAME_3?.toLowerCase();
let name_fr2 = "arrondissement d'" + muni.properties?.NAME_3?.toLowerCase();
row = nisData.find(row => row[4].toLowerCase() == name_nl) || nisData.find(row => row[1].toLowerCase() == name_fr) || nisData.find(row => row[1].toLowerCase() == name_fr2);
if (!row) { console.log(name_nl) }
if (row) {
muni.properties.arr_nis = row[0];
muni.properties.arr_fr = row[1];
muni.properties.arr_nl = row[4];
}
// merge population data
row = populationData.find(row => row[0] === muni.properties.nis);
if (row) {
muni.properties.population = +row[4];
}
// remove unused properties
delete muni.properties.ISO;
delete muni.properties.ID_0;
delete muni.properties.ID_1;
delete muni.properties.ID_2;
delete muni.properties.ID_3;
delete muni.properties.ID_4;
delete muni.properties.NAME_0;
delete muni.properties.NAME_1;
delete muni.properties.NAME_2;
delete muni.properties.NAME_3;
delete muni.properties.NAME_4;
delete muni.properties.VARNAME_4;
delete muni.properties.TYPE_4;
delete muni.properties.ENGTYPE_4;
delete muni.properties.NAME_4_NEW;
delete muni.properties["NAME_3 - NEW"];
delete muni.properties["NAME_2 - New"];
});
// fix arrondissements
topojson.objects.arrondissements.geometries.map(arr => {
// manually fix the spelling in the original topojson file
if (arr.properties?.NAME_2 === "Vlaams Brabant") arr.properties.NAME_2 = "Vlaams-Brabant";
if (arr.properties?.NAME_2 === "Brabant Wallon") arr.properties.NAME_2 = "Waals-Brabant";
if (arr.properties?.NAME_2 === "Hainaut") arr.properties.NAME_2 = "Henegouwen";
if (arr.properties?.NAME_2 === "Luxembourg") arr.properties.NAME_2 = "Luxemburg";
if (arr.properties?.NAME_3 === "Moeskroen") arr.properties.NAME_3 = "Tournai-Mouscron";
if (arr.properties?.NAME_3 === "Tournai") arr.properties.NAME_3 = "Tournai-Mouscron";
if (arr.properties?.NAME_3 === "Brussel") arr.properties.NAME_3 = "Brussel Hoofdstad";
// set the region
if (arr.properties?.NAME_1 === "Bruxelles") {
arr.properties.reg_nis = "04000";
arr.properties.reg_nl = "Brussels Hoofdstedelijk Gewest"
arr.properties.reg_fr = "Région de Bruxelles-Capitale"
} else if (arr.properties?.NAME_1 === "Wallonie") {
arr.properties.reg_nis = "03000";
arr.properties.reg_nl = "Waals Gewest"
arr.properties.reg_fr = "Région Walonne"
} else if (arr.properties?.NAME_1 === "Vlaanderen") {
arr.properties.reg_nis = "02000";
arr.properties.reg_nl = "Vlaams Gewest"
arr.properties.reg_fr = "Région Flamande"
}
// merge the provinces
name_nl = "provincie " + arr.properties?.NAME_2?.toLowerCase();
name_fr = "province de " + arr.properties?.NAME_2?.toLowerCase();
row = nisData.find(row => row[4].toLowerCase() == name_nl) || nisData.find(row => row[1].toLowerCase() == name_fr);
if (row) {
arr.properties.prov_nis = row[0];
arr.properties.prov_fr = row[1];
arr.properties.prov_nl = row[4];
}
// merge the arrondissements
name_nl = "arrondissement " + arr.properties?.NAME_3?.toLowerCase();
name_fr = "arrondissement de " + arr.properties?.NAME_3?.toLowerCase();
let name_fr2 = "arrondissement d'" + arr.properties?.NAME_3?.toLowerCase();
row = nisData.find(row => row[4].toLowerCase() == name_nl) || nisData.find(row => row[1].toLowerCase() == name_fr) || nisData.find(row => row[1].toLowerCase() == name_fr2);
if (!row) { console.log(name_nl) }
if (row) {
arr.properties.nis = row[0];
arr.properties.name_fr = row[1];
arr.properties.name_nl = row[4];
}
// remove unused properties
delete arr.properties.ISO;
delete arr.properties.ID_0;
delete arr.properties.ID_1;
delete arr.properties.ID_2;
delete arr.properties.ID_3;
delete arr.properties.NAME_0;
delete arr.properties.NAME_1;
delete arr.properties.NAME_2;
delete arr.properties.NAME_3;
delete arr.properties.NAME_2_NEW;
delete arr.properties.NAME_3_NEW;
});
// fix provinces
topojson.objects.provinces.geometries.map(prov => {
// manually fix the spelling in the original topojson file
if (prov.properties?.NAME_2 === "Vlaams Brabant") prov.properties.NAME_2 = "Vlaams-Brabant";
if (prov.properties?.NAME_2 === "Brabant Wallon") prov.properties.NAME_2 = "Waals-Brabant";
if (prov.properties?.NAME_2 === "Hainaut") prov.properties.NAME_2 = "Henegouwen";
if (prov.properties?.NAME_2 === "Luxembourg") prov.properties.NAME_2 = "Luxemburg";
// set the region
if (prov.properties?.NAME_1 === "Bruxelles") {
prov.properties.reg_nis = "04000";
prov.properties.reg_nl = "Brussels Hoofdstedelijk Gewest"
prov.properties.reg_fr = "Région de Bruxelles-Capitale"
} else if (prov.properties?.NAME_1 === "Wallonie") {
prov.properties.reg_nis = "03000";
prov.properties.reg_nl = "Waals Gewest"
prov.properties.reg_fr = "Région Walonne"
} else if (prov.properties?.NAME_1 === "Vlaanderen") {
prov.properties.reg_nis = "02000";
prov.properties.reg_nl = "Vlaams Gewest"
prov.properties.reg_fr = "Région Flamande"
}
// merge the provinces
name_nl = "provincie " + prov.properties?.NAME_2?.toLowerCase();
name_fr = "province de " + prov.properties?.NAME_2?.toLowerCase();
row = nisData.find(row => row[4].toLowerCase() == name_nl) || nisData.find(row => row[1].toLowerCase() == name_fr);
if (row) {
prov.properties.nis = row[0];
prov.properties.name_fr = row[1];
prov.properties.name_nl = row[4];
}
// remove unused properties
delete prov.properties.ISO;
delete prov.properties.ID_0;
delete prov.properties.ID_1;
delete prov.properties.ID_2;
delete prov.properties.NAME_0;
delete prov.properties.NAME_1;
delete prov.properties.NAME_2;
delete prov.properties.TYPE_2;
delete prov.properties.ENGTYPE_2;
delete prov.properties.NL_NAME_2;
delete prov.properties.VARNAME_2;
delete prov.properties['NAME_2 - NEW'];
});
await fs.writeFile(outputFile, JSON.stringify(topojson));
}());