osgEarth 2.1.1
|
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/TileSource> 00021 #include <osgEarth/Registry> 00022 #include <osgEarth/ImageToHeightFieldConverter> 00023 #include <osgEarth/FileUtils> 00024 00025 #include <osg/Notify> 00026 #include <osgDB/FileNameUtils> 00027 #include <osgDB/FileUtils> 00028 #include <osgDB/Registry> 00029 #include <osgDB/ReadFile> 00030 #include <osgDB/WriteFile> 00031 00032 #include "TileCacheOptions" 00033 00034 #include <sstream> 00035 00036 using namespace osgEarth; 00037 using namespace osgEarth::Drivers; 00038 00039 00040 class TileCacheSource : public TileSource 00041 { 00042 public: 00043 TileCacheSource( const TileSourceOptions& options ) : TileSource( options ), _options( options ) 00044 { 00045 } 00046 00047 void initialize( const std::string& referenceURI, const Profile* overrideProfile) 00048 { 00049 _configPath = referenceURI; 00050 00051 if (overrideProfile) 00052 { 00053 //If we were given a profile, take it on. 00054 setProfile(overrideProfile); 00055 } 00056 else 00057 { 00058 //Assume it is global-geodetic 00059 setProfile( osgEarth::Registry::instance()->getGlobalGeodeticProfile() ); 00060 } 00061 } 00062 00063 osg::Image* createImage( const TileKey& key, 00064 ProgressCallback* progress) 00065 { 00066 unsigned int level, tile_x, tile_y; 00067 level = key.getLevelOfDetail() +1; 00068 key.getTileXY( tile_x, tile_y ); 00069 00070 unsigned int numCols, numRows; 00071 key.getProfile()->getNumTiles(level, numCols, numRows); 00072 00073 // need to invert the y-tile index 00074 tile_y = numRows - tile_y - 1; 00075 00076 char buf[2048]; 00077 sprintf( buf, "%s/%s/%02d/%03d/%03d/%03d/%03d/%03d/%03d.%s", 00078 _options.url()->full().c_str(), 00079 _options.layer()->c_str(), 00080 level, 00081 (tile_x / 1000000), 00082 (tile_x / 1000) % 1000, 00083 (tile_x % 1000), 00084 (tile_y / 1000000), 00085 (tile_y / 1000) % 1000, 00086 (tile_y % 1000), 00087 _options.format()->c_str() ); 00088 00089 00090 std::string path = buf; 00091 00092 //If we have a relative path and the map file contains a server address, just concat the server path and the cache together. 00093 if (osgEarth::isRelativePath(path) && osgDB::containsServerAddress( _configPath ) ) 00094 { 00095 path = osgDB::getFilePath(_configPath) + std::string("/") + path; 00096 } 00097 00098 //If the path doesn't contain a server address, get the full path to the file. 00099 if (!osgDB::containsServerAddress(path)) 00100 { 00101 path = osgEarth::getFullPath(_configPath, path); 00102 } 00103 00104 osg::ref_ptr<osg::Image> image; 00105 HTTPClient::readImageFile(path, image, 0L, progress ); //getOptions(), progress ); 00106 return image.release(); 00107 00108 //if (osgDB::containsServerAddress(path)) 00109 //{ 00110 // //Use the HTTPClient if it's a server address. 00111 // return HTTPClient::readImageFile( path, getOptions(), progress ); 00112 //} 00113 00114 //return osgDB::readImageFile( path, getOptions() ); 00115 } 00116 00117 virtual std::string getExtension() const 00118 { 00119 return _options.format().value(); 00120 } 00121 00122 private: 00123 std::string _configPath; 00124 const TileCacheOptions _options; 00125 }; 00126 00127 // Reads tiles from a TileCache disk cache. 00128 class TileCacheSourceFactory : public TileSourceDriver 00129 { 00130 public: 00131 TileCacheSourceFactory() {} 00132 00133 virtual const char* className() 00134 { 00135 return "TileCache disk cache ReaderWriter"; 00136 } 00137 00138 virtual bool acceptsExtension(const std::string& extension) const 00139 { 00140 return osgDB::equalCaseInsensitive( extension, "osgearth_tilecache" ); 00141 } 00142 00143 virtual ReadResult readObject(const std::string& file_name, const Options* options) const 00144 { 00145 std::string ext = osgDB::getFileExtension( file_name ); 00146 if ( !acceptsExtension( ext ) ) 00147 { 00148 return ReadResult::FILE_NOT_HANDLED; 00149 } 00150 00151 return new TileCacheSource( getTileSourceOptions(options) ); 00152 } 00153 }; 00154 00155 REGISTER_OSGPLUGIN(osgearth_tilecache, TileCacheSourceFactory) 00156