<html><head></head><body><div>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.</div><div><br></div><div>My idea is to loop through the dataset layers and maintain a mapping from <font face="monospace" size="3">kmldom::ElementPtr</font> to layers</div><div><br></div><div><font face="monospace" size="3"> std::map<kmldom::ElementPtr, OGRLayer*> parents; </font></div><div><br></div><div>Each layer, say l1, is cast to <font face="monospace" size="3">OGRLIBKMLLayer; </font>its associated <font face="monospace" size="3">kmldom::ElementPtr</font> is read using <font face="monospace" size="3">OGRLIBKMLLayer::GetKmlLayer()</font> and a search is performed for one of its ancestor in the <font face="monospace" size="3">parents</font> mapping...</div><div><br></div><div>Is there a simpler way to recover the tree structure of KML files?</div><div><br></div><div>Until now I've only obtained partial results using this approach :-( The following naive implementation works for dataset coming from a KML file:</div><div><br></div><div><font face="monospace" size="3"> for (int i = 0; i < dataset->GetLayerCount(); ++i) {</font></div><div><font face="monospace" size="3"> auto* const layer = dataset->GetLayer(i);</font></div><div><font face="monospace" size="3"> auto* const kmlLayer = static_cast<OGRLIBKMLLayer*>(layer);</font></div><div><br></div><div><pre></pre><pre> kmldom::ElementPtr element = kmlLayer->GetKmlLayer();</pre></div><div><font face="monospace" size="3"> kmldom::ElementPtr ancestor = kmlLayer.GetParent();</font></div><font face="monospace" size="3"><div><font face="monospace" size="3"><br></font></div> std::map<kmldom::ElementPtr, OGRLayer*>::iterator s = parents.find(ancestor);</font><div><font face="monospace" size="3"> while ((s == std::end(parents)) && ancestor) {</font></div><div><font face="monospace" size="3"> kmldom::ElementPtr temp = ancestor->GetParent(); </font></div><div><font face="monospace" size="3"> ancestor = temp;</font></div><div><font face="monospace" size="3"> s = parents.find(ancestor);</font></div><div><font face="monospace" size="3"> }</font></div><div><font face="monospace" size="3"> if (s != std::end(parents)) {</font></div><div><font face="monospace" size="3"> // Ok layer's parent is s->second</font></div><div><font face="monospace" size="3"> ...</font></div><div><font face="monospace" size="3"> } else {</font></div><div><font face="monospace" size="3"> // layer is a root folder</font></div><div><font face="monospace" size="3"> ...</font></div><div><font face="monospace" size="3"> }</font></div><div><font face="monospace" size="3"> parents[element] = layer;</font></div><div><font face="monospace" size="3"> }</font></div><div><br></div><div><div><div>But it crashes on <font face="monospace" size="3">ancestor = temp</font> when the source file is a KM<b>Z</b> file... Any clue?</div></div><div><span><pre><pre>-- <br></pre>Matthias</pre></span></div></div></body></html>