Decimal to Degree Minutes (WGS84) Conversion

Hi Postman Pros,

Over the last few days I’ve been trying to convert a coordinate from DD (decimal degrees) to NMEA format.

the Coordinates on the Endpoint A is in LAT/LON

    "latitude": 52.066177,
    "longitude": 11.666398,

What the Endpoint on B want is a NMEA Sentence like:

$FRCMD,IMEI,_SendMessage,,DDMM.mmmm,N,DDMM.mmmm,E,AA.a,SSS.ss,HHH.h,DDMMYY,hhmmss.dd,valid,var=value

There are some Functions on the Web but not special in Java or Postman.
It must be something like that:

Public Enum enumLongLat
    Latitude = 1
    Longitude = 2
End Enum
Public Enum enumReturnformat
    WithSigns = 0
    NMEA = 1
End Enum

Public Function DecimalPosToDegrees(ByVal Decimalpos As Double, ByVal Type As enumLongLat, ByVal Outputformat As enumReturnformat, Optional ByVal SecondResolution As Integer = 2) As String
        Dim Deg As Integer = 0
        Dim Min As Double = 0
        Dim Sec As Double = 0
        Dim Dir As String = ""
        Dim tmpPos As Double = Decimalpos
        If tmpPos < 0 Then tmpPos = Decimalpos * -1 'Always do math on positive values

        Deg = CType(Math.Floor(tmpPos), Integer)
        Min = (tmpPos - Deg) * 60
        Sec = (Min - Math.Floor(Min)) * 60
        Min = Math.Floor(Min)
        Sec = Math.Round(Sec, SecondResolution)

        Select Case Type
            Case enumLongLat.Latitude '=1
                If Decimalpos < 0 Then
                    Dir = "S"
                Else
                    Dir = "N"
                End If
            Case enumLongLat.Longitude '=2
                If Decimalpos < 0 Then
                    Dir = "W"
                Else
                    Dir = "E"
                End If
        End Select
        Select Case Outputformat
            Case enumReturnformat.NMEA
                Return AddZeros(Deg, 3) & AddZeros(Min, 2) & AddZeros(Sec, 2)
            Case enumReturnformat.WithSigns
                Return Deg & "°" & Min & """" & Sec & "'" & Dir
            Case Else
                Return ""
        End Select
    End Function
    
    
    Public Function AddZeros(ByVal Value As Double, ByVal Zeros As Integer) As String
        If Math.Floor(Value).ToString.Length < Zeros Then
            Return Value.ToString.PadLeft(Zeros, CType("0", Char))
        Else
            Return Value.ToString
        End If
    End Function

But i cant put the Math together. I will buy you 10 Coffees if you can put it together :wink: Maybe someone take the Competition

Postman uses JavaScript under the hood.

A quick search found the following.

NMEA GPS Simulator - CodePal

So taking that code into the Tests Tab.

class NMEAGPSSimulator {

    constructor(latitude, longitude) {
        this.latitude = latitude;
        this.longitude = longitude;
    }

    getLatitude() {
        return this.latitude;
    }

    getLongitude() {
        return this.longitude;
    }

    updateGPS(latitudeDelta, longitudeDelta) {
        this.latitude += latitudeDelta;
        this.longitude += longitudeDelta;
    }

    generateNMEASentence() {
        const latitudeDirection = this.latitude >= 0 ? "N" : "S";
        const longitudeDirection = this.longitude >= 0 ? "E" : "W";
        const latitudeDegrees = Math.abs(this.latitude);
        const longitudeDegrees = Math.abs(this.longitude);

        const nmeaSentence = `$GPGGA,${latitudeDegrees.toFixed(6)},${latitudeDirection},${longitudeDegrees.toFixed(6)},${longitudeDirection}`;
        return nmeaSentence;
    }
}

const gpsSimulator = new NMEAGPSSimulator(37.7749, -122.4194);
const nmeaSentence = gpsSimulator.generateNMEASentence();
console.log(`NMEA Sentence: ${nmeaSentence}`);

This produces the following…

image

Is this something you can build on?

Hi,

WOW, I’m still speechless that you found it so quickly, I’ve been puzzling for a while. That’s exactly what I’m looking for, unfortunately the result isn’t right. If you get the result with e.g.

GPRMC & GPGGA: Online Decoder for GPS NMEA messages

$GPRMC,001225,A,52.066349,N,11.666653,W,12,25,251211,1.2,E,A*03

you check the position is under Ghana / just over :wink:

But im testing a little bit further maybe im doing something wrong?

Hi,

If read Up the best i can. The class builds the sentence but dont convert lat/Lon to deegree Minutes which is the Main Part. But anyways not Bad.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.