Skip to content
Permalink
Browse files

[Server] WMS GetFeatureInfo refactoring cleanup

  • Loading branch information
rldhont committed Jul 17, 2017
1 parent f926033 commit 349c996ad24e783ba110c897fcb4c7cbfad56b29
@@ -25,136 +25,6 @@
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.isEmpty() ) //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
{
throw QgsServiceException( QStringLiteral( "InvalidFormat" ),
QString( "Feature info format '%1' is not supported. Possibilities are 'text/plain', 'text/html' or 'text/xml'." ).arg( infoFormat ) );
}

response.setHeader( QStringLiteral( "Content-Type" ), infoFormat + QStringLiteral( "; charset=utf-8" ) );
response.write( ba );
}


void writeGetFeatureInfo( QgsServerInterface *serverIface, const QgsProject *project,
const QString &version, const QgsServerRequest &request,
QgsServerResponse &response )
@@ -163,9 +33,11 @@ namespace QgsWms
QgsServerRequest::Parameters params = request.parameters();
QgsRenderer renderer( serverIface, project, params, getConfigParser( serverIface ) );

QDomDocument doc = renderer.getFeatureInfo( version );
QString outputFormat = params.value( QStringLiteral( "INFO_FORMAT" ), QStringLiteral( "text/plain" ) );
writeInfoResponse( doc, response, outputFormat );
std::unique_ptr<QByteArray> result( renderer.getFeatureInfo( version ) );
QString infoFormat = params.value( QStringLiteral( "INFO_FORMAT" ), QStringLiteral( "text/plain" ) );

response.setHeader( QStringLiteral( "Content-Type" ), infoFormat + QStringLiteral( "; charset=utf-8" ) );
response.write( *result );
}


@@ -404,7 +404,6 @@ namespace QgsWms
{
mRequestParameters = parameters;

log( "load WMS Request parameters:" );
const QMetaEnum metaEnum( QMetaEnum::fromType<ParameterName>() );
foreach ( QString key, parameters.keys() )
{
@@ -415,7 +414,6 @@ namespace QgsWms
if ( value.canConvert( mParameters[name].mType ) )
{
mParameters[name].mValue = value;
log( " - " + key + " : " + parameters[key] );
}
else
{
@@ -716,10 +714,10 @@ namespace QgsWms
{
QString fStr = infoFormatAsString();

Format f = Format::TEXT;
if ( fStr.isEmpty() )
return Format::NONE;
return f;

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

int QgsWmsParameters::infoFormatVersion() const
{
if ( infoFormat() != Format::GML )
return -1;

QString fStr = infoFormatAsString();
if ( fStr.startsWith( QLatin1String( "application/vnd.ogc.gml/3" ), Qt::CaseInsensitive ) )
return 3;
else
return 2;
}

QString QgsWmsParameters::i() const
{
return value( ParameterName::I ).toString();
@@ -276,6 +276,12 @@ namespace QgsWms
*/
Format infoFormat() const;

/** Returns the infoFormat version for GML. If the INFO_FORMAT is not GML,
* then the default value is -1.
* \returns infoFormat version
*/
int infoFormatVersion() const;

/** Returns I parameter or an empty string if not defined.
* \returns i parameter
*/

0 comments on commit 349c996

Please sign in to comment.
You can’t perform that action at this time.