forked from stuartmatthews/leaflet-geotiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet-geotiff.js
350 lines (311 loc) · 15.3 KB
/
leaflet-geotiff.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Ideas from:
// https://github.com/ScanEx/Leaflet.imageTransform/blob/master/src/L.ImageTransform.js
// https://github.com/BenjaminVadant/leaflet-ugeojson
// Depends on:
// https://github.com/santilland/plotty
// https://github.com/constantinius/geotiff.js
// Note this will only work with ESPG:4326 tiffs
L.LeafletGeotiff = L.ImageOverlay.extend({
initialize: function (url, options) {
if(typeof(plotty)=='undefined'){
throw new Error("plotty not defined");
};
if(typeof(GeoTIFF)=='undefined'){
throw new Error("GeoTIFF not defined");
};
this.raster = {};
if (options.bounds) {
this._rasterBounds = L.latLngBounds(options.bounds);
}
L.Util.setOptions(this, options);
this.options.colorScale = (options.colorScale==undefined) ? 'viridis' : options.colorScale;
this.options.clampLow = (options.clampLow==undefined) ? true : options.clampLow;
this.options.clampHigh = (options.clampHigh==undefined) ? true : options.clampHigh;
this.options.arrowSize = (options.arrowSize==undefined) ? 20 : options.arrowSize;
this._preLoadColorScale(); //Make sure colorScale is ready even if image takes a while to load
this._getData(url);
},
setURL: function(newURL) {
this._getData(newURL);
},
onAdd: function (map) {
this._map = map;
if (!this._image) {
this._initImage();
}
map._panes.overlayPane.appendChild(this._image);
map.on('moveend', this._reset, this);
if (map.options.zoomAnimation && L.Browser.any3d) {
map.on('zoomanim', this._animateZoom, this);
}
this._reset();
},
onRemove: function (map) {
map.getPanes().overlayPane.removeChild(this._image);
map.off('moveend', this._reset, this);
if (map.options.zoomAnimation) {
map.off('zoomanim', this._animateZoom, this);
}
},
_getData: function(url) {
var self = this;
var request = new XMLHttpRequest();
request.onload = function() {
if (this.status >= 200 && this.status < 400) {
self._parseTIFF(this.response);
} //TODO else handle error
};
request.open("GET", url, true);
request.responseType = "arraybuffer";
request.send();
},
_parseTIFF: function (arrayBuffer) {
this.tiff = GeoTIFF.parse(arrayBuffer);
if (typeof(this.options.image)=='undefined') {
this.options.image = 0;
}
if (typeof(this.options.band)=='undefined') {
this.options.band = 0;
}
this.setBand(this.options.band);
if (!this.options.bounds) {
var image = this.tiff.getImage(this.options.image);
var meta = image.getFileDirectory();
var x_min = meta.ModelTiepoint[3];
var x_max = x_min + meta.ModelPixelScale[0]*meta.ImageWidth;
var y_min = meta.ModelTiepoint[4];
var y_max = y_min - meta.ModelPixelScale[1]*meta.ImageLength;
this._rasterBounds = L.latLngBounds([[y_min,x_min],[y_max,x_max]]);
this._reset()
}
},
setBand: function (band) {
this.options.band = band
var image = this.tiff.getImage(this.options.image);
this.raster.data = image.readRasters({samples: [band]})[0];
this.raster.width = image.getWidth();
this.raster.height = image.getHeight();
this._reset()
},
getRasterArray: function () {
return this.raster.data;
},
getRasterCols: function () {
return this.raster.width;
},
getRasterRows: function () {
return this.raster.height;
},
setColorScale: function (colorScale) {
this.options.colorScale = colorScale;
this._reset();
},
setDisplayRange: function (min,max) {
this.options.displayMin = min;
this.options.displayMax = max;
this._reset();
},
getValueAtLatLng: function (lat, lng) {
try {
var x = Math.floor(this.raster.width*(lng - this._rasterBounds._southWest.lng)/(this._rasterBounds._northEast.lng - this._rasterBounds._southWest.lng));
var y = this.raster.height-Math.ceil(this.raster.height*(lat - this._rasterBounds._southWest.lat)/(this._rasterBounds._northEast.lat - this._rasterBounds._southWest.lat));
var i = y*this.raster.width+x
return this.raster.data[i];
}
catch(err) {
return undefined;
}
},
_animateZoom: function (e) {
if (L.version >= "1.0") {
var scale = this._map.getZoomScale(e.zoom),
offset = this._map._latLngBoundsToNewLayerBounds(this._map.getBounds(), e.zoom, e.center).min;
L.DomUtil.setTransform(this._image, offset, scale);
} else {
var scale = this._map.getZoomScale(e.zoom),
nw = this._map.getBounds().getNorthWest(),
se = this._map.getBounds().getSouthEast(),
topLeft = this._map._latLngToNewLayerPoint(nw, e.zoom, e.center),
size = this._map._latLngToNewLayerPoint(se, e.zoom, e.center)._subtract(topLeft);
this._image.style[L.DomUtil.TRANSFORM] =
L.DomUtil.getTranslateString(topLeft) + ' scale(' + scale + ') ';
}
},
_reset: function () {
if (this.hasOwnProperty('_map')) {
if (this._rasterBounds) {
topLeft = this._map.latLngToLayerPoint(this._map.getBounds().getNorthWest()),
size = this._map.latLngToLayerPoint(this._map.getBounds().getSouthEast())._subtract(topLeft);
L.DomUtil.setPosition(this._image, topLeft);
this._image.style.width = size.x + 'px';
this._image.style.height = size.y + 'px';
this._drawImage();
};
};
},
_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();
},
setClip: function(clipLatLngs) {
this.options.clip = clipLatLngs;
this._reset();
},
_clipMaskToPixelPoints: function() {
if (this.options.clip) {
var topLeft = this._map.latLngToLayerPoint(this._map.getBounds().getNorthWest());
var pixelClipPoints = [];
for (var p = 0; p < this.options.clip.length; p++) {
var mercPoint = this._map.latLngToLayerPoint(this.options.clip[p]),
pixel = L.point(mercPoint.x - topLeft.x, mercPoint.y - topLeft.y);
pixelClipPoints.push(pixel);
}
this._pixelClipPoints = pixelClipPoints;
} else {
this._pixelClipPoints = undefined;
}
},
_drawImage: function () {
var self = this;
if (self.raster.hasOwnProperty('data')) {
topLeft = this._map.latLngToLayerPoint(this._map.getBounds().getNorthWest()),
size = this._map.latLngToLayerPoint(this._map.getBounds().getSouthEast())._subtract(topLeft);
var rasterPixelBounds = L.bounds(this._map.latLngToContainerPoint(this._rasterBounds.getNorthWest()),this._map.latLngToContainerPoint(this._rasterBounds.getSouthEast()))
var xStart = (rasterPixelBounds.min.x>0 ? rasterPixelBounds.min.x : 0);
var xFinish = (rasterPixelBounds.max.x<size.x ? rasterPixelBounds.max.x : size.x);
var yStart = (rasterPixelBounds.min.y>0 ? rasterPixelBounds.min.y : 0);
var yFinish = (rasterPixelBounds.max.y<size.y ? rasterPixelBounds.max.y : size.y);
var plotWidth = xFinish-xStart;
var plotHeight = yFinish-yStart;
if ((plotWidth<=0) || (plotHeight<=0)) {
console.log(this.options.name,' is off screen.');
var plotCanvas = document.createElement("canvas");
plotCanvas.width = size.x;
plotCanvas.height = size.y;
var ctx = plotCanvas.getContext("2d");
ctx.clearRect(0, 0, plotCanvas.width, plotCanvas.height);
this._image.src = plotCanvas.toDataURL();
return;
}
var xOrigin = this._map.getPixelBounds().min.x+xStart;
var yOrigin = this._map.getPixelBounds().min.y+yStart;
var lngSpan = (this._rasterBounds._northEast.lng - this._rasterBounds._southWest.lng)/this.raster.width;
var latSpan = (this._rasterBounds._northEast.lat - this._rasterBounds._southWest.lat)/this.raster.height;
//Draw image data to canvas and pass to image element
var plotCanvas = document.createElement("canvas");
plotCanvas.width = size.x;
plotCanvas.height = size.y;
var ctx = plotCanvas.getContext("2d");
ctx.clearRect(0, 0, plotCanvas.width, plotCanvas.height);
if (this.options.vector==true) {
var arrowSize = this.options.arrowSize;
var zoom = this._map.getZoom();
var gridPxelSize = (rasterPixelBounds.max.x - rasterPixelBounds.min.x) / self.raster.width;
var stride = Math.max(1,Math.floor(1.2*arrowSize/gridPxelSize));
for (var y=0;y<self.raster.height;y=y+stride) {
for (var x=0;x<self.raster.width;x=x+stride) {
var rasterIndex = (y*this.raster.width+x);
if (self.raster.data[rasterIndex]>=0) { //Ignore missing values
//calculate lat-lon of of this point
var currentLng = this._rasterBounds._southWest.lng + (x+0.5)*lngSpan;
var currentLat = this._rasterBounds._northEast.lat - (y+0.5)*latSpan;
//convert lat-lon to pixel cordinates
var projected = this._map.latLngToContainerPoint(L.latLng(currentLat,currentLng)); //If slow could unpack this calculation
var xProjected = projected.x;
var yProjected = projected.y;
//draw an arrow
ctx.save();
ctx.translate(xProjected, yProjected);
ctx.rotate((90+self.raster.data[rasterIndex])*Math.PI/180);
ctx.beginPath();
ctx.moveTo(-arrowSize/2, 0);
ctx.lineTo(+arrowSize/2, 0);
ctx.moveTo(arrowSize*0.25, -arrowSize*0.25);
ctx.lineTo(+arrowSize/2, 0);
ctx.lineTo(arrowSize*0.25, arrowSize*0.25);
ctx.stroke();
ctx.restore();
}
}
}
} else {
var plottyCanvas = document.createElement("canvas");
var plot = new plotty.plot({
data: self.raster.data,
width: self.raster.width, height: self.raster.height,
domain: [self.options.displayMin, self.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);
//Create image data and Uint32 views of data to speed up copying
var imageData = new ImageData(plotWidth, plotHeight);
var outData = imageData.data;
var outPixelsU32 = new Uint32Array(outData.buffer);
var inData = rasterImageData.data;
var inPixelsU32 = new Uint32Array(inData.buffer);
var zoom = this._map.getZoom();
var scale = this._map.options.crs.scale(zoom);
var d = 57.29577951308232; //L.LatLng.RAD_TO_DEG;
var transformationA = this._map.options.crs.transformation._a;
var transformationB = this._map.options.crs.transformation._b;
var transformationC = this._map.options.crs.transformation._c;
var transformationD = this._map.options.crs.transformation._d;
if (L.version >= "1.0") {
transformationA = transformationA*this._map.options.crs.projection.R;
transformationC = transformationC*this._map.options.crs.projection.R;
}
for (var y=0;y<plotHeight;y++) {
var yUntransformed = ((yOrigin+y) / scale - transformationD) / transformationC;
var currentLat = (2 * Math.atan(Math.exp(yUntransformed)) - (Math.PI / 2)) * d;
var rasterY = this.raster.height-Math.ceil((currentLat - this._rasterBounds._southWest.lat)/latSpan);
for (var x=0;x<plotWidth;x++) {
//Location to draw to
var index = (y*plotWidth+x);
//Calculate lat-lng of (x,y)
//This code is based on leaflet code, unpacked to run as fast as possible
//Used to deal with TIF being EPSG:4326 (lat,lon) and map being EPSG:3857 (m E,m N)
var xUntransformed = ((xOrigin+x) / scale - transformationB) / transformationA;
var currentLng = xUntransformed * d;
var rasterX = Math.floor((currentLng - this._rasterBounds._southWest.lng)/lngSpan);
var rasterIndex = (rasterY*this.raster.width+rasterX);
//Copy pixel value
outPixelsU32[index] = inPixelsU32[rasterIndex];
}
}
ctx.putImageData(imageData, xStart, yStart);
}
//Draw clipping polygon
if (this.options.clip) {
this._clipMaskToPixelPoints();
ctx.globalCompositeOperation = 'destination-out'
ctx.rect(xStart-10,yStart-10,plotWidth+20,plotHeight+20);
//Draw vertices in reverse order
for (var i = this._pixelClipPoints.length-1; i >= 0; i--) {
var pix = this._pixelClipPoints[i];
ctx['lineTo'](pix.x, pix.y);
}
ctx.closePath();
ctx.fill();
}
this._image.src = String(plotCanvas.toDataURL());
}
},
});
L.leafletGeotiff = function (url, bounds, options) {
return new L.LeafletGeotiff(url, bounds, options);
};