Skip to content

Commit

Permalink
Fix parser lat and lng
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonardo Gatica committed Jul 15, 2015
1 parent 7381dab commit 18752a0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-nmea",
"version": "0.0.2",
"version": "0.0.3",
"description": "Parser for NMEA sentences.",
"main": "lib",
"scripts": {
Expand Down
22 changes: 9 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ const gprmc = new XRegExp(
(?<type> \\w{3}) \\,
(?<time> \\d{6}[.]\\d{3}) \\,
(?<gpsStatus> \\w{1}) \\,
(?<latitude> \\d{0,4}[.]\\d{0,4}\\,[NS]) \\,
(?<longitude> \\d{0,5}[.]\\d{0,4}\\,[WE]) \\,
(?<latitude> \\d{4}[.]\\d{4}\\,[NS]) \\,
(?<longitude> \\d{5}[.]\\d{4}\\,[WE]) \\,
(?<speed> \\d{1,3}[.]\\d{1,3}) \\,
(?<track> \\d{1,3}[.]\\d{1,3}) \\,
(?<date> \\d{6}) \\,
Expand Down Expand Up @@ -74,10 +74,8 @@ export function isValid(data) {
export function latToDmm(data) {
const decimal = Math.abs(data)
const degree = Math.floor(decimal)
const minutes = Math.floor(decimal * 60) % 60
const seconds = Math.round(100 * ((decimal * 3600) % 60)) / 100
const dd = pad(degree, 2, "0")
const mm = (minutes + (seconds / 60)).toFixed(4)
const mm = pad(((decimal - degree) * 60.0).toFixed(4), 7, "0")
const sign = data < 0 ? "S" : "N"
return `${dd}${mm},${sign}`
}
Expand All @@ -91,10 +89,8 @@ export function latToDmm(data) {
export function lngToDmm(data) {
const decimal = Math.abs(data)
const degree = Math.floor(decimal)
const minutes = Math.floor(decimal * 60) % 60
const seconds = Math.round(100 * ((decimal * 3600) % 60)) / 100
const dd = pad(degree, 3, "0")
const mm = (minutes + (seconds / 60)).toFixed(4)
const mm = pad(((decimal - degree) * 60.0).toFixed(4), 7, "0")
const sign = data < 0 ? "W" : "E"
return `${dd}${mm},${sign}`
}
Expand Down Expand Up @@ -200,14 +196,14 @@ export function randomData() {
const now = moment()
const time = now.format("HHmmss.SSS")
const gpsStatus = "A"
const lat = chance.floating({min: -90, max: 90, fixed: 2})
const lng = chance.floating({min: -180, max: 180, fixed: 2})
const lat = chance.floating({min: -90, max: 90})
const lng = chance.floating({min: -180, max: 180})
const latitude = latToDmm(lat)
const longitude = lngToDmm(lng)
const speed = chance.floating({min: 0, max: 300, fixed: 2})
const track = chance.floating({min: 0, max: 40, fixed: 2})
const speed = chance.floating({min: 0, max: 300}).toFixed(2)
const track = chance.floating({min: 0, max: 40}).toFixed(2)
const date = now.format("DDMMYY")
const mvValue = chance.floating({min: 0, max: 40, fixed: 1})
const mvValue = chance.floating({min: 0, max: 40}).toFixed(1)
const mvSign = chance.string({pool: "WE", length: 1})
const mv = `${mvValue},${mvSign}`
const magneticVariation = chance.pick([mv, ","])
Expand Down
2 changes: 0 additions & 2 deletions test/nmea.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import moment from "moment"

describe("Nmea", () => {
const data = nmea.randomData()
console.log(data)
const parser = nmea.parse(data.raw)
console.log(parser)

describe("#this.raw", () => {
it("should return the same value passed in the constructor", () => {
Expand Down

0 comments on commit 18752a0

Please sign in to comment.