2525namespace 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