如何通过代码正确加载shp文件



我正试图通过OGRFeatureSource类加载.shp文件,但它没有显示在场景中,但当我使用osgearth_viewer by参数加载.eearth文件时,形状文件可以工作。

任何错误都将在终端上打印。

我的代码基于我在网上找到的教程,但我认为这些是旧版本的(这些教程的某些类已经不起作用了(程序没有崩溃。

这是我的代码:

osg::Node* node = osgEarth::Contrib::MapNodeHelper().load(arguments, &viewer);
if (node)
{
osgEarth::Contrib::MapNode* mapNode = osgEarth::Contrib::MapNode::findMapNode(node);
std::string shpFile =
"/usr/local/muse/airtraficcontrol/ATC/airtrafficcontrol/data/osg_earth/world.shp";
osgEarth::OGRFeatureSource* featureSrc = new osgEarth::OGRFeatureSource();
featureSrc->setName("teste");
featureSrc->setURL( shpFile );
osgEarth::Contrib::Style earthStyle;
osgEarth::LineSymbol* ls = earthStyle.getOrCreateSymbol<osgEarth::LineSymbol>();
ls->stroke()->color() = {1, 1, 0.3, 1};
ls->stroke()->width() = 1;
ls->tessellationSize()->set(100, osgEarth::Contrib::Units::KILOMETERS);
ls->stroke()->widthUnits() = osgEarth::Units::METERS;
ls->stroke()->smooth() = true;
earthStyle.getOrCreateSymbol<osgEarth::RenderSymbol>()->depthOffset()->enabled() = true;
osgEarth::AltitudeSymbol* alt = earthStyle.getOrCreate<osgEarth::AltitudeSymbol>();
alt->technique() = alt->TECHNIQUE_DRAPE;
osgEarth::FeatureDisplayLayout layout;
layout.tileSize() = 650;
osgEarth::FeatureModelLayer* fml = new osgEarth::FeatureModelLayer();
fml->setName( "layerName" );
fml->setFeatureSource( featureSrc );
fml->options().layout() = layout;
fml->setStyleSheet( new osgEarth::StyleSheet() );
fml->getStyleSheet()->addStyle( earthStyle );
mapNode->getMap()->addLayer( fml );
viewer.setSceneData(node);
}

地球文件:

<!-- flat -->
<options>
<profile>plate-carre</profile>
</options>

<!--    <image driver="gdal">
<url>./world.tif</url>
</image>-->
<!--
<GDALImage name="world">
<url>./world.tif</url>
</GDALImage>-->
<!--    <FeatureModel name="world_boundaries" features="world-data">
<styles>
<style type="text/css">
world {
stroke:             #ffffed;
stroke-width:       4px;
altitude-clamping:  terrain-drape;
}            
</style>
</styles>
</FeatureModel>
-->
<terrainshader>
<code>
<![CDATA[
#version 330
#pragma vp_entryPoint colorize

void colorize(inout vec4 color) {

color.rgb = vec3(0.192156, 0.20392, 0.22352);
}
]]>
</code>
</terrainshader>


<Viewpoints home="0" time="0">
<viewpoint>
<!--             <heading>15.2667</heading> -->
<pitch>-90</pitch>
<range>5583.51m</range>
<lat>45.504275</lat>
<long>12.339304</long>
<height>34720.802966509946</height>
<srs>+proj=longlat +datum=WGS84 +no_defs </srs>
</viewpoint>
</Viewpoints>

<!--     <DebugImage/> -->
<!--    <OGRFeatures name="world-data">
<url>./world.shp</url>
<url>/usr/local/muse/airtraficcontrol/ATC/airtrafficcontrol/data/osg_earth/world.shp</url>
</OGRFeatures>-->
</map>

您的AltitudeSymbol还需要指定夹紧:

alt->clamping() = alt->CLAMP_TO_TERRAIN;

最新更新