Skip to content
Permalink
Browse files

Merge pull request #4846 from rldhont/server-wms-configparser-getfeat…

…ureinfo

[Server] WMS GetFeatureInfo refactoring
  • Loading branch information
rldhont committed Jul 19, 2017
2 parents 744fbc5 + 349c996 commit 08c06def1bbd1146764d3076d2ae630eb1ee4b45
@@ -145,6 +145,62 @@ namespace QgsServerProjectUtils
:rtype: bool
%End

bool wmsFeatureInfoAddWktGeometry( const QgsProject &project );
%Docstring
Returns if the geometry is displayed as Well Known Text in GetFeatureInfo request.
\param project the QGIS project
:return: if the geometry is displayed as Well Known Text in GetFeatureInfo request.
:rtype: bool
%End

bool wmsFeatureInfoSegmentizeWktGeometry( const QgsProject &project );
%Docstring
Returns if the geometry has to be segmentize in GetFeatureInfo request.
\param project the QGIS project
:return: if the geometry has to be segmentize in GetFeatureInfo request.
:rtype: bool
%End

int wmsFeatureInfoPrecision( const QgsProject &project );
%Docstring
Returns the geometry precision for GetFeatureInfo request.
\param project the QGIS project
:return: the geometry precision for GetFeatureInfo request.
:rtype: int
%End

QString wmsFeatureInfoDocumentElement( const QgsProject &project );
%Docstring
Returns the document element name for XML GetFeatureInfo request.
\param project the QGIS project
:return: the document element name for XML GetFeatureInfo request.
:rtype: str
%End

QString wmsFeatureInfoDocumentElementNs( const QgsProject &project );
%Docstring
Returns the document element namespace for XML GetFeatureInfo request.
\param project the QGIS project
:return: the document element namespace for XML GetFeatureInfo request.
:rtype: str
%End

QString wmsFeatureInfoSchema( const QgsProject &project );
%Docstring
Returns the schema URL for XML GetFeatureInfo request.
\param project the QGIS project
:return: the schema URL for XML GetFeatureInfo request.
:rtype: str
%End

QHash<QString, QString> wmsFeatureInfoLayerAliasMap( const QgsProject &project );
%Docstring
Returns the mapping between layer name and wms layer name for GetFeatureInfo request.
\param project the QGIS project
:return: the mapping between layer name and wms layer name for GetFeatureInfo request.
:rtype: QHash<str, QString>
%End

bool wmsInspireActivate( const QgsProject &project );
%Docstring
Returns if Inspire is activated.
@@ -117,6 +117,77 @@ bool QgsServerProjectUtils::wmsInfoFormatSia2045( const QgsProject &project )
return false;
}

bool QgsServerProjectUtils::wmsFeatureInfoAddWktGeometry( const QgsProject &project )
{
QString wktGeom = project.readEntry( QStringLiteral( "WMSAddWktGeometry" ), QStringLiteral( "/" ), "" );

if ( wktGeom.compare( QLatin1String( "enabled" ), Qt::CaseInsensitive ) == 0
|| wktGeom.compare( QLatin1String( "true" ), Qt::CaseInsensitive ) == 0 )
{
return true;
}
return false;
}

bool QgsServerProjectUtils::wmsFeatureInfoSegmentizeWktGeometry( const QgsProject &project )
{
QString segmGeom = project.readEntry( QStringLiteral( "WMSSegmentizeFeatureInfoGeometry" ), QStringLiteral( "/" ), "" );

if ( segmGeom.compare( QLatin1String( "enabled" ), Qt::CaseInsensitive ) == 0
|| segmGeom.compare( QLatin1String( "true" ), Qt::CaseInsensitive ) == 0 )
{
return true;
}
return false;
}

int QgsServerProjectUtils::wmsFeatureInfoPrecision( const QgsProject &project )
{
return project.readNumEntry( QStringLiteral( "WMSPrecision" ), QStringLiteral( "/" ), 6 );
}

QString QgsServerProjectUtils::wmsFeatureInfoDocumentElement( const QgsProject &project )
{
return project.readEntry( QStringLiteral( "WMSFeatureInfoDocumentElement" ), QStringLiteral( "/" ), "" );
}

QString QgsServerProjectUtils::wmsFeatureInfoDocumentElementNs( const QgsProject &project )
{
return project.readEntry( QStringLiteral( "WMSFeatureInfoDocumentElementNS" ), QStringLiteral( "/" ), "" );
}

QString QgsServerProjectUtils::wmsFeatureInfoSchema( const QgsProject &project )
{
return project.readEntry( QStringLiteral( "WMSFeatureInfoSchema" ), QStringLiteral( "/" ), "" );
}

QHash<QString, QString> QgsServerProjectUtils::wmsFeatureInfoLayerAliasMap( const QgsProject &project )
{
QHash<QString, QString> aliasMap;

//WMSFeatureInfoAliasLayers
QStringList aliasLayerStringList = project.readListEntry( QStringLiteral( "WMSFeatureInfoAliasLayers" ), QStringLiteral( "/value" ), QStringList() );
if ( aliasLayerStringList.isEmpty() )
{
return aliasMap;
}

//WMSFeatureInfoLayerAliases
QStringList layerAliasStringList = project.readListEntry( QStringLiteral( "WMSFeatureInfoLayerAliases" ), QStringLiteral( "/value" ), QStringList() );
if ( layerAliasStringList.isEmpty() )
{
return aliasMap;
}

int nMapEntries = qMin( aliasLayerStringList.size(), layerAliasStringList.size() );
for ( int i = 0; i < nMapEntries; ++i )
{
aliasMap.insert( aliasLayerStringList.at( i ), layerAliasStringList.at( i ) );
}

return aliasMap;
}

bool QgsServerProjectUtils::wmsInspireActivate( const QgsProject &project )
{
return project.readBoolEntry( QStringLiteral( "WMSInspire" ), QStringLiteral( "/activated" ) );
@@ -132,6 +132,48 @@ namespace QgsServerProjectUtils
*/
SERVER_EXPORT bool wmsInfoFormatSia2045( const QgsProject &project );

/** Returns if the geometry is displayed as Well Known Text in GetFeatureInfo request.
* \param project the QGIS project
* \returns if the geometry is displayed as Well Known Text in GetFeatureInfo request.
*/
SERVER_EXPORT bool wmsFeatureInfoAddWktGeometry( const QgsProject &project );

/** Returns if the geometry has to be segmentize in GetFeatureInfo request.
* \param project the QGIS project
* \returns if the geometry has to be segmentize in GetFeatureInfo request.
*/
SERVER_EXPORT bool wmsFeatureInfoSegmentizeWktGeometry( const QgsProject &project );

/** Returns the geometry precision for GetFeatureInfo request.
* \param project the QGIS project
* \returns the geometry precision for GetFeatureInfo request.
*/
SERVER_EXPORT int wmsFeatureInfoPrecision( const QgsProject &project );

/** Returns the document element name for XML GetFeatureInfo request.
* \param project the QGIS project
* \returns the document element name for XML GetFeatureInfo request.
*/
SERVER_EXPORT QString wmsFeatureInfoDocumentElement( const QgsProject &project );

/** Returns the document element namespace for XML GetFeatureInfo request.
* \param project the QGIS project
* \returns the document element namespace for XML GetFeatureInfo request.
*/
SERVER_EXPORT QString wmsFeatureInfoDocumentElementNs( const QgsProject &project );

/** Returns the schema URL for XML GetFeatureInfo request.
* \param project the QGIS project
* \returns the schema URL for XML GetFeatureInfo request.
*/
SERVER_EXPORT QString wmsFeatureInfoSchema( const QgsProject &project );

/** Returns the mapping between layer name and wms layer name for GetFeatureInfo request.
* \param project the QGIS project
* \returns the mapping between layer name and wms layer name for GetFeatureInfo request.
*/
SERVER_EXPORT QHash<QString, QString> wmsFeatureInfoLayerAliasMap( const QgsProject &project );

/** Returns if Inspire is activated.
* \param project the QGIS project
* \returns if Inspire is activated.
@@ -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 );
}


0 comments on commit 08c06de

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