-
Notifications
You must be signed in to change notification settings - Fork 9
/
properties.js
94 lines (84 loc) · 3.26 KB
/
properties.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
const shapefile = require('shapefile');
const globby = require('globby');
const districts = require('./utils/districts.json');
// Parse the piped in topojson
function parseInput() {
return new Promise((resolve, reject) => {
const chunks = [];
process.stdin
.on('data', (chunk) => chunks.push(chunk))
.on('end', () => {
try { resolve(JSON.parse(chunks.join(''))); } catch (error) { reject(error); }
})
.setEncoding('utf8');
});
}
// 臺 > 台
function fixTai(obj) {
return { ...obj, COUNTYNAME: obj.COUNTYNAME.replace('臺', '台') };
}
// Generated modified properties
function output([topology, towns, counties]) {
const townsMap = new Map(
towns.features.map((d) => [d.properties.TOWNCODE, d.properties]),
);
const countiesMap = new Map(
counties.features.map((d) => [d.properties.COUNTYCODE, d.properties]),
);
// Process village properties
if (topology.objects.villages) {
// eslint-disable-next-line no-restricted-syntax
for (const village of topology.objects.villages.geometries) {
village.properties = fixTai(village.properties);
village.properties.TOWNENG = townsMap.get(village.properties.TOWNCODE).TOWNENG;
village.properties.COUNTYENG = countiesMap.get(village.properties.COUNTYCODE).COUNTYENG;
// If we are generating districts, add DISTRICTCODE into properties
if (process.env.DISTRICTS === 'true') {
Object.keys(districts).forEach((countyId) => {
Object.keys(districts[countyId]).forEach((districtId) => {
if (
districts[countyId][districtId].indexOf(village.properties.VILLCODE) >= 0
|| districts[countyId][districtId].indexOf(village.properties.TOWNCODE) >= 0
|| districts[countyId][districtId].indexOf(village.properties.COUNTYCODE) >= 0
) village.properties.DISTRICTCODE = `${countyId}-${districtId}`;
});
});
}
delete village.properties.NOTE;
}
}
// Process town properties
if (topology.objects.towns) {
// eslint-disable-next-line no-restricted-syntax
for (const town of topology.objects.towns.geometries) {
town.properties = fixTai(town.properties);
town.properties.TOWNENG = townsMap.get(town.properties.TOWNCODE).TOWNENG;
town.properties.COUNTYENG = countiesMap.get(town.properties.COUNTYCODE).COUNTYENG;
delete town.properties.NOTE;
}
}
// Process county properties
if (topology.objects.counties) {
// eslint-disable-next-line no-restricted-syntax
for (const county of topology.objects.counties.geometries) {
county.properties = fixTai(county.properties);
county.properties.COUNTYENG = countiesMap.get(county.properties.COUNTYCODE).COUNTYENG;
delete county.properties.NOTE;
}
}
// Add nation properties
if (topology.objects.nation && topology.objects.nation.geometries) {
topology.objects.nation.geometries[0].properties = { ID: 'TW', NAME: 'Taiwan' };
}
process.stdout.write(JSON.stringify(topology));
process.stdout.write('\n');
}
// Read the data out of the shp files.
Promise.all([
globby('build/TOWN*.shp'),
globby('build/COUNTY*.shp'),
]).then(([townShp, countyShp]) => Promise.all([
parseInput(),
shapefile.read(townShp[0]),
shapefile.read(countyShp[0]),
]).then(output));