c++openscenegraphosgearth

How to correctly loading shp file by code


I am trying to load a .shp file by OGRFeatureSource class, and it is not showing in the scene, but when I load .earth file, using osgearth_viewer by arguments, the shape file works.

Any errors are printing on terminal.

I based my code on tutorials that i found on web, but i think these are for an old version (some classes of these tutorials no longer work.) The program is not crashing.

Here is my code:

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

earth file:

    <!-- 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>


Solution

  • Your AltitudeSymbol also needs to specify the clamping:

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