# LESSON 25: Display Your GPS Data as Track on Google Earth

In this lesson we will learn how to take the data we are logging from our GPS, and display it properly on Google Earth. This lesson builds on Lesson 22Lesson 23 and Lesson 24. You will need to complete those lessons before undertaking this lesson. In this series of lessons we are using the Arduino,  Adafruit Ultimate GPS, Virtuabotix SD card reader, and a Battery Clip to power the arduino during your mobile jaunts.  You will need a 9V battery. If you are going to be doing a lot of mobile work, it is probably worth it to invest in a good 9v Rechargable battery system.

When you get your gear gathered up, you will be ready to get this project going. First, hook the GPS and Card Reader up as we did in Lesson 23. We will start with the software that we used in Lesson 23. This software correctly reads data from the GPS and then logs the data into two data files on the SD card. The problem, though, we did not really do any fine tuning of the data file we were creating.

In this lesson we want to create a data file that we can display as a track in Google Earth. In order for this to work, we have to save the data in the manner a KML file wants to see. We have to save the data as:

Decimal Longitude, Decimal latitude, Altitude

I find it works to not use a line ending, but put a single white space after the Altitude.  That is, delimit with commas as shown above, but then use one white space to delimit between successive lines of data.

Lesson 24 explained in great detail how to interpret the NMEA sentences. The challenge here is that the Arduino is very poor at parsing strings. The NMEA sentence would be easy to parse in Python, because Python is so good at easily manipulating strings. Since it is tedious to manipulate strings in Arduino, we are going to try and parse the data using numbers and math, not strings. To explain this, I will give an example for Latitude, and Longitude will work in a similar manner. The latitude data we get from the parsed data from the Adafruit GPS looks like this:

3051.8007

From lesson 24, we know that this represents 30 degrees and 51.8007 minutes. In order to parse this in Arduino using just numbers I do the following. First create three variables deg, degWhole, and degDec. The variable deg will hold the final answer, degWhole holds the whole part of the number, and degDec whole the part to the right of the decimal. Adding degWhole to degDec gives you deg, your final answer. So, lets assume x = 3051.8007, our Latitude from the GPS.

Dividing by 100 moves the decimal to the left two spots, and taking the int removes anything to the right of the decimal. Then converting back to float gives us a round number with no values to the right of the decimal. For 3051.8007 it turns it into 30.0, the whole number part of degrees. Now to get the fractional part, we need to take minutes and divide by 60. This will always be a number less than one. To get fractional part of the latitude in degrees, we do the following:

We have to multiply degWhoe by 100 to get the decimal moved back to the right by 2. Then we subtract that number from the original latitude, and we are left with minutes. Then divide by 60 and we have converted minutes to a fraction of a degree. We now have the whole part and the fractional part of the answer, so we just add those together:

We now have a nice decimal number, deg, which is the decimal representation of our Latitude. We have one more thing to deal with. If the GPS is reporting ‘N’, leave the number positive. If the GPS is reporting ‘S’ (that we are in the Southern Hemisphere),  you need to make your latitude number negative. Similarly, when you are doing your longitude, you need to make the longitude negative if you are in the Western Hemisphere.

Finally, when you write the file, Google Earth will want you to store the data as:

Longitude, Latitude,altitude

It is curious that this is reverse from what you would do if you were entering coordinates into the Google Earth search bar. That wants Latitude first.

In any event, with the parsing and formatting described above, you can create a file that is almost ready to read into Google Earth. The code below will create your coordinates in the correct format for KML, which Google Earth will read. Please watch the video for a complete description of the code.

The final thing we have to do is to put a “wrapper” around the coordinates to turn the coordinates into a .kml file Google Earth will like. I just do this manually. I open the text file on the SD card created by the code above, and then just paste it into this KML wrapper, and save the file with a .kml extension. The KML wrapper is as follows:

Just paste your coordinate data in the file above between <coordinates> and </coordinates>, save with a .kml extension, and you should be able to open it with Google Earth and see your track. Good luck!