@@ -2551,17 +2551,36 @@ int QgsWMSServer::featureInfoFromRasterLayer( QgsRasterLayer* layer,
2551
2551
if ( !identifyResult.isValid () )
2552
2552
return 1 ;
2553
2553
2554
- QMap<int , QVariant> attributes = identifyResult.results ();
2554
+ QMap<int , QVariant> values = identifyResult.results ();
2555
2555
if ( infoFormat == " application/vnd.ogc.gml" )
2556
2556
{
2557
2557
QgsFeature feature;
2558
2558
QgsFields fields;
2559
- feature.initAttributes ( attributes .count () );
2559
+ feature.initAttributes ( values .count () );
2560
2560
int index = 0 ;
2561
- for ( QMap< int , QVariant>::const_iterator it = attributes. constBegin (); it != attributes. constEnd (); ++it )
2561
+ Q_FOREACH ( int bandNo, values. keys () )
2562
2562
{
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
+ }
2565
2584
}
2566
2585
feature.setFields ( fields );
2567
2586
@@ -2578,11 +2597,31 @@ int QgsWMSServer::featureInfoFromRasterLayer( QgsRasterLayer* layer,
2578
2597
}
2579
2598
else
2580
2599
{
2581
- for ( QMap< int , QVariant>::const_iterator it = attributes. constBegin (); it != attributes. constEnd (); ++it )
2600
+ Q_FOREACH ( int bandNo, values. keys () )
2582
2601
{
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
+ }
2583
2622
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 );
2586
2625
layerElement.appendChild ( attributeElement );
2587
2626
}
2588
2627
}
0 commit comments