-
Notifications
You must be signed in to change notification settings - Fork 0
/
properties.js
50 lines (43 loc) · 1.62 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
const shapefile = require("shapefile");
Promise.all([
parseInput(),
shapefile.read("build/DISTRITOS_inei_geogpsperu_suyopomalia.shp"),
shapefile.read("build/PROVINCIAS_inei_geogpsperu_suyopomalia.shp"),
shapefile.read("build/DEPARTAMENTOS_inei_geogpsperu_suyopomalia.shp")
]).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, districts, provinces, departments]) {
districts = new Map(districts.features.map(d => [d.properties.UBIGEO, d.properties]));
provinces = new Map(provinces.features.map(d => [d.properties.IDPROV, d.properties]));
// permite agregar el nombre de departamento dentro del topojson
// y dentro de objects.departments.geometries.properties.name
departments = new Map(departments.features.map(d => [d.properties.CCDD, d.properties]));
for (const district of topology.objects.districts.geometries) {
district.properties = {
name: districts.get(district.id).NOMBDIST
};
}
for (const province of topology.objects.provinces.geometries) {
province.properties = {
name: provinces.get(province.id).NOMBPROV
};
}
for (const department of topology.objects.departments.geometries) {
department.properties = {
name: departments.get(department.id).NOMBDEP
};
}
process.stdout.write(JSON.stringify(topology));
process.stdout.write("\n");
}