GPS Data Parser for Google Earth

This page will be updated with more detail by Monday March 4, 2024.

using System.ComponentModel.Design;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace GPS_Data_Parser_2024_02_25_000
{
    internal class Program
    {
        static void Main(string[] args)
        {
 //           Console.WriteLine("Hello, World!");
 //           Console.WriteLine("What is your name?");
 //           var name = Console.ReadLine();
 //           var currentDate = DateTime.Now;
 //           Console.WriteLine($"{Environment.NewLine}Hello, {name}, on {currentDate:d} at {currentDate:t}!");
 //           Console.Write($"{Environment.NewLine}Press any key to exit...");
 //           Console.ReadKey(true);

           String line;
            try
            {
                //Pass the file path and file name to the StreamReader constructor
                StreamReader sr = new StreamReader("C:\\GPS_Test\\GPS_Test_Data.txt");
                //Open the File
                StreamWriter sw = new StreamWriter("C:\\GPS_Test\\GPS_Parsed_Data.txt", true, Encoding.ASCII);

                bool have_Lat = false;
                bool have_Lat_and_Lon = false;
                int name = 1;
                string Lat = " ";
                string Lon = " ";
                string Alt = " ";

                //GPS Visualizer requires the following header line
                // "name,latitude,longitude,alt(ft)"
                sw.WriteLine("name,latitude,longitude,alt(ft)");

                //Read the first line of text
                line = sr.ReadLine();
                //Continue to read until you reach end of file
                while (line != null)
                {
                    //write the line to console window
                    Console.WriteLine(line);

                    // ************************************************
                    // *** PARSING OF THE GPS DATA TAKES PLACE HERE ***
                    // ************************************************

                    //GPS_Lat: 42.468208
                    //GPS_Lon: -71.417703
                    //GPS_Alt: 547

                    if (line.StartsWith("GPS_Lat:"))
                    {
                        Lat = line.Substring(9, 9);
                        have_Lat = true;
                        have_Lat_and_Lon = false;
                        Lon = " ";
                        Alt = " ";
                    }

                    if (line.StartsWith("GPS_Lon:") & have_Lat)
                    {
                        Lon = line.Substring(9, 10);
                        have_Lat_and_Lon = true;
                    }

                    if (line.StartsWith("GPS_Lon:") & ! have_Lat)
                    {
                        Lat = " ";
                        Lon = " ";
                        Alt = " ";
                        have_Lat = false;
                        have_Lat_and_Lon = false;
                    }

                    if (line.StartsWith("GPS_Alt:") & have_Lat_and_Lon)
                    {
                        Alt = line.Substring(9);
                        have_Lat_and_Lon = true;

                        // Write the output line in this order
                        //    name,lat,lon,ALT)
                        sw.WriteLine(name.ToString() + "," + Lat + "," + Lon + "," + Alt);

                        name++; 
                    }

                    if (line.StartsWith("GPS_Alt:") & ! have_Lat_and_Lon)
                    {
                        Lat = " ";
                        Lon = " ";
                        Alt = " ";
                        have_Lat = false;
                        have_Lat_and_Lon = false;
                    }

                    if ((have_Lat == false) & (have_Lat_and_Lon == false))
                    {
                        Lat = " ";
                        Lon = " ";
                        Alt = " ";
                    }

                    // ************************************************
                    // ************************************************

                    //write the data to the output file
                    //sw.WriteLine(line);

                    //Read the next line
                    line = sr.ReadLine();
                }
                //close the files
                sw.Close();
                sr.Close();
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
            finally
            {
                Console.WriteLine("Executing finally block.");
            }

        }
    }
}

Scroll to Top