-
Notifications
You must be signed in to change notification settings - Fork 10
/
cmd-init.js
502 lines (479 loc) · 14.2 KB
/
cmd-init.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
var async = require('async')
var exec = require('child_process').exec
var spawn = require('child_process').spawn
var execFile = require('child_process').execFile
var path = require('path')
var os = require('os')
var fs = require('fs')
var mkdirp = require('mkdirp')
var SphericalMercator = require('sphericalmercator')
var geojsonBbox = require('geojson-bbox')
var concat = require('concat-files')
var username = require('username')
var requestretry = require('requestretry')
var g = {}
var config;
module.exports = function(nconf) {
config = nconf
async.series([
init,
parseRegion,
downloadMyCountryData,
writeCurState,
pbf2osm,
dropCountriesBorder,
downloadCountriesBorder,
downloadMyCountryBorder,
osm2geojson,
simplifyGeoJSON,
writeInitView,
geojson2poly,
mergeOSMData,
osm2pgsql,
createWorldTileList,
createMyCountryTileList,
mergeTilesList,
setKosmtikConfig
], function(err, result) {
if (err) throw err
console.log('all async function over')
})
}
function init(callback) {
g.memory = parseInt(
os.totalmem() * (config.get('system:memoryPercentage')/100)/1000000)
var confFile = config.stores.file.store
Object.keys(confFile).forEach(function(key) {
if (confFile[key].dir) mkdirp.sync(confFile[key].dir, function(err) {
if (err) callback(err)
})
})
callback()
}
function parseRegion(callback) {
console.log('init ...')
debugger
g.fileName = config.get('osm').region
.split(',')
.map(function(part) {
return part.toLowerCase().trim().replace(/ +/g,'-')
})
.join('-')
g.name = config.get('osm').region.split(',').pop().trim().replace(/ +/g,' ')
callback()
}
function downloadMyCountryData(callback) {
console.log('downloadCountryData ...')
var url = config.get('osm:host') + '/'
+ config.get('osm').region.split(',')
.map(function(part) {
return part.toLowerCase().trim().replace(/ +/g, '-')
})
.join('/')
+ '-latest.osm.pbf'
console.log('downloading region data: '+ url)
var curl = 'echo '
+ ' `curl -s' // Note curl command is executing with in tilde
+ ' --connect-timeout 10'
+ ' --retry 10'
+ ' --retry-delay 10'
+ ' --retry-max-time 500'
+ ' -w \'%{http_code}\'' // capturing the http status
+ ' ' + url
+ ' -o my-area.osm.pbf `' // closed tilde at the end
console.log('curl: '+ curl)
exec(
curl,
{cwd: path.resolve(config.get('data:dir'))},
function(err, stdout, stderr) {
if (err) throw err
console.log(stdout)
if (stderr) {
callback(stderr)
}
if (stdout < 400) {
callback(null)
} else {
callback(stdout)
}
}
)
}
function writeCurState(callback) {
console.log('Writing current state of data ...')
var url = config.get('osm:host') + '/'
+ config.get('osm').region.split(',')
.map(function(part) {
return part.toLowerCase().trim().replace(/ +/g, '-')
})
.join('/')
+ '-updates/state.txt'
console.log('Current state: '+ url)
requestretry({
url: url,
maxAttempts: 10,
retryDelay: 10000,
retryStrategy: requestretry.RetryStrategies.HTTPOrNetworkError
},
function(err, resp, body) {
if (err) throw err;
if (!err && resp.statusCode == 200) {
console.log('The number of request attempts: ' + resp.attempts)
var seq;
body.split('\n').forEach(function(line) {
if (line.indexOf('sequenceNumber') >= 0) {
var rec = line.split('=')
seq = parseInt(rec[1].trim())
}
})
if (seq && !isNaN(seq)) {
var seqFile = path.resolve(config.get('data:dir'),'seq-number.json')
fs.writeFile(seqFile, JSON.stringify({sequenceNumber: seq}),
function(err) {
if (err) callback(err)
callback()
}
)
} else {
console.log('Error: Could not write sequence number')
}
}
})
}
function pbf2osm(callback) {
console.log('pbf2osm ...')
var cmd = path.resolve(__dirname, 'osmconvert')
var input = path.resolve(config.get('data:dir'),
'my-area.osm.pbf')
var output = path.resolve(config.get('data:dir'),
'my-area.osm')
execFile(cmd, [
'--hash-memory=' + g.memory,
input,
'--out-osm', '-o=' + output
],
{}, function(err, stdout, stderr) {
if (err) callback(err)
if (stderr) {
console.log(stderr)
callback(stderr)
} else {
callback(stderr)
console.log(stdout)
}
})
}
function dropCountriesBorder(callback) {
console.log('dropCountriesBorder ...')
var cmd = path.resolve(__dirname, 'osmfilter')
var input = path.resolve(config.get('data:dir'),
'my-area.osm')
var output = path.resolve(config.get('data:dir'),
'my-area-dropped-countries.osm')
execFile(cmd, [
input,
'--drop="admin_level=2"',
'--out-osm', '-o=' + output
],
handleExec.bind({callback: callback}))
}
function downloadCountriesBorder(callback) {
console.log('downloading countries border data with overpass api..')
var curl = 'echo '
+ ' `curl -s' // Note curl command is executing with in tilde
+ ' --connect-timeout 10'
+ ' --retry 10'
+ ' --retry-delay 10'
+ ' --retry-max-time 500'
+ ' -H "Host: overpass-api.de" -H "Content-Type: text/xml"'
+ ' -w \'%{http_code}\'' // capturing the http status
+ ' -d \'relation["admin_level"="2"];(._;>;); out body;\''
+ ' http://overpass-api.de/api/interpreter'
+ ' -o countries-border.osm `' // closed tilde at the end
console.log('curl overpass: '+ curl)
exec(
curl,
{cwd: path.resolve(config.get('data:dir'))},
function(err, stdout, stderr) {
if (err) callback(err)
console.log(stdout)
if (stderr) {
callback(stderr)
} else if (stdout < 400) {
console.log('downloaded coutries borders')
callback(null)
} else {
callback('download error: ' + stdout)
}
}
)
}
function downloadMyCountryBorder(callback) {
console.log('downloading my country border data with overpass api..')
var apiData =
'relation["admin_level"="'+ config.get('osm').region.split(',').length +'"] '
+ '["name:en"="'+g.name + '"];'
+ '(._;>;);'
+ ' out body;'
var curl = 'echo '
+ ' `curl -s' // Note curl command is executing with in tilde
+ ' --connect-timeout 10'
+ ' --retry 10'
+ ' --retry-delay 10'
+ ' --retry-max-time 500'
+ ' -H "Host: overpass-api.de" -H "Content-Type: text/xml"'
+ ' -w \'%{http_code}\'' // capturing the http status
+ ' -d \'' + apiData + '\''
+ ' http://overpass-api.de/api/interpreter'
+ ' -o my-area-border.osm `' // closed tilde at the end
console.log('curl my country overpass: '+ curl)
exec(curl,
{cwd: path.resolve(config.get('data:dir'))},
function(err, stdout, stderr) {
if (err) callback(err)
console.log(stdout)
if (stderr) {
callback(stderr)
} else if (stdout < 400) {
console.log('downloaded my country border')
callback(null)
} else {
callback('download error: ' + stdout)
}
}
)
}
function osm2geojson(callback) {
console.log('osm2geojson ...')
var cmd = path.resolve(__dirname, './node_modules/.bin/osmtogeojson')
var input = path.resolve(config.get('data:dir'),
'my-area-border.osm')
var output = path.resolve(config.get('data:dir'),
'my-area-border.geojson')
exec('node '
+ ' --max_old_space_size=' + g.memory
+ ' ' + cmd
+ ' ' + input
+ ' > ' + output
,{},
handleExec.bind({callback: callback})
)
}
function simplifyGeoJSON(callback) {
console.log('simplifyGeoJSON ...')
var cmd = path.resolve(__dirname, './node_modules/.bin/simplify-geojson')
var input = path.resolve(config.get('data:dir'),
'my-area-border.geojson')
var output = path.resolve(config.get('data:dir'),
'my-area-border-simplified.geojson')
var simplify = cmd
+ ' ' + input
+ ' --tolerance ' + config.get('map:boundaryTolerance')
+ ' > ' + output
console.log(simplify)
exec(simplify,
{},
handleExec.bind({callback: callback})
)
}
function writeInitView(callback) {
debugger
var input = path.resolve(config.get('data').dir,
'my-area-border-simplified.geojson')
fs.readFile(input, 'utf8', function(err, data) {
if (err) callback(err)
debugger
var reproject = new SphericalMercator()
var bounds = reproject.convert(geojsonBbox(JSON.parse(data)),'900913')
var width = bounds[2] - bounds[0]
var height = bounds[3] - bounds[1]
var center = [bounds[0] + width/2, bounds[1] + height/2]
var res = width > height ?
width/config.get('demo').width : height/config.get('demo').height
var zoom = getNearestZoom(res)
var view = '// center [long,lat] \n var view = { zoom: ' + zoom + ','
+ 'center: [' + reproject.inverse(center) + ']}'
+ '\nvar tilesPort = '+ config.get('tileServer:port')
var viewFile = path.resolve(config.get('demo').dir, 'view.js')
fs.writeFile(viewFile, view, function(err) {
if (err) callback(err)
console.log('view file saved')
callback()
})
})
}
function setKosmtikConfig(callback) {
username().then(function(name) {
var kosmticConfFile = './config/kosmtik-localconfig.json'
fs.readFile(kosmticConfFile,'utf8', function(err,data) {
debugger
if (err) callback(err)
var conf = JSON.parse(data)
var ind = conf.findIndex(function(item) {
return item && item.if && item.if['Datasource.type']
&& item.if['Datasource.type'] === 'postgis' && item.then
&& item.then['Datasource.user']
})
conf[ind].then['Datasource.user'] = name
fs.writeFile(kosmticConfFile, JSON.stringify(conf,null,2), function(err) {
if (err) callback(err)
console.log('Kosmtik config file saved')
callback()
})
})
})
}
function getNearestZoom(res) {
var zooms = Array.apply(null, Array(30)).map(function(e,i) {
return 156412/Math.pow(2,i)
})
return zooms.findIndex(function(z) {return z < res}) - 1
}
function geojson2poly(callback) {
console.log('geojson2poly ...')
var cmd = path.resolve(__dirname, './node_modules/.bin/geojson2poly')
var input = path.resolve(config.get('data:dir'),
'my-area-border-simplified.geojson')
var output = path.resolve(config.get('data:dir'),
'my-area-border.poly')
execFile(cmd, [input, output]
,{},
handleExec.bind({callback: callback})
)
}
function mergeOSMData(callback) {
var input1 = path.resolve(config.get('data:dir'),
'my-area-dropped-countries.osm')
var input2 = path.resolve(config.get('data:dir'),
'countries-border.osm')
var outFile = path.resolve(config.get('data:dir'),
'my-area-final-data.osm')
execFile('./osmconvert', [
'--hash-memory=' + g.memory,
input1, input2,
'--out-osm', '-o='+ outFile
],
{},
handleExec.bind({callback: callback}))
}
function osm2pgsql(callback) {
var input = path.resolve(config.get('data:dir'),
'my-area-final-data.osm')
var style = path.resolve(config.get('map:cartoDir'),
config.get('map:cartoStyle'))
console.log('osm2pgsql running ...')
console.log(['osm2pgsql',
'--create', '-d gis', '--slim', '--cache', g.memory,
'--number-processes', os.cpus().length,
'--hstore',
'--style', style,
input,
'&> /dev/null'].join(' '))
var cmd = spawn('osm2pgsql', [
'--create', '-d gis', '--slim', '--cache', g.memory,
'--number-processes', os.cpus().length,
'--hstore',
'--style', style,
input
])
cmd.on('error', function(err) {
throw err
})
cmd.stderr.on('data', function(data) {
console.error('Error: ' + data)
//callback(data) //as osm2pgsql logging stdout as stderr
})
cmd.stdout.on('data', function(data) {
outFileStream.write(data)
})
cmd.on('close', function(code) {
console.log('osm2pgsql completed')
callback(null)
})
}
function createWorldTileList(callback) {
console.log('createWorldTileList running ...')
var worldBound = path.resolve('./world-bound.geojson')
var outFileStream = fs.createWriteStream(
path.resolve(config.get('data:dir'), 'world-tile-list.txt'))
var cmd = spawn('./node_modules/.bin/osm-tile-list',
[
'--minZoom', '0',
'--maxZoom', '5',
'--tileBuffer', '3',
'--only-corners', 'true',
worldBound,
], {cwd: path.resolve('.') }
)
cmd.on('error', function(err) {
throw err
})
cmd.stderr.on('data', function(data) {
console.error('Error: ' + data)
callback(data)
})
cmd.stdout.on('data', function(data) {
outFileStream.write(data)
})
cmd.on('close', function(code) {
outFileStream.close()
callback(null)
})
}
function createMyCountryTileList(callback) {
console.log('createMyCountryTileList running ...')
var countryBound = path.resolve(config.get('data:dir'),
'my-area-border-simplified.geojson')
var outFileStream = fs.createWriteStream(
path.resolve(config.get('data:dir'), 'my-area-tile-list.txt'))
var cmd = spawn('./node_modules/.bin/osm-tile-list',
[
'--minZoom', '6',
'--maxZoom', '15',
'--tileBuffer', '3',
'--only-corners', 'true',
countryBound,
], {cwd: path.resolve('.') }
)
cmd.on('error', function(err) {
callback(err)
})
cmd.stderr.on('data', function(data) {
console.error('Error: ' + data)
callback(data)
})
cmd.stdout.on('data', function(data) {
outFileStream.write(data)
})
cmd.on('close', function(code) {
outFileStream.close()
callback(null)
})
}
function mergeTilesList(callback) {
var worldList = path.resolve(config.get('data:dir'), 'world-tile-list.txt')
var myAreaList = path.resolve(config.get('data:dir'),
'my-area-tile-list.txt')
var mergeList = path.resolve(config.get('data:dir'), 'final-tile-list.txt')
concat([worldList, myAreaList], mergeList, function(err, res) {
if (err) callback(err)
console.log('merged files')
callback()
})
}
function handleExec(err, stdout, stderr) {
debugger
if (err) {
console.log('some error in exec.')
throw err //callback(err)
}
if (stderr) {
console.log('some error in running programm')
console.log(stderr)
this.callback(stderr)
} else {
console.log(stdout)
console.log('done')
this.callback(stderr)
}
}