forked from OSM-DK/missingObjectsDK
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_layer.js
More file actions
executable file
·68 lines (48 loc) · 1.46 KB
/
Copy pathsplit_layer.js
File metadata and controls
executable file
·68 lines (48 loc) · 1.46 KB
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
#!/usr/bin/env node
// Script to split a GeoJSON file into parts
'use strict';
const fs = require('fs');
function usage(msg) {
if (msg) {
console.error(msg);
}
console.error("Usage: split_feature.js layerfile parts\n e.g: split_feature.js my_layer.geojson 10");
}
if (process.argv.length != 4) {
usage('Wrong number of arguments');
process.exit(3);
}
const fileName = process.argv[2];
const partsArg = process.argv[3];
if (! fs.existsSync(fileName)) {
usage(`File '${fileName}' does not exist.`);
process.exit(4);
}
const parts = parseInt(partsArg);
if (isNaN(parts) || parts < 1 || !Number.isInteger(parts)) {
usage(`Number of parts must be a positive integer, not '${partsArg}'`);
process.exit(5);
}
var data;
try {
data = JSON.parse(fs.readFileSync(fileName));
} catch ( e ) {
console.error("Could not read and parse GeoJSON file '${fileName}': ", e);
process.exit(6);
}
const fileBase = fileName.replace(/\..+?$/, '');
const fileSuffix = fileName.match(/\..+?$/)[0] || '';
const features = data.features;
const featureCount = features.length;
const partLength = Math.ceil(featureCount/parts);
let idx = 0;
for (let start = 0; start < featureCount; start += partLength) {
data.features = features.slice(start, start + partLength);
let file = `${fileBase}_${++idx}${fileSuffix}`;
try {
fs.writeFileSync(file, JSON.stringify(data));
} catch (e) {
console.error(`Could not wite file ${file}`, e);
process.exit(10);
}
}