Skip to content

Commit

Permalink
WCS data items (browser support)
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Apr 28, 2012
1 parent 99cfa7c commit b7289fb
Show file tree
Hide file tree
Showing 7 changed files with 344 additions and 26 deletions.
239 changes: 237 additions & 2 deletions src/providers/gdal/qgsgdaldataitems.cpp
@@ -1,8 +1,10 @@
#include "qgsgdaldataitems.h"
#include "qgsgdalprovider.h"
#include "qgslogger.h"
//#include "qgsowssourceselect.h"
#include "qgsdatasourceuri.h"
#include "qgswcssourceselect.h"
#include "qgsowsconnection.h"
#include "qgsnewhttpconnection.h"

#include <QFileInfo>

Expand Down Expand Up @@ -54,6 +56,236 @@ bool QgsGdalLayerItem::setCrs( QgsCoordinateReferenceSystem crs )
return true;
}

// ---------------------------------------------------------------------------
QgsWCSConnectionItem::QgsWCSConnectionItem( QgsDataItem* parent, QString name, QString path )
: QgsDataCollectionItem( parent, name, path )
{
mIcon = QIcon( getThemePixmap( "mIconConnect.png" ) );
}

QgsWCSConnectionItem::~QgsWCSConnectionItem()
{
}

QVector<QgsDataItem*> QgsWCSConnectionItem::createChildren()
{
QgsDebugMsg( "Entered" );
QVector<QgsDataItem*> children;

QgsOWSConnection connection( "WCS", mName );

QgsDataSourceURI uri = connection.uri();
QgsDebugMsg( "uri = " + uri.encodedUri() );
mCapabilities.setUri( uri );

// Attention: supportedLayers() gives tree leafes, not top level
if ( !mCapabilities.lastError().isEmpty() )
{
children.append( new QgsErrorItem( this, tr( "Failed to retrieve layers" ), mPath + "/error" ) );
return children;
}

foreach( QgsWcsCoverageSummary coverageSummary, mCapabilities.capabilities().contents.coverageSummary )
{
// Attention, the name may be empty
QgsDebugMsg( QString::number( coverageSummary.orderId ) + " " + coverageSummary.identifier + " " + coverageSummary.title );
QString pathName = coverageSummary.identifier.isEmpty() ? QString::number( coverageSummary.orderId ) : coverageSummary.identifier;

QgsWCSLayerItem * layer = new QgsWCSLayerItem( this, coverageSummary.title, mPath + "/" + pathName, mCapabilities.capabilities(), uri, coverageSummary );

children.append( layer );
}
return children;
}

bool QgsWCSConnectionItem::equal( const QgsDataItem *other )
{
if ( type() != other->type() )
{
return false;
}
const QgsWCSConnectionItem *o = dynamic_cast<const QgsWCSConnectionItem *>( other );
//TODO
//return ( mPath == o->mPath && mName == o->mName && mConnInfo == o->mConnInfo );
return false;
}

QList<QAction*> QgsWCSConnectionItem::actions()
{
QList<QAction*> lst;

QAction* actionEdit = new QAction( tr( "Edit..." ), this );
connect( actionEdit, SIGNAL( triggered() ), this, SLOT( editConnection() ) );
lst.append( actionEdit );

QAction* actionDelete = new QAction( tr( "Delete" ), this );
connect( actionDelete, SIGNAL( triggered() ), this, SLOT( deleteConnection() ) );
lst.append( actionDelete );

return lst;
}

void QgsWCSConnectionItem::editConnection()
{
QgsNewHttpConnection nc( 0, "/Qgis/connections-wcs/", mName );

if ( nc.exec() )
{
// the parent should be updated
mParent->refresh();
}
}

void QgsWCSConnectionItem::deleteConnection()
{
QgsOWSConnection::deleteConnection( "WCS", mName );
// the parent should be updated
mParent->refresh();
}


// ---------------------------------------------------------------------------

QgsWCSLayerItem::QgsWCSLayerItem( QgsDataItem* parent, QString name, QString path, QgsWcsCapabilitiesProperty capabilitiesProperty, QgsDataSourceURI dataSourceUri, QgsWcsCoverageSummary coverageSummary )
: QgsLayerItem( parent, name, path, QString(), QgsLayerItem::Raster, "gdal" ),
mCapabilities( capabilitiesProperty ),
mDataSourceUri( dataSourceUri ),
mCoverageSummary( coverageSummary )
{
QgsDebugMsg( "uri = " + mDataSourceUri.encodedUri() );
mUri = createUri();
// Populate everything, it costs nothing, all info about layers is collected
foreach( QgsWcsCoverageSummary coverageSummary, mCoverageSummary.coverageSummary )
{
// Attention, the name may be empty
QgsDebugMsg( QString::number( coverageSummary.orderId ) + " " + coverageSummary.identifier + " " + coverageSummary.title );
QString pathName = coverageSummary.identifier.isEmpty() ? QString::number( coverageSummary.orderId ) : coverageSummary.identifier;
QgsWCSLayerItem * layer = new QgsWCSLayerItem( this, coverageSummary.title, mPath + "/" + pathName, mCapabilities, mDataSourceUri, coverageSummary );
mChildren.append( layer );
}

if ( mChildren.size() == 0 )
{
mIcon = iconRaster();
}
mPopulated = true;
}

QgsWCSLayerItem::~QgsWCSLayerItem()
{
}

QString QgsWCSLayerItem::createUri()
{
if ( mCoverageSummary.identifier.isEmpty() )
return ""; // layer collection

// Number of styles must match number of layers
mDataSourceUri.setParam( "identifier", mCoverageSummary.identifier );

QString format;
// get first supported by GDAL and server
QStringList mimes = QgsGdalProvider::supportedMimes().keys();
// prefer tiff
if ( mimes.contains( "image/tiff" ) && mCoverageSummary.supportedFormat.contains( "image/tiff" ) )
{
format = "image/tiff";
}
else
{
foreach( QString f, mimes )
{
if ( mCoverageSummary.supportedFormat.indexOf( f ) >= 0 )
{
format = f;
break;
}
}
}
mDataSourceUri.setParam( "format", format );

QString crs;

// TODO: prefer project CRS
// get first known if possible
QgsCoordinateReferenceSystem testCrs;
foreach( QString c, mCoverageSummary.supportedCrs )
{
testCrs.createFromOgcWmsCrs( c );
if ( testCrs.isValid() )
{
crs = c;
break;
}
}
if ( crs.isEmpty() && mCoverageSummary.supportedCrs.size() > 0 )
{
crs = mCoverageSummary.supportedCrs.value( 0 );
}
mDataSourceUri.setParam( "crs", crs );

return mDataSourceUri.encodedUri();
}

// ---------------------------------------------------------------------------

QgsWCSRootItem::QgsWCSRootItem( QgsDataItem* parent, QString name, QString path )
: QgsDataCollectionItem( parent, name, path )
{
mIcon = QIcon( getThemePixmap( "mIconWcs.png" ) );

populate();
}

QgsWCSRootItem::~QgsWCSRootItem()
{
}

QVector<QgsDataItem*>QgsWCSRootItem::createChildren()
{
QVector<QgsDataItem*> connections;
foreach( QString connName, QgsOWSConnection::connectionList( "WCS" ) )
{
QgsDataItem * conn = new QgsWCSConnectionItem( this, connName, mPath + "/" + connName );
connections.append( conn );
}
return connections;
}

QList<QAction*> QgsWCSRootItem::actions()
{
QList<QAction*> lst;

QAction* actionNew = new QAction( tr( "New Connection..." ), this );
connect( actionNew, SIGNAL( triggered() ), this, SLOT( newConnection() ) );
lst.append( actionNew );

return lst;
}


QWidget * QgsWCSRootItem::paramWidget()
{
QgsWCSSourceSelect *select = new QgsWCSSourceSelect( 0, 0, true, true );
connect( select, SIGNAL( connectionsChanged() ), this, SLOT( connectionsChanged() ) );
return select;
return 0;
}
void QgsWCSRootItem::connectionsChanged()
{
refresh();
}

void QgsWCSRootItem::newConnection()
{
QgsNewHttpConnection nc( 0, "/Qgis/connections-wcs/" );

if ( nc.exec() )
{
refresh();
}
}


// ---------------------------------------------------------------------------

Expand All @@ -68,7 +300,10 @@ QGISEXTERN int dataCapabilities()
QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
{
if ( thePath.isEmpty() )
return 0;
{
// Top level WCS
return new QgsWCSRootItem( parentItem, "WCS", "wcs:" );
}

QFileInfo info( thePath );
if ( info.isFile() )
Expand Down
60 changes: 60 additions & 0 deletions src/providers/gdal/qgsgdaldataitems.h
Expand Up @@ -2,6 +2,9 @@
#define QGSGDALDATAITEMS_H

#include "qgsdataitem.h"
#include "qgsdatasourceuri.h"
//#include "qgsowsconnection.h"
#include "qgswcscapabilities.h"

class QgsGdalLayerItem : public QgsLayerItem
{
Expand All @@ -14,5 +17,62 @@ class QgsGdalLayerItem : public QgsLayerItem
Capability capabilities();
};

class QgsWCSConnectionItem : public QgsDataCollectionItem
{
Q_OBJECT
public:
QgsWCSConnectionItem( QgsDataItem* parent, QString name, QString path );
~QgsWCSConnectionItem();

QVector<QgsDataItem*> createChildren();
virtual bool equal( const QgsDataItem *other );

virtual QList<QAction*> actions();

QgsWcsCapabilities mCapabilities;
//QgsDataSourceURI mUri;
//QgsOWSConnection mConnection;
QVector<QgsWcsCoverageSummary> mLayerProperties;

public slots:
void editConnection();
void deleteConnection();
};

// WCS Layers may be nested, so that they may be both QgsDataCollectionItem and QgsLayerItem
// We have to use QgsDataCollectionItem and support layer methods if necessary
class QgsWCSLayerItem : public QgsLayerItem
{
Q_OBJECT
public:
QgsWCSLayerItem( QgsDataItem* parent, QString name, QString path,
QgsWcsCapabilitiesProperty capabilitiesProperty, QgsDataSourceURI dataSourceUri, QgsWcsCoverageSummary coverageSummary );
~QgsWCSLayerItem();

QString createUri();

QgsWcsCapabilitiesProperty mCapabilities;
QgsDataSourceURI mDataSourceUri;
QgsWcsCoverageSummary mCoverageSummary;
};

class QgsWCSRootItem : public QgsDataCollectionItem
{
Q_OBJECT
public:
QgsWCSRootItem( QgsDataItem* parent, QString name, QString path );
~QgsWCSRootItem();

QVector<QgsDataItem*> createChildren();

virtual QList<QAction*> actions();

virtual QWidget * paramWidget();

public slots:
void connectionsChanged();

void newConnection();
};

#endif // QGSGDALDATAITEMS_H
32 changes: 32 additions & 0 deletions src/providers/gdal/qgsgdalprovider.cpp
Expand Up @@ -2017,3 +2017,35 @@ QGISEXTERN void buildSupportedRasterFileFilter( QString & theFileFiltersString )
QStringList wildcards;
buildSupportedRasterFileFilterAndExtensions( theFileFiltersString, exts, wildcards );
}

QMap<QString, QString> QgsGdalProvider::supportedMimes()
{
QMap<QString, QString> mimes;
GDALAllRegister();

QgsDebugMsg( QString( "GDAL drivers cont %1" ).arg( GDALGetDriverCount() ) );
for ( int i = 0; i < GDALGetDriverCount(); ++i )
{
GDALDriverH driver = GDALGetDriver( i );
Q_CHECK_PTR( driver );

if ( !driver )
{
QgsLogger::warning( "unable to get driver " + QString::number( i ) );
continue;
}

QString desc = GDALGetDescription( driver );

QString mimeType = GDALGetMetadataItem( driver, "DMD_MIMETYPE", "" );

if ( mimeType.isEmpty() ) continue;

desc = desc.isEmpty() ? mimeType : desc;

QgsDebugMsg( "add GDAL format " + mimeType + " " + desc );

mimes[mimeType] = desc;
}
return mimes;
}
2 changes: 2 additions & 0 deletions src/providers/gdal/qgsgdalprovider.h
Expand Up @@ -260,6 +260,8 @@ class QgsGdalProvider : public QgsRasterDataProvider
/** Emit a signal to notify of the progress event. */
void emitProgress( int theType, double theProgress, QString theMessage );

static QMap<QString, QString> supportedMimes();

signals:
void statusChanged( QString );

Expand Down
5 changes: 5 additions & 0 deletions src/providers/gdal/qgswcscapabilities.cpp
Expand Up @@ -118,6 +118,11 @@ QString QgsWcsCapabilities::prepareUri( QString uri )
return uri;
}

QgsWcsCapabilitiesProperty QgsWcsCapabilities::capabilities()
{
return mCapabilities;
}

bool QgsWcsCapabilities::supportedCoverages( QVector<QgsWcsCoverageSummary> &coverageSummary )
{
QgsDebugMsg( "Entering." );
Expand Down
2 changes: 2 additions & 0 deletions src/providers/gdal/qgswcscapabilities.h
Expand Up @@ -146,6 +146,8 @@ class QgsWcsCapabilities : public QObject

void setUri( QgsDataSourceURI const &theUri );

QgsWcsCapabilitiesProperty capabilities();

/**
* \brief Returns a list of the supported layers of the WCS server
*
Expand Down

0 comments on commit b7289fb

Please sign in to comment.