2025-09-26 01:53:34 +02:00
|
|
|
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()
|
2025-09-26 02:09:12 +02:00
|
|
|
throw ("!!! Please, specify a source- and cachedir !!!")
|
2025-09-26 01:53:34 +02:00
|
|
|
}
|
|
|
|
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()
|