HaYaMoTo
Posts: 26
Joined: Sat Dec 03, 2016 11:11 pm

Convert NMEA sentence Lat and Lon to Decimal Degrees

Tue Feb 21, 2017 11:33 pm

Hi freinds,

i have this python code, using NMEA sentence ($GPRMC)

i need to convert the latitude ang longitude to decimal degress, The details are in this link : http://notinthemanual.blogspot.com/2008 ... de-to.html

Code: Select all

import serial

ser = serial.Serial('/dev/ttyUSB0', 9600,timeout=1)

x = ser.read(1200)
pos1 = x.find("$GPRMC")
pos2 = x.find("\n", pos1)
loc  = x[pos1:pos2]
data = loc.split(',')
    if data[2] == 'V':
        print 'No location found'
    else 
        print "Latitude =" + data[3]
        print "Longitude =" + data[5]
This is the results when i run the code :
Image

User avatar
Mortimer
Posts: 924
Joined: Sun Jun 10, 2012 3:57 pm

Re: Convert NMEA sentence Lat and Lon to Decimal Degrees

Wed Feb 22, 2017 12:32 am

So...

Lat : 3137.36664 becomes 31 degrees and 37.26664 seconds = 31 + 37.36664/60 = 31.6227773
Lon : 00212.21149 becomes 2 degrees and 12.21149 seconds = 2 + 12.21149/60 = 2.20352483

So as latitude is in format DDSS.SSSSS
DD = int(float(Lat)/100) = int(3137.36664/100) = int(31.3736664) = 31
SS = float(lat) - DD * 100 = 3137.36664 - 31 * 100 = 3137.36664 - 3100 = 37.36664

LatDec = DD + SS/60 = 31 + 37.36664/60 = 31 + 0.6227773333333333 = 31.6227773333333333

Don't forget that data[4] will be your North/South indicator, i.e. "S" or "N".
If this is "S" you need to negate your LatDec value.

The processing for the longitude will be very similar. This time data[6] will be your East/West indicator. So negate if "W".
--------------
The purpose of a little toe is to ensure you keep your furniture in the right place.

HaYaMoTo
Posts: 26
Joined: Sat Dec 03, 2016 11:11 pm

Re: Convert NMEA sentence Lat and Lon to Decimal Degrees

Wed Feb 22, 2017 12:44 am

Perfect and very detailed reply, thank you very much
Mortimer wrote:So...

Lat : 3137.36664 becomes 31 degrees and 37.26664 seconds = 31 + 37.36664/60 = 31.6227773
Lon : 00212.21149 becomes 2 degrees and 12.21149 seconds = 2 + 12.21149/60 = 2.20352483

So as latitude is in format DDSS.SSSSS
DD = int(float(Lat)/100) = int(3137.36664/100) = int(31.3736664) = 31
SS = float(lat) - DD * 100 = 3137.36664 - 31 * 100 = 3137.36664 - 3100 = 37.36664

LatDec = DD + SS/60 = 31 + 37.36664/60 = 31 + 0.6227773333333333 = 31.6227773333333333

Don't forget that data[4] will be your North/South indicator, i.e. "S" or "N".
If this is "S" you need to negate your LatDec value.

The processing for the longitude will be very similar. This time data[6] will be your East/West indicator. So negate if "W".

dimonic
Posts: 41
Joined: Fri Jun 08, 2012 9:08 pm

Re: Convert NMEA sentence Lat and Lon to Decimal Degrees

Thu Apr 11, 2019 7:03 pm

Just to be clear, I believe that wherever "seconds" was written, what was intended was minutes.

User avatar
Mortimer
Posts: 924
Joined: Sun Jun 10, 2012 3:57 pm

Re: Convert NMEA sentence Lat and Lon to Decimal Degrees

Sun Apr 14, 2019 10:52 am

My word! How silly of me.
Amazing it stood for so long undetected!

Corrected (for what it's worth):

So...

Lat : 3137.36664 becomes 31 degrees and 37.26664 seconds = 31 + 37.36664/60 = 31.6227773
Lon : 00212.21149 becomes 2 degrees and 12.21149 seconds = 2 + 12.21149/60 = 2.20352483

So as latitude is in format DDMM.MMMMM
DD = int(float(Lat)/100) = int(3137.36664/100) = int(31.3736664) = 31
MM = float(lat) - DD * 100 = 3137.36664 - 31 * 100 = 3137.36664 - 3100 = 37.36664

LatDec = DD + MM/60 = 31 + 37.36664/60 = 31 + 0.6227773333333333 = 31.6227773333333333

Don't forget that data[4] will be your North/South indicator, i.e. "S" or "N".
If this is "S" you need to negate your LatDec value.

The processing for the longitude will be very similar. This time data[6] will be your East/West indicator. So negate if "W".


Still at least the sums worked out OK! :lol:
--------------
The purpose of a little toe is to ensure you keep your furniture in the right place.

Return to “Beginners”