コード例 #1
0
void TestQgsWcsPublicServers::test( )
{
  QStringList versions;
  // It may happen that server supports 1.1.1, but does not accept 1.1 (http://zeus.pin.unifi.it/gi-wcs/http)
  versions << "" << "1.0.0" << "1.1.0"; // empty for default
  QStringList servers;
  // Some (first) coverages do not advertize any supportedCRS and sever gives
  // error both with native CRS (EPSG::561005) and EPSG:4326
  // MOD* coverages work OK
  servers << "http://argon.geogr.uni-jena.de:8080/geoserver/ows";
  servers << "http://demo.geonode.org/geoserver/wcs";
  servers << "http://demo.mapserver.org/cgi-bin/wcs";
  servers << "http://demo.opengeo.org/geoserver/wcs";
  // geobrain.laits.gmu.edu servers are quite slow
  servers << "http://geobrain.laits.gmu.edu/cgi-bin/gbwcs-dem";
  servers << "http://geobrain.laits.gmu.edu/cgi-bin/ows8/wcseo";
  servers << "http://geobrain.laits.gmu.edu/cgi-bin/wcs110";
  servers << "http://geobrain.laits.gmu.edu/cgi-bin/wcs-all";
  servers << "http://iceds.ge.ucl.ac.uk/cgi-bin/icedswcs";
  servers << "http://motherlode.ucar.edu:8080/thredds/wcs/fmrc/NCEP/DGEX/Alaska_12km/NCEP-DGEX-Alaska_12km_best.ncd";
  servers << "http://navigator.state.or.us/ArcGIS/services/Framework/Imagery_Mosaic2009/ImageServer/WCSServer";
  servers << "http://nsidc.org/cgi-bin/atlas_north";
  servers << "http://sedac.ciesin.columbia.edu/geoserver/wcs";
  // Big and slow
  //servers << "http://webmap.ornl.gov/ogcbroker/wcs";
  servers << "http://ws.csiss.gmu.edu/cgi-bin/wcs-t";
  // Big and slow
  //servers << "http://ws.laits.gmu.edu/cgi-bin/wcs-all";
  // Currently very slow or down
  //servers << "http://www.sogeo.ch/geoserver/wcs";
  // Slow and erroneous
  //servers << "http://zeus.pin.unifi.it/gi-wcs/http";

  foreach ( QString server, servers )
  {
    QStringList myServerLog;
    myServerLog << "server:" + server;
    QString myServerDirName = server;
    myServerDirName.replace( QRegExp( "[:/]+" ), "." );
    myServerDirName.replace( QRegExp( "\\.$" ), "" );
    QgsDebugMsg( "myServerDirName = " + myServerDirName );

    QDir myServerDir( mCacheDir.absolutePath() + QDir::separator() + myServerDirName );
    QString myServerLogPath = myServerDir.absolutePath() + QDir::separator() + "server.log";
    if ( QFileInfo( myServerLogPath ).exists() )
    {
      QgsDebugMsg( "cache exists " + myServerDir.absolutePath() );
      continue;
    }

    if ( !myServerDir.exists() )
    {
      mCacheDir.mkdir( myServerDirName );
    }

    foreach ( QString version, versions )
    {
      QgsDebugMsg( "server: " + server + " version: " + version );

      QgsDataSourceURI myServerUri;

      myServerUri.setParam( "url", server );
      if ( !version.isEmpty() )
      {
        myServerUri.setParam( "version", version );
      }

      QgsWcsCapabilities myCapabilities;
      myCapabilities.setUri( myServerUri );

      if ( !myCapabilities.lastError().isEmpty() )
      {
        QgsDebugMsg( myCapabilities.lastError() );
        myServerLog << "error: (version: " + version + ") " +  myCapabilities.lastError().replace( "\n", " " );
        continue;
      }

      QVector<QgsWcsCoverageSummary> myCoverages;
      if ( !myCapabilities.supportedCoverages( myCoverages ) )
      {
        QgsDebugMsg( "Cannot get list of coverages" );
        myServerLog << "error: (version: " + version + ") Cannot get list of coverages";
        continue;
      }

      int myCoverageCount = 0;
      int myStep = myCoverages.size() / mMaxCoverages;
      int myStepCount = -1;
      foreach ( QgsWcsCoverageSummary myCoverage, myCoverages )
      {
        QgsDebugMsg( "coverage: " + myCoverage.identifier );
        // Go in steps to get more success/errors
        if ( myStepCount == -1 || myStepCount > myStep )
        {
          myStepCount = 0;
        }
        else
        {
          myStepCount++;
          continue;
        }

        myCoverageCount++;
        if ( myCoverageCount > mMaxCoverages ) break;

        QString myPath = myServerDir.absolutePath() + QDir::separator() + myCoverage.identifier;

        if ( !version.isEmpty() )
        {
          myPath += "-" + version;
        }
        QString myLogPath = myPath + ".log";

        if ( QFileInfo( myLogPath ).exists() )
        {
          QMap<QString, QString> log = readLog( myLogPath );
          if ( !log.value( "identifier" ).isEmpty() && log.value( "error" ).isEmpty() ) continue;
        }

        QStringList myLog;
        myLog << "identifier:" + myCoverage.identifier;
        myCapabilities.describeCoverage( myCoverage.identifier );
        myCoverage = myCapabilities.coverage( myCoverage.identifier ); // get described
        QgsDataSourceURI myUri = myServerUri;
        myUri.setParam( "identifier", myCoverage.identifier );
        if ( myCoverage.times.size() > 0 )
        {
          myUri.setParam( "time", myCoverage.times.value( 0 ) );
        }
        myLog << "version:" + version;
        myLog << "uri:" + myUri.encodedUri();

        int myWidth = 100;
        int myHeight = 100;
        if ( myCoverage.hasSize )
        {
          myHeight = static_cast<int>( qRound( 1.0 * myWidth * myCoverage.height / myCoverage.width ) );
        }
        myLog << QString( "hasSize:%1" ).arg( myCoverage.hasSize );

        QgsRasterLayer * myLayer = new QgsRasterLayer( myUri.encodedUri(), myCoverage.identifier, "wcs", true );
        if ( myLayer->isValid() )
        {
          int myBandCount = myLayer->dataProvider()->bandCount();
          myLog << "bandCount:" + QString::number( myBandCount );
          if ( myBandCount > 0 )
          {
            myLog << "srcType:" + QString::number( myLayer->dataProvider()->srcDataType( 1 ) );

            QgsRasterBandStats myStats = myLayer->dataProvider()->bandStatistics( 1, QgsRasterBandStats::All, QgsRectangle(), myWidth * myHeight );
            myLog << "min:" + QString::number( myStats.minimumValue );
            myLog << "max:" + QString::number( myStats.maximumValue );
          }

          QgsMapRenderer myMapRenderer;
          QList<QgsMapLayer *> myLayersList;

          myLayersList.append( myLayer );
          QgsMapLayerRegistry::instance()->addMapLayers( myLayersList, false );

          QMap<QString, QgsMapLayer*> myLayersMap = QgsMapLayerRegistry::instance()->mapLayers();

          myMapRenderer.setLayerSet( myLayersMap.keys() );

          myMapRenderer.setExtent( myLayer->extent() );

          QImage myImage( myWidth, myHeight, QImage::Format_ARGB32_Premultiplied );
          myImage.fill( 0 );

          myMapRenderer.setOutputSize( QSize( myWidth, myHeight ), myImage.logicalDpiX() );

          QPainter myPainter( &myImage );
          myMapRenderer.render( &myPainter );

          // Save rendered image
          QString myPngPath = myPath + ".png";
          QgsDebugMsg( "myPngPath = " + myPngPath );
          myImage.save( myPngPath );

          // Verify data
          QSet<QString> myValues; // cannot be QSet<double>
          void *myData = myLayer->dataProvider()->readBlock( 1, myLayer->extent(), myWidth, myHeight );
          if ( myData )
          {
            int myType = myLayer->dataProvider()->dataType( 1 );
            for ( int row = 0; row < myHeight; row++ )
            {
              for ( int col = 0; col < myWidth; col++ )
              {
                double value = myLayer->dataProvider()->readValue( myData, myType, row * myWidth + col );
                QString valueStr = QString::number( value );
                if ( !myValues.contains( valueStr ) ) myValues.insert( valueStr );
              }
            }
            free( myData );
          }
          QgsDebugMsg( QString( "%1 values" ).arg( myValues.size() ) );
          myLog << QString( "valuesCount:%1" ).arg( myValues.size() );

          // Verify image colors
          QSet<QRgb> myColors;
          for ( int row = 0; row < myHeight; row++ )
          {
            for ( int col = 0; col < myWidth; col++ )
            {
              QRgb color = myImage.pixel( col, row );
              if ( !myColors.contains( color ) ) myColors.insert( color );
            }
          }
          QgsDebugMsg( QString( "%1 colors" ).arg( myColors.size() ) );
          myLog << QString( "colorsCount:%1" ).arg( myColors.size() );
        }
        else
        {
          QgsDebugMsg( "Layer is not valid" );
          myLog << "error:Layer is not valid";
        }

        QFile myLogFile( myLogPath );
        myLogFile.open( QIODevice::WriteOnly | QIODevice::Text );
        QTextStream myStream( &myLogFile );
        myStream << myLog.join( "\n" );

        myLogFile.close();
        QgsMapLayerRegistry::instance()->removeAllMapLayers();
      }