bool HTTPClient::doDownload(const std::string& url, const std::string& filename) { initialize(); // download the data HTTPResponse response = this->doGet( HTTPRequest(url) ); if ( response.isOK() ) { unsigned int part_num = response.getNumParts() > 1? 1 : 0; std::istream& input_stream = response.getPartStream( part_num ); std::ofstream fout; fout.open(filename.c_str(), std::ios::out | std::ios::binary); input_stream.seekg (0, std::ios::end); int length = input_stream.tellg(); input_stream.seekg (0, std::ios::beg); char *buffer = new char[length]; input_stream.read(buffer, length); fout.write(buffer, length); delete[] buffer; fout.close(); return true; } else { OE_WARN << LC << "Error downloading file " << filename << " (" << response.getCode() << ")" << std::endl; return false; } }
bool EarthFile::readXML( const std::string& location ) { bool success = false; if ( osgDB::containsServerAddress( location ) ) { HTTPResponse response = HTTPClient::get( location ); if ( response.isOK() && response.getNumParts() > 0 ) { success = readXML( response.getPartStream( 0 ), location ); } } else { if (osgDB::fileExists(location) && (osgDB::fileType(location) == osgDB::REGULAR_FILE)) { std::ifstream in( location.c_str() ); success = readXML( in, location ); } } if ( success ) { std::string filename = location; if (!osgDB::containsServerAddress(filename)) { filename = osgDB::getRealPath( location ); } _map->setReferenceURI( filename ); } return success; }
osg::Image* WCS11Source::createImage(const TileKey& key, ProgressCallback* progress) { HTTPRequest request = createRequest( key ); OE_INFO << "[osgEarth::WCS1.1] Key=" << key.str() << " URL = " << request.getURL() << std::endl; double lon0,lat0,lon1,lat1; key.getExtent().getBounds( lon0, lat0, lon1, lat1 ); // download the data. It's a multipart-mime stream, so we have to use HTTP directly. HTTPResponse response = HTTPClient::get( request, _dbOptions.get(), progress ); if ( !response.isOK() ) { OE_WARN << "[osgEarth::WCS1.1] WARNING: HTTP request failed" << std::endl; return NULL; } //TODO: Make WCS driver use progress callback unsigned int part_num = response.getNumParts() > 1? 1 : 0; std::istream& input_stream = response.getPartStream( part_num ); //TODO: un-hard-code TIFFs osgDB::ReaderWriter* reader = osgDB::Registry::instance()->getReaderWriterForExtension( "tiff" ); if ( !reader ) { OE_NOTICE << "[osgEarth::WCS1.1] WARNING: no reader for \"tiff\"" << std::endl; return NULL; } osgDB::ReaderWriter::ReadResult result = reader->readImage( input_stream ); //, getOptions() ); if ( !result.success() ) { OE_NOTICE << "[osgEarth::WCS1.1] WARNING: readImage() failed for Reader " << reader->getName() << std::endl; return NULL; } osg::Image* image = result.getImage(); //OE_NOTICE << "Returned grid is " << image->s() << "x" << image->t() << std::endl; if ( image ) image->ref(); return image; }
TileService* TileServiceReader::read( const std::string &location, const osgDB::ReaderWriter::Options* options ) { TileService *tileService = NULL; if ( osgDB::containsServerAddress( location ) ) { HTTPResponse response = HTTPClient::get( location, options); if (response.isOK() && response.getNumParts() > 0 ) { tileService = read( response.getPartStream( 0 ) ); } } else { if ((osgDB::fileExists(location)) && (osgDB::fileType(location) == osgDB::REGULAR_FILE)) { std::ifstream in( location.c_str() ); tileService = read( in ); } } return tileService; }