zoukankan      html  css  js  c++  java
  • [原][OE][官方例子]osgearth_features OE地球添加shp文件(特征标识)

    OE所有官方样例

    官方示例代码

    /* -*-c++-*- */
    /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
    * Copyright 2016 Pelican Mapping
    * http://osgearth.org
    *
    * osgEarth is free software; you can redistribute it and/or modify
    * it under the terms of the GNU Lesser General Public License as published by
    * the Free Software Foundation; either version 2 of the License, or
    * (at your option) any later version.
    *
    * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    * IN THE SOFTWARE.
    *
    * You should have received a copy of the GNU Lesser General Public License
    * along with this program.  If not, see <http://www.gnu.org/licenses/>
    */
    
    #include <osg/Notify>
    #include <osgGA/StateSetManipulator>
    #include <osgViewer/Viewer>
    #include <osgViewer/ViewerEventHandlers>
    #include <osgEarth/Map>
    #include <osgEarth/MapNode>
    #include <osgEarth/ImageLayer>
    #include <osgEarth/ModelLayer>
    #include <osgEarthUtil/ExampleResources>
    #include <osgEarthUtil/EarthManipulator>
    #include <osgEarthUtil/AutoClipPlaneHandler>
    
    #include <osgEarthSymbology/Style>
    #include <osgEarthFeatures/FeatureModelLayer>
    #include <osgEarthFeatures/ConvertTypeFilter>
    
    #include <osgEarthDrivers/engine_rex/RexTerrainEngineOptions>
    #include <osgEarthDrivers/gdal/GDALOptions>
    #include <osgEarthDrivers/feature_ogr/OGRFeatureOptions>
    #include <osgEarthDrivers/agglite/AGGLiteOptions>
    #include <osgEarthDrivers/model_feature_geom/FeatureGeomModelOptions>
    
    #include <osgDB/WriteFile>
    
    using namespace osgEarth;
    using namespace osgEarth::Features;
    using namespace osgEarth::Drivers;
    using namespace osgEarth::Symbology;
    using namespace osgEarth::Util;
    
    int usage(const std::string& app)
    {
        OE_NOTICE "
    " << app << "
    "
            << "    --rasterize           : draw features as rasterized image tiles 
    "
            << "    --overlay             : draw features as projection texture 
    "
            << "    --mem                 : load features from memory 
    "
            << "    --labels              : add feature labels 
    "
            << "
    "
            << MapNodeHelper().usage();
    
        return -1;
    }
    
    //
    // NOTE: run this sample from the repo/tests directory.
    //
    int main(int argc, char** argv)
    {
        osg::ArgumentParser arguments(&argc, argv);
    
        if (arguments.read("--help"))
            return usage(argv[0]);
    
        bool useRaster = arguments.read("--rasterize");
        bool useOverlay = arguments.read("--overlay");
        bool useMem = arguments.read("--mem");
        bool useLabels = arguments.read("--labels");
    
    
        osgViewer::Viewer viewer(arguments);
    
        // Start by creating the map:
        Map* map = new Map();
    
        // Start with a basemap imagery layer; we'll be using the GDAL driver
        // to load a local GeoTIFF file:
        GDALOptions basemap;
        basemap.url() = "E:/temp/OE/data/world.tif";
        map->addLayer(new ImageLayer(ImageLayerOptions("basemap", basemap)));
    
        // Next we add a feature layer. 
        OGRFeatureOptions ogrData;
        if (!useMem)
        {
            // Configures the feature driver to load the vectors from a shapefile:
            ogrData.url() = "E:/temp/OE/data/world.shp";
        }
        else
        {
            // the --mem options tells us to just make an in-memory geometry:
            Ring* line = new Ring();
            line->push_back(osg::Vec3d(-60, 20, 0));
            line->push_back(osg::Vec3d(-120, 20, 0));
            line->push_back(osg::Vec3d(-120, 60, 0));
            line->push_back(osg::Vec3d(-60, 60, 0));
            ogrData.geometry() = line;
        }
    
        // Make a feature source layer and add it to the Map:
        FeatureSourceLayerOptions ogrLayer;
        ogrLayer.name() = "vector-data";
        ogrLayer.featureSource() = ogrData;
        map->addLayer(new FeatureSourceLayer(ogrLayer));
    
        // Define a style for the feature data. Since we are going to render the
        // vectors as lines, configure the line symbolizer:
        Style style;
    
        LineSymbol* ls = style.getOrCreateSymbol<LineSymbol>();
        ls->stroke()->color() = Color::Yellow;
        ls->stroke()->width() = 2.0f;
    
        // That's it, the map is ready; now create a MapNode to render the Map:
        osgEarth::Drivers::RexTerrainEngine::RexTerrainEngineOptions rex;
    
        MapNodeOptions mapNodeOptions;
        mapNodeOptions.enableLighting() = false;
        mapNodeOptions.setTerrainOptions(rex);
        MapNode* mapNode = new MapNode(map, mapNodeOptions);
    
        osg::Group* root = new osg::Group();
        root->addChild(mapNode);
        viewer.setSceneData(root);
        viewer.setCameraManipulator(new EarthManipulator());
    
        // Process cmdline args
        MapNodeHelper().parse(mapNode, arguments, &viewer, root, new LabelControl("Features Demo"));
    
        if (useRaster)
        {
            AGGLiteOptions rasterOptions;
            rasterOptions.featureOptions() = ogrData;
            rasterOptions.styles() = new StyleSheet();
            rasterOptions.styles()->addStyle(style);
            map->addLayer(new ImageLayer("My Features", rasterOptions));
        }
        else //if (useGeom || useOverlay)
        {
            FeatureModelLayerOptions fml;
            fml.name() = "My Features";
            fml.featureSourceLayer() = "vector-data";
            fml.styles() = new StyleSheet();
            fml.styles()->addStyle(style);
            fml.enableLighting() = false;
    
            map->addLayer(new FeatureModelLayer(fml));
        }
    
        if (useLabels)
        {
            // set up symbology for drawing labels. We're pulling the label
            // text from the name attribute, and its draw priority from the
            // population attribute.
            Style labelStyle;
    
            TextSymbol* text = labelStyle.getOrCreateSymbol<TextSymbol>();
            text->content() = StringExpression("[cntry_name]");
            text->priority() = NumericExpression("[pop_cntry]");
            text->size() = 16.0f;
            text->alignment() = TextSymbol::ALIGN_CENTER_CENTER;
            text->fill()->color() = Color::White;
            text->halo()->color() = Color::DarkGray;
    
            // and configure a model layer:
            FeatureModelLayerOptions fml;
            fml.name() = "Labels";
            fml.featureSourceLayer() = "vector-data";
            //fml.featureSource() = featureOptions;
            fml.styles() = new StyleSheet();
            fml.styles()->addStyle(labelStyle);
    
            map->addLayer(new FeatureModelLayer(fml));
        }
    
    
        // add some stock OSG handlers:
        viewer.addEventHandler(new osgViewer::StatsHandler());
        viewer.addEventHandler(new osgViewer::WindowSizeHandler());
        viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
    
    
        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
        traits->x = 200;
        traits->y = 200;
        traits->width = 800;
        traits->height = 680;
        traits->windowDecoration = true;
        traits->doubleBuffer = true;
        traits->sharedContext = 0;
    
        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
    
        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
        camera->setGraphicsContext(gc.get());
        camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));
        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
        camera->setDrawBuffer(buffer);
        camera->setReadBuffer(buffer);
        viewer.addSlave(camera);
    
        return viewer.run();
    }

    开启"--labels"  标注的图元信息,这些是读取shp中的字段加载的!

            Ring* line = new Ring();
            line->push_back(osg::Vec3d(-60, 20, 0));
            line->push_back(osg::Vec3d(-120, 20, 0));
            line->push_back(osg::Vec3d(-120, 60, 0));
            line->push_back(osg::Vec3d(-60, 60, 0));
            ogrData.geometry() = line;

    开启"--mem"代码绘制

    开启"--mem"代码绘制

    开启栅格化"--rasterize"

  • 相关阅读:
    mysql 7.5.8 服务无法启动 服务没有报告任何错误
    Ubuntu 16.04 php卸载
    函数式编程(3)-匿名函数
    函数式编程(2)-返回函数
    函数式编程(1)-高阶变成(3)-sorted
    函数式编程(1)-高阶变成(2)-filter
    函数式编程(1)-高阶变成(1)-map/reduce
    高级特性(4)-生成器
    高级特性(3)-列表生成式
    高级特性(2)-迭代
  • 原文地址:https://www.cnblogs.com/lyggqm/p/11322584.html
Copyright © 2011-2022 走看看