How to add osgEarth Features at runtime?

1.5k views Asked by At

I'm playing around with osgEarth and while it's crazy easy to add features in the .earth file, I'm struggling to do it at runtime via the API. I want to let the user draw polygons on the map/globe so I need to be able to dynamically define geometry and styles based on user input.

Right now I'm just going for a static implementation to figure out what I need to do, but for the life of me I can't get anything to show up. Here is my sample code. I've already loaded a .earth file that defines the MapNode which is what I'm using here.

// Style
osgEarth::Symbology::Style shapeStyle;
shapeStyle.getOrCreate<osgEarth::Symbology::PolygonSymbol>()->fill()->color() = osgEarth::Symbology::Color::Green;

// Geometry
osgEarth::Symbology::Polygon* polygon = new osgEarth::Symbology::Polygon();
polygon->push_back(0, 0);
polygon->push_back(0, 10);
polygon->push_back(10, 10);

// Feature
osgEarth::Features::Feature* feature = new osgEarth::Features::Feature(polygon, mapNode->getMapSRS(), shapeStyle);

// Node
osgEarth::Annotation::FeatureNode* featureNode = new osgEarth::Annotation::FeatureNode(mapNode, feature);
featureNode->setStyle(shapeStyle);
featureNode->init();

mapNode->addChild(featureNode);

This should draw a green triangle near the middle of the map, but I don't see anything. Am I wrong in assuming that my polygon points are geo coordinates (lon, lat)? Is it wrong to just create my Style and Geometry on the fly like this? What am I doing wrong?

Update: This seems to work fine on a 3D (geocentric) map, but not on a 2D (projected) map which is what I'm after.

1

There are 1 answers

0
Moohasha On

After poking around a bit I stumbled upon the osgearth_features example that comes with the SDK which includes examples of creating features programatically. I followed the pattern from the sample and came up with something that works.

// Style
osgEarth::Symbology::Style shapeStyle;
osgEarth::Symbology::PolygonSymbol* fillStyle = shapeStyle.getOrCreate<osgEarth::Symbology::PolygonSymbol>();
fillStyle->fill()->color() = osgEarth::Symbology::Color::Green;
osgEarth::Symbology::LineSymbol* lineStyle = shapeStyle.getOrCreate<osgEarth::Symbology::LineSymbol>();
lineStyle->stroke()->color() = osgEarth::Symbology::Color::Black;
lineStyle->stroke()->width() = 2.0f;

// Geometry
osgEarth::Symbology::Polygon* polygon = new osgEarth::Symbology::Polygon();
polygon->push_back(0, 0, 10000);
polygon->push_back(0, 10, 10000);
polygon->push_back(10, 10, 10000);

// Feature Options (references the geometry)
osgEarth::Drivers::OGRFeatureOptions featureOptions;
featureOptions.geometry() = polygon;

// Model Options (references the feature options and style)
osgEarth::Drivers::FeatureGeomModelOptions geomOptions;
geomOptions.featureOptions() = featureOptions;
geomOptions.styles() = new osgEarth::StyleSheet();
geomOptions.styles()->addStyle( shapeStyle );
geomOptions.enableLighting() = false;

// Model Layer Options (created using the model options)
osgEarth::ModelLayerOptions layerOptions("test polygon", geomOptions);
mapNode->getMap()->addModelLayer(new osgEarth::ModelLayer(layerOptions));

Defining the style and geometry is more or less the same as what I was doing before (I added a line symbol this time), but in this case I'm adding a ModelLayer to the Map. That ModelLayer uses some model options that reference my style and geometry through the feature options.

I don't know if this is the best way to do it or how scalable it is (can I do this over and over thousands of times?), bit it's at least got me going,