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/serverPmTileExtracts.ts
Normal file
64
scripts/serverPmTileExtracts.ts
Normal file
|
@ -0,0 +1,64 @@
|
|||
import { Server } from "./server"
|
||||
import Script from "./Script"
|
||||
import { OfflineBasemapManager } from "../src/Logic/OfflineBasemapManager"
|
||||
import http from "node:http"
|
||||
import { ServerResponse } from "http"
|
||||
import { existsSync } from "fs"
|
||||
import ScriptUtils from "./ScriptUtils"
|
||||
import { PmTilesExtractGenerator } from "./pmTilesExtractGenerator"
|
||||
|
||||
class ServerPmTileExtracts extends Script {
|
||||
constructor() {
|
||||
super("Starts a server that serves PMtiles. Usage:\n" +
|
||||
"sourceFile cachedir [portnumber??2346]")
|
||||
}
|
||||
|
||||
async main(args: string[]): Promise<void> {
|
||||
if(args.length < 2){
|
||||
this.printHelp()
|
||||
return
|
||||
}
|
||||
const sourcefile = args[0]
|
||||
const targetDir = args[1]
|
||||
const port = Number(args[2] ?? "2346")
|
||||
|
||||
const zoomlevels = OfflineBasemapManager.zoomelevels
|
||||
const generator = new PmTilesExtractGenerator(sourcefile, targetDir)
|
||||
|
||||
new Server(port, {},
|
||||
[
|
||||
{
|
||||
mustMatch: /\d+\/\d+\/\d+.pmtiles/,
|
||||
unmanaged: true,
|
||||
mimetype: "application/octet-stream",
|
||||
handle: async (path: string,
|
||||
queryParams: URLSearchParams,
|
||||
req: http.IncomingMessage,
|
||||
body: string,
|
||||
res: ServerResponse) => {
|
||||
const [z,x,y] = path.split(".")[0].split("/").map(x => Number(x))
|
||||
const maxzoom = zoomlevels[z]
|
||||
if(!maxzoom){
|
||||
throw "Invalid zoomlevel, must be one of "+Array.from(Object.keys(zoomlevels)).join(", ")
|
||||
}
|
||||
|
||||
const targetFile = generator.getFilename(z, x, y)
|
||||
if(!existsSync(targetFile)){
|
||||
ScriptUtils.createParentDir(targetFile)
|
||||
console.log("Creating", targetFile)
|
||||
await generator.generateArchive(z, x, y)
|
||||
}
|
||||
|
||||
res.writeHead(200, { "Content-Type": "application/octet-stream" })
|
||||
Server.sendFile(targetFile, res)
|
||||
|
||||
return null
|
||||
},
|
||||
},
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
new ServerPmTileExtracts().run()
|
Loading…
Add table
Add a link
Reference in a new issue