Skip to content

Commit

Permalink
Merge branch 'mylesmc123-CardinalDirectionAdded'
Browse files Browse the repository at this point in the history
  • Loading branch information
danwild committed Feb 25, 2021
2 parents 5aa69b6 + e1f617c commit 60215fd
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 15 deletions.
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,28 @@ Live Demo: https://danwild.github.io/leaflet-velocity/
var velocityLayer = L.velocityLayer({
displayValues: true,
displayOptions: {
// label prefix
velocityType: "Global Wind",

// leaflet control position
position: "bottomleft",

// no data at cursor
emptyString: "No velocity data",

// see explanation below
angleConvention: "bearingCW",
displayPosition: "bottomleft",
displayEmptyString: "No velocity data",
speedUnit: "kt",

// display cardinal direction alongside degrees
showCardinal: false,

// one of: ['ms', 'k/h', 'kt']
speedUnit: "ms",

// direction label prefix
directionString: "Direction",

// speed label prefix
speedString: "Speed"
},
data: data, // see demo/*.json, or wind-js-server for example data service
Expand Down
13 changes: 7 additions & 6 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ $.getJSON("wind-gbr.json", function(data) {
displayValues: true,
displayOptions: {
velocityType: "GBR Wind",
displayPosition: "bottomleft",
displayEmptyString: "No wind data"
position: "bottomleft",
emptyString: "No wind data",
showCardinal: true
},
data: data,
maxVelocity: 10
Expand All @@ -64,8 +65,8 @@ $.getJSON("water-gbr.json", function(data) {
displayValues: true,
displayOptions: {
velocityType: "GBR Water",
displayPosition: "bottomleft",
displayEmptyString: "No water data"
position: "bottomleft",
emptyString: "No water data"
},
data: data,
maxVelocity: 0.6,
Expand All @@ -80,8 +81,8 @@ $.getJSON("wind-global.json", function(data) {
displayValues: true,
displayOptions: {
velocityType: "Global Wind",
displayPosition: "bottomleft",
displayEmptyString: "No wind data"
position: "bottomleft",
emptyString: "No wind data"
},
data: data,
maxVelocity: 15
Expand Down
44 changes: 43 additions & 1 deletion dist/leaflet-velocity.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ L.Control.Velocity = L.Control.extend({
// Could be any combination of 'bearing' (angle toward which the flow goes) or 'meteo' (angle from which the flow comes)
// and 'CW' (angle value increases clock-wise) or 'CCW' (angle value increases counter clock-wise)
angleConvention: "bearingCCW",
showCardinal: false,
// Could be 'm/s' for meter per second, 'k/h' for kilometer per hour or 'kt' for knots
speedUnit: "m/s",
directionString: "Direction",
Expand Down Expand Up @@ -199,6 +200,45 @@ L.Control.Velocity = L.Control.extend({

return velocityDirToDegrees;
},
degreesToCardinalDirection: function degreesToCardinalDirection(deg) {
var cardinalDirection = '';

if (deg >= 0 && deg < 11.25 || deg >= 348.75) {
cardinalDirection = 'N';
} else if (deg >= 11.25 && deg < 33.75) {
cardinalDirection = 'NNW';
} else if (deg >= 33.75 && deg < 56.25) {
cardinalDirection = 'NW';
} else if (deg >= 56.25 && deg < 78.75) {
cardinalDirection = 'WNW';
} else if (deg >= 78.25 && deg < 101.25) {
cardinalDirection = 'W';
} else if (deg >= 101.25 && deg < 123.75) {
cardinalDirection = 'WSW';
} else if (deg >= 123.75 && deg < 146.25) {
cardinalDirection = 'SW';
} else if (deg >= 146.25 && deg < 168.75) {
cardinalDirection = 'SSW';
} else if (deg >= 168.75 && deg < 191.25) {
cardinalDirection = 'S';
} else if (deg >= 191.25 && deg < 213.75) {
cardinalDirection = 'SSE';
} else if (deg >= 213.75 && deg < 236.25) {
cardinalDirection = 'SE';
} else if (deg >= 236.25 && deg < 258.75) {
cardinalDirection = 'ESE';
} else if (deg >= 258.75 && deg < 281.25) {
cardinalDirection = 'E';
} else if (deg >= 281.25 && deg < 303.75) {
cardinalDirection = 'ENE';
} else if (deg >= 303.75 && deg < 326.25) {
cardinalDirection = 'NE';
} else if (deg >= 326.25 && deg < 348.75) {
cardinalDirection = 'NNE';
}

return cardinalDirection;
},
meterSec2Knots: function meterSec2Knots(meters) {
return meters / 0.514;
},
Expand All @@ -215,7 +255,9 @@ L.Control.Velocity = L.Control.extend({
var htmlOut = "";

if (gridValue && !isNaN(gridValue[0]) && !isNaN(gridValue[1]) && gridValue[2]) {
htmlOut = "<strong> ".concat(this.options.velocityType, " ").concat(this.options.directionString, ": </strong> ").concat(self.vectorToDegrees(gridValue[0], gridValue[1], this.options.angleConvention).toFixed(2), " \xB0, <strong> ").concat(this.options.velocityType, " ").concat(this.options.speedString, ": </strong> ").concat(self.vectorToSpeed(gridValue[0], gridValue[1], this.options.speedUnit).toFixed(2), " ").concat(this.options.speedUnit);
var deg = self.vectorToDegrees(gridValue[0], gridValue[1], this.options.angleConvention);
var cardinal = this.options.showCardinal ? " (".concat(self.degreesToCardinalDirection(deg), ") ") : '';
htmlOut = "<strong> ".concat(this.options.velocityType, " ").concat(this.options.directionString, ": </strong> ").concat(deg.toFixed(2), "\xB0").concat(cardinal, ", <strong> ").concat(this.options.velocityType, " ").concat(this.options.speedString, ": </strong> ").concat(self.vectorToSpeed(gridValue[0], gridValue[1], this.options.speedUnit).toFixed(2), " ").concat(this.options.speedUnit);
} else {
htmlOut = this.options.emptyString;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/leaflet-velocity.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 60 additions & 3 deletions src/js/L.Control.Velocity.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ L.Control.Velocity = L.Control.extend({
// Could be any combination of 'bearing' (angle toward which the flow goes) or 'meteo' (angle from which the flow comes)
// and 'CW' (angle value increases clock-wise) or 'CCW' (angle value increases counter clock-wise)
angleConvention: "bearingCCW",
showCardinal: false,
// Could be 'm/s' for meter per second, 'k/h' for kilometer per hour or 'kt' for knots
speedUnit: "m/s",
directionString: "Direction",
Expand Down Expand Up @@ -60,6 +61,61 @@ L.Control.Velocity = L.Control.extend({
return velocityDirToDegrees;
},

degreesToCardinalDirection: function(deg) {

let cardinalDirection = ''
if (deg >= 0 && deg < 11.25 || deg >= 348.75) {
cardinalDirection = 'N'
}
else if (deg >= 11.25 && deg < 33.75){
cardinalDirection = 'NNW'
}
else if (deg >= 33.75 && deg < 56.25){
cardinalDirection = 'NW'
}
else if (deg >= 56.25 && deg < 78.75){
cardinalDirection = 'WNW'
}
else if (deg >= 78.25 && deg < 101.25){
cardinalDirection = 'W'
}
else if (deg >= 101.25 && deg < 123.75){
cardinalDirection = 'WSW'
}
else if (deg >= 123.75 && deg < 146.25){
cardinalDirection = 'SW'
}
else if (deg >= 146.25 && deg < 168.75){
cardinalDirection = 'SSW'
}
else if (deg >= 168.75 && deg < 191.25){
cardinalDirection = 'S'
}
else if (deg >= 191.25 && deg < 213.75){
cardinalDirection = 'SSE'
}
else if (deg >= 213.75 && deg < 236.25){
cardinalDirection = 'SE'
}
else if (deg >= 236.25 && deg < 258.75){
cardinalDirection = 'ESE'
}
else if (deg >= 258.75 && deg < 281.25){
cardinalDirection = 'E'
}
else if (deg >= 281.25 && deg < 303.75){
cardinalDirection = 'ENE'
}
else if (deg >= 303.75 && deg < 326.25){
cardinalDirection = 'NE'
}
else if (deg >= 326.25 && deg < 348.75){
cardinalDirection = 'NNE'
}

return cardinalDirection;
},

meterSec2Knots: function(meters) {
return meters / 0.514;
},
Expand All @@ -85,11 +141,12 @@ L.Control.Velocity = L.Control.extend({
!isNaN(gridValue[1]) &&
gridValue[2]
) {
var deg = self.vectorToDegrees(gridValue[0], gridValue[1], this.options.angleConvention);
var cardinal = this.options.showCardinal ? ` (${self.degreesToCardinalDirection(deg)}) ` : '';

htmlOut = `<strong> ${this.options.velocityType} ${
this.options.directionString
}: </strong> ${self
.vectorToDegrees(gridValue[0], gridValue[1], this.options.angleConvention)
.toFixed(2)} °, <strong> ${this.options.velocityType} ${
}: </strong> ${deg.toFixed(2)}°${cardinal}, <strong> ${this.options.velocityType} ${
this.options.speedString
}: </strong> ${self
.vectorToSpeed(gridValue[0], gridValue[1], this.options.speedUnit)
Expand Down

0 comments on commit 60215fd

Please sign in to comment.