osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarthFeatures/FeatureModelSource.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 #include <osgEarthFeatures/FeatureModelSource>
00020 #include <osgEarthFeatures/FeatureModelGraph>
00021 #include <osgEarth/SpatialReference>
00022 #include <osg/Notify>
00023 #include <osg/Timer>
00024 #include <osg/LOD>
00025 #include <osg/ClusterCullingCallback>
00026 #include <osgUtil/Optimizer>
00027 
00028 using namespace osgEarth;
00029 using namespace osgEarth::Features;
00030 using namespace osgEarth::Symbology;
00031 
00032 #define LC "[FeatureModelSource] "
00033 
00034 //------------------------------------------------------------------------
00035 
00036 FeatureModelSourceOptions::FeatureModelSourceOptions( const ConfigOptions& options ) :
00037 ModelSourceOptions( options ),
00038 _geomTypeOverride( Geometry::TYPE_UNKNOWN ),
00039 _lit( true ),
00040 _maxGranularity_deg( 5.0 ),
00041 _mergeGeometry( false ),
00042 _clusterCulling( true )
00043 {
00044     fromConfig( _conf );
00045 }
00046 
00047 void
00048 FeatureModelSourceOptions::fromConfig( const Config& conf )
00049 {
00050     conf.getObjIfSet( "features", _featureOptions );
00051     //if ( conf.hasChild("features") )
00052     //    _featureOptions->merge( conf.child("features") );
00053     _featureSource = conf.getNonSerializable<FeatureSource>("feature_source");
00054 
00055     conf.getObjIfSet( "styles", _styles );
00056     conf.getObjIfSet( "layout", _levels );
00057     conf.getObjIfSet( "paging", _levels ); // backwards compat.. to be deprecated
00058     conf.getObjIfSet( "gridding", _gridding ); // to be deprecated
00059     conf.getObjIfSet( "feature_name", _featureNameExpr );
00060     conf.getIfSet( "lighting", _lit );
00061     conf.getIfSet( "max_granularity", _maxGranularity_deg );
00062     conf.getIfSet( "merge_geometry", _mergeGeometry );
00063     conf.getIfSet( "cluster_culling", _clusterCulling );
00064 
00065     std::string gt = conf.value( "geometry_type" );
00066     if ( gt == "line" || gt == "lines" || gt == "linestring" )
00067         _geomTypeOverride = Geometry::TYPE_LINESTRING;
00068     else if ( gt == "point" || gt == "pointset" || gt == "points" )
00069         _geomTypeOverride = Geometry::TYPE_POINTSET;
00070     else if ( gt == "polygon" || gt == "polygons" )
00071         _geomTypeOverride = Geometry::TYPE_POLYGON;
00072 }
00073 
00074 Config
00075 FeatureModelSourceOptions::getConfig() const
00076 {
00077     Config conf = ModelSourceOptions::getConfig();
00078 
00079     conf.updateObjIfSet( "features", _featureOptions );    
00080     if (_featureSource.valid())
00081     {
00082         conf.addNonSerializable("feature_source", _featureSource.get());
00083     }
00084     //conf.updateObjIfSet( "feature_source", _featureSource);
00085     conf.updateObjIfSet( "gridding", _gridding ); // to be deprecated
00086     conf.updateObjIfSet( "styles", _styles );
00087     conf.updateObjIfSet( "layout", _levels );
00088 
00089     conf.updateIfSet( "lighting", _lit );
00090     conf.updateIfSet( "max_granularity", _maxGranularity_deg );
00091     conf.updateIfSet( "merge_geometry", _mergeGeometry );
00092     conf.updateIfSet( "cluster_culling", _clusterCulling );
00093 
00094 
00095     if ( _geomTypeOverride.isSet() ) {
00096         if ( _geomTypeOverride == Geometry::TYPE_LINESTRING )
00097             conf.update( "geometry_type", "line" );
00098         else if ( _geomTypeOverride == Geometry::TYPE_POINTSET )
00099             conf.update( "geometry_type", "point" );
00100         else if ( _geomTypeOverride == Geometry::TYPE_POLYGON )
00101             conf.update( "geometry_type", "polygon" );
00102     }
00103 
00104     return conf;
00105 }
00106 
00107 //------------------------------------------------------------------------
00108 
00109 FeatureModelSource::FeatureModelSource( const FeatureModelSourceOptions& options ) :
00110 ModelSource( options ),
00111 _options( options )
00112 {
00113     // the data source from which to pull features:
00114     if ( _options.featureSource().valid() )
00115     {
00116         _features = _options.featureSource().get();
00117     }
00118     else if ( _options.featureOptions().isSet() )
00119     {
00120         _features = FeatureSourceFactory::create( _options.featureOptions().value() );
00121         if ( !_features.valid() )
00122         {
00123             OE_WARN << "FeatureModelSource - no valid feature source provided" << std::endl;
00124         }
00125     }
00126 }
00127 
00128 void
00129 FeatureModelSource::setFeatureSource( FeatureSource* source )
00130 {
00131     if ( !_features.valid() )
00132     {
00133         _features = source;
00134     }
00135     else
00136     {
00137         OE_WARN << LC << "Illegal: cannot set a feature source after one is already set" << std::endl;
00138     }
00139 }
00140 
00141 void 
00142 FeatureModelSource::initialize( const std::string& referenceURI, const osgEarth::Map* map )
00143 {
00144     ModelSource::initialize( referenceURI, map );
00145 
00146     if ( _features.valid() )
00147     {
00148         _features->initialize( referenceURI );
00149     }
00150     else
00151     {
00152         OE_WARN << LC << "No FeatureSource; nothing will be rendered (" << getName() << ")" << std::endl;
00153     }
00154 
00155     _map = map;
00156 }
00157 
00158 osg::Node*
00159 FeatureModelSource::createNode( ProgressCallback* progress )
00160 {
00161     if ( !_factory.valid() )
00162         _factory = createFeatureNodeFactory();
00163 
00164     if ( !_factory.valid() )
00165         return 0L;
00166 
00167     if ( !_features.valid() || !_features->getFeatureProfile() )
00168     {
00169         OE_WARN << LC << "Invalid feature source" << std::endl;
00170         return 0L;
00171     }
00172 
00173     Session* session = new Session( _map.get(), _options.styles().get() );
00174 
00175     FeatureModelGraph* graph = new FeatureModelGraph( 
00176         _features.get(), 
00177         _options, 
00178         _factory.get(),
00179         session );
00180 
00181     return graph;
00182 }
00183 
00184 //------------------------------------------------------------------------
00185 GeomFeatureNodeFactory::GeomFeatureNodeFactory( const GeometryCompilerOptions& options )
00186             : _options( options ) 
00187 { 
00188 }
00189 
00190 bool GeomFeatureNodeFactory::createOrUpdateNode(       
00191     FeatureCursor*            features,
00192     const Style&              style,
00193     const FilterContext&      context,
00194     osg::ref_ptr<osg::Node>&  node )
00195 {
00196     GeometryCompiler compiler( _options );
00197     node = compiler.compile( features, style, context );
00198     return node.valid();
00199 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines