1

我正在玩 osgEarth,虽然在 .earth 文件中添加功能非常容易,但我在运行时通过 API 努力做到这一点。我想让用户在地图/地球上绘制多边形,所以我需要能够根据用户输入动态定义几何和样式。

现在我只是想通过一个静态实现来弄清楚我需要做什么,但对于我的生活来说,我什么也得不到。这是我的示例代码。我已经加载了一个 .earth 文件,它定义了我在这里使用的 MapNode。

// 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);

应该在地图中间附近绘制一个绿色三角形,但我什么也没看到。假设我的多边形点是地理坐标(经度,纬度),我错了吗?像这样动态创建我的样式和几何图形是错误的吗?我究竟做错了什么?

更新:这似乎在 3D(地心)地图上工作得很好,但在我所追求的 2D(投影)地图上却不行。

4

1 回答 1

1

经过一番探索,我偶然发现了 SDK 附带的osgearth_features示例,其中包括以编程方式创建功能的示例。我遵循示例中的模式并想出了一些可行的方法。

// 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));

定义样式和几何形状与我之前所做的或多或少相同(这次我添加了一个线符号),但在这种情况下,我将一个 ModelLayer 添加到地图中。该 ModelLayer 使用了一些模型选项,这些选项通过功能选项引用了我的样式和几何图形。

我不知道这是否是最好的方法,或者它的可扩展性如何(我可以一遍又一遍地这样做吗?),它至少让我前进,

于 2018-11-17T17:10:48.687 回答