-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathQgisCanvasDataSource.ts
83 lines (72 loc) · 2.21 KB
/
QgisCanvasDataSource.ts
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
import type { QgisApi } from "qgis-js";
import ImageSource, { Options } from "ol/source/Image";
import { getWidth, getHeight } from "ol/extent";
export interface QgisCanvasDataSourceOptions extends Options {
renderFunction?: QgisCanvasRenderFunction;
}
export type QgisCanvasRenderFunction = (
api: QgisApi,
srid: string,
xMin: number,
yMin: number,
xMax: number,
yMax: number,
width: number,
height: number,
pixelRatio: number,
) => Promise<ImageData>;
export class QgisCanvasDataSource extends ImageSource {
protected api: QgisApi;
protected static DEFAULT_RENDERFUNCTION: QgisCanvasRenderFunction = (
api: QgisApi,
srid: string,
xMin: number,
yMin: number,
xMax: number,
yMax: number,
width: number,
height: number,
pixelRatio: number,
) => {
return api.renderImage(
srid,
new api.Rectangle(xMin, yMin, xMax, yMax),
width,
height,
pixelRatio,
);
};
protected renderFunction: QgisCanvasRenderFunction | undefined;
protected getrenderFunction(): QgisCanvasRenderFunction {
return this.renderFunction || QgisCanvasDataSource.DEFAULT_RENDERFUNCTION;
}
constructor(api: QgisApi, options: QgisCanvasDataSourceOptions = {}) {
super({
loader: (extent, resolution, requestPixelRatio) => {
return new Promise(async (resolve) => {
// note: requestPixelRatio is managed by ol and will not change on zoom
const pixelRatio = requestPixelRatio || window?.devicePixelRatio || 1;
const imageResolution = resolution / pixelRatio;
const width = Math.round(getWidth(extent) / imageResolution);
const height = Math.round(getHeight(extent) / imageResolution);
const renderFunction = this.getrenderFunction();
const imageData = await renderFunction(
this.api,
this.getProjection()?.getCode() || "EPSG:3857",
extent[0],
extent[1],
extent[2],
extent[3],
width,
height,
pixelRatio,
);
resolve(createImageBitmap(imageData));
});
},
...options,
});
this.api = api;
this.renderFunction = options.renderFunction;
}
}