Skip to content

Commit 72c97f3

Browse files
committedNov 6, 2017
[Server] GetFeatureInfo: enhance raster data values displayed
1 parent 61e421d commit 72c97f3

File tree

1 file changed

+47
-8
lines changed

1 file changed

+47
-8
lines changed
 

‎src/server/qgswmsserver.cpp

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2551,17 +2551,36 @@ int QgsWMSServer::featureInfoFromRasterLayer( QgsRasterLayer* layer,
25512551
if ( !identifyResult.isValid() )
25522552
return 1;
25532553

2554-
QMap<int, QVariant> attributes = identifyResult.results();
2554+
QMap<int, QVariant> values = identifyResult.results();
25552555
if ( infoFormat == "application/vnd.ogc.gml" )
25562556
{
25572557
QgsFeature feature;
25582558
QgsFields fields;
2559-
feature.initAttributes( attributes.count() );
2559+
feature.initAttributes( values.count() );
25602560
int index = 0;
2561-
for ( QMap<int, QVariant>::const_iterator it = attributes.constBegin(); it != attributes.constEnd(); ++it )
2561+
Q_FOREACH ( int bandNo, values.keys() )
25622562
{
2563-
fields.append( QgsField( layer->bandName( it.key() ), QVariant::Double ) );
2564-
feature.setAttribute( index++, QString::number( it.value().toDouble() ) );
2563+
if ( values.value( bandNo ).isNull() )
2564+
{
2565+
fields.append( QgsField( layer->bandName( bandNo ), QVariant::String ) );
2566+
feature.setAttribute( index++, "no data" );
2567+
}
2568+
else
2569+
{
2570+
QVariant value( values.value( bandNo ) );
2571+
fields.append( QgsField( layer->bandName( bandNo ), QVariant::Double ) );
2572+
// The cast is legit. Quoting QT doc :
2573+
// "Although this function is declared as returning QVariant::Type,
2574+
// the return value should be interpreted as QMetaType::Type"
2575+
if ( static_cast<QMetaType::Type>( value.type() ) == QMetaType::Float )
2576+
{
2577+
feature.setAttribute( index++, QString::number( value.toFloat() ) );
2578+
}
2579+
else
2580+
{
2581+
feature.setAttribute( index++, QString::number( value.toDouble() ) );
2582+
}
2583+
}
25652584
}
25662585
feature.setFields( fields );
25672586

@@ -2578,11 +2597,31 @@ int QgsWMSServer::featureInfoFromRasterLayer( QgsRasterLayer* layer,
25782597
}
25792598
else
25802599
{
2581-
for ( QMap<int, QVariant>::const_iterator it = attributes.constBegin(); it != attributes.constEnd(); ++it )
2600+
Q_FOREACH ( int bandNo, values.keys() )
25822601
{
2602+
QString valueString;
2603+
if ( values.value( bandNo ).isNull() )
2604+
{
2605+
valueString = "no data";
2606+
}
2607+
else
2608+
{
2609+
QVariant value( values.value( bandNo ) );
2610+
// The cast is legit. Quoting QT doc :
2611+
// "Although this function is declared as returning QVariant::Type,
2612+
// the return value should be interpreted as QMetaType::Type"
2613+
if ( static_cast<QMetaType::Type>( value.type() ) == QMetaType::Float )
2614+
{
2615+
valueString = QgsRasterBlock::printValue( value.toFloat() );
2616+
}
2617+
else
2618+
{
2619+
valueString = QgsRasterBlock::printValue( value.toDouble() );
2620+
}
2621+
}
25832622
QDomElement attributeElement = infoDocument.createElement( "Attribute" );
2584-
attributeElement.setAttribute( "name", layer->bandName( it.key() ) );
2585-
attributeElement.setAttribute( "value", QString::number( it.value().toDouble() ) );
2623+
attributeElement.setAttribute( "name", layer->bandName( bandNo ) );
2624+
attributeElement.setAttribute( "value", valueString );
25862625
layerElement.appendChild( attributeElement );
25872626
}
25882627
}

0 commit comments

Comments
 (0)
Please sign in to comment.