Skip to content
Permalink
Browse files
WFS adapted to QgsOWSConnection, remaining functionality from QgsWFSC…
…onnection moved to QgsWFSCapabilities
  • Loading branch information
blazek committed Apr 30, 2012
1 parent b7289fb commit bcd9bd74d216391bb4bce561553b3f375f409bfc
@@ -4,7 +4,7 @@

SET(WFS_SRCS
qgswfsprovider.cpp
qgswfsconnection.cpp
qgswfscapabilities.cpp
qgswfsdataitems.cpp
qgswfsdata.cpp
qgswfssourceselect.cpp
@@ -13,7 +13,7 @@ SET(WFS_SRCS

SET (WFS_MOC_HDRS
qgswfsdata.h
qgswfsconnection.h
qgswfscapabilities.h
qgswfsdataitems.h
qgswfsprovider.h
qgswfssourceselect.h
@@ -1,4 +1,4 @@
#include "qgswfsconnection.h"
#include "qgswfscapabilities.h"
#include "qgsexpression.h"
#include "qgslogger.h"
#include "qgsnetworkaccessmanager.h"
@@ -11,18 +11,25 @@

static const QString WFS_NAMESPACE = "http://www.opengis.net/wfs";

QgsWFSConnection::QgsWFSConnection( QString connName, QObject *parent ) :
QObject( parent ),
mConnName( connName ),
QgsWFSCapabilities::QgsWFSCapabilities( QString theUri ) :
//QObject( parent ),
//mConnName( connName ),
mCapabilitiesReply( 0 ),
mErrorCode( QgsWFSConnection::NoError )
mErrorCode( QgsWFSCapabilities::NoError )
{
mUri.setEncodedUri( theUri ),
QgsDebugMsg ( "theUri = " + theUri );
mBaseUrl = prepareUri ( mUri.param("url") );

QgsDebugMsg ( "mBaseUrl = " + mBaseUrl );

//find out the server URL
/*
QSettings settings;
QString key = "/Qgis/connections-wfs/" + mConnName + "/url";
mUri = settings.value( key ).toString();
QgsDebugMsg( QString( "url is: %1" ).arg( mUri ) );

//make a GetCapabilities request
//modify mUri to add '?' or '&' at the end if it is not already there
if ( !( mUri.contains( "?" ) ) )
@@ -33,19 +40,34 @@ QgsWFSConnection::QgsWFSConnection( QString connName, QObject *parent ) :
{
mUri.append( "&" );
}
*/
}

QString QgsWFSConnection::uriGetCapabilities() const
QString QgsWFSCapabilities::prepareUri( QString uri )
{
return mUri + "SERVICE=WFS&REQUEST=GetCapabilities&VERSION=1.0.0";
if ( !uri.contains( "?" ) )
{
uri.append( "?" );
}
else if ( uri.right( 1 ) != "?" && uri.right( 1 ) != "&" )
{
uri.append( "&" );
}

return uri;
}

QString QgsWFSCapabilities::uriGetCapabilities() const
{
return mBaseUrl + "SERVICE=WFS&REQUEST=GetCapabilities&VERSION=1.0.0";
}

QString QgsWFSConnection::uriDescribeFeatureType( const QString& typeName ) const
QString QgsWFSCapabilities::uriDescribeFeatureType( const QString& typeName ) const
{
return mUri + "SERVICE=WFS&REQUEST=DescribeFeatureType&VERSION=1.0.0&TYPENAME=" + typeName;
return mBaseUrl + "SERVICE=WFS&REQUEST=DescribeFeatureType&VERSION=1.0.0&TYPENAME=" + typeName;
}

QString QgsWFSConnection::uriGetFeature( QString typeName, QString crsString, QString filter, QgsRectangle bBox ) const
QString QgsWFSCapabilities::uriGetFeature( QString typeName, QString crsString, QString filter, QgsRectangle bBox ) const
{
//get CRS
if ( !crsString.isEmpty() )
@@ -89,11 +111,7 @@ QString QgsWFSConnection::uriGetFeature( QString typeName, QString crsString, QS
.arg( bBox.yMaximum(), 0, 'f' );
}

QString uri = mUri;
if ( !( uri.contains( "?" ) ) )
{
uri.append( "?" );
}
QString uri = mBaseUrl;

//add a wfs layer to the map
uri += "SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=" + typeName + crsString + bBoxString + filterString;
@@ -102,9 +120,9 @@ QString QgsWFSConnection::uriGetFeature( QString typeName, QString crsString, QS
}


void QgsWFSConnection::requestCapabilities()
void QgsWFSCapabilities::requestCapabilities()
{
mErrorCode = QgsWFSConnection::NoError;
mErrorCode = QgsWFSCapabilities::NoError;
mErrorMessage.clear();

QNetworkRequest request( uriGetCapabilities() );
@@ -113,12 +131,12 @@ void QgsWFSConnection::requestCapabilities()
connect( mCapabilitiesReply, SIGNAL( finished() ), this, SLOT( capabilitiesReplyFinished() ) );
}

void QgsWFSConnection::capabilitiesReplyFinished()
void QgsWFSCapabilities::capabilitiesReplyFinished()
{
// handle network errors
if ( mCapabilitiesReply->error() != QNetworkReply::NoError )
{
mErrorCode = QgsWFSConnection::NetworkError;
mErrorCode = QgsWFSCapabilities::NetworkError;
mErrorMessage = mCapabilitiesReply->errorString();
emit gotCapabilities();
return;
@@ -149,7 +167,7 @@ void QgsWFSConnection::capabilitiesReplyFinished()
QDomDocument capabilitiesDocument;
if ( !capabilitiesDocument.setContent( buffer, true, &capabilitiesDocError ) )
{
mErrorCode = QgsWFSConnection::XmlError;
mErrorCode = QgsWFSCapabilities::XmlError;
mErrorMessage = capabilitiesDocError;
emit gotCapabilities();
return;
@@ -163,7 +181,7 @@ void QgsWFSConnection::capabilitiesReplyFinished()
QDomNode ex = doc.firstChild();
QString exc = ex.toElement().attribute( "exceptionCode", "Exception" );
QDomElement ext = ex.firstChild().toElement();
mErrorCode = QgsWFSConnection::ServerExceptionError;
mErrorCode = QgsWFSCapabilities::ServerExceptionError;
mErrorMessage = exc + ": " + ext.firstChild().nodeValue();
emit gotCapabilities();
return;
@@ -226,31 +244,3 @@ void QgsWFSConnection::capabilitiesReplyFinished()
emit gotCapabilities();
}




QStringList QgsWFSConnection::connectionList()
{
QSettings settings;
settings.beginGroup( "/Qgis/connections-wfs" );
return settings.childGroups();
}

QString QgsWFSConnection::selectedConnection()
{
QSettings settings;
return settings.value( "/Qgis/connections-wfs/selected" ).toString();
}

void QgsWFSConnection::setSelectedConnection( QString name )
{
QSettings settings;
settings.setValue( "/Qgis/connections-wfs/selected", name );
}

void QgsWFSConnection::deleteConnection( QString name )
{
QSettings settings;
settings.remove( "/Qgis/connections-wfs/" + name );
settings.remove( "/Qgis/WFS/" + name );
}
@@ -1,27 +1,25 @@
#ifndef QGSWFSCONNECTION_H
#define QGSWFSCONNECTION_H
#ifndef QGSWFSCAPABILITIES_H
#define QGSWFSCAPABILITIES_H

#include <QObject>

#include "qgsrectangle.h"
#include "qgsdatasourceuri.h"

class QNetworkReply;

class QgsWFSConnection : public QObject
class QgsWFSCapabilities : public QObject
{
Q_OBJECT
public:
explicit QgsWFSConnection( QString connName, QObject *parent = 0 );
//explicit QgsWFSCapabilities( QString connName, QObject *parent = 0 );
QgsWFSCapabilities( QString theUri );

static QStringList connectionList();

static void deleteConnection( QString name );

static QString selectedConnection();
static void setSelectedConnection( QString name );
//! Append ? or & if necessary
QString prepareUri( QString uri );

//! base service URI
QString uri() const { return mUri; }
QString uri() const { return mBaseUrl; }
//! URI to get capabilities
QString uriGetCapabilities() const;
//! URI to get schema of wfs layer
@@ -67,13 +65,17 @@ class QgsWFSConnection : public QObject
void capabilitiesReplyFinished();

protected:
QString mConnName;
QString mUri;
//QString mConnName;
//QString mUri;

QgsDataSourceURI mUri;

QString mBaseUrl;

QNetworkReply *mCapabilitiesReply;
GetCapabilities mCaps;
ErrorCode mErrorCode;
QString mErrorMessage;
};

#endif // QGSWFSCONNECTION_H
#endif // QGSWFSCAPABILITIES_H
@@ -1,7 +1,8 @@
#include "qgswfsdataitems.h"

#include "qgswfsprovider.h"
#include "qgswfsconnection.h"
#include "qgsowsconnection.h"
#include "qgswfscapabilities.h"
#include "qgswfssourceselect.h"

#include "qgsnewhttpconnection.h"
@@ -10,10 +11,10 @@
#include <QCoreApplication>


QgsWFSLayerItem::QgsWFSLayerItem( QgsDataItem* parent, QString connName, QString name, QString title )
QgsWFSLayerItem::QgsWFSLayerItem( QgsDataItem* parent, QString name, QgsDataSourceURI uri, QString featureType, QString title )
: QgsLayerItem( parent, title, parent->path() + "/" + name, QString(), QgsLayerItem::Vector, "WFS" )
{
mUri = QgsWFSConnection( connName ).uriGetFeature( name );
mUri = QgsWFSCapabilities( uri.encodedUri() ).uriGetFeature( featureType );
mPopulated = true;
}

@@ -24,7 +25,7 @@ QgsWFSLayerItem::~QgsWFSLayerItem()
////

QgsWFSConnectionItem::QgsWFSConnectionItem( QgsDataItem* parent, QString name, QString path )
: QgsDataCollectionItem( parent, name, path ), mName( name ), mConn( NULL )
: QgsDataCollectionItem( parent, name, path ), mName( name ), mCapabilities( NULL )
{
mIcon = QIcon( getThemePixmap( "mIconConnect.png" ) );
}
@@ -36,23 +37,29 @@ QgsWFSConnectionItem::~QgsWFSConnectionItem()
QVector<QgsDataItem*> QgsWFSConnectionItem::createChildren()
{
mGotCapabilities = false;
mConn = new QgsWFSConnection( mName, this );
connect( mConn, SIGNAL( gotCapabilities() ), this, SLOT( gotCapabilities() ) );

mConn->requestCapabilities();
QgsOWSConnection connection( "WFS", mName );
QgsDataSourceURI uri = connection.uri();
QString encodedUri = uri.encodedUri();

mCapabilities = new QgsWFSCapabilities( encodedUri );
connect( mCapabilities, SIGNAL( gotCapabilities() ), this, SLOT( gotCapabilities() ) );

mCapabilities->requestCapabilities();

while ( !mGotCapabilities )
{
QCoreApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
}

QVector<QgsDataItem*> layers;
if ( mConn->errorCode() == QgsWFSConnection::NoError )
if ( mCapabilities->errorCode() == QgsWFSCapabilities::NoError )
{
QgsWFSConnection::GetCapabilities caps = mConn->capabilities();
foreach( const QgsWFSConnection::FeatureType& featureType, caps.featureTypes )
QgsWFSCapabilities::GetCapabilities caps = mCapabilities->capabilities();
foreach( const QgsWFSCapabilities::FeatureType& featureType, caps.featureTypes )
{
QgsWFSLayerItem* layer = new QgsWFSLayerItem( this, mName, featureType.name, featureType.title );
//QgsWFSLayerItem* layer = new QgsWFSLayerItem( this, mName, featureType.name, featureType.title );
QgsWFSLayerItem* layer = new QgsWFSLayerItem( this, mName, uri, featureType.name, featureType.title );
layers.append( layer );
}
}
@@ -61,8 +68,8 @@ QVector<QgsDataItem*> QgsWFSConnectionItem::createChildren()
layers.append( new QgsErrorItem( this, tr( "Failed to retrieve layers" ), mPath + "/error" ) );
}

mConn->deleteLater();
mConn = NULL;
mCapabilities->deleteLater();
mCapabilities = NULL;

return layers;
}
@@ -101,7 +108,7 @@ void QgsWFSConnectionItem::editConnection()

void QgsWFSConnectionItem::deleteConnection()
{
QgsWFSConnection::deleteConnection( mName );
QgsOWSConnection::deleteConnection( "WFS", mName );
// the parent should be updated
mParent->refresh();
}
@@ -127,7 +134,7 @@ QVector<QgsDataItem*> QgsWFSRootItem::createChildren()
{
QVector<QgsDataItem*> connections;

foreach( QString connName, QgsWFSConnection::connectionList() )
foreach( QString connName, QgsOWSConnection::connectionList( "WFS" ) )
{
QgsDataItem * conn = new QgsWFSConnectionItem( this, connName, mPath + "/" + connName );
connections.append( conn );
@@ -2,6 +2,8 @@
#define QGSWFSDATAITEMS_H

#include "qgsdataitem.h"
#include "qgsdatasourceuri.h"
#include "qgswfscapabilities.h"

class QgsWFSRootItem : public QgsDataCollectionItem
{
@@ -45,15 +47,15 @@ class QgsWFSConnectionItem : public QgsDataCollectionItem
private:
QString mName;

QgsWFSConnection* mConn;
QgsWFSCapabilities* mCapabilities;
bool mGotCapabilities;
};


class QgsWFSLayerItem : public QgsLayerItem
{
public:
QgsWFSLayerItem( QgsDataItem* parent, QString connName, QString name, QString title );
QgsWFSLayerItem( QgsDataItem* parent, QString name, QgsDataSourceURI uri, QString featureType, QString title );
~QgsWFSLayerItem();

};
Loading

0 comments on commit bcd9bd7

Please sign in to comment.