void getFeatures(HTTPResponse &response, FeatureList& features) { //OE_NOTICE << "mimetype=" << response.getMimeType() << std::endl; //TODO: Handle more than just geojson... std::string ext = getExtensionForMimeType(response.getMimeType()); //Save the response to a temp file std::string tmpPath = getTempPath(); std::string name = getTempName(tmpPath, ext); saveResponse(response, name ); //OE_NOTICE << "Saving to " << name << std::endl; //OGRDataSourceH ds = OGROpen(name.c_str(), FALSE, &driver); OGRDataSourceH ds = OGROpen(name.c_str(), FALSE, NULL); if (!ds) { OE_NOTICE << "Error opening data with contents " << std::endl << response.getPartAsString(0) << std::endl; } OGRLayerH layer = OGR_DS_GetLayer(ds, 0); //Read all the features if (layer) { OGR_L_ResetReading(layer); OGRFeatureH feat_handle; while ((feat_handle = OGR_L_GetNextFeature( layer )) != NULL) { if ( feat_handle ) { Feature* f = OgrUtils::createFeature( feat_handle ); if ( f ) { features.push_back( f ); } OGR_F_Destroy( feat_handle ); } } } //Destroy the datasource OGR_DS_Destroy( ds ); //Remove the temporary file remove( name.c_str() ); }
HTTPClient::ResultCode HTTPClient::doReadNodeFile(const std::string& filename, osg::ref_ptr<osg::Node>& output, const osgDB::ReaderWriter::Options *options, ProgressBase *callback) { ResultCode result = RESULT_OK; if ( osgDB::containsServerAddress( filename ) ) { HTTPResponse response = this->doGet(filename, options, callback); if (response.isOK()) { // Try to find a reader by file extension. If this fails, we will fetch the file // anyway and try to get a reader via mime-type. std::string ext = osgDB::getFileExtension( filename ); osgDB::ReaderWriter *reader = osgDB::Registry::instance()->getReaderWriterForExtension( ext ); //If we didn't get a reader by extension, try to get it via mime type if (!reader) { std::string mimeType = response.getMimeType(); //OE_DEBUG << LC << "Looking up extension for mime-type " << mimeType << std::endl; if ( mimeType.length() > 0 ) { reader = owUtil::Registry::instance().getReaderWriterForMimeType(mimeType); } } // if we still didn't get it, bad news if (!reader) { //OE_NOTICE<<LC<<"Error: No ReaderWriter for file "<<filename<<std::endl; result = RESULT_NO_READER; } else { osgDB::ReaderWriter::ReadResult rr = reader->readNode(response.getPartStream(0), options); if ( rr.validNode() ) { output = rr.takeNode(); } else { if ( rr.error() ) { //OE_WARN << LC << "HTTP Reader Error: " << rr.message() << std::endl; } result = RESULT_READER_ERROR; } } } else { result = response.isCancelled() ? RESULT_CANCELED : response.getCode() == HTTPResponse::NOT_FOUND ? RESULT_NOT_FOUND : response.getCode() == HTTPResponse::SERVER_ERROR ? RESULT_SERVER_ERROR : RESULT_UNKNOWN_ERROR; //If we have an error but it's recoverable, like a server error or timeout then set the callback to retry. if (HTTPClient::isRecoverable( result ) ) { if (callback) { //OE_DEBUG << "Error in HTTPClient for " << filename << " but it's recoverable" << std::endl; callback->setNeedsRetry( true ); } } /*if (response.isCancelled()) OE_NOTICE << "Request for " << filename << " was cancelled " << std::endl;*/ } } else { output = osgDB::readNodeFile( filename, options ); if ( !output.valid() ) result = RESULT_NOT_FOUND; } return result; }
HTTPClient::ResultCode HTTPClient::doReadImageFile(const std::string& filename, osg::ref_ptr<osg::Image>& output, const osgDB::ReaderWriter::Options *options, osgEarth::ProgressCallback *callback) { ResultCode result = RESULT_OK; if ( osgDB::containsServerAddress( filename ) ) { HTTPResponse response = this->doGet(filename, options, callback); if (response.isOK()) { osgDB::ReaderWriter* reader = 0L; // try to look up a reader by mime-type first: std::string mimeType = response.getMimeType(); OE_DEBUG << LC << "Looking up extension for mime-type " << mimeType << std::endl; if ( !mimeType.empty() ) { reader = osgEarth::Registry::instance()->getReaderWriterForMimeType(mimeType); } if ( !reader ) { // Try to find a reader by file extension. If this fails, we will fetch the file // anyway and try to get a reader via mime-type. std::string ext = osgDB::getFileExtension( filename ); reader = osgDB::Registry::instance()->getReaderWriterForExtension( ext ); //OE_NOTICE << "Reading " << filename << " with mime " << response.getMimeType() << std::endl; } if (!reader) { OE_WARN << LC << "Can't find an OSG plugin to read "<<filename<<std::endl; result = RESULT_NO_READER; } else { osgDB::ReaderWriter::ReadResult rr = reader->readImage(response.getPartStream(0), options); if ( rr.validImage() ) { output = rr.takeImage(); } else { if ( !rr.message().empty() ) { OE_WARN << LC << "HTTP error: " << rr.message() << std::endl; } OE_WARN << LC << reader->className() << " failed to read image from " << filename << std::endl; result = RESULT_READER_ERROR; } } } else { result = response.isCancelled() ? RESULT_CANCELED : response.getCode() == HTTPResponse::NOT_FOUND ? RESULT_NOT_FOUND : response.getCode() == HTTPResponse::SERVER_ERROR ? RESULT_SERVER_ERROR : RESULT_UNKNOWN_ERROR; //If we have an error but it's recoverable, like a server error or timeout then set the callback to retry. if (HTTPClient::isRecoverable( result ) ) { if (callback) { OE_DEBUG << "Error in HTTPClient for " << filename << " but it's recoverable" << std::endl; callback->setNeedsRetry( true ); } } //if ( response.isCancelled() ) // OE_NOTICE << "HTTP cancel: " << filename << std::endl; //else // OE_NOTICE << "HTTP ERROR " << response.getCode() << ": " << filename << std::endl; /*if (response.isCancelled()) OE_NOTICE << "Request for " << filename << " was cancelled " << std::endl;*/ } } else { output = osgDB::readImageFile( filename, options ); if ( !output.valid() ) result = RESULT_NOT_FOUND; } return result; }