/
qgswmsgetfeatureinfo.cpp
185 lines (164 loc) · 7.84 KB
/
qgswmsgetfeatureinfo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/***************************************************************************
qgswmsgetfeatureinfo.cpp
-------------------------
begin : December 20 , 2016
copyright : (C) 2007 by Marco Hugentobler (original code)
(C) 2014 by Alessandro Pasotti (original code)
(C) 2016 by David Marteau
email : marco dot hugentobler at karto dot baug dot ethz dot ch
a dot pasotti at itopen dot it
david dot marteau at 3liz dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgswmsutils.h"
#include "qgswmsgetfeatureinfo.h"
#include "qgswmsservertransitional.h"
namespace QgsWms
{
void writeInfoResponse( QDomDocument& infoDoc, QgsServerResponse& response, const QString& infoFormat )
{
QByteArray ba;
QgsMessageLog::logMessage( "Info format is:" + infoFormat );
if ( infoFormat == QLatin1String( "text/xml" ) || infoFormat.startsWith( QLatin1String( "application/vnd.ogc.gml" ) ) )
{
ba = infoDoc.toByteArray();
}
else if ( infoFormat == QLatin1String( "text/plain" ) || infoFormat == QLatin1String( "text/html" ) )
{
//create string
QString featureInfoString;
if ( infoFormat == QLatin1String( "text/plain" ) )
{
featureInfoString.append( "GetFeatureInfo results\n" );
featureInfoString.append( "\n" );
}
else if ( infoFormat == QLatin1String( "text/html" ) )
{
featureInfoString.append( "<HEAD>\n" );
featureInfoString.append( "<TITLE> GetFeatureInfo results </TITLE>\n" );
featureInfoString.append( "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">\n" );
featureInfoString.append( "</HEAD>\n" );
featureInfoString.append( "<BODY>\n" );
}
QDomNodeList layerList = infoDoc.elementsByTagName( QStringLiteral( "Layer" ) );
//layer loop
for ( int i = 0; i < layerList.size(); ++i )
{
QDomElement layerElem = layerList.at( i ).toElement();
if ( infoFormat == QLatin1String( "text/plain" ) )
{
featureInfoString.append( "Layer '" + layerElem.attribute( QStringLiteral( "name" ) ) + "'\n" );
}
else if ( infoFormat == QLatin1String( "text/html" ) )
{
featureInfoString.append( "<TABLE border=1 width=100%>\n" );
featureInfoString.append( "<TR><TH width=25%>Layer</TH><TD>" + layerElem.attribute( QStringLiteral( "name" ) ) + "</TD></TR>\n" );
featureInfoString.append( "</BR>" );
}
//feature loop (for vector layers)
QDomNodeList featureNodeList = layerElem.elementsByTagName( QStringLiteral( "Feature" ) );
QDomElement currentFeatureElement;
if ( featureNodeList.size() < 1 ) //raster layer?
{
QDomNodeList attributeNodeList = layerElem.elementsByTagName( QStringLiteral( "Attribute" ) );
for ( int j = 0; j < attributeNodeList.size(); ++j )
{
QDomElement attributeElement = attributeNodeList.at( j ).toElement();
if ( infoFormat == QLatin1String( "text/plain" ) )
{
featureInfoString.append( attributeElement.attribute( QStringLiteral( "name" ) ) + " = '" +
attributeElement.attribute( QStringLiteral( "value" ) ) + "'\n" );
}
else if ( infoFormat == QLatin1String( "text/html" ) )
{
featureInfoString.append( "<TR><TH>" + attributeElement.attribute( QStringLiteral( "name" ) ) + "</TH><TD>" +
attributeElement.attribute( QStringLiteral( "value" ) ) + "</TD></TR>\n" );
}
}
}
else //vector layer
{
for ( int j = 0; j < featureNodeList.size(); ++j )
{
QDomElement featureElement = featureNodeList.at( j ).toElement();
if ( infoFormat == QLatin1String( "text/plain" ) )
{
featureInfoString.append( "Feature " + featureElement.attribute( QStringLiteral( "id" ) ) + "\n" );
}
else if ( infoFormat == QLatin1String( "text/html" ) )
{
featureInfoString.append( "<TABLE border=1 width=100%>\n" );
featureInfoString.append( "<TR><TH>Feature</TH><TD>" + featureElement.attribute( QStringLiteral( "id" ) ) + "</TD></TR>\n" );
}
//attribute loop
QDomNodeList attributeNodeList = featureElement.elementsByTagName( QStringLiteral( "Attribute" ) );
for ( int k = 0; k < attributeNodeList.size(); ++k )
{
QDomElement attributeElement = attributeNodeList.at( k ).toElement();
if ( infoFormat == QLatin1String( "text/plain" ) )
{
featureInfoString.append( attributeElement.attribute( QStringLiteral( "name" ) ) + " = '" +
attributeElement.attribute( QStringLiteral( "value" ) ) + "'\n" );
}
else if ( infoFormat == QLatin1String( "text/html" ) )
{
featureInfoString.append( "<TR><TH>" + attributeElement.attribute( QStringLiteral( "name" ) ) + "</TH><TD>" + attributeElement.attribute( QStringLiteral( "value" ) ) + "</TD></TR>\n" );
}
}
if ( infoFormat == QLatin1String( "text/html" ) )
{
featureInfoString.append( "</TABLE>\n</BR>\n" );
}
}
}
if ( infoFormat == QLatin1String( "text/plain" ) )
{
featureInfoString.append( "\n" );
}
else if ( infoFormat == QLatin1String( "text/html" ) )
{
featureInfoString.append( "</TABLE>\n<BR></BR>\n" );
}
}
if ( infoFormat == QLatin1String( "text/html" ) )
{
featureInfoString.append( "</BODY>\n" );
}
ba = featureInfoString.toUtf8();
}
else //unsupported format, set exception
{
writeError( response, QStringLiteral( "InvalidFormat" ),
QString( "Feature info format '%1' is not supported. Possibilities are 'text/plain', 'text/html' or 'text/xml'." ).arg( infoFormat ) );
return;
}
response.setHeader( QStringLiteral( "Content-Type" ), infoFormat + QStringLiteral( "; charset=utf-8" ) );
response.write( ba );
}
void writeGetFeatureInfo( QgsServerInterface* serverIface, const QString& version,
const QgsServerRequest& request, QgsServerResponse& response )
{
Q_UNUSED( version );
QgsServerRequest::Parameters params = request.parameters();
QgsWmsServer server( serverIface->configFilePath(), params,
getConfigParser( serverIface ),
serverIface->accessControls() );
try
{
QDomDocument doc = server.getFeatureInfo( version );
QString outputFormat = params.value( QStringLiteral( "INFO_FORMAT" ), QStringLiteral( "text/plain" ) );
writeInfoResponse( doc, response, outputFormat );
}
catch ( QgsMapServiceException& ex )
{
writeError( response, ex.code(), ex.message() );
}
}
} // samespace QgsWms