Skip to content
Permalink
Browse files

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

…p-layers-3_4

[server] Allow WMS GetFeatureInfo on root layer and groups
  • Loading branch information
elpaso committed Apr 8, 2019
2 parents ff16662 + c1cec89 commit 658d35d4e5ce3f4049d1eb1e3a94971d73f2e6bd
@@ -822,6 +822,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 );
@@ -843,7 +852,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 )
@@ -910,6 +919,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 );
@@ -1812,6 +1831,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

@@ -83,6 +83,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
@@ -886,7 +886,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" );
@@ -1183,7 +1184,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() );

@@ -1366,8 +1368,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 );
}
}
}

@@ -3139,4 +3173,41 @@ namespace QgsWms
std::unique_ptr<QImage> tmpImage( createImage( 1, 1, false ) );
return tmpImage->dotsPerMeterX() / 1000.0;
}

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

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

public:

@@ -130,6 +130,9 @@ def setUp(self):
self.projectUseLayerIdsPath = os.path.join(d, "project_use_layerids.qgs")
self.projectGroupsPath = os.path.join(d, "project_groups.qgs")

os.environ['QGIS_SERVER_SHOW_GROUP_SEPARATOR'] = '0'
os.environ['QGIS_SERVER_OVERRIDE_SYSTEM_LOCALE'] = 'en'

# Clean env just to be sure
env_vars = ['QUERY_STRING', 'QGIS_PROJECT_FILE']
for ev in env_vars:
@@ -516,6 +516,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=text/plain' +
'&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=text/plain' +
'&I=0&J=1' +
'&FEATURE_COUNT=10',
'wms_getfeatureinfo_group_name_areas_nested',
'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=text/plain' +
'&I=0&J=1' +
'&FEATURE_COUNT=10',
'wms_getfeatureinfo_group_name_areas_nested_shortname',
'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=text/plain' +
'&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=text/plain' +
'&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=text/plain' +
'&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=text/plain' +
'&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=text/plain' +
'&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=text/plain' +
'&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.
@@ -1,4 +1,4 @@
Content-Length: 6939
*****
Content-Type: text/xml; charset=utf-8

<?xml version="1.0" encoding="utf-8"?>
@@ -119,7 +119,7 @@ Content-Type: text/xml; charset=utf-8
<WFSLayers>
<WFSLayer name="testlayer èé"/>
</WFSLayers>
<Layer>
<Layer queryable="1">
<Title>QGIS Test Project</Title>
<Abstract>QGIS Test Project</Abstract>
<CRS>CRS:84</CRS>
@@ -198,7 +198,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 queryable="1" mutuallyExclusive="0" visible="1">
<Name>group_name</Name>
<Title>Group title</Title>
<Abstract>Group abstract</Abstract>
@@ -244,7 +244,7 @@ Content-Type: text/xml; charset=utf-8
</Attributes>
</Layer>
</Layer>
<Layer mutuallyExclusive="0" visible="1">
<Layer queryable="0" mutuallyExclusive="0" visible="1">
<Name>groupwithoutshortname</Name>
<Title>groupwithoutshortname</Title>
<CRS>CRS:84</CRS>

0 comments on commit 658d35d

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