osgEarth 2.1.1

/home/cube/sources/osgearth/src/applications/osgearth_features/osgearth_features.cpp

Go to the documentation of this file.
00001 /* -*-c++-*- */
00002 /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
00003 * Copyright 2008-2010 Pelican Mapping
00004 * http://osgearth.org
00005 *
00006 * osgEarth is free software; you can redistribute it and/or modify
00007 * it under the terms of the GNU Lesser General Public License as published by
00008 * the Free Software Foundation; either version 2 of the License, or
00009 * (at your option) any later version.
00010 *
00011 * This program is distributed in the hope that it will be useful,
00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 * GNU Lesser General Public License for more details.
00015 *
00016 * You should have received a copy of the GNU Lesser General Public License
00017 * along with this program.  If not, see <http://www.gnu.org/licenses/>
00018 */
00019 
00020 #include <osg/Notify>
00021 #include <osgGA/StateSetManipulator>
00022 #include <osgViewer/Viewer>
00023 #include <osgViewer/ViewerEventHandlers>
00024 #include <osgEarth/Map>
00025 #include <osgEarth/MapNode>
00026 #include <osgEarthUtil/EarthManipulator>
00027 #include <osgEarthUtil/AutoClipPlaneHandler>
00028 
00029 #include <osgEarthSymbology/Style>
00030 
00031 #include <osgEarthDrivers/gdal/GDALOptions>
00032 #include <osgEarthDrivers/feature_ogr/OGRFeatureOptions>
00033 #include <osgEarthDrivers/agglite/AGGLiteOptions>
00034 #include <osgEarthDrivers/model_feature_geom/FeatureGeomModelOptions>
00035 #include <osgEarthDrivers/model_feature_stencil/FeatureStencilModelOptions>
00036 
00037 using namespace osgEarth;
00038 using namespace osgEarth::Features;
00039 using namespace osgEarth::Drivers;
00040 using namespace osgEarth::Symbology;
00041 using namespace osgEarth::Util;
00042 
00043 //
00044 // NOTE: run this sample from the repo/tests directory.
00045 //
00046 int main(int argc, char** argv)
00047 {
00048     osg::ArgumentParser arguments(&argc,argv);
00049 
00050     bool useRaster  = arguments.read("--rasterize");
00051     bool useOverlay = arguments.read("--overlay");
00052     bool useStencil = arguments.read("--stencil");
00053     bool useMem     = arguments.read("--mem");
00054     bool useLabels  = arguments.read("--labels");
00055 
00056     osgViewer::Viewer viewer(arguments);
00057 
00058     // Start by creating the map:
00059     Map* map = new Map();
00060 
00061     // Start with a basemap imagery layer; we'll be using the GDAL driver
00062     // to load a local GeoTIFF file:
00063     GDALOptions basemapOpt;
00064     basemapOpt.url() = "../data/world.tif";
00065     map->addImageLayer( new ImageLayer( ImageLayerOptions("basemap", basemapOpt) ) );
00066 
00067     // Next we add a feature layer. First configure a feature driver to 
00068     // load the vectors from a shapefile:
00069     OGRFeatureOptions featureOpt;
00070     if ( !useMem )
00071     {
00072         featureOpt.url() = "../data/usa.shp";
00073     }
00074     else
00075     {
00076         Ring* line = new Ring();
00077         line->push_back( osg::Vec3d(-60, 20, 0) );
00078         line->push_back( osg::Vec3d(-120, 20, 0) );
00079         line->push_back( osg::Vec3d(-120, 60, 0) );
00080         line->push_back( osg::Vec3d(-60, 60, 0) );
00081         featureOpt.geometry() = line;
00082     }
00083 
00084     // Define a style for the feature data. Since we are going to render the
00085     // vectors as lines, configure the line symbolizer:
00086     Style style;
00087 
00088     LineSymbol* ls = style.getOrCreateSymbol<LineSymbol>();
00089     ls->stroke()->color() = osg::Vec4f( 1,1,0,1 ); // yellow
00090     ls->stroke()->width() = 2.0f;
00091 
00092     // Add some text labels.
00093     if ( useLabels )
00094     {
00095         TextSymbol* text = style.getOrCreateSymbol<TextSymbol>();
00096         text->provider() = "overlay";
00097         text->content() = StringExpression( "[name]" );
00098         text->priority() = NumericExpression( "[area]" );
00099         text->removeDuplicateLabels() = true;
00100         text->size() = 16.0f;
00101         text->fill()->color() = Color::White;
00102         text->halo()->color() = Color::DarkGray;
00103     }
00104 
00105     // That's it, the map is ready; now create a MapNode to render the Map:
00106     MapNodeOptions mapNodeOptions;
00107     mapNodeOptions.enableLighting() = false;
00108 
00109     MapNode* mapNode = new MapNode( map, mapNodeOptions );
00110 
00111     // Now we'll choose the AGG-Lite driver to render the features. By the way, the
00112     // feature data is actually polygons, so we override that to treat it as lines.
00113     // We apply the feature driver and set the style as well.
00114     if (useStencil)
00115     {
00116         FeatureStencilModelOptions worldOpt;
00117         worldOpt.featureOptions() = featureOpt;
00118         worldOpt.geometryTypeOverride() = Geometry::TYPE_LINESTRING;
00119         worldOpt.styles() = new StyleSheet();
00120         worldOpt.styles()->addStyle( style );
00121         worldOpt.enableLighting() = false;
00122         worldOpt.depthTestEnabled() = false;
00123         map->addModelLayer( new ModelLayer( "my features", worldOpt ) );
00124     }
00125     else if (useRaster)
00126     {
00127         AGGLiteOptions worldOpt;
00128         worldOpt.featureOptions() = featureOpt;
00129         worldOpt.geometryTypeOverride() = Geometry::TYPE_LINESTRING;
00130         worldOpt.styles() = new StyleSheet();
00131         worldOpt.styles()->addStyle( style );
00132         map->addImageLayer( new ImageLayer( ImageLayerOptions("world", worldOpt) ) );
00133     }
00134     else //if (useGeom || useOverlay)
00135     {
00136         FeatureGeomModelOptions worldOpt;
00137         worldOpt.featureOptions() = featureOpt;
00138         worldOpt.geometryTypeOverride() = Geometry::TYPE_LINESTRING;
00139         worldOpt.styles() = new StyleSheet();
00140         worldOpt.styles()->addStyle( style );
00141         worldOpt.enableLighting() = false;
00142         worldOpt.depthTestEnabled() = false;
00143 
00144         ModelLayerOptions options( "my features", worldOpt );
00145         options.overlay() = useOverlay;
00146         map->addModelLayer( new ModelLayer(options) );
00147     }
00148 
00149     viewer.setSceneData( mapNode );
00150     viewer.setCameraManipulator( new EarthManipulator() );
00151 
00152     if ( !useStencil && !useOverlay )
00153         viewer.addEventHandler( new osgEarth::Util::AutoClipPlaneHandler );
00154 
00155     // add some stock OSG handlers:
00156     viewer.addEventHandler(new osgViewer::StatsHandler());
00157     viewer.addEventHandler(new osgViewer::WindowSizeHandler());
00158     viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
00159 
00160     return viewer.run();
00161 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines