-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·82 lines (70 loc) · 2.46 KB
/
index.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
const fs = require('fs');
const stringify = require('csv-stringify');
const topojson = require('topojson-server');
const { simplify, presimplify } = require('topojson-simplify');
const { feature } = require('topojson-client');
const shapefile = require('shapefile');
const geobuf = require('geobuf');
const Pbf = require('pbf');
const geojsonExtent = require('geojson-extent');
const shpPath = process.argv[2];
const dbfPath = process.argv[3];
const outfilePath = process.argv[4] || 'converted_shapefile.csv';
const simplificationLevels = [1, 5, 10, 20, 50];
shapefile
.read(shpPath, dbfPath)
.then((res) => {
const properties = getAllProperties(res.features);
let topology = topojson.topology([res], 1e5);
topology = presimplify(topology);
const arcWeightPercentiles = getArcWeightPercentiles(topology.arcs);
const simplifiedGeoJson = simplificationLevels.map((level, i) =>
feature(simplify(topology, arcWeightPercentiles[i]), topology.objects[Object.keys(topology.objects)[0]]),
);
const csvData = [];
for (let i = 0; i < res.features.length; i++) {
const feature = res.features[i];
const row = {};
for (const property of properties) {
row[property] = feature.properties[property];
}
row.bbox = geojsonExtent(feature).join(',');
row.geojson = JSON.stringify(feature);
feature.properties = undefined;
row.geobuf = Buffer.from(geobuf.encode(feature, new Pbf())).toString('base64');
for (let n = 0; n < simplificationLevels.length; n++) {
const feature = simplifiedGeoJson[n].features[i];
feature.properties = undefined;
row[`geobuf_simplified_${simplificationLevels[n]}`] = Buffer.from(
geobuf.encode(feature, new Pbf()),
).toString('base64');
}
csvData.push(row);
}
stringify(csvData, { header: true }).pipe(fs.createWriteStream(outfilePath));
})
.catch(console.log);
// Returns an array of all properties observed across the features
function getAllProperties(features) {
const propertiesSet = new Set();
for (const feature of features) {
for (const property in feature.properties) {
propertiesSet.add(property);
}
}
return [...propertiesSet];
}
function getArcWeightPercentiles(arcs) {
const arcAreas = [];
for (const arc of arcs) {
for (const coord of arc) {
if (coord[2] !== Infinity) {
arcAreas.push(coord[2]);
}
}
}
arcAreas.sort((a, b) => a - b);
return simplificationLevels.map((level) => {
return arcAreas[arcAreas.length - 1 - Math.floor(arcAreas.length / (100 / level))];
});
}