-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (51 loc) · 2.31 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
const readline = require('readline');
const fs = require('fs');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter path to Your GeoJSON file: ', (multiGeojsonPath) => {
rl.question('Enter variable name for folder name: ', (shapeGroup) => {
rl.question('Enter variable name for single file name: ', (shapeName) => {
try {
const multiGeojson = JSON.parse(fs.readFileSync(multiGeojsonPath, 'utf8'));
for (let index = 0; index < multiGeojson.features.length; index++) {
const feature = multiGeojson.features[index];
if (feature.properties[shapeGroup] !== undefined) {
const folderPath = `${feature.properties[shapeGroup]}`
.replace(/\s/g, '_')
.replace(/'/g, '')
.replace(/[/\\?%*:|"<>]/g, '');
if (shapeName) {
if (feature.properties[shapeName] !== undefined) {
const fileName = `${shapeName}_${index}.geojson`;
const filePath = `./data/${folderPath}/${fileName}`;
if (!fs.existsSync(`./data/${folderPath}`)) {
fs.mkdirSync(`./data/${folderPath}`, { recursive: true });
}
fs.writeFileSync(filePath, JSON.stringify(feature));
console.log(`Created file ${index + 1} of ${multiGeojson.features.length}: ${filePath}`);
} else {
console.error("==================================================================")
console.error("== CANT FIND THE FIELD ` " + shapeName + " ` IN ` feature.properties `")
console.error("==================================================================")
break;
}
} else {
console.log(JSON.stringify(feature));
}
} else {
console.error("==================================================================")
console.error("== CANT FIND THE FIELD ` " + shapeGroup + " ` IN ` feature.properties `")
console.error("==================================================================")
break;
}
}
rl.close();
} catch (error) {
console.error("File Name dose not Exist")
rl.close();
}
});
});
});