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

How to correctly loading shp file by code

我正在尝试通过 OGRFeatureSource class 加载一个 .shp 文件,但它没有显示在场景中,但是当我加载 .earth 文件时,使用参数 osgearth_viewer,形状文件有效。

终端上正在打印任何错误。

我的代码基于我在网上找到的教程,但我认为这些是针对旧版本的(这些教程中的一些 class 不再有效。) 程序没有崩溃。

这是我的代码:

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;