-
Notifications
You must be signed in to change notification settings - Fork 143
/
properties.js
37 lines (34 loc) · 1.13 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
const shapefile = require("shapefile");
Promise.all([
parseInput(),
shapefile.read("build/cb_2017_us_county_5m.shp", undefined, {encoding: "utf-8"}),
shapefile.read("build/cb_2017_us_state_5m.shp", undefined, {encoding: "utf-8"})
]).then(output);
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 output([topology, counties, states]) {
counties = new Map(counties.features.map(d => [d.properties.GEOID, d.properties]));
states = new Map(states.features.map(d => [d.properties.GEOID, d.properties]));
for (const county of topology.objects.counties.geometries) {
county.properties = {
name: counties.get(county.id).NAME
};
}
for (const state of topology.objects.states.geometries) {
state.properties = {
name: states.get(state.id).NAME
};
}
process.stdout.write(JSON.stringify(topology));
process.stdout.write("\n");
}