-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathleaflet-geotiff-plotty.js
85 lines (70 loc) · 2.35 KB
/
leaflet-geotiff-plotty.js
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
84
85
// Depends on:
// https://github.com/santilland/plotty
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
var L = require('leaflet-geotiff');
var plotty = require('plotty');
}
L.LeafletGeotiff.Plotty = L.LeafletGeotiffRenderer.extend({
options: {
colorScale: 'viridis',
clampLow: true,
clampHigh: true,
displayMin: 0,
displayMax: 1
},
initialize: function(options) {
if (typeof (plotty) === 'undefined') {
throw new Error("plotty not defined");
}
this.name = "Plotty";
L.setOptions(this, options);
this._preLoadColorScale();
},
setColorScale: function (colorScale) {
this.options.colorScale = colorScale;
this.parent._reset();
},
setDisplayRange: function (min,max) {
this.options.displayMin = min;
this.options.displayMax = max;
this.parent._reset();
},
_preLoadColorScale: function () {
var canvas = document.createElement('canvas');
var plot = new plotty.plot({
canvas: canvas,
data: [0],
width: 1, height: 1,
domain: [this.options.displayMin, this.options.displayMax],
colorScale: this.options.colorScale,
clampLow: this.options.clampLow,
clampHigh: this.options.clampHigh,
});
this.colorScaleData = plot.colorScaleCanvas.toDataURL();
},
render: function(raster, canvas, ctx, args) {
var plottyCanvas = document.createElement("canvas");
var plot = new plotty.plot({
data: raster.data,
width: raster.width, height: raster.height,
domain: [this.options.displayMin, this.options.displayMax],
colorScale: this.options.colorScale,
clampLow: this.options.clampLow,
clampHigh: this.options.clampHigh,
canvas: plottyCanvas,
useWebGL: false
});
plot.setNoDataValue(-9999);
plot.render();
this.colorScaleData = plot.colorScaleCanvas.toDataURL();
var rasterImageData = plottyCanvas.getContext("2d").getImageData(0, 0, plottyCanvas.width, plottyCanvas.height);
var imageData = this.parent.transform(rasterImageData, args);
ctx.putImageData(imageData, args.xStart, args.yStart);
}
});
L.LeafletGeotiff.plotty = function (options) {
return new L.LeafletGeotiff.Plotty(options);
};
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = L.LeafletGeotiff;
}