Skip to content
Permalink
Browse files

Merge pull request #9718 from elpaso/bugfix-21697-server-gfi-top-grou…

…p-layers-3_6

[server] Allow WMS GetFeatureInfo on root layer and groups
  • Loading branch information
elpaso committed Apr 5, 2019
2 parents eb88cc4 + ecefb84 commit 0e8b90de44945e1a1b20a2b6fd6aec2c055bfb03
@@ -841,6 +841,15 @@ namespace QgsWms
layerParentElem.appendChild( treeNameElem );
}

if ( hasQueryableChildren( projectLayerTreeRoot, QgsServerProjectUtils::wmsRestrictedLayers( *project ) ) )
{
layerParentElem.setAttribute( QStringLiteral( "queryable" ), QStringLiteral( "1" ) );
}
else
{
layerParentElem.setAttribute( QStringLiteral( "queryable" ), QStringLiteral( "0" ) );
}

appendLayersFromTreeGroup( doc, layerParentElem, serverIface, project, version, request, projectLayerTreeRoot, projectSettings );

combineExtentAndCrsOfGroupChildren( doc, layerParentElem, project, true );
@@ -862,7 +871,7 @@ namespace QgsWms
{
bool useLayerIds = QgsServerProjectUtils::wmsUseLayerIds( *project );
bool siaFormat = QgsServerProjectUtils::wmsInfoFormatSia2045( *project );
QStringList restrictedLayers = QgsServerProjectUtils::wmsRestrictedLayers( *project );
const QStringList restrictedLayers = QgsServerProjectUtils::wmsRestrictedLayers( *project );

QList< QgsLayerTreeNode * > layerTreeGroupChildren = layerTreeGroup->children();
for ( int i = 0; i < layerTreeGroupChildren.size(); ++i )
@@ -929,6 +938,16 @@ namespace QgsWms
layerElem.appendChild( treeNameElem );
}

// Set queryable if any of the children are
if ( hasQueryableChildren( treeNode, restrictedLayers ) )
{
layerElem.setAttribute( QStringLiteral( "queryable" ), QStringLiteral( "1" ) );
}
else
{
layerElem.setAttribute( QStringLiteral( "queryable" ), QStringLiteral( "0" ) );
}

appendLayersFromTreeGroup( doc, layerElem, serverIface, project, version, request, treeGroupChild, projectSettings );

combineExtentAndCrsOfGroupChildren( doc, layerElem, project );
@@ -1857,6 +1876,26 @@ namespace QgsWms
}
}

bool hasQueryableChildren( const QgsLayerTreeNode *childNode, const QStringList &wmsRestrictedLayers )
{
if ( childNode->nodeType() == QgsLayerTreeNode::NodeGroup )
{
for ( int j = 0; j < childNode->children().size(); ++j )
{
if ( hasQueryableChildren( childNode->children().at( j ), wmsRestrictedLayers ) )
return true;
}
return false;
}
else if ( childNode->nodeType() == QgsLayerTreeNode::NodeLayer )
{
const auto treeLayer { static_cast<const QgsLayerTreeLayer *>( childNode ) };
const auto l { treeLayer->layer() };
return ! wmsRestrictedLayers.contains( l->name() ) && l->flags().testFlag( QgsMapLayer::Identifiable );
}
return false;
}


} // namespace QgsWms

@@ -82,6 +82,8 @@ namespace QgsWms
QDomDocument getCapabilities( QgsServerInterface *serverIface, const QgsProject *project,
const QString &version, const QgsServerRequest &request,
bool projectSettings );

bool hasQueryableChildren( const QgsLayerTreeNode *childNode, const QStringList &wmsRestrictedLayers );
} // namespace QgsWms

#endif
@@ -1020,7 +1020,8 @@ namespace QgsWms
{
// Verifying Mandatory parameters
// The QUERY_LAYERS parameter is Mandatory
QStringList queryLayers = mWmsParameters.queryLayersNickname();
QStringList queryLayers = flattenedQueryLayers();

if ( queryLayers.isEmpty() )
{
QString msg = QObject::tr( "QUERY_LAYERS parameter is required for GetFeatureInfo" );
@@ -1319,7 +1320,8 @@ namespace QgsWms
QDomDocument QgsRenderer::featureInfoDocument( QList<QgsMapLayer *> &layers, const QgsMapSettings &mapSettings,
const QImage *outputImage, const QString &version ) const
{
QStringList queryLayers = mWmsParameters.queryLayersNickname();

const QStringList queryLayers = flattenedQueryLayers( );

bool ijDefined = ( !mWmsParameters.i().isEmpty() && !mWmsParameters.j().isEmpty() );

@@ -1502,8 +1504,40 @@ namespace QgsWms
}
else if ( ( validLayer && !queryableLayer ) || ( !validLayer && mLayerGroups.contains( queryLayer ) ) )
{
QString msg = QObject::tr( "Layer '%1' is not queryable" ).arg( queryLayer );
throw QgsBadRequestException( QStringLiteral( "LayerNotQueryable" ), msg );
auto queryLayerName { queryLayer };
// Check if this layer belongs to a group and the group has any queryable layers
bool hasGroupAndQueryable { false };
if ( ! mWmsParameters.queryLayersNickname().contains( queryLayer ) )
{
// Find which group this layer belongs to
const auto &constNicks { mWmsParameters.queryLayersNickname() };
for ( const auto &ql : constNicks )
{
if ( mLayerGroups.contains( ql ) )
{
const auto &constLayers { mLayerGroups[ql] };
for ( const auto &ml : constLayers )
{
if ( ( ! ml->shortName().isEmpty() && ml->shortName() == queryLayer ) || ( ml->name() == queryLayer ) )
{
queryLayerName = ql;
}
if ( ml->flags().testFlag( QgsMapLayer::Identifiable ) )
{
hasGroupAndQueryable = true;
break;
}
}
break;
}
}
}
// Only throw if it's not a group or the group has no queryable children
if ( ! hasGroupAndQueryable )
{
const QString msg { QObject::tr( "The layer '%1' is not queryable." ).arg( queryLayerName ) };
throw QgsBadRequestException( QStringLiteral( "LayerNotQueryable" ), msg );
}
}
}

@@ -2386,7 +2420,7 @@ namespace QgsWms
exporter.setAttributes( attributes );
exporter.setIncludeGeometry( withGeometry );

for ( const auto feature : features )
for ( const auto &feature : qgis::as_const( features ) )
{
if ( json.right( 1 ).compare( QStringLiteral( "}" ) ) == 0 )
{
@@ -2399,6 +2433,10 @@ namespace QgsWms
}
else // raster layer
{
if ( json.right( 1 ).compare( QStringLiteral( "}" ) ) == 0 )
{
json.append( QStringLiteral( "," ) );
}
json.append( QStringLiteral( "{" ) );
json.append( QStringLiteral( "\"type\":\"Feature\",\n" ) );
json.append( QStringLiteral( "\"id\":\"%1\",\n" ).arg( layer->name() ) );
@@ -3451,4 +3489,31 @@ namespace QgsWms
}
}

QStringList QgsRenderer::flattenedQueryLayers() const
{
QStringList result;
std::function <QStringList( const QString &name )> findLeaves = [ & ]( const QString & name ) -> QStringList
{
QStringList _result;
if ( mLayerGroups.contains( name ) )
{
for ( const auto &l : mLayerGroups[ name ] )
{
_result.append( findLeaves( l->shortName().isEmpty() ? l->name() : l->shortName() ) );
}
}
else
{
_result.append( name );
}
return _result;
};
for ( const auto &name : mWmsParameters.queryLayersNickname() )
{
result.append( findLeaves( name ) );
}
return result;
}


} // namespace QgsWms
@@ -306,7 +306,8 @@ namespace QgsWms
QStringList mRestrictedLayers;
QMap<QString, QgsMapLayer *> mNicknameLayers;
QMap<QString, QList<QgsMapLayer *> > mLayerGroups;
QList<QgsMapLayer *> mTemporaryLayers;
QList<QgsMapLayer *> mTemporaryLayers;
QStringList flattenedQueryLayers() const;

public:

@@ -591,6 +591,117 @@ def testGetFeatureInfoPostgresTypes(self):
attribute.get('value')), {
'c': 4.0, 'd': 5.0})

def testGetFeatureInfoGroupedLayers(self):
"""Test that we can get feature info from the top and group layers"""

# areas+and+symbols (not nested)
self.wms_request_compare('GetFeatureInfo',
'&BBOX=52.44095517977704901,10.71171069440170776,52.440955186258563,10.71171070552261817' +
'&CRS=EPSG:4326' +
'&WIDTH=2&HEIGHT=2' +
'&QUERY_LAYERS=areas+and+symbols' +
'&INFO_FORMAT=application/json' +
'&I=0&J=1' +
'&FEATURE_COUNT=10',
'wms_getfeatureinfo_group_name_areas',
'test_project_wms_grouped_layers.qgs')

# areas+and+symbols (nested)
self.wms_request_compare('GetFeatureInfo',
'&BBOX=52.44095517977704901,10.71171069440170776,52.440955186258563,10.71171070552261817' +
'&CRS=EPSG:4326' +
'&WIDTH=2&HEIGHT=2' +
'&QUERY_LAYERS=areas+and+symbols' +
'&INFO_FORMAT=application/json' +
'&I=0&J=1' +
'&FEATURE_COUNT=10',
'wms_getfeatureinfo_group_name_areas',
'test_project_wms_grouped_nested_layers.qgs')

# as-areas-short-name
self.wms_request_compare('GetFeatureInfo',
'&BBOX=52.44095517977704901,10.71171069440170776,52.440955186258563,10.71171070552261817' +
'&CRS=EPSG:4326' +
'&WIDTH=2&HEIGHT=2' +
'&QUERY_LAYERS=as-areas-short-name' +
'&INFO_FORMAT=application/json' +
'&I=0&J=1' +
'&FEATURE_COUNT=10',
'wms_getfeatureinfo_group_name_areas',
'test_project_wms_grouped_nested_layers.qgs')

# Top level: QGIS Server - Grouped Layer
self.wms_request_compare('GetFeatureInfo',
'&BBOX=52.44095517977704901,10.71171069440170776,52.440955186258563,10.71171070552261817' +
'&CRS=EPSG:4326' +
'&WIDTH=2&HEIGHT=2' +
'&QUERY_LAYERS=QGIS+Server+-+Grouped Nested Layer' +
'&INFO_FORMAT=application/json' +
'&I=0&J=1' +
'&FEATURE_COUNT=10',
'wms_getfeatureinfo_group_name_top',
'test_project_wms_grouped_nested_layers.qgs')

# Multiple matches from 2 layer groups
self.wms_request_compare('GetFeatureInfo',
'&BBOX=52.44095517977704901,10.71171069440170776,52.440955186258563,10.71171070552261817' +
'&CRS=EPSG:4326' +
'&WIDTH=2&HEIGHT=2' +
'&QUERY_LAYERS=areas+and+symbols,city+and+district+boundaries' +
'&INFO_FORMAT=application/json' +
'&I=0&J=1' +
'&FEATURE_COUNT=10',
'wms_getfeatureinfo_group_name_areas_cities',
'test_project_wms_grouped_nested_layers.qgs')

# no_query group (nested)
self.wms_request_compare('GetFeatureInfo',
'&BBOX=52.44095517977704901,10.71171069440170776,52.440955186258563,10.71171070552261817' +
'&CRS=EPSG:4326' +
'&WIDTH=2&HEIGHT=2' +
'&QUERY_LAYERS=no_query' +
'&INFO_FORMAT=application/json' +
'&I=0&J=1' +
'&FEATURE_COUNT=10',
'wms_getfeatureinfo_group_no_query',
'test_project_wms_grouped_nested_layers.qgs')

# query_child group (nested)
self.wms_request_compare('GetFeatureInfo',
'&BBOX=52.44095517977704901,10.71171069440170776,52.440955186258563,10.71171070552261817' +
'&CRS=EPSG:4326' +
'&WIDTH=2&HEIGHT=2' +
'&QUERY_LAYERS=query_child' +
'&INFO_FORMAT=application/json' +
'&I=0&J=1' +
'&FEATURE_COUNT=10',
'wms_getfeatureinfo_group_query_child',
'test_project_wms_grouped_nested_layers.qgs')

# child_ok group (nested)
self.wms_request_compare('GetFeatureInfo',
'&BBOX=52.44095517977704901,10.71171069440170776,52.440955186258563,10.71171070552261817' +
'&CRS=EPSG:4326' +
'&WIDTH=2&HEIGHT=2' +
'&QUERY_LAYERS=child_ok' +
'&INFO_FORMAT=application/json' +
'&I=0&J=1' +
'&FEATURE_COUNT=10',
'wms_getfeatureinfo_group_query_child',
'test_project_wms_grouped_nested_layers.qgs')

# as_areas_query_copy == as-areas-short-name-query-copy (nested)
self.wms_request_compare('GetFeatureInfo',
'&BBOX=52.44095517977704901,10.71171069440170776,52.440955186258563,10.71171070552261817' +
'&CRS=EPSG:4326' +
'&WIDTH=2&HEIGHT=2' +
'&QUERY_LAYERS=as-areas-short-name-query-copy' +
'&INFO_FORMAT=application/json' +
'&I=0&J=1' +
'&FEATURE_COUNT=10',
'wms_getfeatureinfo_group_query_child',
'test_project_wms_grouped_nested_layers.qgs')


if __name__ == '__main__':
unittest.main()
Binary file not shown.
@@ -283,7 +283,7 @@ Content-Type: text/xml; charset=utf-8
<Attribute precision="0" type="QString" editType="TextEdit" typeName="String" name="utf8nameè" comment="" length="13"/>
</Attributes>
</Layer>
<Layer mutuallyExclusive="0" visible="1">
<Layer mutuallyExclusive="0" visible="1" queryable="1">
<Name>group_name</Name>
<Title>Group title</Title>
<Abstract>Group abstract</Abstract>
@@ -329,7 +329,7 @@ Content-Type: text/xml; charset=utf-8
</Attributes>
</Layer>
</Layer>
<Layer mutuallyExclusive="0" visible="1">
<Layer mutuallyExclusive="0" queryable="0" visible="1">
<Name>groupwithoutshortname</Name>
<Title>groupwithoutshortname</Title>
<CRS>CRS:84</CRS>

0 comments on commit 0e8b90d

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