osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarthDrivers/mask_feature/FeatureMaskSource.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 <osgEarth/MaskSource>
00021 #include <osgEarth/Registry>
00022 #include <osgEarth/Map>
00023 #include <osgEarthFeatures/TransformFilter>
00024 #include <osgEarthFeatures/FeatureSource>
00025 
00026 #include <osgDB/FileNameUtils>
00027 #include <OpenThreads/Mutex>
00028 #include <OpenThreads/ScopedLock>
00029 
00030 #include "FeatureMaskOptions"
00031 
00032 #define LC "[FeatureMaskDriver] "
00033 
00034 using namespace osgEarth;
00035 using namespace osgEarth::Features;
00036 using namespace osgEarth::Drivers;
00037 
00038 //------------------------------------------------------------------------
00039 
00040 class FeatureMaskSource : public MaskSource
00041 {
00042 public:
00043     FeatureMaskSource( const MaskSourceOptions& options )
00044         : MaskSource( options ), _options( options ), _failed( false )
00045     {
00046         // the data source from which to pull features:
00047         if ( _options.featureSource().valid() )
00048         {
00049             _features = _options.featureSource().get();
00050         }
00051         else if ( _options.featureOptions().isSet() )
00052         {
00053             _features = FeatureSourceFactory::create( _options.featureOptions().value() );
00054             if ( !_features.valid() )
00055             {
00056                 OE_WARN << "FeatureModelSource - no valid feature source provided" << std::endl;
00057             }
00058         }
00059     }
00060 
00061     const MaskSourceOptions& getOptions() const { return _options; }
00062 
00063     //override
00064     void initialize( const std::string& referenceURI, const osgEarth::Map* map )
00065     {
00066         MaskSource::initialize( referenceURI, map );
00067 
00068         if ( _features.valid() )
00069         {
00070             _features->initialize( referenceURI );
00071         } 
00072         else
00073         {
00074             OE_WARN << LC << "No FeatureSource; nothing will be rendered (" << getName() << ")" << std::endl;
00075         }
00076     }
00077 
00078     osg::Vec3dArray* createBoundary( const SpatialReference* srs, ProgressCallback* progress )
00079     {
00080         if ( _failed )
00081             return 0L;
00082 
00083         if ( _features.valid() )
00084         {
00085             if ( _features->getFeatureProfile() )
00086             {
00087                 osg::ref_ptr<FeatureCursor> cursor = _features->createFeatureCursor();
00088                 if ( cursor )
00089                 {
00090                     if ( cursor->hasMore() )
00091                     {
00092                         Feature* f = cursor->nextFeature();
00093                         if ( f && f->getGeometry() )
00094                         {
00095                             // Init a filter to tranform feature in desired SRS 
00096                             if (!srs->isEquivalentTo(_features->getFeatureProfile()->getSRS())) {
00097                                 FilterContext cx;
00098                                 cx.profile() = new FeatureProfile(_features->getFeatureProfile()->getExtent());
00099                                 //cx.isGeocentric() = _features->getFeatureProfile()->getSRS()->isGeographic();
00100 
00101                                 TransformFilter xform( srs );
00102                                 FeatureList featureList;
00103                                 featureList.push_back(f);
00104                                 cx = xform.push(featureList, cx);
00105                             }
00106 
00107                             return f->getGeometry()->toVec3dArray();
00108                         }
00109                     }
00110                 }
00111             }
00112             else
00113             {
00114                 OE_WARN << LC << "Failed to create boundary; feature source has no SRS" << std::endl;
00115                 _failed = true;
00116             }
00117         }
00118         else
00119         {
00120             OE_WARN << LC << "Unable to create boundary; invalid feature source" << std::endl;
00121             _failed = true;
00122         }
00123         return 0L;
00124     }
00125 
00126 private:
00127     bool _failed;
00128     const FeatureMaskOptions _options;
00129     osg::ref_ptr<FeatureSource> _features;
00130 };
00131 
00132 //------------------------------------------------------------------------
00133 
00134 class FeatureMaskDriver : public MaskSourceDriver
00135 {
00136 public:
00137     FeatureMaskDriver()
00138     {
00139         supportsExtension( "osgearth_mask_feature", "osgEarth feature mask plugin" );
00140     }
00141 
00142     virtual const char* className()
00143     {
00144         return "osgEarth Feature Mask Plugin";
00145     }
00146 
00147     virtual ReadResult readObject(const std::string& file_name, const Options* options) const
00148     {
00149         if ( !acceptsExtension(osgDB::getLowerCaseFileExtension( file_name )))
00150             return ReadResult::FILE_NOT_HANDLED;
00151 
00152         return new FeatureMaskSource( getMaskSourceOptions(options) );
00153     }
00154 };
00155 
00156 REGISTER_OSGPLUGIN(osgearth_mask_feature, FeatureMaskDriver)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines