Skip to content

Commit 3016158

Browse files
ismailsunninyalldawson
authored andcommitted
Add style retriever for a geonode layer.
1 parent 500c5c1 commit 3016158

File tree

4 files changed

+177
-11
lines changed

4 files changed

+177
-11
lines changed

python/core/geonode/qgsgeonoderequest.sip

+27
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ struct QgsServiceLayerDetail
2323
QString xyzURL;
2424
};
2525

26+
struct QgsGeoNodeStyle
27+
{
28+
%TypeHeaderCode
29+
#include <qgsgeonoderequest.h>
30+
%End
31+
QString id;
32+
QString name;
33+
QString title;
34+
QDomDocument body;
35+
QString styleUrl;
36+
};
37+
2638
class QgsGeoNodeRequest : QObject
2739
{
2840

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

59+
QList<QgsGeoNodeStyle> getStyles( QString layerName );
60+
%Docstring
61+
:rtype: list of QgsGeoNodeStyle
62+
%End
63+
64+
QgsGeoNodeStyle getDefaultStyle( QString layerName );
65+
%Docstring
66+
:rtype: QgsGeoNodeStyle
67+
%End
68+
69+
QgsGeoNodeStyle getStyle( QString styleID );
70+
%Docstring
71+
:rtype: QgsGeoNodeStyle
72+
%End
73+
4774
QStringList serviceUrls( QString serviceType );
4875
%Docstring
4976
:rtype: list of str

src/core/geonode/qgsgeonoderequest.cpp

+94-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,66 @@ QList<QgsServiceLayerDetail> QgsGeoNodeRequest::getLayers()
7070
return parseLayers( this->response() );
7171
}
7272

73+
QgsGeoNodeStyle QgsGeoNodeRequest::getDefaultStyle( QString layerName )
74+
{
75+
QgsGeoNodeStyle defaultStyle;
76+
bool success = request( QStringLiteral( "/api/layers?name=" ) + layerName );
77+
if ( !success )
78+
{
79+
return defaultStyle;
80+
}
81+
82+
QJsonDocument jsonDocument = QJsonDocument::fromJson( this->response() );
83+
QJsonObject jsonObject = jsonDocument.object();
84+
QList<QVariant> layers = jsonObject.toVariantMap()["objects"].toList();
85+
if ( layers.count() < 1 )
86+
{
87+
return defaultStyle;
88+
}
89+
QString defaultStyleUrl = layers[0].toMap()["default_style"].toString();
90+
91+
defaultStyle = retrieveStyle( defaultStyleUrl );
92+
93+
return defaultStyle;
94+
95+
}
96+
97+
QList<QgsGeoNodeStyle> QgsGeoNodeRequest::getStyles( QString layerName )
98+
{
99+
QList<QgsGeoNodeStyle> geoNodeStyles;
100+
bool success = request( QStringLiteral( "/api/styles?layer__name=" ) + layerName );
101+
if ( !success )
102+
{
103+
return geoNodeStyles;
104+
}
105+
106+
QJsonDocument jsonDocument = QJsonDocument::fromJson( this->response() );
107+
QJsonObject jsobObject = jsonDocument.object();
108+
QList<QVariant> styles = jsobObject.toVariantMap()["objects"].toList();
109+
110+
Q_FOREACH ( QVariant style, styles )
111+
{
112+
QVariantMap styleMap = style.toMap();
113+
QString styleUrl = styleMap["resource_uri"].toString();
114+
QgsGeoNodeStyle geoNodeStyle = retrieveStyle( styleUrl );
115+
if ( !geoNodeStyle.name.isEmpty() )
116+
{
117+
geoNodeStyles.append( geoNodeStyle );
118+
}
119+
}
120+
121+
return geoNodeStyles;
122+
123+
}
124+
125+
QgsGeoNodeStyle QgsGeoNodeRequest::getStyle( QString styleID )
126+
{
127+
QString endPoint = QStringLiteral( "/api/styles/" ) + styleID;
128+
129+
return retrieveStyle( endPoint );
130+
131+
}
132+
73133
void QgsGeoNodeRequest::replyProgress( qint64 bytesReceived, qint64 bytesTotal )
74134
{
75135
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
298358
return layers;
299359
}
300360

361+
QgsGeoNodeStyle QgsGeoNodeRequest::retrieveStyle( QString styleUrl )
362+
{
363+
QgsGeoNodeStyle geoNodeStyle;
364+
365+
bool success = request( styleUrl );
366+
if ( !success )
367+
{
368+
return geoNodeStyle;
369+
}
370+
QJsonDocument jsonDocument = QJsonDocument::fromJson( this->response() );
371+
QJsonObject jsonObject = jsonDocument.object();
372+
373+
geoNodeStyle.id = jsonObject.toVariantMap()["id"].toString();
374+
geoNodeStyle.name = jsonObject.toVariantMap()["name"].toString();
375+
geoNodeStyle.title = jsonObject.toVariantMap()["title"].toString();
376+
geoNodeStyle.styleUrl = jsonObject.toVariantMap()["style_url"].toString();
377+
378+
success = request( geoNodeStyle.styleUrl );
379+
if ( !success )
380+
{
381+
return geoNodeStyle;
382+
}
383+
384+
success = geoNodeStyle.body.setContent( this->response() );
385+
if ( !success )
386+
{
387+
return geoNodeStyle;
388+
}
389+
390+
return geoNodeStyle;
391+
}
392+
301393
QStringList QgsGeoNodeRequest::serviceUrls( QString serviceType )
302394
{
303395
QStringList urls;
@@ -393,7 +485,8 @@ bool QgsGeoNodeRequest::request( QString endPoint )
393485
abort();
394486
mIsAborted = false;
395487
QgsMessageLog::logMessage( mBaseUrl, tr( "GeoNode" ) );
396-
QString url = mBaseUrl + endPoint;
488+
// Handle case where the endpoint is full url
489+
QString url = endPoint.startsWith( mBaseUrl ) ? endPoint : mBaseUrl + endPoint;
397490
QgsMessageLog::logMessage( url, tr( "GeoNode" ) );
398491
setProtocol( url.split( "://" )[0] );
399492
QUrl layerUrl( url );

src/core/geonode/qgsgeonoderequest.h

+22
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "qgis.h"
1919
#include "qgis_core.h"
2020
#include <qnetworkreply.h>
21+
#include <QDomDocument>
2122

2223

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

43+
struct CORE_EXPORT QgsGeoNodeStyle
44+
{
45+
#ifdef SIP_RUN
46+
% TypeHeaderCode
47+
#include <qgsgeonoderequest.h>
48+
% End
49+
#endif
50+
QString id;
51+
QString name;
52+
QString title;
53+
QDomDocument body;
54+
QString styleUrl;
55+
};
56+
4257
class CORE_EXPORT QgsGeoNodeRequest : public QObject
4358
{
4459
Q_OBJECT
@@ -51,6 +66,12 @@ class CORE_EXPORT QgsGeoNodeRequest : public QObject
5166

5267
QList<QgsServiceLayerDetail> getLayers();
5368

69+
QList<QgsGeoNodeStyle> getStyles( QString layerName );
70+
71+
QgsGeoNodeStyle getDefaultStyle( QString layerName );
72+
73+
QgsGeoNodeStyle getStyle( QString styleID );
74+
5475
// Obtain list of unique URL in the geonode
5576
QStringList serviceUrls( QString serviceType );
5677

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

7293
private:
7394
QList<QgsServiceLayerDetail> parseLayers( QByteArray layerResponse );
95+
QgsGeoNodeStyle retrieveStyle( QString styleUrl );
7496

7597
signals:
7698
//! \brief emit a signal to be caught by qgisapp and display a statusQString on status bar

tests/src/core/testqgsgeonodeconnection.cpp

+34-10
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@
1515
#include "qgstest.h"
1616
#include <QtTest/QtTest>
1717

18-
#include <QtTest/QSignalSpy>
19-
#include <QString>
20-
#include <QMultiMap>
21-
#include <iostream>
22-
23-
//#include "qgis_core.h"
2418
#include "qgsgeonodeconnection.h"
2519
#include "qgssettings.h"
20+
#include "qgsgeonoderequest.h"
21+
#include "qgslogger.h"
2622

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

49+
// Test API
50+
void testStyleAPI();
51+
5352
private:
5453
QString mGeoNodeConnectionName;
5554
QString mGeoNodeConnectionURL;
@@ -71,13 +70,13 @@ void TestQgsGeoNodeConnection::initTestCase()
7170
{
7271
std::cout << "CTEST_FULL_OUTPUT" << std::endl;
7372
mGeoNodeConnectionName = QStringLiteral( "ThisIsAGeoNodeConnection" );
74-
mGeoNodeConnectionURL = QStringLiteral( "www.thisisageonodeurl.com" );
73+
mGeoNodeConnectionURL = QStringLiteral( "http://www.thisisageonodeurl.com" );
7574
mDemoGeoNodeName = QStringLiteral( "Demo GeoNode" );
76-
mDemoGeoNodeURL = QStringLiteral( "demo.geonode.org" );
75+
mDemoGeoNodeURL = QStringLiteral( "http://demo.geonode.org" );
7776
mKartozaGeoNodeQGISServerName = QStringLiteral( "Staging Kartoza GeoNode QGIS Server" );
78-
mKartozaGeoNodeQGISServerURL = QStringLiteral( "staging.geonode.kartoza.com" );
77+
mKartozaGeoNodeQGISServerURL = QStringLiteral( "http://staging.geonode.kartoza.com" );
7978
mKartozaGeoNodeGeoServerName = QStringLiteral( "Staging Kartoza GeoNode GeoServer" );
80-
mKartozaGeoNodeGeoServerURL = QStringLiteral( "staginggs.geonode.kartoza.com" );
79+
mKartozaGeoNodeGeoServerURL = QStringLiteral( "http://staginggs.geonode.kartoza.com" );
8180

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

124+
// Test Layer API
125+
void TestQgsGeoNodeConnection::testStyleAPI()
126+
{
127+
if ( !mSkipRemoteTest )
128+
{
129+
QSKIP( "Skip remote test for faster testing" );
130+
}
131+
132+
QgsGeoNodeRequest geonodeRequest( mKartozaGeoNodeQGISServerURL, true );
133+
QgsGeoNodeStyle defaultStyle = geonodeRequest.getDefaultStyle( QStringLiteral( "airports" ) );
134+
QVERIFY( !defaultStyle.name.isEmpty() );
135+
QVERIFY( defaultStyle.body.toString().startsWith( QStringLiteral( "<qgis" ) ) );
136+
QVERIFY( defaultStyle.body.toString().contains( QStringLiteral( "</qgis>" ) ) );
137+
138+
QgsGeoNodeStyle geoNodeStyle = geonodeRequest.getStyle( "76" );
139+
QVERIFY( !geoNodeStyle.name.isEmpty() );
140+
QVERIFY( geoNodeStyle.body.toString().startsWith( QStringLiteral( "<qgis" ) ) );
141+
QVERIFY( geoNodeStyle.body.toString().contains( QStringLiteral( "</qgis>" ) ) );
142+
143+
QList<QgsGeoNodeStyle> geoNodeStyles = geonodeRequest.getStyles( QStringLiteral( "airports" ) );
144+
QgsDebugMsg( geoNodeStyles.count() );
145+
QVERIFY( geoNodeStyles.count() == 2 );
146+
147+
}
148+
125149
QGSTEST_MAIN( TestQgsGeoNodeConnection )
126150
#include "testqgsgeonodeconnection.moc"

0 commit comments

Comments
 (0)