Skip to content

Commit 18752a0

Browse files
author
Leonardo Gatica
committed
Fix parser lat and lng
1 parent 7381dab commit 18752a0

File tree

3 files changed

+10
-16
lines changed

3 files changed

+10
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-nmea",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "Parser for NMEA sentences.",
55
"main": "lib",
66
"scripts": {

src/index.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ const gprmc = new XRegExp(
4545
(?<type> \\w{3}) \\,
4646
(?<time> \\d{6}[.]\\d{3}) \\,
4747
(?<gpsStatus> \\w{1}) \\,
48-
(?<latitude> \\d{0,4}[.]\\d{0,4}\\,[NS]) \\,
49-
(?<longitude> \\d{0,5}[.]\\d{0,4}\\,[WE]) \\,
48+
(?<latitude> \\d{4}[.]\\d{4}\\,[NS]) \\,
49+
(?<longitude> \\d{5}[.]\\d{4}\\,[WE]) \\,
5050
(?<speed> \\d{1,3}[.]\\d{1,3}) \\,
5151
(?<track> \\d{1,3}[.]\\d{1,3}) \\,
5252
(?<date> \\d{6}) \\,
@@ -74,10 +74,8 @@ export function isValid(data) {
7474
export function latToDmm(data) {
7575
const decimal = Math.abs(data)
7676
const degree = Math.floor(decimal)
77-
const minutes = Math.floor(decimal * 60) % 60
78-
const seconds = Math.round(100 * ((decimal * 3600) % 60)) / 100
7977
const dd = pad(degree, 2, "0")
80-
const mm = (minutes + (seconds / 60)).toFixed(4)
78+
const mm = pad(((decimal - degree) * 60.0).toFixed(4), 7, "0")
8179
const sign = data < 0 ? "S" : "N"
8280
return `${dd}${mm},${sign}`
8381
}
@@ -91,10 +89,8 @@ export function latToDmm(data) {
9189
export function lngToDmm(data) {
9290
const decimal = Math.abs(data)
9391
const degree = Math.floor(decimal)
94-
const minutes = Math.floor(decimal * 60) % 60
95-
const seconds = Math.round(100 * ((decimal * 3600) % 60)) / 100
9692
const dd = pad(degree, 3, "0")
97-
const mm = (minutes + (seconds / 60)).toFixed(4)
93+
const mm = pad(((decimal - degree) * 60.0).toFixed(4), 7, "0")
9894
const sign = data < 0 ? "W" : "E"
9995
return `${dd}${mm},${sign}`
10096
}
@@ -200,14 +196,14 @@ export function randomData() {
200196
const now = moment()
201197
const time = now.format("HHmmss.SSS")
202198
const gpsStatus = "A"
203-
const lat = chance.floating({min: -90, max: 90, fixed: 2})
204-
const lng = chance.floating({min: -180, max: 180, fixed: 2})
199+
const lat = chance.floating({min: -90, max: 90})
200+
const lng = chance.floating({min: -180, max: 180})
205201
const latitude = latToDmm(lat)
206202
const longitude = lngToDmm(lng)
207-
const speed = chance.floating({min: 0, max: 300, fixed: 2})
208-
const track = chance.floating({min: 0, max: 40, fixed: 2})
203+
const speed = chance.floating({min: 0, max: 300}).toFixed(2)
204+
const track = chance.floating({min: 0, max: 40}).toFixed(2)
209205
const date = now.format("DDMMYY")
210-
const mvValue = chance.floating({min: 0, max: 40, fixed: 1})
206+
const mvValue = chance.floating({min: 0, max: 40}).toFixed(1)
211207
const mvSign = chance.string({pool: "WE", length: 1})
212208
const mv = `${mvValue},${mvSign}`
213209
const magneticVariation = chance.pick([mv, ","])

test/nmea.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import moment from "moment"
66

77
describe("Nmea", () => {
88
const data = nmea.randomData()
9-
console.log(data)
109
const parser = nmea.parse(data.raw)
11-
console.log(parser)
1210

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

0 commit comments

Comments
 (0)