Skip to content

Commit 349c996

Browse files
committed
[Server] WMS GetFeatureInfo refactoring cleanup
1 parent f926033 commit 349c996

File tree

6 files changed

+419
-372
lines changed

6 files changed

+419
-372
lines changed

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

src/server/services/wms/qgswmsparameters.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,6 @@ namespace QgsWms
404404
{
405405
mRequestParameters = parameters;
406406

407-
log( "load WMS Request parameters:" );
408407
const QMetaEnum metaEnum( QMetaEnum::fromType<ParameterName>() );
409408
foreach ( QString key, parameters.keys() )
410409
{
@@ -415,7 +414,6 @@ namespace QgsWms
415414
if ( value.canConvert( mParameters[name].mType ) )
416415
{
417416
mParameters[name].mValue = value;
418-
log( " - " + key + " : " + parameters[key] );
419417
}
420418
else
421419
{
@@ -716,10 +714,10 @@ namespace QgsWms
716714
{
717715
QString fStr = infoFormatAsString();
718716

717+
Format f = Format::TEXT;
719718
if ( fStr.isEmpty() )
720-
return Format::NONE;
719+
return f;
721720

722-
Format f = Format::TEXT;
723721
if ( fStr.startsWith( QLatin1String( "text/xml" ), Qt::CaseInsensitive ) )
724722
f = Format::XML;
725723
else if ( fStr.startsWith( QLatin1String( "text/html" ), Qt::CaseInsensitive ) )
@@ -730,6 +728,18 @@ namespace QgsWms
730728
return f;
731729
}
732730

731+
int QgsWmsParameters::infoFormatVersion() const
732+
{
733+
if ( infoFormat() != Format::GML )
734+
return -1;
735+
736+
QString fStr = infoFormatAsString();
737+
if ( fStr.startsWith( QLatin1String( "application/vnd.ogc.gml/3" ), Qt::CaseInsensitive ) )
738+
return 3;
739+
else
740+
return 2;
741+
}
742+
733743
QString QgsWmsParameters::i() const
734744
{
735745
return value( ParameterName::I ).toString();

src/server/services/wms/qgswmsparameters.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,12 @@ namespace QgsWms
276276
*/
277277
Format infoFormat() const;
278278

279+
/** Returns the infoFormat version for GML. If the INFO_FORMAT is not GML,
280+
* then the default value is -1.
281+
* \returns infoFormat version
282+
*/
283+
int infoFormatVersion() const;
284+
279285
/** Returns I parameter or an empty string if not defined.
280286
* \returns i parameter
281287
*/

0 commit comments

Comments
 (0)