forked from MapComplete/MapComplete
Scripts: create server script for pmtiles
This commit is contained in:
parent
1527627cd0
commit
676bb8a250
6 changed files with 175 additions and 60 deletions
64
scripts/pmTilesExtractGenerator.ts
Normal file
64
scripts/pmTilesExtractGenerator.ts
Normal file
|
@ -0,0 +1,64 @@
|
|||
import { spawn } from "child_process"
|
||||
import { Tiles } from "../src/Models/TileRange"
|
||||
|
||||
export class PmTilesExtractGenerator {
|
||||
private readonly _targetDir: string
|
||||
private readonly _sourceFile: string
|
||||
private readonly _executeableLocation: string
|
||||
|
||||
constructor(sourceFile: string, targetDir: string, executeableLocation: string = "/data/pmtiles") {
|
||||
this._sourceFile = sourceFile
|
||||
this._targetDir = targetDir
|
||||
this._executeableLocation = executeableLocation
|
||||
}
|
||||
|
||||
startProcess(script: string, captureStdioChunks?: (string: string) => void): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn(this._executeableLocation, script.split(" "), {
|
||||
stdio: captureStdioChunks === undefined ? "ignore" : ["pipe", "pipe", "pipe"],
|
||||
cwd: this._targetDir,
|
||||
})
|
||||
|
||||
if (captureStdioChunks !== undefined) {
|
||||
child.stdout.on("data", data => {
|
||||
captureStdioChunks(data)
|
||||
})
|
||||
}
|
||||
|
||||
/*child.stderr.on('data', data => {
|
||||
console.error(`stderr: ${data}`);
|
||||
});*/
|
||||
|
||||
child.on("close", (code) => {
|
||||
if (code === 0) resolve()
|
||||
else reject(new Error(`Process exited with code ${code}`))
|
||||
})
|
||||
|
||||
child.on("error", reject)
|
||||
})
|
||||
}
|
||||
|
||||
getFilename(z: number, x: number, y: number) {
|
||||
return `${this._targetDir}/${z}/${x}/${y}.pmtiles`
|
||||
}
|
||||
|
||||
async generateArchive(z: number, x: number, y: number, maxzoom?: number): Promise<string> {
|
||||
const [[max_lat, min_lon], [min_lat, max_lon]] = Tiles.tile_bounds(z, x, y)
|
||||
let maxzoomflag = ""
|
||||
if (maxzoom !== undefined) {
|
||||
maxzoomflag = " --maxzoom=" + maxzoom
|
||||
}
|
||||
const outputFileName = this.getFilename(z, x, y)
|
||||
await this.startProcess(
|
||||
`extract ${this._sourceFile} --download-threads=1 --minzoom=${z}${maxzoomflag} --bbox=${[
|
||||
min_lon,
|
||||
min_lat + 0.0001,
|
||||
max_lon,
|
||||
max_lat,
|
||||
].join(",")} ${outputFileName}`,
|
||||
)
|
||||
return outputFileName
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue