[gdal-dev] Reading KML tree using LIBKML driver

Matthias Meulien orontee at gmail.com
Sun Nov 27 14:27:29 PST 2016


I am trying to recover the tree structure of KML folders while
reading  a dataset created from a .kml  or a .kmz file with the LIBKML
driver.

My idea is to loop through the dataset layers and maintain a mapping
from kmldom::ElementPtr to  layers

    std::map<kmldom::ElementPtr, OGRLayer*> parents;  

Each layer, say l1, is cast to OGRLIBKMLLayer; its associated
kmldom::ElementPtr is read using OGRLIBKMLLayer::GetKmlLayer() and a
search is performed for one of its ancestor in the parents mapping...

Is there a simpler way to recover the tree structure of KML files?

Until now I've only obtained partial results using this approach :-(
The following naive implementation works for dataset coming from a KML
file:

    for (int i = 0; i < dataset->GetLayerCount(); ++i) {
        auto* const layer = dataset->GetLayer(i);
        auto* const kmlLayer = static_cast<OGRLIBKMLLayer*>(layer);

        kmldom::ElementPtr element = kmlLayer->GetKmlLayer();
        kmldom::ElementPtr ancestor = kmlLayer.GetParent();

        std::map<kmldom::ElementPtr, OGRLayer*>::iterator s =
parents.find(ancestor);        while ((s == std::end(parents)) &&
ancestor) {
            kmldom::ElementPtr temp = ancestor->GetParent();           
   
            ancestor = temp;
            s = parents.find(ancestor);
        }
        if (s != std::end(parents)) {
            // Ok layer's parent is s->second
            ...
        } else {
            // layer is a root folder
            ...
        }
        parents[element] = layer;
    }

But it crashes on ancestor = temp when the source file is a KMZ file...
Any clue?
-- 
Matthias
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20161127/1d99c59b/attachment.html>


More information about the gdal-dev mailing list