Parasing a GPS String in C#

Hey Guys,

I have GPS Device in my mobile phone, I get the following data from the device.
What is the best and most effcient way to parase the GPS Strig to get Lat and Lng?

$GPGGA,040144.21,,,,,00,02,5.0,,M,-0.582109,M,0.00701,*4B??$GPRMC,040144.21,V,,,,,,,090208,,*1A??$GPGSA,A,1,30,31,,,,,,,,,,,05,05,05*1A??$GPGGA,040145.14,,,,,00,03,5.0,,M,-0.582100,M,0.00701,*44??$GPRMC,040145.14,V,,,,,,,090208,,*1D??$GPGSA,A,1,01,30,31,,,,,,,,,,05,05,05*1B??$GPGGA,040146.23,,,,,00,03,5.0,,M,-0.582100,M,0.00701,*43??$GPRMC,040146.23,V,,,,,,,090208,,*1A??$GPGSA,A,1,01,30,31,,,,,,,,,,05,05,05*1B??$GPGGA,040147.18,,,,,00,03,5.0,,M,-0.582110,M,0.00701,*4B??$GPRMC,040147.18,V,,,,,,,090208,,*13??$GPGSA,A,1,01,30,31,,,,,,,,,,05,05,05*1B??$GPGGA,040148.20,,,,,00,03,5.0,,M,-0.582110,M,0.00701,*4F??$GPRMC,040148.20,V,,,,,,,090208,,*17??$GPGSV,3,1,12,31,75,336,29,01,75,058,26,14,48,104,,30,35,050,25*74??$GPGSV,3,2,12,20,25,310,,16,22,013,,05,18,044,,29,16,013,*7B??$GPGSV,3,3,12,06,15,118,,22,13,166,,12,06,038,,23,04,303,*78??$GPGSA,A,1,01,30,31,,,,,,,,,,05,05,05*1B??$GPGGA,040149.21,,,,,00,03,5.0,,M,-0.582110,M,0.00701,*4F??$GPRMC,040149.21,V,,,,,,,090208,,*17??$GPGSA,A,1,01,30,31,,,,,,,,,,05,05,05*1B??$GPGGA,040150.22,,,,,00,03,5.0,,M,-0.582110,M,0.00701,*44??$GPRMC,040150.22,V,,,,,,,090208,,*1C??$GPGSA,A,1,01,30,31,,,,,,,,,,05,05,05*1B??$GPGGA,040151.22,,,,,00,03,5.0,,M,-0.582110,M,0.00701,*45??$GPRMC,040151.22,V,,,,,,,090208,,*1D??$GPGSA,A,1,01,30,31,,,,,,,,,,05,05,05*1B??$GPGGA,040152.21,,,,,00,00,5.0,,M,-0.509010,M,0.00701,*44??$GPRMC,040152.21,V,,,,,,,090208,,*1D??$GPGSA,A,1,,,,,,,,,,,,,05,05,05*1B??$GPGGA,040153.15,,,,,00,01,5.0,,M,-0.509010,M,0.00701,*43??$GPRMC,040153.15,V,,,,,,,090208,,*1B??$GPGSV,3,1,12,31,75,336,27,01,75,058,,14,48,104,,30,35,050,*79??$GPGSV,3,2,12,20,25,310,,16,22,042,,05,18,044,,29,16,042,*7B??$GPGSV,3,3,12,06,15,118,,22,13,166,,12,06,038,,23,04,303,*78??$GPGSA,A,1,31,,,,,,,,,,,,05,05,05*19??$GPGGA,040154.21,,,,,00,01,5.0,,M,-0.509010,M,0.00701,*43??$GPRMC,040154.21,V,,,,,,,090208,,*1B??$GPGSA,A,1,31,,,,,,,,,,,,05,05,05*19??$GPGGA,040155.18,,,,,00,01,5.0,,M,-0.509010,M,0.00701,*48??$GPRMC,040155.18,V,,,,,,,090208,,*10??$GPGSA,A,1,31,,,,,,,,,,,,05,05,05*19??$GPGGA,040156.14,,,,,00,02,5.0,,M,-0.509010,M,0.00701,*44??$GPRMC,040156.14,V,,,,,,,090208,,*1F??$GPGSA,A,1,06,31,,,,,,,,,,,05,05,05*1F??$GPGGA,040157.16,,,,,00,03,5.0,,M,-0.509014,M,0.00701,*42??$GPRMC,040157.16,V,,,,,,,090208,,*1C??$GPGSA,A,1,06,30,31,,,,,,,,,,05,05,05*1C??$GPGGA,040158.19,,,,,00,03,5.0,,M,-0.509014,M,0.00701,*42??$GPRMC,040158.19,V,,,,,,,090208,,*1C??$GPGSV,3,1,12,31,75,336,27,01,75,058,,14,48,104,,30,35,050,18*70??$GPGSV,3,2,12,20,25,310,,16,22,073,,05,18,044,,29,16,073,*7B??$GPGSV,3,3,12,06,15,118,22,22,13,166,,12,06,038,,23,04,303,*78??$GPGSA,A,1,06,30,31,,,,,,,,,,05,05,05*1C??$GPGGA,040159.21,,,,,00,03,5.0,,M,-0.509014,M,0.00701,*48??$GPRMC,040159.21,V,,,,,,,090208,,*16??$GPGSA,A,1,06,30,31,,,,,,,,,,05,05,05*1C
sonu19Asked:
Who is Participating?
 
Bob LearnedConnect With a Mentor Commented:
You can use the Microsoft.VisualBasic.FileIO.TextFieldParser to parse comma-delimited files to get the entries, looking for the GGA entries the Jim so carefully pointed out.

Bob
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualBasic.FileIO;
 
public class CsvFileReader
{
  /// <summary>
  /// Read a CSV file, and add each line to a list of string arrays.
  /// </summary>
  /// <param name="fileName">The file to parse</param>
  /// <returns>A list of string arrays, which are the fields for the file</returns>
  public List<String[]> ReadFile(string fileName)
  {
    // Initialize the return values
    List<String[]> list = new List<string[]>();
    errorLines = new List<int>();
 
    using (TextFieldParser parser = new TextFieldParser(fileName))
    {
      // Setup the comma-delimited file parser.
      parser.TextFieldType = FieldType.Delimited;
      parser.Delimiters = new String[] { "," };
 
      while (!parser.EndOfData)
      {
        try
        {
          // Read the comma-delimited text as fields into a string array.
          string[] input = parser.ReadFields();
 
          // Collect all the lines as fields.
          list.Add(input);
        }
        catch (MalformedLineException ex)
        {
          Console.WriteLine("Error:  " + (int)ex.LineNumber);
        }
      }
    }
    return list;
  }
 
}

Open in new window

0
 
Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
What is the layout of the string?
0
 
JimBrandleyCommented:
In general, the longitude and latitude are contained in th GPGGA sentences. However, your data does not match NEMA sentence definitions. Can you get encoding information from the Manufacturer's site?

Jim
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
JimBrandleyCommented:
Just an FYI - If you have not already seen it, there's the NEMA mapping for the GPGGA sentence.

The most important NMEA sentences include the GGA which provides the current Fix data, the RMC which provides the minimum gps sentences information, and the GSA which provides the Satellite status data.

GGA - essential fix data which provide 3D location and accuracy data.

 $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47

Where:
     GGA          Global Positioning System Fix Data
     123519       Fix taken at 12:35:19 UTC
     4807.038,N   Latitude 48 deg 07.038' N
     01131.000,E  Longitude 11 deg 31.000' E
     1            Fix quality: 0 = invalid
                               1 = GPS fix (SPS)
                               2 = DGPS fix
                               3 = PPS fix
                         4 = Real Time Kinematic
                         5 = Float RTK
                               6 = estimated (dead reckoning) (2.3 feature)
                         7 = Manual input mode
                         8 = Simulation mode
     08           Number of satellites being tracked
     0.9          Horizontal dilution of position
     545.4,M      Altitude, Meters, above mean sea level
     46.9,M       Height of geoid (mean sea level) above WGS84
                      ellipsoid
     (empty field) time in seconds since last DGPS update
     (empty field) DGPS station ID number
     *47          the checksum data, always begins with *

If the height of geoid is missing then the altitude should be suspect. Some non-standard implementations report altitude with respect to the ellipsoid rather than geoid altitude. Some units do not report negative altitudes at all. This is the only sentence that reports altitude.


Jim
0
 
Bob LearnedCommented:
Ok, this question is a perfect example of disrespecting experts.  First, it doesn't even recognize the efforts of Jim to describe the text, and then it gives it a "B" grade.  I mean, what did you expect that you didn't get?

.NET Zone Advisor
0
 
sonu19Author Commented:
OK,
Let's see!

Jim did put a lot of effort in to this, but I already knew what he answered. I understand the GPS output!

You answer seems correct and what I was looking.
The reason you got an B grade even though your code is correct, but you didn't not answer the question of parasing a string rather you answered opening a files the parasing.

No disrespect, I got an answer to my question, even before your post. I still gave you the points.

I don't have anything else, but respect to all the the Experts!

0
All Courses

From novice to tech pro — start learning today.