How to convert latitude and longitude of NMEA format data to decimal?

The format for NMEA coordinates is (d)ddmm.mmmm
d=degrees and m=minutes
There are 60 minutes in a degree so divide the minutes by 60 and add that to the degrees.

For the Latitude=35.15 N
35.15/60 = .5858 N

For the Longitude= 12849.52 E,
128+ 49.52/60 = 128.825333 E

In php, you could do this:

<?php
$lng = "12849.52 W";

$brk = strpos($lng,".") - 2;
if($brk < 0){ $brk = 0; }

$minutes = substr($lng, $brk);
$degrees = substr($lng, 0,$brk);

$newLng = $degrees + $minutes/60;

if(stristr($lng,"W")){
    $newLng = -1 * $newLng;
}

?>

Yes, NMEA format is ddmm.mmmm, n/s (d)ddmm.mmmm, e/w

To get to decimal degrees from degrees ad minutes, you use the following formula:

(d)dd + (mm.mmmm/60) (* -1 for W and S)

There is a nice little calculator here: http://www.hiddenvision.co.uk/ez/


Here is a minimalist C function to do it.

It returns decimal coordinates and shall be fed with the NMEA coordinate and respective quadrant or "indicator" character (N,S,E,W). E.g:

float latitude= GpsToDecimalDegrees("4349.7294",'N');
// latitude == 43.82882

float longitude= GpsToDecimalDegrees("10036.1057",'W');
// latitude == 43.82882

It is not optimized but should be readable, should be safe and does the job:

/**
 * Convert NMEA absolute position to decimal degrees
 * "ddmm.mmmm" or "dddmm.mmmm" really is D+M/60,
 * then negated if quadrant is 'W' or 'S'
 */
float GpsToDecimalDegrees(const char* nmeaPos, char quadrant)
{
  float v= 0;
  if(strlen(nmeaPos)>5)
  {
    char integerPart[3+1];
    int digitCount= (nmeaPos[4]=='.' ? 2 : 3);
    memcpy(integerPart, nmeaPos, digitCount);
    integerPart[digitCount]= 0;
    nmeaPos+= digitCount;
    v= atoi(integerPart) + atof(nmeaPos)/60.;
    if(quadrant=='W' || quadrant=='S')
      v= -v;
  }
  return v;
}

Tags:

Php

Java

Nmea