MapComplete/src/Utils/pngMapCreator.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

121 lines
4.3 KiB
TypeScript
Raw Normal View History

import ThemeViewState from "../Models/ThemeViewState"
import { Utils } from "../Utils"
import { UIEventSource } from "../Logic/UIEventSource"
import { Map as MlMap } from "maplibre-gl"
import { MapLibreAdaptor } from "../UI/Map/MapLibreAdaptor"
import { AvailableRasterLayers } from "../Models/RasterLayers"
2022-09-14 12:18:51 +02:00
2022-10-27 01:50:01 +02:00
export interface PngMapCreatorOptions {
/**
* In mm
*/
2022-10-27 01:50:01 +02:00
readonly width: number
/**
* In mm
*/
2022-10-27 01:50:01 +02:00
readonly height: number
2022-09-14 12:18:51 +02:00
}
2022-09-12 20:14:03 +02:00
export class PngMapCreator {
private static id = 0
2022-10-27 01:50:01 +02:00
private readonly _options: PngMapCreatorOptions
private readonly _state: ThemeViewState
2022-09-12 20:14:03 +02:00
constructor(state: ThemeViewState, options: PngMapCreatorOptions) {
2022-10-27 01:50:01 +02:00
this._state = state
this._options = options
2022-09-12 20:14:03 +02:00
}
/**
* Creates a base64-encoded PNG image
* @constructor
*/
2023-06-04 22:52:13 +02:00
public async CreatePng(freeComponentId: string, status?: UIEventSource<string>): Promise<Blob> {
const div = document.createElement("div")
div.id = "mapdiv-" + PngMapCreator.id
/**
* We want a certain amount of pixels per mm² for a high print quality
* For this, we create a bigger map on the screen with a canvas, which has a pixelratio given
*
* We know that the default DPI of a canvas is 92, but to print something, we need a bit more
* So, instead, we give it PIXELRATIO more mm and let it render.
* We then draw this onto the PDF as if it were smaller, so it'll have plenty of quality there.
*
* However, we also need to compensate for this in the zoom level
*
*/
const pixelRatio = 2 // dots per mm
2023-06-04 22:52:13 +02:00
div.style.width = this._options.width + "mm"
div.style.height = this._options.height + "mm"
PngMapCreator.id++
2023-06-04 23:58:29 +02:00
try {
const layout = this._state.layout
2023-06-04 22:52:13 +02:00
2023-06-04 23:58:29 +02:00
function setState(msg: string) {
status?.setData(layout.id + ": " + msg)
}
setState("Initializing map")
2023-06-04 22:52:13 +02:00
2023-06-04 23:58:29 +02:00
const settings = this._state.mapProperties
const l = settings.location.data
2023-06-04 22:52:13 +02:00
2023-06-04 23:58:29 +02:00
document.getElementById(freeComponentId).appendChild(div)
const newZoom = settings.zoom.data + Math.log2(pixelRatio) - 1
2023-06-04 23:58:29 +02:00
const mapElem = new MlMap({
container: div.id,
style: AvailableRasterLayers.maptilerDefaultLayer.properties.url,
2023-06-04 23:58:29 +02:00
center: [l.lon, l.lat],
zoom: newZoom,
pixelRatio,
})
2023-06-04 22:52:13 +02:00
2023-06-04 23:58:29 +02:00
const map = new UIEventSource<MlMap>(mapElem)
const mla = new MapLibreAdaptor(map)
mla.zoom.setData(newZoom)
2023-06-04 23:58:29 +02:00
mla.location.setData(settings.location.data)
mla.rasterLayer.setData(settings.rasterLayer.data)
mla.allowZooming.setData(false)
mla.allowMoving.setData(false)
2023-06-04 22:52:13 +02:00
2023-06-04 23:58:29 +02:00
this._state?.showNormalDataOn(map)
console.log("Creating a map with size", this._options.width, this._options.height)
2023-06-04 22:52:13 +02:00
2023-06-04 23:58:29 +02:00
setState("Waiting for the data")
await this._state.dataIsLoading.AsPromise((loading) => !loading)
setState("Waiting for styles to be fully loaded")
while (!map?.data?.isStyleLoaded()) {
console.log("Waiting for the style to be loaded...")
await Utils.waitFor(250)
}
2023-06-04 23:58:29 +02:00
// Some extra buffer...
for (let i = 0; i < 5; i++) {
setState(5 - i + " seconds pause to make sure all images are loaded...")
await Utils.waitFor(1000)
}
setState(
"Exporting png (" +
this._options.width +
"mm * " +
this._options.height +
"mm , maplibre-canvas-pixelratio: " +
pixelRatio +
")"
)
const progress = new UIEventSource<{ current: number; total: number }>(undefined)
progress.addCallbackD(({ current, total }) => {
setState(`Rendering marker ${current}/${total}`)
})
const png = await mla.exportAsPng(pixelRatio, progress)
setState("Offering as download...")
return png
2023-06-04 23:58:29 +02:00
} finally {
div.parentElement.removeChild(div)
}
2022-09-12 20:14:03 +02:00
}
}