libkml Marches On!

Monday, May 19, 2008 at 5:04:00 PM

Google has released version 0.2 of libkml, an open source library for serializing and deserializing KML files. libkml now uses a memory management scheme based on "smart pointers", and has deprecated the use of SCons. On Linux and Mac OS X it now use the traditional automake, and on Windows Microsoft Visual Studio. The "smart pointer" scheme presently restricts support for some alternate language bindings, so libkml 0.2 can only be called from C++, Java, and Python. Version 0.1 also supported PHP, Perl, and Ruby, and is still available in the subversion repository if you're interested. We plan on restoring the those bindings as soon as we can.

Check out the User Guide, and particularly the future development list.

Here's an example of what the code looks like:

// This program uses the KmlFactory to create a Point Placemark and
// prints the resultant KML on standard output.

#include "kml/dom.h"

// libkml types are in the kmldom namespace
using kmldom::CoordinatesPtr;
using kmldom::KmlPtr;
using kmldom::KmlFactory;
using kmldom::PlacemarkPtr;
using kmldom::PointPtr;

int main() {
  // Get the factory singleton to create KML elements.
  KmlFactory* factory = KmlFactory::GetFactory();

  // Create .
  CoordinatesPtr coordinates = factory->CreateCoordinates();
  // Create -122.0816695,37.42052549

  // Create  and give it .
  PointPtr point = factory->CreatePoint();

  // Create  and give it a  and the .
  PlacemarkPtr placemark = factory->CreatePlacemark();
  placemark->set_name("Cool Statue");

  // Create  and give it .
  KmlPtr kml = factory->CreateKml();

  // Serialize to XML
  std::string xml = kmldom::SerializePretty(kml);

  // Print to stdout
  std::cout << xml;

The engineers who worked on it put a lot of thought into making it fast and light weight. However, it is an alpha release. We really would love to have comments and feedback on it, both in the KML Developer Support forum and in the libkml issue tracker.