forked from stuartmatthews/leaflet-geotiff
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathleaflet-geotiff-vector-arrows.js
64 lines (53 loc) · 1.97 KB
/
leaflet-geotiff-vector-arrows.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
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
var L = require('leaflet-geotiff');
}
L.LeafletGeotiff.VectorArrows = L.LeafletGeotiffRenderer.extend({
options: {
arrowSize: 20
},
initialize: function(options) {
this.name = "Vector";
L.setOptions(this, options);
},
setArrowSize: function (colorScale) {
this.options.colorScale = colorScale;
this.parent._reset();
},
render: function(raster, canvas, ctx, args) {
var arrowSize = this.options.arrowSize;
var gridPxelSize = (args.rasterPixelBounds.max.x - args.rasterPixelBounds.min.x) / raster.width;
var stride = Math.max(1,Math.floor(1.2*arrowSize/gridPxelSize));
for (var y=0;y<raster.height;y=y+stride) {
for (var x=0;x<raster.width;x=x+stride) {
var rasterIndex = (y*raster.width+x);
if (raster.data[rasterIndex]>=0) { //Ignore missing values
//calculate lat-lon of of this point
var currentLng = this.parent._rasterBounds._southWest.lng + (x+0.5)*args.lngSpan;
var currentLat = this.parent._rasterBounds._northEast.lat - (y+0.5)*args.latSpan;
//convert lat-lon to pixel cordinates
var projected = this.parent._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+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();
}
}
}
}
});
L.LeafletGeotiff.vectorArrows= function (options) {
return new L.LeafletGeotiff.VectorArrows(options);
};
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = L.LeafletGeotiff;
}