Creating KML files

A KML file is the file format that Google has developed and supports for transferring coordinate data into their Maps and Earth products. If we can convert our GPS data into a KML file, then we will be able to upload this into Google Maps and/or Google Earth to be able to view where we have been while using our GPS logger.

The library we shall use to create our KML files is called SimpleKML, and you install it by typing this:

sudo pip install simplekml

The documentation for this package is available at https://simplekml.readthedocs.io/en/latest/. Using the documentation available on this site, let's create a quick KML file, which has five readings from our GPS logger in it. Open a new file in Nano by typing nano gpsKMLTest.py, and type the following code into it:

import gps
import simplekml

session = gps.gps("localhost", "2947")
session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)
kml = simplekml.Kml()

i = 0

try:
while True:
report = session.next()
if report['class'] == 'TPV':
if hasattr(report, 'lon'):
gpsLon = report.lon
if hasattr(report, 'lat'):
gpsLat = report.lat
if hasattr(report, 'alt'):
gpsAlt = report.alt
gpsName = "Test GPS Point {0}".format(i)
print gpsName
kml.newpoint(name=gpsName, coords=[(gpsLon,gpsLat,gpsAlt)])
i = i + 1

except KeyboardInterrupt:
kml.save("gpsKMLTest.kml")
quit()

Save the changes by pressing Ctrl + O, followed by Enter, and then exit Nano by pressing Ctrl + X. Before we run this file, let's just talk through what we are doing as this will form the basis of our final program.

Firstly, we import the gps and simplekml libraries, we then connect to our gps socket, and create a simplekml object called kml. Just before we enter our infinite while loop, we create a counter variable and set it to 0.

We wrap our while loop inside a try and except error catching loop, which listens for a KeyboardInterrupt (Ctrl + C); and when it hears that key press, we save the KML file and then quit.

If we step into the while loop to look at what is happening in this part of the program, first we create a report object and tell it to step into the next section of the session stream (report = session.next()). We then check whether our report object contains a TPV attribute; if it does, we then check for a longitude attribute; then we check for a latitude reading, and finally we check for an altitude reading. If all four of these values are found, we create a new KML point and assign the coordinate data we have just found to it. If we do not find any of those four values, we jump to the end of the loop where we increment our counter variable and go back to the beginning.

Run the file for a short while by typing python ./gpsKMLTest.py. Once it has run for about 10 seconds, and you have seen a number of Test GPS Point names be printed to the SSH Terminal, press Ctrl + C to interrupt the program. At this point, Python will create our KML file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.75.227