-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
158 lines (138 loc) · 4.66 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// This is being edited.
const config = require('config')
const fs = require('fs')
const Queue = require('better-queue')
const { spawn } = require('child_process')
const Parser = require('json-text-sequence').parser
const tilebelt = require('@mapbox/tilebelt')
const srcdb = config.get('srcdb')
const ogr2ogrPath = config.get('ogr2ogrPath')
const tippecanoePath = config.get('tippecanoePath')
const minzoom = config.get('minzoom')
const maxzoom = config.get('maxzoom')
const mbtilesDir = config.get('mbtilesDir')
const geojsonsDir = config.get('geojsonsDir')
let keyInProgress = []
let idle = true
const isIdle = () => {
return idle
}
const fsOptions = {
encoding: "utf8"
}
const sleep = (wait) => {
return new Promise((resolve, reject) => {
setTimeout( () => {resolve()}, wait)
})
}
const queue = new Queue(async (t, cb) => {
const startTime = new Date()
const key = t.key
const tile = t.tile
const [z, x, y] = tile
const gjsPath = `${geojsonsDir}/inter-${key}.geojsons`
const tmpPath = `${mbtilesDir}/part-${key}.mbtiles`
const dstPath = `${mbtilesDir}/${key}.mbtiles`
const bbox = tilebelt.tileToBBOX([x, y, z])
keyInProgress.push(key)
console.log(`[${keyInProgress}] in progress`)
const FSstream = fs.createWriteStream(gjsPath, fsOptions)
const parser = new Parser()
.on('data', f => {
f.tippecanoe = {
layer: srcdb.layer,
minzoom: srcdb.minzoom,
maxzoom: srcdb.maxzoom
}
delete f.properties.SHAPE_Length
if ((f.properties.contour % 200) == 0){
f.tippecanoe.minzoom = srcdb.minzoom
} else if ((f.properties.contour % 100) == 0){
f.tippecanoe.minzoom = srcdb.minzoom + 1
} else {
f.tippecanoe.minzoom = srcdb.minzoom + 3
}
FSstream.write(`\x1e${JSON.stringify(f)}\n`)
})
.on('finish', () => {
FSstream.end()
const PendTime = new Date()
//console.log(`FS write end ${key}: ${startTime.toISOString()} --> ${PendTime.toISOString()}`)
//from here
const VTconversion = new Promise((resolve, reject)=>{
const tippecanoe = spawn(tippecanoePath, [
`--output=${tmpPath}`,
'--no-feature-limit',
'--no-tile-size-limit',
'--force',
'--simplification=2',
`--clip-bounding-box=${bbox.join(',')}`,
'--quiet',
`--minimum-zoom=${minzoom}`,
`--maximum-zoom=${maxzoom}`,
gjsPath
])
.on('exit', () => {
fs.renameSync(tmpPath, dstPath)
fs.unlinkSync(gjsPath)
//const endTime = new Date()
//console.log(`Tippecanoe: ${key} ends at ${endTime.toISOString()} (^o^)/`)
//keyInProgress = keyInProgress.filter((v) => !(v === key))
resolve()
})
})
.then(()=> {
const endTime = new Date()
console.log(` - ${key} ends: ${startTime.toISOString()} --> ${endTime.toISOString()} (^o^)/`)
keyInProgress = keyInProgress.filter((v) => !(v === key))
return cb()
})
//until here
})
const ogr2ogr = spawn(ogr2ogrPath, [
'-f', 'GeoJSONSeq',
'-lco', 'RS=YES',
'/vsistdout/',
'-clipdst', bbox[0], bbox[1], bbox[2], bbox[3],
srcdb.url
])
//just in case (from here)
while(!isIdle()){
await sleep(3000)
}
//just in case (until here)
ogr2ogr.stdout.pipe(parser)
// The following part is moved into .then of
// const endTime = new Date()
// console.log(`${key} ends: ${startTime} --> ${endTime} (^o^)/`)
// keyInProgress = keyInProgress.filter((v) => !(v === key))
// return cb()
},{
concurrent: config.get('concurrent'),
maxRetries: config.get('maxRetries'),
retryDelay: config.get('retryDelay')
})
const queueTasks = () => {
for (let tile of srcdb.tiles){
//for (let tile of [[6,32,20],[6,32,21],[6,32,22],[6,32,23],[6,33,20],[6,33,21],[6,33,22]]){
const key = `${tile[0]}-${tile[1]}-${tile[2]}`
queue.push({
key: key,
tile: tile
})
}
}
const shutdown = () => {
console.log('System shutdown (^_^)')
}
const main = async () =>{
const stTime = new Date()
console.log(`${stTime.toISOString()}: Production starts. `)
queueTasks()
queue.on('drain', () => {
const closeTime = new Date()
console.log(`Production ends: ${stTime.toISOString()} --> ${closeTime.toISOString()}`)
shutdown()
})
}
main()