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 00023 #include <osg/Notify> 00024 #include <osgDB/FileNameUtils> 00025 #include <osgDB/FileUtils> 00026 #include <osgDB/Registry> 00027 #include <osgDB/ReadFile> 00028 #include <osgDB/WriteFile> 00029 #include <sstream> 00030 00031 #include "YahooOptions" 00032 00033 using namespace osgEarth; 00034 using namespace osgEarth::Drivers; 00035 00036 class YahooSource : public TileSource 00037 { 00038 public: 00039 YahooSource( const TileSourceOptions& options ) : TileSource( options ), _options(options) 00040 { 00041 //nop 00042 } 00043 00044 // Yahoo! uses spherical mercator, but the top LOD is a 2x2 tile set. 00045 void initialize( const std::string& referenceURI, const Profile* overrideProfile) 00046 { 00047 setProfile( Profile::create( "spherical-mercator", "", 2, 2 ) ); 00048 } 00049 00050 osg::Image* createImage( const TileKey& key, 00051 ProgressCallback* progress ) 00052 { 00053 //Return NULL if we are given a non global-mercator key 00054 if ( !key.getProfile()->getProfileType() == Profile::TYPE_MERCATOR ) return 0; 00055 00056 std::stringstream buf; 00057 00058 std::string dataset = 00059 _options.dataset().isSet() ? _options.dataset().value() : "roads"; 00060 00061 if ( dataset == "roads" || dataset == "map" ) 00062 { 00063 // http://us.maps1.yimg.com/us.tile.maps.yimg.com/tl?v=4.1&md=2&x=0&y=0&z=2&r=1 00064 unsigned int tile_x, tile_y; 00065 key.getTileXY( tile_x, tile_y ); 00066 unsigned int zoom = key.getLevelOfDetail(); 00067 unsigned int size_x, size_y; 00068 key.getProfile()->getNumTiles( zoom, size_x, size_y ); 00069 00070 buf << "http://us.maps1.yimg.com/us.tile.maps.yimg.com/tl" 00071 << "?v=4.1&md=2&r=1" 00072 << "&x=" << (int)tile_x 00073 << "&y=" << ((int)size_y-1-(int)tile_y) - (int)size_y/2 00074 << "&z=" << zoom + 2; 00075 } 00076 else if ( dataset == "aerial" || dataset == "satellite" ) 00077 { 00078 unsigned int tile_x, tile_y; 00079 key.getTileXY( tile_x, tile_y ); 00080 unsigned int zoom = key.getLevelOfDetail(); 00081 unsigned int size_x, size_y; 00082 key.getProfile()->getNumTiles( zoom, size_x, size_y ); 00083 00084 buf << "http://us.maps3.yimg.com/aerial.maps.yimg.com/ximg" 00085 << "?v=1.8&s=256&t=a&r=1" 00086 << "&x=" << (int)tile_x 00087 << "&y=" << ((int)size_y-1-(int)tile_y) - (int)size_y/2 00088 << "&z=" << zoom + 2; 00089 } 00090 00091 std::string base; 00092 base = buf.str(); 00093 00094 OE_DEBUG << key.str() << "=" << base << std::endl; 00095 00096 osg::ref_ptr<osg::Image> image; 00097 HTTPClient::readImageFile( base, image, 0L, progress ); //getOptions(), progress ); 00098 return image.release(); 00099 } 00100 00101 osg::HeightField* createHeightField( const TileKey& key, 00102 ProgressCallback* progress) 00103 { 00104 //NI 00105 OE_WARN << "[Yahoo] Driver does not support heightfields" << std::endl; 00106 return NULL; 00107 } 00108 00109 virtual std::string getExtension() const 00110 { 00111 //All Yahoo tiles are in JPEG format 00112 return "jpg"; 00113 } 00114 00115 virtual bool supportsPersistentCaching() const 00116 { 00117 return false; 00118 } 00119 00120 private: 00121 const YahooOptions _options; 00122 }; 00123 00124 00125 class ReaderWriterYahoo : public TileSourceDriver 00126 { 00127 public: 00128 ReaderWriterYahoo() 00129 { 00130 supportsExtension( "osgearth_yahoo", "Yahoo maps data" ); 00131 } 00132 00133 virtual const char* className() 00134 { 00135 return "Yahoo Imagery ReaderWriter"; 00136 } 00137 00138 virtual ReadResult readObject(const std::string& file_name, const Options* options) const 00139 { 00140 if ( !acceptsExtension(osgDB::getLowerCaseFileExtension( file_name ))) 00141 return ReadResult::FILE_NOT_HANDLED; 00142 00143 return new YahooSource( getTileSourceOptions(options) ); 00144 } 00145 }; 00146 00147 REGISTER_OSGPLUGIN(osgearth_yahoo, ReaderWriterYahoo) 00148