C++ class to generate GoogleEarth KML paths


Most of the time we have to chance to play around with a GPS module and somehow log its data, but no easy and quick way to visualize it. For me the solution was to create a simple C++ class that generates a GoogleEarth compatible KML file. This can be easily imported so that I can visualize the path my robot has taken.

API



  • Constructor(kml_file_path, path_name)

The constructor takes two arguments. The first one is the name of the KML file to write. Be careful as it will overwrite any previously existing file with this same name.

The second argument is the name to show inside of GoogleEarth for the path. I recommend making it descriptive.

  • Destructor

The destructor appends the closing KML elements. No user actions needed here.

  • addPoint(longitude,latitude)

As the name implies, it adds a point to the path. Therefore it needs the longitude and latitude coordinates. For each successive point in your path you will want to call this member function.

Example of use



#include <GoogleEarthPath.hpp>        //This class
#include <unistd.h>            //Sleep
#include <magic_GPS_library.hpp>    //Your GPS library

int main()
{
    // CREATE PATH
    GoogleEarthPath path(myPath.kml, NameToShowInGEarth)
    double longitude, latitude;

    for(int i=0; i < 10; i++)
    {
        //SOMEHOW GET GPS COORDINATES
        yourGPSfunction(longitude,latitude);

        //ADD POINT TO PATH
        path.addPoint(longitude,latitude);

        //WAIT FOR NEW POINTS
        sleep(10); //sleep 10 seconds
    }
    return 0;
}
This example code obtains GPS coordenates from an external function and stores them in the GoogleEarthPath instance. It samples 10 points during 100 seconds. Once the desctructor is called, the file myPath.kml is ready to be imported in GoogleEarth.


Code



Download GoogleEarthPath.h

No comments :

Post a Comment