Friday, July 24, 2015

Google Earth and KML - Part Two (GETECH)

In the previous post I introduced the topic of working with geographic data in the KML format. As mentioned there, KML is the native data format used by Google Earth (GE) and it is widely supported by both GPS devices and software. I won't attempt to cover all the gory details of working with and editing KML in this series of posts (resource links at the end), but there a few practical things I've learned about working with GPS data in the KML format that I want to share.

As a basis for this discussion I'm looking at two GPS tracks I recorded on a short Kayak trip on the Kayaderosseras Creek near Saratoga Springs. One was recorded using my trusty Garmin GPS and the other was recorded using the GPS receiver in my camera; a Canon Sx260. I'll discuss and compare the accuracy of the two tracks and touch on some other issues you might encounter while using GPS to collect geo-referenced data for use in your projects.

The first issue is evident in the screen shot below (Image 1). The red line represents the track captured by the GPS receiver in the camera. The purple line shows the route recorded by the Garmin. The obvious difference is that the purple line extends beyond the point where the red line stops. That's because the red line stops at the place where I put the kayak into the creek and later took it out (at the Spa State Park Canoe Launch Site on Driscoll Road). I paddled upstream for a mile or so and then floated back down. I had both GPS units turned on and both units recorded the path. The purple line continues because when I got back to the launch point I forgot to turn off the Garmin GPS. So it recorded a track for the short carry from the creek up to the parking area and for my drive home. That data is not useful so the first thing I want to do is remove it.

Image 1: Screen capture from Google Earth. The two lines (red and purple) are GPS tracks recorded simultaneously on Kayak trip on the Kayaderosseras Creek near Saratoga Springs (NY)

I could do that by editing the track directly in Google Earth. That would be easy but it would be extremely tedious. There are several mouse clicks required to delete a point and there are a lot of points to get rid of to clean up this problem. You can see this in image two. For this screen capture I turned off the display of the track recorded by the camera (the red line) and selected the purple track for editing This makes visible the points recorded by the GPS. Each recorded point is represented by a red dot in the image and as you can see, there are a lot of points in just this short section of the route from the creek to my house. To remove this part of the track by editing it in GE you'd click on each points, select Delete, confirm the deletion and repeat. Lots of repeats.

Image 2: Screen capture from Google Earth. With the display of the track recorded by the camera (the red line) off, the track represented by the purple line was selected for editing. This allows us to see the individual points recorded by the GPS receiver. 
A better alternative is to open the KML file containing the track in a text editor and remove the unwanted points from the file. Once you have the hang of it you can edit the data in the file very quickly. There are few steps involved in this process which goes something like this:
  1. Export the track to a file. In Google Earth right click on the item in the Places pane, select Save Place As. Be sure to select KML from the format drop down so the file is saved in the KML format.
  2. Open the file in a text editor or an XML editor (I use Notepad++; a free and open source editor that is popular with programmers). You may need to configure your editor to recognize that the KML file is an XML file. That should allow the editor display the contents with nice formatting.
  3. In this case, removing the points I don't want comes down to removing all the points where the latitude is greater than the latitude of the put-in point. I'm in the northern hemisphere --so the latitude coordinates increase as you go north-- and all the points I want to keep are south of the put-in. Image three shows the map zoomed in on that location with the properties dialog for the point displayed. The properties dialog shows the latitude and longitude of the marker (and the location). This is how I got the latitude I will use as the basis for my edits; any point with a latitude greater than 43.034293 can be removed from the file. A potential complication is that that the coordinates in the KML file are stored using the decimal degree format. That's as you see it in the dialog seen in image three, but that representation is not the default for coordinates in Google Earth. The default is to represent coordinates in the Degree/Minute/Second format; like this: 43°02'03.4548" You can change the representation used by Google Earth using the Options dialog on the Tools menu.


Image 3: Screen capture from Google Earth. Viewing the properties of a marker added at the location of the start/end point of the trip. The properties dialog shows the coordinates of that point.

Once you have a KML file containing your track, and you have a way to identify the point(s) to remove (in this case, latitude > 43.034293) you can remove the unwanted points from the file. To do this you do need to know a little bit about the XML data format and how to edit data stored in XML, but there are many introductions to XML available on-line and I've included references at the end of this post. Also, if you've done any work with HTML this will all look familiar. XML data elements are placed inside of tag pairs and you must delete the entire tag pair or you wil break the formatting of the file (and GE will complain when you try to reload it). You'll also need to recognize how the coordinates are represented. An example from my file looks like this:

<gx:coord>-73.79277999999999 43.034036 102.52</gx:coord>

The coordinate of the location is represented by three values with a space placed between each value (it might be hard to see the space; -73.79277999999999<space>43.034036<space>102.52). The values are:

-73.79277999999999longitudea "west" longitude represented as a negative number
43.034336latitudea "north" latitude represented as a positive number
102.52elevationmeters above sea level

As discussed in the previous section, I want to delete any point where the latitude is greater than 43.034293, so this point can be removed from the file. The points are stored in the file in order, so once you find the place break between what you want and what you want to remove you can select all the bad rows and remove them. If editing data in this way is familiar to you then this should get you started. If this is entirely new then you may need more help. Look at the references or ask a friend who knows about this stuff.

With unwanted data removed from your file there is one final step; opening the file in Google Earth so you can see the results of your work. In Google Earth select "Open" from the File menu and find your edited file on disk. If you get warnings about errors check the file over for mis-matched tag pairs. That's it, you can save the file in your Places folder so that Google Earth reopens it automatically or you can reopen the file when you need it in the future.

This post is already rather long so I'm going to cover the other topics I raised at the start in a separate "part two" posting.