-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (95 loc) · 3.86 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const {postgis2geojson} = require('@watergis/postgis2geojson');
const deepcopy = require('deepcopy');
const loadJsonFile = require('load-json-file');
const fs = require('fs');
const path = require('path');
const config = require('./config');
const getLayer = (name, callback) => {
for (let i = 0; i < config.layers.length; i++) {
const layer = config.layers[i];
if (layer.name === name){
if (callback){
return deepcopy(callback(layer));
}else{
return [deepcopy(layer)];
}
}
}
}
const getGeoJSON = async(srcConfig, callback) =>{
const pg2json = new postgis2geojson(srcConfig);
const geojsonFiles = await pg2json.run();
let layers = [];
for (let i = 0; i < geojsonFiles.length; i++){
const geojsonFile = geojsonFiles[i];
console.log(`geojson file was generated: ${geojsonFile}`);
const features = await loadJsonFile(geojsonFile)
features.features.forEach(f=>{
if (!callback)return;
layers.push(callback(f))
})
}
let distConfig = deepcopy(srcConfig);
distConfig.layers = layers;
return distConfig;
}
const generate = async() =>{
console.time('rw-admins');
let orgConfig = deepcopy(config);
orgConfig.layers = getLayer('province');
getGeoJSON(orgConfig, (province)=>{
const prov_id = province.properties.id;
return getLayer('district', (layer=>{
const newLayer = deepcopy(layer);
newLayer.geojsonFileName = layer.geojsonFileName.replace(/{prov_id}/g,prov_id);
fs.mkdirSync(path.dirname(newLayer.geojsonFileName), { recursive: true});
newLayer.select = layer.select.replace(/{prov_id}/g,prov_id);
return newLayer;
}));
}).then(provConfig=>{
return getGeoJSON(provConfig, (district)=>{
const dist_id = district.properties.id.toString();
const prov_id = dist_id.substr(0,1);
return getLayer('sector', (layer=>{
const newLayer = deepcopy(layer);
newLayer.geojsonFileName = layer.geojsonFileName.replace(/{prov_id}/g,prov_id).replace(/{dist_id}/g,dist_id);
fs.mkdirSync(path.dirname(newLayer.geojsonFileName), { recursive: true});
newLayer.select = layer.select.replace(/{prov_id}/g,prov_id).replace(/{dist_id}/g,dist_id);
return newLayer;
}));
})
}).then(districtConfig=>{
return getGeoJSON(districtConfig, (sector)=>{
const sect_id = sector.properties.id.toString();
const prov_id = sect_id.substr(0,1);
const dist_id = sect_id.substr(0,2);
return getLayer('cell', (layer=>{
const newLayer = deepcopy(layer);
newLayer.geojsonFileName = layer.geojsonFileName.replace(/{prov_id}/g,prov_id).replace(/{dist_id}/g,dist_id).replace(/{sect_id}/g,sect_id);
fs.mkdirSync(path.dirname(newLayer.geojsonFileName), { recursive: true});
newLayer.select = layer.select.replace(/{prov_id}/g,prov_id).replace(/{dist_id}/g,dist_id).replace(/{sect_id}/g,sect_id);
return newLayer;
}));
})
}).then(sectorConfig=>{
return getGeoJSON(sectorConfig, (cell)=>{
const cell_id = cell.properties.id.toString();
const prov_id = cell_id.substr(0,1);
const dist_id = cell_id.substr(0,2);
const sect_id = cell_id.substr(0,4);
return getLayer('village', (layer=>{
const newLayer = deepcopy(layer);
newLayer.geojsonFileName = layer.geojsonFileName.replace(/{prov_id}/g,prov_id).replace(/{dist_id}/g,dist_id).replace(/{sect_id}/g,sect_id).replace(/{cell_id}/g,cell_id);
fs.mkdirSync(path.dirname(newLayer.geojsonFileName), { recursive: true});
newLayer.select = layer.select.replace(/{prov_id}/g,prov_id).replace(/{dist_id}/g,dist_id).replace(/{sect_id}/g,sect_id).replace(/{cell_id}/g,cell_id);
return newLayer;
}));
})
}).then(cellConfig=>{
return getGeoJSON(cellConfig)
}).then(()=>{
console.timeEnd('rw-admins');
})
.catch(err=>console.error)
};
module.exports = generate();