From f4a44dcc93171682399dd6f995ca3ffce4a22c65 Mon Sep 17 00:00:00 2001 From: pathmapper Date: Sat, 13 Jan 2024 15:25:04 +0100 Subject: [PATCH] Add WMS with_maptip parameter value to request only maptip for HTML feature info response --- src/server/services/wms/qgswmsparameters.cpp | 25 ++++++- src/server/services/wms/qgswmsparameters.h | 15 ++++ src/server/services/wms/qgswmsrenderer.cpp | 73 +++++++++++++++---- .../test_qgsserver_wms_getfeatureinfo.py | 20 +++++ ...atureinfo-html-only-with-maptip-raster.txt | 4 + ...atureinfo-html-only-with-maptip-vector.txt | 4 + 6 files changed, 123 insertions(+), 18 deletions(-) create mode 100644 tests/testdata/qgis_server/wms_getfeatureinfo-html-only-with-maptip-raster.txt create mode 100644 tests/testdata/qgis_server/wms_getfeatureinfo-html-only-with-maptip-vector.txt diff --git a/src/server/services/wms/qgswmsparameters.cpp b/src/server/services/wms/qgswmsparameters.cpp index fd09aae686cd..33946c4366ca 100644 --- a/src/server/services/wms/qgswmsparameters.cpp +++ b/src/server/services/wms/qgswmsparameters.cpp @@ -541,8 +541,7 @@ namespace QgsWms save( pWithGeometry ); const QgsWmsParameter pWithMapTip( QgsWmsParameter::WITH_MAPTIP, - QVariant::Bool, - QVariant( false ) ); + QVariant::String ); save( pWithMapTip ); const QgsWmsParameter pWithDisplayName( QgsWmsParameter::WITH_DISPLAY_NAME, @@ -2105,9 +2104,29 @@ namespace QgsWms return mWmsParameters.value( QgsWmsParameter::WITH_GEOMETRY ).toBool(); } + QString QgsWmsParameters::withMapTipAsString() const + { + return mWmsParameters.value( QgsWmsParameter::WITH_MAPTIP ).toString(); + } + bool QgsWmsParameters::withMapTip() const { - return mWmsParameters.value( QgsWmsParameter::WITH_MAPTIP ).toBool(); + const QString mStr = withMapTipAsString(); + + if ( mStr.startsWith( QLatin1String( "true" ), Qt::CaseInsensitive ) ) + return true; + else + return false; + } + + bool QgsWmsParameters::htmlInfoOnlyMapTip() const + { + const QString mStr = withMapTipAsString(); + + if ( mStr.startsWith( QLatin1String( "true_and_html_fi_only_maptip" ), Qt::CaseInsensitive ) ) + return true; + else + return false; } bool QgsWmsParameters::withDisplayName() const diff --git a/src/server/services/wms/qgswmsparameters.h b/src/server/services/wms/qgswmsparameters.h index 7f0f2dd21b63..c808fb9ca54f 100644 --- a/src/server/services/wms/qgswmsparameters.h +++ b/src/server/services/wms/qgswmsparameters.h @@ -1308,12 +1308,27 @@ namespace QgsWms */ bool withGeometry() const; + /** + * \brief withMapTipAsString + * \returns WITH_MAPTIP parameter as string + * \since QGIS 3.36 + */ + QString withMapTipAsString() const; + /** * \brief withMapTip * \returns TRUE if maptip information is requested for feature info response */ bool withMapTip() const; + /** + * Returns TRUE if only maptip information is requested for HTML + * feature info response + * \returns htmlInfoOnlyMapTip + * \since QGIS 3.36 + */ + bool htmlInfoOnlyMapTip() const; + /** * \brief withDisplayName * \returns TRUE if the display name is requested for feature info response diff --git a/src/server/services/wms/qgswmsrenderer.cpp b/src/server/services/wms/qgswmsrenderer.cpp index f6256569352a..54db38b53465 100644 --- a/src/server/services/wms/qgswmsrenderer.cpp +++ b/src/server/services/wms/qgswmsrenderer.cpp @@ -2580,7 +2580,11 @@ namespace QgsWms QByteArray QgsRenderer::convertFeatureInfoToHtml( const QDomDocument &doc ) const { - QString featureInfoString = QStringLiteral( R"HTML( + const bool onlyMapTip = mWmsParameters.htmlInfoOnlyMapTip(); + QString featureInfoString; + if ( !onlyMapTip ) + { + featureInfoString.append( QStringLiteral( R"HTML( Information @@ -2613,7 +2617,8 @@ namespace QgsWms - )HTML" ); + )HTML" ) ); + } const QDomNodeList layerList = doc.elementsByTagName( QStringLiteral( "Layer" ) ); @@ -2628,14 +2633,20 @@ namespace QgsWms if ( !featureNodeList.isEmpty() ) //vector layer { - const QString featureInfoLayerTitleString = QStringLiteral( "
%1
" ).arg( layerElem.attribute( QStringLiteral( "title" ) ).toHtmlEscaped() ); - featureInfoString.append( featureInfoLayerTitleString ); + if ( !onlyMapTip ) + { + const QString featureInfoLayerTitleString = QStringLiteral( "
%1
" ).arg( layerElem.attribute( QStringLiteral( "title" ) ).toHtmlEscaped() ); + featureInfoString.append( featureInfoLayerTitleString ); + } for ( int j = 0; j < featureNodeList.size(); ++j ) { const QDomElement featureElement = featureNodeList.at( j ).toElement(); - featureInfoString.append( QStringLiteral( R"HTML( + if ( !onlyMapTip ) + { + featureInfoString.append( QStringLiteral( R"HTML( )HTML" ) ); + } //attribute loop const QDomNodeList attributeNodeList = featureElement.elementsByTagName( QStringLiteral( "Attribute" ) ); @@ -2649,16 +2660,28 @@ namespace QgsWms value = value.toHtmlEscaped(); } - const QString featureInfoAttributeString = QStringLiteral( R"HTML( + if ( !onlyMapTip ) + { + const QString featureInfoAttributeString = QStringLiteral( R"HTML( )HTML" ).arg( name, value ); - featureInfoString.append( featureInfoAttributeString ); + featureInfoString.append( featureInfoAttributeString ); + } + else if ( name == QStringLiteral( "maptip" ) ) + { + featureInfoString.append( value ); + break; + } + } - featureInfoString.append( QStringLiteral( R"HTML( + if ( !onlyMapTip ) + { + featureInfoString.append( QStringLiteral( R"HTML(
%1 %2
)HTML" ) ); + } } } else //no result or raster layer @@ -2668,11 +2691,15 @@ namespace QgsWms // raster layer if ( !attributeNodeList.isEmpty() ) { - const QString featureInfoLayerTitleString = QStringLiteral( "
%1
" ).arg( layerElem.attribute( QStringLiteral( "title" ) ).toHtmlEscaped() ); - featureInfoString.append( featureInfoLayerTitleString ); + if ( !onlyMapTip ) + { + const QString featureInfoLayerTitleString = QStringLiteral( "
%1
" ).arg( layerElem.attribute( QStringLiteral( "title" ) ).toHtmlEscaped() ); + featureInfoString.append( featureInfoLayerTitleString ); - featureInfoString.append( QStringLiteral( R"HTML( + featureInfoString.append( QStringLiteral( R"HTML( )HTML" ) ); + } + for ( int j = 0; j < attributeNodeList.size(); ++j ) { const QDomElement attributeElement = attributeNodeList.at( j ).toElement(); @@ -2687,23 +2714,39 @@ namespace QgsWms value = value.toHtmlEscaped(); } - const QString featureInfoAttributeString = QStringLiteral( R"HTML( + if ( !onlyMapTip ) + { + const QString featureInfoAttributeString = QStringLiteral( R"HTML( )HTML" ).arg( name, value ); - featureInfoString.append( featureInfoAttributeString ); + + featureInfoString.append( featureInfoAttributeString ); + } + else if ( name == QStringLiteral( "maptip" ) ) + { + featureInfoString.append( value ); + break; + } + } - featureInfoString.append( QStringLiteral( R"HTML( + if ( !onlyMapTip ) + { + featureInfoString.append( QStringLiteral( R"HTML(
%1 %2
)HTML" ) ); + } } } } //end the html body - featureInfoString.append( QStringLiteral( R"HTML( + if ( !onlyMapTip ) + { + featureInfoString.append( QStringLiteral( R"HTML( )HTML" ) ); + } return featureInfoString.toUtf8(); } diff --git a/tests/src/python/test_qgsserver_wms_getfeatureinfo.py b/tests/src/python/test_qgsserver_wms_getfeatureinfo.py index 63a494ad951b..afbc79b656aa 100644 --- a/tests/src/python/test_qgsserver_wms_getfeatureinfo.py +++ b/tests/src/python/test_qgsserver_wms_getfeatureinfo.py @@ -145,6 +145,16 @@ def testGetFeatureInfo(self): 'with_maptip=true', 'wms_getfeatureinfo-text-html-maptip') + # Test getfeatureinfo response html only with maptip for vector layer + self.wms_request_compare('GetFeatureInfo', + '&layers=testlayer%20%C3%A8%C3%A9&styles=&' + + 'info_format=text%2Fhtml&transparent=true&' + + 'width=600&height=400&srs=EPSG%3A3857&bbox=913190.6389747962%2C' + + '5606005.488876367%2C913235.426296057%2C5606035.347090538&' + + 'query_layers=testlayer%20%C3%A8%C3%A9&X=190&Y=320&' + + 'with_maptip=true_and_html_fi_only_maptip', + 'wms_getfeatureinfo-html-only-with-maptip-vector') + # Test getfeatureinfo response html with maptip and display name in text mode for vector layer self.wms_request_compare('GetFeatureInfo', '&layers=testlayer%20%C3%A8%C3%A9&styles=&' + @@ -271,6 +281,16 @@ def testGetFeatureInfo(self): 'with_maptip=true', 'wms_getfeatureinfo-raster-text-xml-maptip') + # Test GetFeatureInfo on raster layer HTML only with maptip + self.wms_request_compare('GetFeatureInfo', + '&layers=landsat&styles=&' + + 'info_format=text%2Fhtml&transparent=true&' + + 'width=500&height=500&srs=EPSG%3A3857&' + + 'bbox=1989139.6,3522745.0,2015014.9,3537004.5&' + + 'query_layers=landsat&X=250&Y=250&' + + 'with_maptip=true_and_html_fi_only_maptip', + 'wms_getfeatureinfo-html-only-with-maptip-raster') + def testGetFeatureInfoValueRelation(self): """Test GetFeatureInfo resolves "value relation" widget values. regression 18518""" mypath = self.testdata_path + "test_project_values.qgz" diff --git a/tests/testdata/qgis_server/wms_getfeatureinfo-html-only-with-maptip-raster.txt b/tests/testdata/qgis_server/wms_getfeatureinfo-html-only-with-maptip-raster.txt new file mode 100644 index 000000000000..b52bdf366945 --- /dev/null +++ b/tests/testdata/qgis_server/wms_getfeatureinfo-html-only-with-maptip-raster.txt @@ -0,0 +1,4 @@ +***** +Content-Type: text/html; charset=utf-8 + +Value Band 5: 90 diff --git a/tests/testdata/qgis_server/wms_getfeatureinfo-html-only-with-maptip-vector.txt b/tests/testdata/qgis_server/wms_getfeatureinfo-html-only-with-maptip-vector.txt new file mode 100644 index 000000000000..9bbcbfecded7 --- /dev/null +++ b/tests/testdata/qgis_server/wms_getfeatureinfo-html-only-with-maptip-vector.txt @@ -0,0 +1,4 @@ +***** +Content-Type: text/html; charset=utf-8 + +Name: three