Skip to content

Commit

Permalink
[geonode] a generic way to consume geonode api. fixes #31376
Browse files Browse the repository at this point in the history
  • Loading branch information
myarjunar authored and nyalldawson committed Nov 10, 2019
1 parent fb81d40 commit b45d93a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 86 deletions.
169 changes: 83 additions & 86 deletions src/core/geocms/geonode/qgsgeonoderequest.cpp
Expand Up @@ -269,111 +269,106 @@ QList<QgsGeoNodeRequest::ServiceLayerDetail> QgsGeoNodeRequest::parseLayers( con
const QJsonObject jsonObject = jsonDocument.object();
const QVariantMap jsonVariantMap = jsonObject.toVariantMap();
const QVariantList layerList = jsonVariantMap.value( QStringLiteral( "objects" ) ).toList();
qint16 majorVersion;
qint16 minorVersion;
if ( jsonVariantMap.contains( QStringLiteral( "geonode_version" ) ) )

QString wmsURLFormat, wfsURLFormat, xyzURLFormat;

for ( const QVariant &layer : qgis::as_const( layerList ) )
{
QRegularExpression re( "((\\d+)(\\.\\d+))" );
QRegularExpressionMatch match = re.match( jsonVariantMap.value( QStringLiteral( "geonode_version" ) ).toString() );
if ( match.hasMatch() )
QgsGeoNodeRequest::ServiceLayerDetail layerStruct;
const QVariantMap layerMap = layer.toMap();
QVariantList layerLinks = layerMap.value( QStringLiteral( "links" ) ).toList();

if ( layerMap.value( QStringLiteral( "typename" ) ).toString().isEmpty() )
{
const QStringList geonodeVersionSplit = match.captured( 0 ).split( '.' );
majorVersion = geonodeVersionSplit.at( 0 ).toInt();
minorVersion = geonodeVersionSplit.at( 1 ).toInt();
const QStringList splitURL = layerMap.value( QStringLiteral( "detail_url" ) ).toString().split( '/' );
layerStruct.typeName = splitURL.at( splitURL.length() - 1 );
}
else
layerStruct.uuid = layerMap.value( QStringLiteral( "uuid" ) ).toString();
layerStruct.id = layerMap.value( QStringLiteral( "id" ) ).toString();
layerStruct.name = layerMap.value( QStringLiteral( "name" ) ).toString();
layerStruct.typeName = layerMap.value( QStringLiteral( "typename" ) ).toString();
layerStruct.title = layerMap.value( QStringLiteral( "title" ) ).toString();

layerStruct = parseOWSUrl( layerStruct, layerLinks );

if ( layerStruct.wmsURL.isEmpty() && layerStruct.wfsURL.isEmpty() && layerStruct.xyzURL.isEmpty() )
{
return layers;
if ( wmsURLFormat.isEmpty() && wfsURLFormat.isEmpty() && xyzURLFormat.isEmpty() )
{
bool success = requestBlocking( QStringLiteral( "/api/layers/" ) + layerStruct.id );
if ( success )
{
const QJsonDocument resourceUriDocument = QJsonDocument::fromJson( this->lastResponse() );
const QJsonObject resourceUriObject = resourceUriDocument.object();
const QVariantMap resourceUriMap = resourceUriObject.toVariantMap();
QVariantList resourceUriLinks = resourceUriMap.value( QStringLiteral( "links" ) ).toList();
QgsGeoNodeRequest::ServiceLayerDetail tempLayerStruct;
tempLayerStruct = parseOWSUrl( tempLayerStruct, resourceUriLinks );

// Avoid iterating all the layers to get the service url. Instead, generate a string format once we found one service url
// for every service (wms, wfs, xyz). And then use the string format for the other layers since they are identical.
if ( tempLayerStruct.server == "qgis-server" )
{
wmsURLFormat = !tempLayerStruct.wmsURL.isEmpty() ? tempLayerStruct.wmsURL.replace( layerStruct.name, "%1" ) : "";
wfsURLFormat = !tempLayerStruct.wfsURL.isEmpty() ? tempLayerStruct.wfsURL.replace( layerStruct.name, "%1" ) : "";
xyzURLFormat = !tempLayerStruct.xyzURL.isEmpty() ? tempLayerStruct.xyzURL.replace( layerStruct.name, "%1" ) : "";
}
else if ( tempLayerStruct.server == "geoserver" )
{
wmsURLFormat = !tempLayerStruct.wmsURL.isEmpty() ? tempLayerStruct.wmsURL : "";
wfsURLFormat = !tempLayerStruct.wfsURL.isEmpty() ? tempLayerStruct.wfsURL : "";
xyzURLFormat = !tempLayerStruct.xyzURL.isEmpty() ? tempLayerStruct.xyzURL.replace( layerStruct.name, "%1" ) : "";
}
}
else
{
layers.append( layerStruct );
continue;
}
}

// Replace string argument with the layer id.
layerStruct.wmsURL = wmsURLFormat.contains( "%1" ) ? wmsURLFormat.arg( layerStruct.name ) : wmsURLFormat;
layerStruct.wfsURL = wfsURLFormat.contains( "%1" ) ? wfsURLFormat.arg( layerStruct.name ) : wfsURLFormat;
layerStruct.xyzURL = xyzURLFormat.contains( "%1" ) ? xyzURLFormat.arg( layerStruct.name ) : xyzURLFormat;
}
}
else
{
majorVersion = 2;
minorVersion = 6;

layers.append( layerStruct );
}

if ( majorVersion == 2 && minorVersion == 6 )
return layers;
}

QgsGeoNodeRequest::ServiceLayerDetail QgsGeoNodeRequest::parseOWSUrl( QgsGeoNodeRequest::ServiceLayerDetail &layerStruct, const QVariantList &layerLinks )
{
QString urlFound;
for ( const QVariant &link : layerLinks )
{
for ( const QVariant &layer : qgis::as_const( layerList ) )
const QVariantMap linkMap = link.toMap();
if ( linkMap.contains( QStringLiteral( "link_type" ) ) )
{
QgsGeoNodeRequest::ServiceLayerDetail layerStruct;
const QVariantMap layerMap = layer.toMap();
// Find WMS and WFS. XYZ is not available
// Trick to get layer's typename from distribution_url or detail_url
QString layerTypeName = layerMap.value( QStringLiteral( "detail_url" ) ).toString().split( '/' ).last();
if ( layerTypeName.isEmpty() )
if ( linkMap.value( QStringLiteral( "link_type" ) ) == QStringLiteral( "OGC:WMS" ) )
{
layerTypeName = layerMap.value( QStringLiteral( "distribution_url" ) ).toString().split( '/' ).last();
urlFound = layerStruct.wmsURL = linkMap.value( QStringLiteral( "url" ) ).toString();
}
// On this step, layerTypeName is in WORKSPACE%3ALAYERNAME or WORKSPACE:LAYERNAME format
if ( layerTypeName.contains( QStringLiteral( "%3A" ) ) )
else if ( linkMap.value( QStringLiteral( "link_type" ) ) == QStringLiteral( "OGC:WFS" ) )
{
layerTypeName.replace( QStringLiteral( "%3A" ), QStringLiteral( ":" ) );
urlFound = layerStruct.wfsURL = linkMap.value( QStringLiteral( "url" ) ).toString();
}
// On this step, layerTypeName is in WORKSPACE:LAYERNAME format
const QStringList splitURL = layerTypeName.split( ':' );
QString layerWorkspace = splitURL.at( 0 );
QString layerName = splitURL.at( 1 );

layerStruct.name = layerName;
layerStruct.typeName = layerTypeName;
layerStruct.uuid = layerMap.value( QStringLiteral( "uuid" ) ).toString();
layerStruct.title = layerMap.value( QStringLiteral( "title" ) ).toString();

// WMS url : BASE_URI/geoserver/WORKSPACE/wms
layerStruct.wmsURL = mBaseUrl + QStringLiteral( "/geoserver/" ) + layerWorkspace + QStringLiteral( "/wms" );
// WFS url : BASE_URI/geoserver/WORKSPACE/wfs
layerStruct.wfsURL = mBaseUrl + QStringLiteral( "/geoserver/" ) + layerWorkspace + QStringLiteral( "/wfs" );
// XYZ url : set to empty string
layerStruct.xyzURL.clear();

layers.append( layerStruct );
}
}
// Geonode version 2.7 or newer
else if ( ( majorVersion == 2 && minorVersion >= 7 ) || ( majorVersion >= 3 ) )
{
for ( const QVariant &layer : qgis::as_const( layerList ) )
{
QgsGeoNodeRequest::ServiceLayerDetail layerStruct;
const QVariantMap layerMap = layer.toMap();
// Find WMS, WFS, and XYZ link
const QVariantList layerLinks = layerMap.value( QStringLiteral( "links" ) ).toList();
for ( const QVariant &link : layerLinks )
else if ( linkMap.value( QStringLiteral( "link_type" ) ) == QStringLiteral( "image" ) )
{
const QVariantMap linkMap = link.toMap();
if ( linkMap.contains( QStringLiteral( "link_type" ) ) )
if ( linkMap.contains( QStringLiteral( "name" ) ) && linkMap.value( QStringLiteral( "name" ) ) == QStringLiteral( "Tiles" ) )
{
if ( linkMap.value( QStringLiteral( "link_type" ) ) == QStringLiteral( "OGC:WMS" ) )
{
layerStruct.wmsURL = linkMap.value( QStringLiteral( "url" ) ).toString();
}
else if ( linkMap.value( QStringLiteral( "link_type" ) ) == QStringLiteral( "OGC:WFS" ) )
{
layerStruct.wfsURL = linkMap.value( QStringLiteral( "url" ) ).toString();
}
else if ( linkMap.value( QStringLiteral( "link_type" ) ) == QStringLiteral( "image" ) )
{
if ( linkMap.contains( QStringLiteral( "name" ) ) && linkMap.value( QStringLiteral( "name" ) ) == QStringLiteral( "Tiles" ) )
{
layerStruct.xyzURL = linkMap.value( QStringLiteral( "url" ) ).toString();
}
}
urlFound = layerStruct.xyzURL = linkMap.value( QStringLiteral( "url" ) ).toString();
}
}
if ( layerMap.value( QStringLiteral( "typename" ) ).toString().isEmpty() )
{
const QStringList splitURL = layerMap.value( QStringLiteral( "detail_url" ) ).toString().split( '/' );
layerStruct.typeName = splitURL.at( splitURL.length() - 1 );
}
layerStruct.uuid = layerMap.value( QStringLiteral( "uuid" ) ).toString();
layerStruct.name = layerMap.value( QStringLiteral( "name" ) ).toString();
layerStruct.typeName = layerMap.value( QStringLiteral( "typename" ) ).toString();
layerStruct.title = layerMap.value( QStringLiteral( "title" ) ).toString();
layers.append( layerStruct );
}

if ( layerStruct.server.isEmpty() )
layerStruct.server = urlFound.contains( QStringLiteral( "qgis-server" ) ) ? QStringLiteral( "qgis-server" ) : QStringLiteral( "geoserver" );
}
return layers;

return layerStruct;
}

QgsGeoNodeStyle QgsGeoNodeRequest::retrieveStyle( const QString &styleUrl )
Expand Down Expand Up @@ -529,6 +524,8 @@ bool QgsGeoNodeRequest::requestBlocking( const QString &endPoint )
QNetworkReply *QgsGeoNodeRequest::requestUrl( const QString &url )
{
QNetworkRequest request( url );
request.setAttribute( QNetworkRequest::FollowRedirectsAttribute, true );

QgsSetRequestInitiatorClass( request, QStringLiteral( "QgsGeoNodeRequest" ) );
// Add authentication check here

Expand Down
9 changes: 9 additions & 0 deletions src/core/geocms/geonode/qgsgeonoderequest.h
Expand Up @@ -74,6 +74,8 @@ class CORE_EXPORT QgsGeoNodeRequest : public QObject
{
//! Unique identifier (generate on the client side, not at the GeoNode server)
QUuid uuid;
//! Layer id
QString id;
//! Layer name
QString name;
//! Layer type name
Expand All @@ -86,6 +88,8 @@ class CORE_EXPORT QgsGeoNodeRequest : public QObject
QString wfsURL;
//! XYZ tileserver URL for layer
QString xyzURL;
//! Backend server (geoserver or qgis-server)
QString server;
};

/**
Expand Down Expand Up @@ -213,6 +217,11 @@ class CORE_EXPORT QgsGeoNodeRequest : public QObject
*/
void setProtocol( const QString &protocol );

/**
* Returns the updated ServiceLayerDetail struct with WMS/WFS/XYZ url.
*/
QgsGeoNodeRequest::ServiceLayerDetail parseOWSUrl( QgsGeoNodeRequest::ServiceLayerDetail &layerStruct, const QVariantList &layerLinks );

public slots:

/**
Expand Down

0 comments on commit b45d93a

Please sign in to comment.