Skip to content
Permalink
Browse files

Add style retriever for a geonode layer.

  • Loading branch information
ismailsunni authored and nyalldawson committed Aug 30, 2017
1 parent 500c5c1 commit 301615855838793c7504e87e2de2567808bfbf58
@@ -23,6 +23,18 @@ struct QgsServiceLayerDetail
QString xyzURL;
};

struct QgsGeoNodeStyle
{
%TypeHeaderCode
#include <qgsgeonoderequest.h>
%End
QString id;
QString name;
QString title;
QDomDocument body;
QString styleUrl;
};

class QgsGeoNodeRequest : QObject
{

@@ -44,6 +56,21 @@ class QgsGeoNodeRequest : QObject
:rtype: list of QgsServiceLayerDetail
%End

QList<QgsGeoNodeStyle> getStyles( QString layerName );
%Docstring
:rtype: list of QgsGeoNodeStyle
%End

QgsGeoNodeStyle getDefaultStyle( QString layerName );
%Docstring
:rtype: QgsGeoNodeStyle
%End

QgsGeoNodeStyle getStyle( QString styleID );
%Docstring
:rtype: QgsGeoNodeStyle
%End

QStringList serviceUrls( QString serviceType );
%Docstring
:rtype: list of str
@@ -70,6 +70,66 @@ QList<QgsServiceLayerDetail> QgsGeoNodeRequest::getLayers()
return parseLayers( this->response() );
}

QgsGeoNodeStyle QgsGeoNodeRequest::getDefaultStyle( QString layerName )
{
QgsGeoNodeStyle defaultStyle;
bool success = request( QStringLiteral( "/api/layers?name=" ) + layerName );
if ( !success )
{
return defaultStyle;
}

QJsonDocument jsonDocument = QJsonDocument::fromJson( this->response() );
QJsonObject jsonObject = jsonDocument.object();
QList<QVariant> layers = jsonObject.toVariantMap()["objects"].toList();
if ( layers.count() < 1 )
{
return defaultStyle;
}
QString defaultStyleUrl = layers[0].toMap()["default_style"].toString();

defaultStyle = retrieveStyle( defaultStyleUrl );

return defaultStyle;

}

QList<QgsGeoNodeStyle> QgsGeoNodeRequest::getStyles( QString layerName )
{
QList<QgsGeoNodeStyle> geoNodeStyles;
bool success = request( QStringLiteral( "/api/styles?layer__name=" ) + layerName );
if ( !success )
{
return geoNodeStyles;
}

QJsonDocument jsonDocument = QJsonDocument::fromJson( this->response() );
QJsonObject jsobObject = jsonDocument.object();
QList<QVariant> styles = jsobObject.toVariantMap()["objects"].toList();

Q_FOREACH ( QVariant style, styles )
{
QVariantMap styleMap = style.toMap();
QString styleUrl = styleMap["resource_uri"].toString();
QgsGeoNodeStyle geoNodeStyle = retrieveStyle( styleUrl );
if ( !geoNodeStyle.name.isEmpty() )
{
geoNodeStyles.append( geoNodeStyle );
}
}

return geoNodeStyles;

}

QgsGeoNodeStyle QgsGeoNodeRequest::getStyle( QString styleID )
{
QString endPoint = QStringLiteral( "/api/styles/" ) + styleID;

return retrieveStyle( endPoint );

}

void QgsGeoNodeRequest::replyProgress( qint64 bytesReceived, qint64 bytesTotal )
{
QString msg = tr( "%1 of %2 bytes of request downloaded." ).arg( bytesReceived ).arg( bytesTotal < 0 ? QStringLiteral( "unknown number of" ) : QString::number( bytesTotal ) );
@@ -298,6 +358,38 @@ QList<QgsServiceLayerDetail> QgsGeoNodeRequest::parseLayers( QByteArray layerRes
return layers;
}

QgsGeoNodeStyle QgsGeoNodeRequest::retrieveStyle( QString styleUrl )
{
QgsGeoNodeStyle geoNodeStyle;

bool success = request( styleUrl );
if ( !success )
{
return geoNodeStyle;
}
QJsonDocument jsonDocument = QJsonDocument::fromJson( this->response() );
QJsonObject jsonObject = jsonDocument.object();

geoNodeStyle.id = jsonObject.toVariantMap()["id"].toString();
geoNodeStyle.name = jsonObject.toVariantMap()["name"].toString();
geoNodeStyle.title = jsonObject.toVariantMap()["title"].toString();
geoNodeStyle.styleUrl = jsonObject.toVariantMap()["style_url"].toString();

success = request( geoNodeStyle.styleUrl );
if ( !success )
{
return geoNodeStyle;
}

success = geoNodeStyle.body.setContent( this->response() );
if ( !success )
{
return geoNodeStyle;
}

return geoNodeStyle;
}

QStringList QgsGeoNodeRequest::serviceUrls( QString serviceType )
{
QStringList urls;
@@ -393,7 +485,8 @@ bool QgsGeoNodeRequest::request( QString endPoint )
abort();
mIsAborted = false;
QgsMessageLog::logMessage( mBaseUrl, tr( "GeoNode" ) );
QString url = mBaseUrl + endPoint;
// Handle case where the endpoint is full url
QString url = endPoint.startsWith( mBaseUrl ) ? endPoint : mBaseUrl + endPoint;
QgsMessageLog::logMessage( url, tr( "GeoNode" ) );
setProtocol( url.split( "://" )[0] );
QUrl layerUrl( url );
@@ -18,6 +18,7 @@
#include "qgis.h"
#include "qgis_core.h"
#include <qnetworkreply.h>
#include <QDomDocument>


#include <QObject>
@@ -39,6 +40,20 @@ struct CORE_EXPORT QgsServiceLayerDetail
QString xyzURL;
};

struct CORE_EXPORT QgsGeoNodeStyle
{
#ifdef SIP_RUN
% TypeHeaderCode
#include <qgsgeonoderequest.h>
% End
#endif
QString id;
QString name;
QString title;
QDomDocument body;
QString styleUrl;
};

class CORE_EXPORT QgsGeoNodeRequest : public QObject
{
Q_OBJECT
@@ -51,6 +66,12 @@ class CORE_EXPORT QgsGeoNodeRequest : public QObject

QList<QgsServiceLayerDetail> getLayers();

QList<QgsGeoNodeStyle> getStyles( QString layerName );

QgsGeoNodeStyle getDefaultStyle( QString layerName );

QgsGeoNodeStyle getStyle( QString styleID );

// Obtain list of unique URL in the geonode
QStringList serviceUrls( QString serviceType );

@@ -71,6 +92,7 @@ class CORE_EXPORT QgsGeoNodeRequest : public QObject

private:
QList<QgsServiceLayerDetail> parseLayers( QByteArray layerResponse );
QgsGeoNodeStyle retrieveStyle( QString styleUrl );

signals:
//! \brief emit a signal to be caught by qgisapp and display a statusQString on status bar
@@ -15,14 +15,10 @@
#include "qgstest.h"
#include <QtTest/QtTest>

#include <QtTest/QSignalSpy>
#include <QString>
#include <QMultiMap>
#include <iostream>

//#include "qgis_core.h"
#include "qgsgeonodeconnection.h"
#include "qgssettings.h"
#include "qgsgeonoderequest.h"
#include "qgslogger.h"

/** \ingroup UnitTests
* This is a unit test for the QgsGeoConnection class.
@@ -50,6 +46,9 @@ class TestQgsGeoNodeConnection: public QObject
// Check if we can create geonode connection from database.
void testCreation();

// Test API
void testStyleAPI();

private:
QString mGeoNodeConnectionName;
QString mGeoNodeConnectionURL;
@@ -71,13 +70,13 @@ void TestQgsGeoNodeConnection::initTestCase()
{
std::cout << "CTEST_FULL_OUTPUT" << std::endl;
mGeoNodeConnectionName = QStringLiteral( "ThisIsAGeoNodeConnection" );
mGeoNodeConnectionURL = QStringLiteral( "www.thisisageonodeurl.com" );
mGeoNodeConnectionURL = QStringLiteral( "http://www.thisisageonodeurl.com" );
mDemoGeoNodeName = QStringLiteral( "Demo GeoNode" );
mDemoGeoNodeURL = QStringLiteral( "demo.geonode.org" );
mDemoGeoNodeURL = QStringLiteral( "http://demo.geonode.org" );
mKartozaGeoNodeQGISServerName = QStringLiteral( "Staging Kartoza GeoNode QGIS Server" );
mKartozaGeoNodeQGISServerURL = QStringLiteral( "staging.geonode.kartoza.com" );
mKartozaGeoNodeQGISServerURL = QStringLiteral( "http://staging.geonode.kartoza.com" );
mKartozaGeoNodeGeoServerName = QStringLiteral( "Staging Kartoza GeoNode GeoServer" );
mKartozaGeoNodeGeoServerURL = QStringLiteral( "staginggs.geonode.kartoza.com" );
mKartozaGeoNodeGeoServerURL = QStringLiteral( "http://staginggs.geonode.kartoza.com" );

// Change it to skip remote testing
mSkipRemoteTest = true;
@@ -122,5 +121,30 @@ void TestQgsGeoNodeConnection::testCreation()
QVERIFY( newConnectionList.contains( mGeoNodeConnectionName ) );
}

// Test Layer API
void TestQgsGeoNodeConnection::testStyleAPI()
{
if ( !mSkipRemoteTest )
{
QSKIP( "Skip remote test for faster testing" );
}

QgsGeoNodeRequest geonodeRequest( mKartozaGeoNodeQGISServerURL, true );
QgsGeoNodeStyle defaultStyle = geonodeRequest.getDefaultStyle( QStringLiteral( "airports" ) );
QVERIFY( !defaultStyle.name.isEmpty() );
QVERIFY( defaultStyle.body.toString().startsWith( QStringLiteral( "<qgis" ) ) );
QVERIFY( defaultStyle.body.toString().contains( QStringLiteral( "</qgis>" ) ) );

QgsGeoNodeStyle geoNodeStyle = geonodeRequest.getStyle( "76" );
QVERIFY( !geoNodeStyle.name.isEmpty() );
QVERIFY( geoNodeStyle.body.toString().startsWith( QStringLiteral( "<qgis" ) ) );
QVERIFY( geoNodeStyle.body.toString().contains( QStringLiteral( "</qgis>" ) ) );

QList<QgsGeoNodeStyle> geoNodeStyles = geonodeRequest.getStyles( QStringLiteral( "airports" ) );
QgsDebugMsg( geoNodeStyles.count() );
QVERIFY( geoNodeStyles.count() == 2 );

}

QGSTEST_MAIN( TestQgsGeoNodeConnection )
#include "testqgsgeonodeconnection.moc"

0 comments on commit 3016158

Please sign in to comment.
You can’t perform that action at this time.