Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for speed unit #8

Merged
merged 1 commit into from
Jun 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ var velocityLayer = L.velocityLayer({
displayOptions: {
velocityType: 'Global Wind',
displayPosition: 'bottomleft',
displayEmptyString: 'No velocity data'
displayEmptyString: 'No velocity data',
speedUnit: 'kt'
},
data: data, // see demo/*.json, or wind-js-server for example data service

Expand All @@ -29,6 +30,9 @@ var velocityLayer = L.velocityLayer({
});
```

The speed unit option refers to the unit used to express the wind speed in the control.
It can be `m/s` for meter per second, `k/h` for kilometer per hour or `kt` for knots. If not given defaults to `m/s`.

## Reference
`leaflet-velocity` is possible because of things like:
- [L.CanvasOverlay.js](https://gist.github.com/Sumbera/11114288)
Expand Down
27 changes: 22 additions & 5 deletions dist/leaflet-velocity.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,21 @@ L.CanvasLayer = (L.Layer ? L.Layer : L.Class).extend({
L.canvasLayer = function () {
return new L.CanvasLayer();
};
function meterSec2Knots(meters) {
return meters / 0.514;
}

function meterSec2kilometerHour(meters) {
return meters * 3.6;
}

L.Control.Velocity = L.Control.extend({

options: {
position: 'bottomleft',
emptyString: 'Unavailable'
emptyString: 'Unavailable',
// Could be 'm/s' for meter per second, 'k/h' for kilometer per hour or 'kt' for knots
speedUnit: 'm/s'
},

onAdd: function onAdd(map) {
Expand All @@ -171,17 +181,24 @@ L.Control.Velocity = L.Control.extend({
map.off('mousemove', this._onMouseMove, this);
},

vectorToSpeed: function vectorToSpeed(uMs, vMs) {
vectorToSpeed: function vectorToSpeed(uMs, vMs, unit) {
var velocityAbs = Math.sqrt(Math.pow(uMs, 2) + Math.pow(vMs, 2));
return velocityAbs;
// Default is m/s
if (unit === 'k/h') {
return meterSec2kilometerHour(velocityAbs);
} else if (unit === 'kt') {
return meterSec2Knots(velocityAbs);
} else {
return velocityAbs;
}
},

vectorToDegrees: function vectorToDegrees(uMs, vMs) {
var velocityAbs = Math.sqrt(Math.pow(uMs, 2) + Math.pow(vMs, 2));
var velocityDirTrigTo = Math.atan2(uMs / velocityAbs, vMs / velocityAbs);
var velocityDirTrigToDegrees = velocityDirTrigTo * 180 / Math.PI;
var velocityDirTrigFromDegrees = velocityDirTrigToDegrees + 180;
return velocityDirTrigFromDegrees.toFixed(3);
return velocityDirTrigFromDegrees.toFixed(2);
},

_onMouseMove: function _onMouseMove(e) {
Expand All @@ -197,7 +214,7 @@ L.Control.Velocity = L.Control.extend({
var vMs = gridValue[1];
vMs = vMs > 0 ? vMs = vMs - vMs * 2 : Math.abs(vMs);

htmlOut = "<strong>" + this.options.velocityType + " Direction: </strong>" + self.vectorToDegrees(gridValue[0], vMs) + "°" + ", <strong>" + this.options.velocityType + " Speed: </strong>" + self.vectorToSpeed(gridValue[0], vMs).toFixed(1) + "m/s";
htmlOut = "<strong>" + this.options.velocityType + " Direction: </strong>" + self.vectorToDegrees(gridValue[0], vMs) + "°" + ", <strong>" + this.options.velocityType + " Speed: </strong>" + self.vectorToSpeed(gridValue[0], vMs, this.options.speedUnit).toFixed(2) + this.options.speedUnit;
} else {
htmlOut = this.options.displayEmptyString;
}
Expand Down