forked from cartesapp/serveur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiles.js
167 lines (136 loc) · 4.41 KB
/
tiles.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
import axios from 'axios'
import { spawn } from 'child_process'
import fs from 'fs'
import path from 'path'
/********
* Prerequisites :
* wget https://github.com/protomaps/go-pmtiles/releases/download/v1.22.0/go-pmtiles_1.22.0_Linux_x86_64.tar.gz -O ~/pmtiles.tar.gz
* mkdir ~/pmtiles
* tar -xvf ~/pmtiles.tar.gz -C ~/pmtiles
*
*
* *********
*/
import * as chalk from 'chalk'
import { MultiProgressBars } from 'multi-progress-bars'
const dryExec = async (text) => {
console.log(text)
return Promise.resolve({ stdout: '', stderr: '' })
}
const log = (...messages) => console.log(...messages)
export const liveExec = async (command) => {
const promise = new Promise((resolve, reject) => {
const child = spawn(command, [], { shell: true })
child.stdout.on('data', function (data) {
log('🟢stdout: ' + data.toString())
})
child.stderr.on('data', function (data) {
const message = '🔴stderr: ' + data.toString()
log(message)
//reject(message)
})
child.on('close', function (code) {
const message = 'child process exited with code ' + code
console.log(message)
resolve(message)
})
})
return promise
}
/*
liveExec(
'wget https://download.geofabrik.de/europe/france/bretagne-latest.osm.pbf'
)
*/
const exec = liveExec
export async function updatePlanetTiles() {
console.log('Will download panoramax planet pmtiles')
const url = `https://panoramax.openstreetmap.fr/pmtiles/planet.pmtiles`
await download(url)
// sudo because this dir is handled by www-data
await exec('sudo mv planet.pmtiles ~/serveur/data/pmtiles/planet.pmtiles')
console.log('-------------------------------')
console.log('✅ Downloaded 🌍️')
}
//updateFranceTiles()
const frenchGrid10Names = ['N50E000', 'N40E010', 'N40E000', 'N50E010'],
frenchGrid10 = frenchGrid10Names.map(
(name) => `https://osm.download.movisda.io/grid/${name}-10-latest.osm.pbf`
)
export async function updateFranceTiles(
osmPbfUrls = frenchGrid10,
outputFilename = 'hexagone-plus',
noDownload = false
) {
// Now france tiles
if (!noDownload) await Promise.all(osmPbfUrls.map((url) => download(url)))
const tilemakerMerges = osmPbfUrls.map((url, i) => {
const filename = url.split('/').slice(-1)[0]
const command = `tilemaker --input ${filename} --output ${outputFilename}.mbtiles --config ~/serveur/tilemaker/resources/config-openmaptiles.json --process ~/serveur/tilemaker/resources/process-openmaptiles.lua${
i > 0 ? ' --merge' : ''
}`
const [program, ...args] = command.split(' ')
console.log(program, args)
return command
})
for (const command of tilemakerMerges) {
console.log(command)
await exec(command)
}
await exec(
`~/pmtiles/pmtiles convert ${outputFilename}.mbtiles ${outputFilename}.pmtiles`
)
// sudo because this dir is handled by www-data
await exec(`sudo mv ${outputFilename}.pmtiles ~/serveur/data/pmtiles/`)
// wait until the above step is verified before deleting, we've got disk space
//await exec('rm hexagone-plus.mbtiles')
//await Promise.all(grid.map((zone) => exec(`rm ${zone}-10-latest.osm.pbf`)))
console.log('Done updating 😀')
}
false &&
(await updateFranceTiles(
['https://osm.download.movisda.io/grid/N48E002-latest.osm.pbf'],
'35'
))
//updateTiles()
// Initialize mpb
function createProgressBar() {
return new MultiProgressBars({
initMessage: ' $ Example Fullstack Build ',
anchor: 'top',
persist: true,
border: true,
})
}
export function download(url) {
const mpb = createProgressBar()
return new Promise(async (resolve, reject) => {
const filename = url.split('/').slice(-1)[0]
console.log('Will write', filename)
console.log('Connecting …')
const { data, headers } = await axios({
url,
method: 'GET',
responseType: 'stream',
})
const totalLength = headers['content-length']
console.log('Starting download')
const task = 'Downloading ' + filename
mpb.addTask(task, {
type: 'percentage',
barColorFn: chalk.yellow,
})
const total = parseInt(totalLength)
const writer = fs.createWriteStream(path.resolve('./', filename))
data.on('data', (chunk) =>
mpb.incrementTask(task, { percentage: (chunk.length / total) * 1 })
)
data.pipe(writer)
data.on('end', () => resolve())
})
}
/*
download(
'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4'
)
*/