osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarth/TileSource

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 #ifndef OSGEARTH_TILE_SOURCE_H
00021 #define OSGEARTH_TILE_SOURCE_H 1
00022 
00023 #include <limits.h>
00024 
00025 #include <osg/Version>
00026 
00027 #include <osgEarth/Common>
00028 #include <osgEarth/TileKey>
00029 #include <osgEarth/Profile>
00030 #include <osgEarth/Caching>
00031 #include <osgEarth/Progress>
00032 #include <osgEarth/ThreadingUtils>
00033 
00034 #include <osg/Referenced>
00035 #include <osg/Object>
00036 #include <osg/Image>
00037 #include <osg/Shape>
00038 #if OSG_MIN_VERSION_REQUIRED(2,9,5)
00039 #include <osgDB/Options>
00040 #endif
00041 #include <osgDB/ReadFile>
00042 
00043 #include <OpenThreads/Mutex>
00044 
00045 #include <string>
00046 
00047 
00048 #define TILESOURCE_CONFIG "tileSourceConfig"
00049 
00050 
00051 namespace osgEarth
00052 {   
00056     class TileSourceOptions : public DriverConfigOptions // no export; header only
00057     {
00058     public:
00059         
00060         optional<int>& tileSize() { return _tileSize; }
00061         const optional<int>& tileSize() const { return _tileSize; }
00062 
00063         optional<float>& noDataValue() { return _noDataValue; }
00064         const optional<float>& noDataValue() const { return _noDataValue; }
00065 
00066         optional<float>& noDataMinValue() { return _noDataMinValue; }
00067         const optional<float>& noDataMinValue() const { return _noDataMinValue; }
00068 
00069         optional<float>& noDataMaxValue() { return _noDataMaxValue; }
00070         const optional<float>& noDataMaxValue() const { return _noDataMaxValue; }
00071 
00072         optional<std::string>& blacklistFilename() { return _blacklistFilename; }
00073         const optional<std::string>& blacklistFilename() const { return _blacklistFilename; }
00074 
00075         optional<ProfileOptions>& profile() { return _profileOptions; }
00076         const optional<ProfileOptions>& profile() const { return _profileOptions; }
00077 
00078         optional<int>& L2CacheSize() { return _L2CacheSize; }
00079         const optional<int>& L2CacheSize() const { return _L2CacheSize; }
00080 
00081     public:
00082         TileSourceOptions( const ConfigOptions& options =ConfigOptions() )
00083             : DriverConfigOptions( options ),
00084               _tileSize( 256 ),
00085               _noDataValue( (float)SHRT_MIN ),
00086               _noDataMinValue( -FLT_MAX ),
00087               _noDataMaxValue( FLT_MAX ),
00088               _L2CacheSize( 16 )
00089         { 
00090             fromConfig( _conf );
00091         }
00092 
00093     public:
00094         virtual Config getConfig() const {
00095             Config conf = DriverConfigOptions::getConfig();
00096             conf.updateIfSet( "tile_size", _tileSize );
00097             conf.updateIfSet( "nodata_value", _noDataValue );
00098             conf.updateIfSet( "nodata_min", _noDataMinValue );
00099             conf.updateIfSet( "nodata_max", _noDataMaxValue );
00100             conf.updateIfSet( "blacklist_filename", _blacklistFilename);
00101             //conf.updateIfSet( "enable_l2_cache", _enableL2Cache );
00102             conf.updateIfSet( "l2_cache_size", _L2CacheSize );
00103             conf.updateObjIfSet( "profile", _profileOptions );
00104             return conf;
00105         }
00106 
00107     protected:
00108         virtual void mergeConfig( const Config& conf ) {
00109             DriverConfigOptions::mergeConfig( conf );
00110             fromConfig( conf );
00111         }
00112 
00113     private:
00114         void fromConfig( const Config& conf ) {
00115             conf.getIfSet( "tile_size", _tileSize );
00116             conf.getIfSet( "nodata_value", _noDataValue );
00117             conf.getIfSet( "nodata_min", _noDataMinValue );
00118             conf.getIfSet( "nodata_max", _noDataMaxValue );
00119             conf.getIfSet( "blacklist_filename", _blacklistFilename);
00120             //conf.getIfSet( "enable_l2_cache", _enableL2Cache );
00121             conf.getIfSet( "l2_cache_size", _L2CacheSize );
00122             conf.getObjIfSet( "profile", _profileOptions );
00123 
00124             // special handling of default tile size:
00125             if ( !tileSize().isSet() )
00126                 conf.getIfSet( "default_tile_size", _tileSize );
00127             // remove it now so it does not get serialized
00128             _conf.remove( "default_tile_size" );
00129         }
00130 
00131         optional<int> _tileSize;
00132         optional<float> _noDataValue, _noDataMinValue, _noDataMaxValue;
00133         optional<ProfileOptions> _profileOptions;
00134         optional<std::string> _blacklistFilename;
00135         optional<int> _L2CacheSize;
00136         //optional<bool> _enableL2Cache;
00137     };
00138 
00139     typedef std::vector<TileSourceOptions> TileSourceOptionsVector;
00140 
00144     class OSGEARTH_EXPORT TileBlacklist : public virtual osg::Referenced
00145     {
00146     public:
00150         TileBlacklist();
00151 
00155         void add(const osgTerrain::TileID &tile);
00156 
00160         void remove(const osgTerrain::TileID& tile);
00161 
00165         void clear();
00166 
00170         bool contains(const osgTerrain::TileID &tile) const;
00171 
00175         unsigned int size() const;
00176 
00180         static TileBlacklist* read(std::istream &in);
00181 
00185         static TileBlacklist* read(const std::string &filename);
00186 
00190         void write(std::ostream &output) const;
00191 
00195         void write(const std::string &filename) const;
00196 
00197     private:
00198         typedef std::set< osgTerrain::TileID > BlacklistedTiles;
00199         BlacklistedTiles _tiles;
00200         osgEarth::Threading::ReadWriteMutex _mutex;
00201     };
00202 
00208     class OSGEARTH_EXPORT TileSource : public virtual osg::Object
00209     {
00210     public:
00211         struct ImageOperation : public osg::Referenced {
00212             virtual void operator()( osg::ref_ptr<osg::Image>& in_out_image ) =0;
00213         };
00214 
00215         struct HeightFieldOperation : public osg::Referenced {
00216             virtual void operator()( osg::ref_ptr<osg::HeightField>& in_out_hf ) =0;
00217         };
00218 
00219     public:        
00220         TileSource( const TileSourceOptions& options =TileSourceOptions() );
00221 
00225         virtual int getPixelsPerTile() const;   
00226 
00230         const DataExtentList& getDataExtents() const { return _dataExtents; }
00231         DataExtentList& getDataExtents() { return _dataExtents; }
00232 
00236         virtual osg::Image* createImage(
00237             const TileKey& key,
00238             ImageOperation* op =0L,
00239             ProgressCallback* progress =0L );
00240 
00244         virtual osg::HeightField* createHeightField(
00245             const TileKey& key,
00246             HeightFieldOperation* prepOp =0L,
00247             ProgressCallback* progress = 0L );     
00248 
00249     public:
00250 
00255         virtual bool isOK() const;
00256         bool isValid() const { return isOK(); }
00257 
00261         virtual const Profile* getProfile() const;
00262 
00266         virtual float getNoDataValue() {
00267             return _options.noDataValue().value(); }
00268 
00272         virtual float getNoDataMinValue() {
00273             return _options.noDataMinValue().value(); }
00274 
00278         virtual float getNoDataMaxValue() {
00279             return _options.noDataMaxValue().value(); }
00280 
00287         virtual unsigned int getMaxDataLevel() const;
00288 
00295         virtual unsigned int getMinDataLevel() const;
00296 
00300         virtual bool supportsPersistentCaching() const;
00301 
00305         virtual std::string getExtension() const {return "png";}
00306 
00310         TileBlacklist* getBlacklist();
00311         const TileBlacklist* getBlacklist() const;     
00312 
00316         virtual bool hasData(const TileKey& key) const;
00317 
00321         virtual bool hasDataAtLOD( unsigned lod ) const;
00322 
00326         virtual bool hasDataInExtent( const GeoExtent& extent ) const;
00327 
00332         virtual bool isDynamic() const { return false; }
00333 
00334 
00335         virtual osg::Object* cloneType() const { return 0; } // cloneType() not appropriate
00336         virtual osg::Object* clone(const osg::CopyOp&) const { return 0; } // clone() not appropriate
00337         virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const TileSource*>(obj)!=NULL; }
00338         virtual const char* className() const { return "TileSource"; }
00339         virtual const char* libraryName() const { return "osgEarth"; }
00340 
00344                 virtual void initialize( const std::string& referenceURI, const Profile* overrideProfile = NULL) = 0;
00345 
00346         const TileSourceOptions& getOptions() const {
00347             return _options; }
00348    
00349     protected:
00350 
00351         virtual ~TileSource();
00352 
00357                 virtual osg::Image* createImage( const TileKey& key, ProgressCallback* progress ) = 0;
00358 
00363         virtual osg::HeightField* createHeightField( const TileKey& key, ProgressCallback* progress );
00364 
00368                 void setProfile( const Profile* profile );
00369 
00370     private:
00371         osg::ref_ptr<const Profile> _profile;
00372         const TileSourceOptions _options;
00373 
00374         friend class Map;
00375         friend class MapEngine;
00376         friend class TileSourceFactory;
00377 
00378         osg::ref_ptr< TileBlacklist > _blacklist;
00379         std::string _blacklistFilename;
00380 
00381                 osg::ref_ptr<MemCache> _memCache;
00382 
00383         DataExtentList _dataExtents;
00384         //osg::ref_ptr< RTree<unsigned int> > _dataExtentsIndex;
00385     };
00386 
00387     
00388     typedef std::vector< osg::ref_ptr<TileSource> > TileSourceVector;
00389 
00390     //--------------------------------------------------------------------
00391 
00392     class OSGEARTH_EXPORT TileSourceDriver : public osgDB::ReaderWriter
00393     {
00394     protected:
00395         const TileSourceOptions& getTileSourceOptions( const osgDB::ReaderWriter::Options* rwOpt ) const;
00396     };
00397 
00398     //--------------------------------------------------------------------
00399 
00404     class OSGEARTH_EXPORT TileSourceFactory
00405     {   
00406         public:
00407         static TileSource* create( const TileSourceOptions& options );
00408     };
00409 }
00410 
00411 #endif // OSGEARTH_TILE_SOURCE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines