Skip to content

Commit 08c06de

Browse files
authored
Merge pull request #4846 from rldhont/server-wms-configparser-getfeatureinfo
[Server] WMS GetFeatureInfo refactoring
2 parents 744fbc5 + 349c996 commit 08c06de

File tree

11 files changed

+1020
-473
lines changed

11 files changed

+1020
-473
lines changed

python/server/qgsserverprojectutils.sip

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,62 @@ namespace QgsServerProjectUtils
145145
:rtype: bool
146146
%End
147147

148+
bool wmsFeatureInfoAddWktGeometry( const QgsProject &project );
149+
%Docstring
150+
Returns if the geometry is displayed as Well Known Text in GetFeatureInfo request.
151+
\param project the QGIS project
152+
:return: if the geometry is displayed as Well Known Text in GetFeatureInfo request.
153+
:rtype: bool
154+
%End
155+
156+
bool wmsFeatureInfoSegmentizeWktGeometry( const QgsProject &project );
157+
%Docstring
158+
Returns if the geometry has to be segmentize in GetFeatureInfo request.
159+
\param project the QGIS project
160+
:return: if the geometry has to be segmentize in GetFeatureInfo request.
161+
:rtype: bool
162+
%End
163+
164+
int wmsFeatureInfoPrecision( const QgsProject &project );
165+
%Docstring
166+
Returns the geometry precision for GetFeatureInfo request.
167+
\param project the QGIS project
168+
:return: the geometry precision for GetFeatureInfo request.
169+
:rtype: int
170+
%End
171+
172+
QString wmsFeatureInfoDocumentElement( const QgsProject &project );
173+
%Docstring
174+
Returns the document element name for XML GetFeatureInfo request.
175+
\param project the QGIS project
176+
:return: the document element name for XML GetFeatureInfo request.
177+
:rtype: str
178+
%End
179+
180+
QString wmsFeatureInfoDocumentElementNs( const QgsProject &project );
181+
%Docstring
182+
Returns the document element namespace for XML GetFeatureInfo request.
183+
\param project the QGIS project
184+
:return: the document element namespace for XML GetFeatureInfo request.
185+
:rtype: str
186+
%End
187+
188+
QString wmsFeatureInfoSchema( const QgsProject &project );
189+
%Docstring
190+
Returns the schema URL for XML GetFeatureInfo request.
191+
\param project the QGIS project
192+
:return: the schema URL for XML GetFeatureInfo request.
193+
:rtype: str
194+
%End
195+
196+
QHash<QString, QString> wmsFeatureInfoLayerAliasMap( const QgsProject &project );
197+
%Docstring
198+
Returns the mapping between layer name and wms layer name for GetFeatureInfo request.
199+
\param project the QGIS project
200+
:return: the mapping between layer name and wms layer name for GetFeatureInfo request.
201+
:rtype: QHash<str, QString>
202+
%End
203+
148204
bool wmsInspireActivate( const QgsProject &project );
149205
%Docstring
150206
Returns if Inspire is activated.

src/server/qgsserverprojectutils.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,77 @@ bool QgsServerProjectUtils::wmsInfoFormatSia2045( const QgsProject &project )
117117
return false;
118118
}
119119

120+
bool QgsServerProjectUtils::wmsFeatureInfoAddWktGeometry( const QgsProject &project )
121+
{
122+
QString wktGeom = project.readEntry( QStringLiteral( "WMSAddWktGeometry" ), QStringLiteral( "/" ), "" );
123+
124+
if ( wktGeom.compare( QLatin1String( "enabled" ), Qt::CaseInsensitive ) == 0
125+
|| wktGeom.compare( QLatin1String( "true" ), Qt::CaseInsensitive ) == 0 )
126+
{
127+
return true;
128+
}
129+
return false;
130+
}
131+
132+
bool QgsServerProjectUtils::wmsFeatureInfoSegmentizeWktGeometry( const QgsProject &project )
133+
{
134+
QString segmGeom = project.readEntry( QStringLiteral( "WMSSegmentizeFeatureInfoGeometry" ), QStringLiteral( "/" ), "" );
135+
136+
if ( segmGeom.compare( QLatin1String( "enabled" ), Qt::CaseInsensitive ) == 0
137+
|| segmGeom.compare( QLatin1String( "true" ), Qt::CaseInsensitive ) == 0 )
138+
{
139+
return true;
140+
}
141+
return false;
142+
}
143+
144+
int QgsServerProjectUtils::wmsFeatureInfoPrecision( const QgsProject &project )
145+
{
146+
return project.readNumEntry( QStringLiteral( "WMSPrecision" ), QStringLiteral( "/" ), 6 );
147+
}
148+
149+
QString QgsServerProjectUtils::wmsFeatureInfoDocumentElement( const QgsProject &project )
150+
{
151+
return project.readEntry( QStringLiteral( "WMSFeatureInfoDocumentElement" ), QStringLiteral( "/" ), "" );
152+
}
153+
154+
QString QgsServerProjectUtils::wmsFeatureInfoDocumentElementNs( const QgsProject &project )
155+
{
156+
return project.readEntry( QStringLiteral( "WMSFeatureInfoDocumentElementNS" ), QStringLiteral( "/" ), "" );
157+
}
158+
159+
QString QgsServerProjectUtils::wmsFeatureInfoSchema( const QgsProject &project )
160+
{
161+
return project.readEntry( QStringLiteral( "WMSFeatureInfoSchema" ), QStringLiteral( "/" ), "" );
162+
}
163+
164+
QHash<QString, QString> QgsServerProjectUtils::wmsFeatureInfoLayerAliasMap( const QgsProject &project )
165+
{
166+
QHash<QString, QString> aliasMap;
167+
168+
//WMSFeatureInfoAliasLayers
169+
QStringList aliasLayerStringList = project.readListEntry( QStringLiteral( "WMSFeatureInfoAliasLayers" ), QStringLiteral( "/value" ), QStringList() );
170+
if ( aliasLayerStringList.isEmpty() )
171+
{
172+
return aliasMap;
173+
}
174+
175+
//WMSFeatureInfoLayerAliases
176+
QStringList layerAliasStringList = project.readListEntry( QStringLiteral( "WMSFeatureInfoLayerAliases" ), QStringLiteral( "/value" ), QStringList() );
177+
if ( layerAliasStringList.isEmpty() )
178+
{
179+
return aliasMap;
180+
}
181+
182+
int nMapEntries = qMin( aliasLayerStringList.size(), layerAliasStringList.size() );
183+
for ( int i = 0; i < nMapEntries; ++i )
184+
{
185+
aliasMap.insert( aliasLayerStringList.at( i ), layerAliasStringList.at( i ) );
186+
}
187+
188+
return aliasMap;
189+
}
190+
120191
bool QgsServerProjectUtils::wmsInspireActivate( const QgsProject &project )
121192
{
122193
return project.readBoolEntry( QStringLiteral( "WMSInspire" ), QStringLiteral( "/activated" ) );

src/server/qgsserverprojectutils.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,48 @@ namespace QgsServerProjectUtils
132132
*/
133133
SERVER_EXPORT bool wmsInfoFormatSia2045( const QgsProject &project );
134134

135+
/** Returns if the geometry is displayed as Well Known Text in GetFeatureInfo request.
136+
* \param project the QGIS project
137+
* \returns if the geometry is displayed as Well Known Text in GetFeatureInfo request.
138+
*/
139+
SERVER_EXPORT bool wmsFeatureInfoAddWktGeometry( const QgsProject &project );
140+
141+
/** Returns if the geometry has to be segmentize in GetFeatureInfo request.
142+
* \param project the QGIS project
143+
* \returns if the geometry has to be segmentize in GetFeatureInfo request.
144+
*/
145+
SERVER_EXPORT bool wmsFeatureInfoSegmentizeWktGeometry( const QgsProject &project );
146+
147+
/** Returns the geometry precision for GetFeatureInfo request.
148+
* \param project the QGIS project
149+
* \returns the geometry precision for GetFeatureInfo request.
150+
*/
151+
SERVER_EXPORT int wmsFeatureInfoPrecision( const QgsProject &project );
152+
153+
/** Returns the document element name for XML GetFeatureInfo request.
154+
* \param project the QGIS project
155+
* \returns the document element name for XML GetFeatureInfo request.
156+
*/
157+
SERVER_EXPORT QString wmsFeatureInfoDocumentElement( const QgsProject &project );
158+
159+
/** Returns the document element namespace for XML GetFeatureInfo request.
160+
* \param project the QGIS project
161+
* \returns the document element namespace for XML GetFeatureInfo request.
162+
*/
163+
SERVER_EXPORT QString wmsFeatureInfoDocumentElementNs( const QgsProject &project );
164+
165+
/** Returns the schema URL for XML GetFeatureInfo request.
166+
* \param project the QGIS project
167+
* \returns the schema URL for XML GetFeatureInfo request.
168+
*/
169+
SERVER_EXPORT QString wmsFeatureInfoSchema( const QgsProject &project );
170+
171+
/** Returns the mapping between layer name and wms layer name for GetFeatureInfo request.
172+
* \param project the QGIS project
173+
* \returns the mapping between layer name and wms layer name for GetFeatureInfo request.
174+
*/
175+
SERVER_EXPORT QHash<QString, QString> wmsFeatureInfoLayerAliasMap( const QgsProject &project );
176+
135177
/** Returns if Inspire is activated.
136178
* \param project the QGIS project
137179
* \returns if Inspire is activated.

src/server/services/wms/qgswmsgetfeatureinfo.cpp

Lines changed: 5 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -25,136 +25,6 @@
2525
namespace QgsWms
2626
{
2727

28-
void writeInfoResponse( QDomDocument &infoDoc, QgsServerResponse &response, const QString &infoFormat )
29-
{
30-
QByteArray ba;
31-
QgsMessageLog::logMessage( "Info format is:" + infoFormat );
32-
33-
if ( infoFormat == QLatin1String( "text/xml" ) || infoFormat.startsWith( QLatin1String( "application/vnd.ogc.gml" ) ) )
34-
{
35-
ba = infoDoc.toByteArray();
36-
}
37-
else if ( infoFormat == QLatin1String( "text/plain" ) || infoFormat == QLatin1String( "text/html" ) )
38-
{
39-
//create string
40-
QString featureInfoString;
41-
42-
if ( infoFormat == QLatin1String( "text/plain" ) )
43-
{
44-
featureInfoString.append( "GetFeatureInfo results\n" );
45-
featureInfoString.append( "\n" );
46-
}
47-
else if ( infoFormat == QLatin1String( "text/html" ) )
48-
{
49-
featureInfoString.append( "<HEAD>\n" );
50-
featureInfoString.append( "<TITLE> GetFeatureInfo results </TITLE>\n" );
51-
featureInfoString.append( "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">\n" );
52-
featureInfoString.append( "</HEAD>\n" );
53-
featureInfoString.append( "<BODY>\n" );
54-
}
55-
56-
QDomNodeList layerList = infoDoc.elementsByTagName( QStringLiteral( "Layer" ) );
57-
58-
//layer loop
59-
for ( int i = 0; i < layerList.size(); ++i )
60-
{
61-
QDomElement layerElem = layerList.at( i ).toElement();
62-
if ( infoFormat == QLatin1String( "text/plain" ) )
63-
{
64-
featureInfoString.append( "Layer '" + layerElem.attribute( QStringLiteral( "name" ) ) + "'\n" );
65-
}
66-
else if ( infoFormat == QLatin1String( "text/html" ) )
67-
{
68-
featureInfoString.append( "<TABLE border=1 width=100%>\n" );
69-
featureInfoString.append( "<TR><TH width=25%>Layer</TH><TD>" + layerElem.attribute( QStringLiteral( "name" ) ) + "</TD></TR>\n" );
70-
featureInfoString.append( "</BR>" );
71-
}
72-
73-
//feature loop (for vector layers)
74-
QDomNodeList featureNodeList = layerElem.elementsByTagName( QStringLiteral( "Feature" ) );
75-
QDomElement currentFeatureElement;
76-
77-
if ( featureNodeList.isEmpty() ) //raster layer?
78-
{
79-
QDomNodeList attributeNodeList = layerElem.elementsByTagName( QStringLiteral( "Attribute" ) );
80-
for ( int j = 0; j < attributeNodeList.size(); ++j )
81-
{
82-
QDomElement attributeElement = attributeNodeList.at( j ).toElement();
83-
if ( infoFormat == QLatin1String( "text/plain" ) )
84-
{
85-
featureInfoString.append( attributeElement.attribute( QStringLiteral( "name" ) ) + " = '" +
86-
attributeElement.attribute( QStringLiteral( "value" ) ) + "'\n" );
87-
}
88-
else if ( infoFormat == QLatin1String( "text/html" ) )
89-
{
90-
featureInfoString.append( "<TR><TH>" + attributeElement.attribute( QStringLiteral( "name" ) ) + "</TH><TD>" +
91-
attributeElement.attribute( QStringLiteral( "value" ) ) + "</TD></TR>\n" );
92-
}
93-
}
94-
}
95-
else //vector layer
96-
{
97-
for ( int j = 0; j < featureNodeList.size(); ++j )
98-
{
99-
QDomElement featureElement = featureNodeList.at( j ).toElement();
100-
if ( infoFormat == QLatin1String( "text/plain" ) )
101-
{
102-
featureInfoString.append( "Feature " + featureElement.attribute( QStringLiteral( "id" ) ) + "\n" );
103-
}
104-
else if ( infoFormat == QLatin1String( "text/html" ) )
105-
{
106-
featureInfoString.append( "<TABLE border=1 width=100%>\n" );
107-
featureInfoString.append( "<TR><TH>Feature</TH><TD>" + featureElement.attribute( QStringLiteral( "id" ) ) + "</TD></TR>\n" );
108-
}
109-
//attribute loop
110-
QDomNodeList attributeNodeList = featureElement.elementsByTagName( QStringLiteral( "Attribute" ) );
111-
for ( int k = 0; k < attributeNodeList.size(); ++k )
112-
{
113-
QDomElement attributeElement = attributeNodeList.at( k ).toElement();
114-
if ( infoFormat == QLatin1String( "text/plain" ) )
115-
{
116-
featureInfoString.append( attributeElement.attribute( QStringLiteral( "name" ) ) + " = '" +
117-
attributeElement.attribute( QStringLiteral( "value" ) ) + "'\n" );
118-
}
119-
else if ( infoFormat == QLatin1String( "text/html" ) )
120-
{
121-
featureInfoString.append( "<TR><TH>" + attributeElement.attribute( QStringLiteral( "name" ) ) + "</TH><TD>" + attributeElement.attribute( QStringLiteral( "value" ) ) + "</TD></TR>\n" );
122-
}
123-
}
124-
125-
if ( infoFormat == QLatin1String( "text/html" ) )
126-
{
127-
featureInfoString.append( "</TABLE>\n</BR>\n" );
128-
}
129-
}
130-
}
131-
if ( infoFormat == QLatin1String( "text/plain" ) )
132-
{
133-
featureInfoString.append( "\n" );
134-
}
135-
else if ( infoFormat == QLatin1String( "text/html" ) )
136-
{
137-
featureInfoString.append( "</TABLE>\n<BR></BR>\n" );
138-
139-
}
140-
}
141-
if ( infoFormat == QLatin1String( "text/html" ) )
142-
{
143-
featureInfoString.append( "</BODY>\n" );
144-
}
145-
ba = featureInfoString.toUtf8();
146-
}
147-
else //unsupported format, set exception
148-
{
149-
throw QgsServiceException( QStringLiteral( "InvalidFormat" ),
150-
QString( "Feature info format '%1' is not supported. Possibilities are 'text/plain', 'text/html' or 'text/xml'." ).arg( infoFormat ) );
151-
}
152-
153-
response.setHeader( QStringLiteral( "Content-Type" ), infoFormat + QStringLiteral( "; charset=utf-8" ) );
154-
response.write( ba );
155-
}
156-
157-
15828
void writeGetFeatureInfo( QgsServerInterface *serverIface, const QgsProject *project,
15929
const QString &version, const QgsServerRequest &request,
16030
QgsServerResponse &response )
@@ -163,9 +33,11 @@ namespace QgsWms
16333
QgsServerRequest::Parameters params = request.parameters();
16434
QgsRenderer renderer( serverIface, project, params, getConfigParser( serverIface ) );
16535

166-
QDomDocument doc = renderer.getFeatureInfo( version );
167-
QString outputFormat = params.value( QStringLiteral( "INFO_FORMAT" ), QStringLiteral( "text/plain" ) );
168-
writeInfoResponse( doc, response, outputFormat );
36+
std::unique_ptr<QByteArray> result( renderer.getFeatureInfo( version ) );
37+
QString infoFormat = params.value( QStringLiteral( "INFO_FORMAT" ), QStringLiteral( "text/plain" ) );
38+
39+
response.setHeader( QStringLiteral( "Content-Type" ), infoFormat + QStringLiteral( "; charset=utf-8" ) );
40+
response.write( *result );
16941
}
17042

17143

0 commit comments

Comments
 (0)