Skip to content

Instantly share code, notes, and snippets.

@gwaldron
Last active April 13, 2024 09:16
Show Gist options
  • Save gwaldron/408c137e8a9c902c5df9f56fb0d3a1b7 to your computer and use it in GitHub Desktop.
Save gwaldron/408c137e8a9c902c5df9f56fb0d3a1b7 to your computer and use it in GitHub Desktop.
KML asynchronous load in osgEarth
#include <osgViewer/Viewer>
#include <osgEarthUtil/EarthManipulator>
#include <osgEarthUtil/ExampleResources>
#include <osgEarth/MapNode>
#include <osgEarth/ModelLayer>
#include <osgEarthDrivers/kml/KML>
#include <osg/ProxyNode>
using namespace osgEarth;
using namespace osgEarth::Util;
using namespace osgEarth::Drivers;
int main(int argc, char** argv)
{
osg::ArgumentParser arguments(&argc,argv);
osgViewer::Viewer viewer(arguments);
viewer.setCameraManipulator(new EarthManipulator());
osg::Node* node = MapNodeHelper().load(arguments, &viewer);
viewer.setSceneData( node );
MapNode* mapNode = MapNode::get(node);
std::string kmlFile("../data/KML_Samples.kml");
// Set up your KML defaults... optional but a good idea
KMLOptions kml_options;
kml_options.declutter() = true;
// Icon for point placemarks:
IconSymbol* defaultIcon = new IconSymbol();
defaultIcon->url()->setLiteral("../data/placemark32.png");
kml_options.defaultIconSymbol() = defaultIcon;
// Text appearance:
TextSymbol* defaultText = new TextSymbol();
defaultText->halo() = Stroke(0.3, 0.3, 0.3, 1.0);
kml_options.defaultTextSymbol() = defaultText;
// Pass the MapNode and your KML Options to the loader in an Options structure:
osgDB::Options* dbOptions = new osgDB::Options();
dbOptions->setPluginData("osgEarth::MapNode", mapNode);
dbOptions->setPluginData("osgEarth::KMLOptions", (void*)&kml_options);
// Set up a proxy node that will load asynchronously in a pager thread:
osg::ProxyNode* proxy = new osg::ProxyNode();
proxy->setFileName(0, kmlFile);
proxy->setLoadingExternalReferenceMode(proxy->DEFER_LOADING_TO_DATABASE_PAGER);
proxy->setDatabaseOptions(dbOptions);
// Install in the scene graph!
mapNode->addChild(proxy);
return viewer.run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment