Tuesday, November 1, 2016

GPS nmea GPX (or KLM) converter for Google Maps

This script take large amount of GPS data in nmea format and convert into gpx format to import into Google maps.

I had large amount of data coming from a trip to Iceland and I wanted quicly import it on Google My Maps.

Google has a limit of 5 MB but my data was 90 MB.

To reduce the data the script

  • only export latitude and longitude
  • approximae the latitude and longitude values reducing the number of decimal digit
  • skeep points

There are two variables that you can adjust to get an putput file smaller than 5 MB. In my case the best compromise was

  • savePointsEvery = 4
  • precision = 6

This generate an ouput file of 4,5 MB

Using instead

  • savePointsEvery = 4
  • precision = 6

The output file was 6 MB

The output files are in the folder data.

Source

Find the source at https://github.com/lucamug/gps-converter

Example

This is the result after imported in Google Maps:


Source

// gps-converter by lucamug
// more info at
// https://github.com/lucamug/gps-converter
// https://coding-and-design.blogspot.de/2016/11/gps-nmea-gpx-or-klm-converter-for.html
var fs = require('fs'),
folderOriginalData = 'data/original-gps-data/',
folderOutputData = 'data/',
r = [],
savePointsEvery = 4,
precision = 6,
files = fs.readdirSync(folderOriginalData),
ouputFileName = folderOutputData + "output." + savePointsEvery + "." + precision + ".gpx";
for (var j in files.sort()) {
var file = files[j];
r.push(processThisFile(file));
// break;
}
fs.writeFileSync(ouputFileName, [
'<?xml version="1.0"?>',
'<gpx creator="lucamug https://github.com/lucamug/gps-converter" version="1.0" xmlns="http://www.topografix.com/GPX/1/0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">',
'<trk>',
'<name>All</name>',
'<trkseg>',
r.join(''),
'</trkseg>',
'</trk>',
'</gpx>',
].join(''));
console.log("Check the output in " + ouputFileName);
function processThisFile(filename) {
var array = fs.readFileSync(folderOriginalData + filename).toString().split("\n");
var c = 0;
var r = [];
for (var i in array) {
var row = array[i];
if (row.match(/^\$GPRMC/)) {
c++;
if (!(c % savePointsEvery)) {
var ll = getLatLng(row);
r.push([
'<trkpt lat="' + ll.lat + '" lon="' + ll.lon + '">',
'</trkpt>',
].join(''));
}
// break;
}
}
return r;
}
function getLatLng(d) {
// Example of nmea
// $GPRMC,002516.000,A,6358.0464,N,02234.5556,W,1.69,324.78,290812,,,A*72
var nmea = d.split(",");
var coord, direction, days, minutes, lat, lon;
// LAT: North South
coord = nmea[3];
direction = nmea[4];
days = coord.substring(0, 2);
minutes = coord.substring(2, 10);
lat = toDD(days, minutes, direction);
// LON: East West
coord = nmea[5];
direction = nmea[6];
days = coord.substring(0, 3);
minutes = coord.substring(3, 11);
lon = toDD(days, minutes, direction);
return { lat: lat, lon: lon };
}
function toDD(degrees, minutes, direction) {
var out = parseInt(degrees) + (parseFloat(minutes) / 60);
if (direction == "S" || direction == "W") {
out = out * -1.0;
}
return out.toFixed(precision);
}

Others


Online converter:

http://www.gpsvisualizer.com/convert_input

No comments :

Post a Comment