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/ImageToHeightFieldConverter> 00022 #include <osgEarth/Registry> 00023 00024 #include <osgDB/FileNameUtils> 00025 #include <osgDB/FileUtils> 00026 #include <osgDB/Registry> 00027 #include <osgDB/ReadFile> 00028 #include <osgDB/WriteFile> 00029 00030 #include <sstream> 00031 #include <stdlib.h> 00032 #include <iomanip> 00033 00034 #include "TileServiceOptions" 00035 00036 using namespace osgEarth; 00037 using namespace osgEarth::Drivers; 00038 00039 //http://www.worldwindcentral.com/wiki/TileService 00040 class TileServiceSource : public TileSource 00041 { 00042 public: 00043 TileServiceSource( const TileSourceOptions& options ) : TileSource( options ), _options(options) 00044 { 00045 _formatToUse = 00046 _options.format()->empty() ? "png" : _options.format().value(); 00047 } 00048 00049 public: 00050 void initialize( const std::string& referenceURI, const Profile* overrideProfile) 00051 { 00052 //Take on the override profile if one is given. 00053 if (overrideProfile) 00054 { 00055 setProfile( overrideProfile ); 00056 } 00057 else 00058 { 00059 //Assume it is global geodetic 00060 setProfile( osgEarth::Registry::instance()->getGlobalGeodeticProfile() ); 00061 } 00062 } 00063 00064 public: 00065 osg::Image* createImage( const TileKey& key, ProgressCallback* progress) 00066 { 00067 osg::ref_ptr<osg::Image> image; 00068 HTTPClient::readImageFile( createURI( key ), image, 0L, progress ); //getOptions(), progress ); 00069 return image.release(); 00070 } 00071 00072 osg::HeightField* createHeightField( const TileKey& key, 00073 ProgressCallback* progress) 00074 { 00075 //NOP 00076 return NULL; 00077 } 00078 00079 std::string createURI( const TileKey& key ) const 00080 { 00081 unsigned int x, y; 00082 key.getTileXY(x, y); 00083 00084 unsigned int lod = key.getLevelOfDetail()+1; 00085 00086 std::stringstream buf; 00087 //http://s0.tileservice.worldwindcentral.com/getTile?interface=map&version=1&dataset=bmng.topo.bathy.200401&level=0&x=0&y=0 00088 buf << _options.url()->full() << "interface=map&version=1" 00089 << "&dataset=" << _options.dataset().value() 00090 << "&level=" << lod 00091 << "&x=" << x 00092 << "&y=" << y 00093 << "&." << _formatToUse; //Add this to trick osg into using the correct loader. 00094 std::string bufStr; 00095 bufStr = buf.str(); 00096 return bufStr; 00097 } 00098 00099 virtual std::string getExtension() const 00100 { 00101 return _formatToUse; 00102 } 00103 00104 private: 00105 std::string _formatToUse; 00106 const TileServiceOptions _options; 00107 }; 00108 00109 00110 class TileServiceSourceFactory : public TileSourceDriver 00111 { 00112 public: 00113 TileServiceSourceFactory() {} 00114 00115 virtual const char* className() 00116 { 00117 return "TileService Reader"; 00118 } 00119 00120 virtual bool acceptsExtension(const std::string& extension) const 00121 { 00122 return osgDB::equalCaseInsensitive( extension, "osgearth_tileservice" ); 00123 } 00124 00125 virtual ReadResult readObject(const std::string& file_name, const Options* opt) const 00126 { 00127 std::string ext = osgDB::getFileExtension( file_name ); 00128 if ( !acceptsExtension( ext ) ) 00129 { 00130 return ReadResult::FILE_NOT_HANDLED; 00131 } 00132 00133 return new TileServiceSource( getTileSourceOptions(opt) ); 00134 } 00135 }; 00136 00137 REGISTER_OSGPLUGIN(osgearth_tileservice, TileServiceSourceFactory) 00138