Skip to content

Commit

Permalink
Add style retriever for a geonode layer.
Browse files Browse the repository at this point in the history
  • Loading branch information
ismailsunni authored and nyalldawson committed Sep 12, 2017
1 parent 500c5c1 commit 3016158
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 11 deletions.
27 changes: 27 additions & 0 deletions python/core/geonode/qgsgeonoderequest.sip
Expand Up @@ -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
{

Expand All @@ -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
Expand Down
95 changes: 94 additions & 1 deletion src/core/geonode/qgsgeonoderequest.cpp
Expand Up @@ -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 ) );
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
Expand Down
22 changes: 22 additions & 0 deletions src/core/geonode/qgsgeonoderequest.h
Expand Up @@ -18,6 +18,7 @@
#include "qgis.h"
#include "qgis_core.h"
#include <qnetworkreply.h>
#include <QDomDocument>


#include <QObject>
Expand All @@ -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
Expand All @@ -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 );

Expand All @@ -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
Expand Down
44 changes: 34 additions & 10 deletions tests/src/core/testqgsgeonodeconnection.cpp
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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.