48 #include <QApplication>
50 #include <QDomElement>
55 #include <QFontMetrics>
62 #include <QMessageBox>
73 #define ERR(message) QGS_ERROR_MESSAGE(message,"Raster layer")
81 , QSTRING_NOT_SET(
"Not Set" )
82 , TRSTRING_NOT_SET(
tr(
"Not Set" ) )
91 QString
const & baseName,
92 bool loadDefaultStyleFlag )
94 , QSTRING_NOT_SET(
"Not Set" )
95 , TRSTRING_NOT_SET(
tr(
"Not Set" ) )
105 bool defaultLoadedFlag =
false;
106 if (
mValid && loadDefaultStyleFlag )
110 if ( !defaultLoadedFlag )
122 const QString & baseName,
123 const QString & providerKey,
124 bool loadDefaultStyleFlag )
127 , QSTRING_NOT_SET(
"Not Set" )
128 , TRSTRING_NOT_SET(
tr(
"Not Set" ) )
130 , mProviderKey( providerKey )
138 bool defaultLoadedFlag =
false;
139 if (
mValid && loadDefaultStyleFlag )
143 if ( !defaultLoadedFlag )
173 QgsDebugMsg(
"Could not resolve isValidRasterFileName in gdal provider library" );
177 bool myIsValid = pValid( theFileNameQString, retErrMsg );
192 QFileInfo fi( name );
198 t = fi.lastModified();
277 QgsDebugMsg(
"coordinateTransform set -> project extents." );
301 myProjectedViewExtent = rendererContext.
extent();
302 myProjectedLayerExtent =
extent();
305 QPainter* theQPainter = rendererContext.
painter();
314 if ( myRasterExtent.
isEmpty() )
316 QgsDebugMsg(
"draw request outside view extent." );
335 myRasterViewPort->mDrawnExtent = myRasterExtent;
338 myRasterViewPort->mSrcCRS =
crs();
349 myRasterViewPort->mBottomRightPoint = theQgsMapToPixel.
transform( myRasterExtent.
xMaximum(), myRasterExtent.
yMinimum() );
357 myRasterViewPort->mTopLeftPoint.
setX( floor( myRasterViewPort->mTopLeftPoint.x() ) );
358 myRasterViewPort->mTopLeftPoint.setY( floor( myRasterViewPort->mTopLeftPoint.y() ) );
359 myRasterViewPort->mBottomRightPoint.setX( ceil( myRasterViewPort->mBottomRightPoint.x() ) );
360 myRasterViewPort->mBottomRightPoint.setY( ceil( myRasterViewPort->mBottomRightPoint.y() ) );
364 myRasterViewPort->mBottomRightPoint.y() ),
366 myRasterViewPort->mTopLeftPoint.y() )
370 myRasterViewPort->mWidth =
static_cast<int>( myRasterViewPort->mBottomRightPoint.x() - myRasterViewPort->mTopLeftPoint.x() );
371 myRasterViewPort->mHeight =
static_cast<int>( myRasterViewPort->mBottomRightPoint.y() - myRasterViewPort->mTopLeftPoint.y() );
386 QgsDebugMsgLevel( QString(
"mTopLeftPoint.x() = %1" ).arg( myRasterViewPort->mTopLeftPoint.x() ), 3 );
387 QgsDebugMsgLevel( QString(
"mBottomRightPoint.x() = %1" ).arg( myRasterViewPort->mBottomRightPoint.x() ), 3 );
388 QgsDebugMsgLevel( QString(
"mTopLeftPoint.y() = %1" ).arg( myRasterViewPort->mTopLeftPoint.y() ), 3 );
389 QgsDebugMsgLevel( QString(
"mBottomRightPoint.y() = %1" ).arg( myRasterViewPort->mBottomRightPoint.y() ), 3 );
391 QgsDebugMsgLevel( QString(
"mWidth = %1" ).arg( myRasterViewPort->mWidth ), 3 );
392 QgsDebugMsgLevel( QString(
"mHeight = %1" ).arg( myRasterViewPort->mHeight ), 3 );
401 draw( theQPainter, myRasterViewPort, &theQgsMapToPixel );
403 delete myRasterViewPort;
436 drawer.draw( theQPainter, theRasterViewPort, theQgsMapToPixel );
438 QgsDebugMsg( QString(
"total raster draw time (ms): %1" ).arg( time.elapsed(), 5 ) );
453 QList< QPair< QString, QColor > > symbolList;
465 myMetadata +=
"<p class=\"glossy\">" +
tr(
"Driver" ) +
"</p>\n";
468 myMetadata +=
"</p>\n";
474 myMetadata +=
"<p class=\"glossy\">";
475 myMetadata +=
tr(
"No Data Value" );
476 myMetadata +=
"</p>\n";
485 myMetadata +=
"*" +
tr(
"NoDataValue not set" ) +
"*";
487 myMetadata +=
"</p>\n";
489 myMetadata +=
"</p>\n";
490 myMetadata +=
"<p class=\"glossy\">";
491 myMetadata +=
tr(
"Data Type" );
492 myMetadata +=
"</p>\n";
498 myMetadata +=
tr(
"Byte - Eight bit unsigned integer" );
501 myMetadata +=
tr(
"UInt16 - Sixteen bit unsigned integer " );
504 myMetadata +=
tr(
"Int16 - Sixteen bit signed integer " );
507 myMetadata +=
tr(
"UInt32 - Thirty two bit unsigned integer " );
510 myMetadata +=
tr(
"Int32 - Thirty two bit signed integer " );
513 myMetadata +=
tr(
"Float32 - Thirty two bit floating point " );
516 myMetadata +=
tr(
"Float64 - Sixty four bit floating point " );
519 myMetadata +=
tr(
"CInt16 - Complex Int16 " );
522 myMetadata +=
tr(
"CInt32 - Complex Int32 " );
525 myMetadata +=
tr(
"CFloat32 - Complex Float32 " );
528 myMetadata +=
tr(
"CFloat64 - Complex Float64 " );
531 myMetadata +=
tr(
"Could not determine raster data type." );
533 myMetadata +=
"</p>\n";
535 myMetadata +=
"<p class=\"glossy\">";
536 myMetadata +=
tr(
"Pyramid overviews" );
537 myMetadata +=
"</p>\n";
540 myMetadata +=
"<p class=\"glossy\">";
541 myMetadata +=
tr(
"Layer Spatial Reference System" );
542 myMetadata +=
"</p>\n";
545 myMetadata +=
"</p>\n";
547 myMetadata +=
"<p class=\"glossy\">";
548 myMetadata +=
tr(
"Layer Extent (layer original source projection)" );
549 myMetadata +=
"</p>\n";
552 myMetadata +=
"</p>\n";
557 myMetadata +=
"<tr><td class=\"glossy\">";
558 myMetadata +=
tr(
"Project Spatial Reference System" );
559 myMetadata +=
"</p>\n";
561 myMetadata += mCoordinateTransform->destCRS().toProj4();
562 myMetadata +=
"</p>\n";
569 for (
int myIteratorInt = 1; myIteratorInt <= myBandCountInt; ++myIteratorInt )
571 QgsDebugMsg(
"Raster properties : checking if band " + QString::number( myIteratorInt ) +
" has stats? " );
573 myMetadata +=
"<p class=\"glossy\">\n";
574 myMetadata +=
tr(
"Band" );
575 myMetadata +=
"</p>\n";
577 myMetadata +=
bandName( myIteratorInt );
578 myMetadata +=
"</p>\n";
581 myMetadata +=
tr(
"Band No" );
582 myMetadata +=
"</p>\n";
583 myMetadata +=
"<p>\n";
584 myMetadata += QString::number( myIteratorInt );
585 myMetadata +=
"</p>\n";
593 myMetadata +=
tr(
"No Stats" );
594 myMetadata +=
"</p>\n";
595 myMetadata +=
"<p>\n";
596 myMetadata +=
tr(
"No stats collected yet" );
597 myMetadata +=
"</p>\n";
606 myMetadata +=
tr(
"Min Val" );
607 myMetadata +=
"</p>\n";
608 myMetadata +=
"<p>\n";
609 myMetadata += QString::number( myRasterBandStats.
minimumValue,
'f', 10 );
610 myMetadata +=
"</p>\n";
614 myMetadata +=
tr(
"Max Val" );
615 myMetadata +=
"</p>\n";
616 myMetadata +=
"<p>\n";
617 myMetadata += QString::number( myRasterBandStats.
maximumValue,
'f', 10 );
618 myMetadata +=
"</p>\n";
622 myMetadata +=
tr(
"Range" );
623 myMetadata +=
"</p>\n";
624 myMetadata +=
"<p>\n";
625 myMetadata += QString::number( myRasterBandStats.
range,
'f', 10 );
626 myMetadata +=
"</p>\n";
630 myMetadata +=
tr(
"Mean" );
631 myMetadata +=
"</p>\n";
632 myMetadata +=
"<p>\n";
633 myMetadata += QString::number( myRasterBandStats.
mean,
'f', 10 );
634 myMetadata +=
"</p>\n";
638 myMetadata +=
tr(
"Sum of squares" );
639 myMetadata +=
"</p>\n";
640 myMetadata +=
"<p>\n";
641 myMetadata += QString::number( myRasterBandStats.
sumOfSquares,
'f', 10 );
642 myMetadata +=
"</p>\n";
646 myMetadata +=
tr(
"Standard Deviation" );
647 myMetadata +=
"</p>\n";
648 myMetadata +=
"<p>\n";
649 myMetadata += QString::number( myRasterBandStats.
stdDev,
'f', 10 );
650 myMetadata +=
"</p>\n";
654 myMetadata +=
tr(
"Sum of all cells" );
655 myMetadata +=
"</p>\n";
656 myMetadata +=
"<p>\n";
657 myMetadata += QString::number( myRasterBandStats.
sum,
'f', 10 );
658 myMetadata +=
"</p>\n";
662 myMetadata +=
tr(
"Cell Count" );
663 myMetadata +=
"</p>\n";
664 myMetadata +=
"<p>\n";
665 myMetadata += QString::number( myRasterBandStats.
elementCount );
666 myMetadata +=
"</p>\n";
690 if ( myColorRampItemList.size() > 0 )
697 QPixmap myPalettePixmap( mySize, mySize );
698 QPainter myQPainter( &myPalettePixmap );
700 QImage myQImage = QImage( mySize, mySize, QImage::Format_RGB32 );
702 myPalettePixmap.fill();
704 double myStep = (( double )myColorRampItemList.size() - 1 ) / (
double )( mySize * mySize );
705 double myValue = 0.0;
706 for (
int myRow = 0; myRow < mySize; myRow++ )
708 QRgb* myLineBuffer = ( QRgb* )myQImage.scanLine( myRow );
709 for (
int myCol = 0; myCol < mySize; myCol++ )
711 myValue = myStep * ( double )( myCol + myRow * mySize );
713 myShader.
shade( myValue, &c1, &c2, &c3, &c4 );
714 myLineBuffer[ myCol ] = qRgba( c1, c2, c3, c4 );
718 myQPainter.drawImage( 0, 0, myQImage );
719 return myPalettePixmap;
721 QPixmap myNullPixmap;
727 QPixmap myNullPixmap;
813 if ( provider ==
"gdal" )
837 QString mySourceWkt =
crs().
toWkt();
844 QSettings myQSettings;
966 this, SLOT(
onProgress(
int,
double, QString ) )
990 QgsDebugMsg( QString(
"theAlgorithm = %1 theLimits = %2 theExtent.isEmpty() = %3" ).arg( theAlgorithm ).arg( theLimits ).arg( theExtent.
isEmpty() ) );
997 QList<QgsContrastEnhancement*> myEnhancements;
1001 if ( rendererType ==
"singlebandgray" )
1004 if ( !myGrayRenderer )
return;
1005 myBands << myGrayRenderer->
grayBand();
1007 else if ( rendererType ==
"multibandcolor" )
1010 if ( !myMultiBandRenderer )
return;
1014 foreach (
int myBand, myBands )
1022 double myMin = std::numeric_limits<double>::quiet_NaN();
1023 double myMax = std::numeric_limits<double>::quiet_NaN();
1033 double myStdDev = 1;
1035 myMin = myRasterBandStats.
mean - ( myStdDev * myRasterBandStats.
stdDev );
1036 myMax = myRasterBandStats.
mean + ( myStdDev * myRasterBandStats.
stdDev );
1040 QSettings mySettings;
1041 double myLower = mySettings.value(
"/Raster/cumulativeCutLower", QString::number(
CUMULATIVE_CUT_LOWER ) ).toDouble();
1042 double myUpper = mySettings.value(
"/Raster/cumulativeCutUpper", QString::number(
CUMULATIVE_CUT_UPPER ) ).toDouble();
1043 QgsDebugMsg( QString(
"myLower = %1 myUpper = %2" ).arg( myLower ).arg( myUpper ) );
1047 QgsDebugMsg( QString(
"myBand = %1 myMin = %2 myMax = %3" ).arg( myBand ).arg( myMin ).arg( myMax ) );
1050 myEnhancements.append( myEnhancement );
1054 myEnhancements.append( 0 );
1058 if ( rendererType ==
"singlebandgray" )
1062 else if ( rendererType ==
"multibandcolor" )
1074 QSettings mySettings;
1080 if ( dynamic_cast<QgsSingleBandGrayRenderer*>(
renderer() ) )
1082 myKey =
"singleBand";
1083 myDefault =
"StretchToMinimumMaximum";
1085 else if ( dynamic_cast<QgsMultiBandColorRenderer*>(
renderer() ) )
1089 myKey =
"multiBandSingleByte";
1090 myDefault =
"NoEnhancement";
1094 myKey =
"multiBandMultiByte";
1095 myDefault =
"StretchToMinimumMaximum";
1099 if ( myKey.isEmpty() )
1101 QgsDebugMsg(
"No default contrast enhancement for this drawing style" );
1105 QString myAlgorithmString = mySettings.value(
"/Raster/defaultContrastEnhancementAlgorithm/" + myKey, myDefault ).toString();
1106 QgsDebugMsg(
"myAlgorithmString = " + myAlgorithmString );
1115 QString myLimitsString = mySettings.value(
"/Raster/defaultContrastEnhancementLimits",
"CumulativeCut" ).toString();
1128 QgsDebugMsg(
"DrawingStyle = " + theDrawingStyleQString );
1130 if ( theDrawingStyleQString ==
"SingleBandGray" )
1134 else if ( theDrawingStyleQString ==
"SingleBandPseudoColor" )
1138 else if ( theDrawingStyleQString ==
"PalettedColor" )
1142 else if ( theDrawingStyleQString ==
"PalettedSingleBandGray" )
1146 else if ( theDrawingStyleQString ==
"PalettedSingleBandPseudoColor" )
1150 else if ( theDrawingStyleQString ==
"PalettedMultiBandColor" )
1154 else if ( theDrawingStyleQString ==
"MultiBandSingleBandGray" )
1158 else if ( theDrawingStyleQString ==
"MultiBandSingleBandPseudoColor" )
1162 else if ( theDrawingStyleQString ==
"MultiBandColor" )
1166 else if ( theDrawingStyleQString ==
"SingleBandColorDataStyle" )
1170 QgsDebugMsg(
"Setted drawingStyle to " + QString::number( drawingStyle ) );
1185 QgsDebugMsg(
"About to mDataProvider->setLayerOrder(layers)." );
1196 QgsDebugMsg(
"About to mDataProvider->setSubLayerVisibility(name, vis)." );
1205 if ( !theRenderer ) {
return; }
1231 QPixmap myQPixmap( size );
1233 myQPixmap.fill( bgColor );
1237 double myMapUnitsPerPixel;
1241 if ( myExtent.
width() / myExtent.
height() >= myQPixmap.width() / myQPixmap.height() )
1243 myMapUnitsPerPixel = myExtent.
width() / myQPixmap.width();
1244 myY = ( myQPixmap.height() - myExtent.
height() / myMapUnitsPerPixel ) / 2;
1248 myMapUnitsPerPixel = myExtent.
height() / myQPixmap.height();
1249 myX = ( myQPixmap.width() - myExtent.
width() / myMapUnitsPerPixel ) / 2;
1252 double myPixelWidth = myExtent.
width() / myMapUnitsPerPixel;
1253 double myPixelHeight = myExtent.
height() / myMapUnitsPerPixel;
1257 myRasterViewPort->
mWidth = myQPixmap.width();
1258 myRasterViewPort->
mHeight = myQPixmap.height();
1266 QPainter * myQPainter =
new QPainter( &myQPixmap );
1267 draw( myQPainter, myRasterViewPort, myMapToPixel );
1268 delete myRasterViewPort;
1269 delete myMapToPixel;
1289 Q_UNUSED( theType );
1290 Q_UNUSED( theMessage );
1291 QgsDebugMsg( QString(
"theProgress = %1" ).arg( theProgress ) );
1307 Q_UNUSED( errorMessage );
1308 QDomElement rasterRendererElem;
1314 QDomNode pipeNode = layer_node.firstChildElement(
"pipe" );
1315 if ( pipeNode.isNull() )
1317 pipeNode = layer_node;
1321 if ( !layer_node.firstChildElement(
"rasterproperties" ).isNull() )
1324 QDomNode layerNodeCopy = layer_node.cloneNode();
1325 QDomDocument doc = layerNodeCopy.ownerDocument();
1326 QDomElement rasterPropertiesElem = layerNodeCopy.firstChildElement(
"rasterproperties" );
1329 rasterRendererElem = layerNodeCopy.firstChildElement(
"rasterrenderer" );
1334 rasterRendererElem = pipeNode.firstChildElement(
"rasterrenderer" );
1337 if ( !rasterRendererElem.isNull() )
1339 QString rendererType = rasterRendererElem.attribute(
"type" );
1353 QDomElement brightnessElem = pipeNode.firstChildElement(
"brightnesscontrast" );
1354 if ( !brightnessElem.isNull() )
1356 brightnessFilter->
readXML( brightnessElem );
1364 QDomElement hueSaturationElem = pipeNode.firstChildElement(
"huesaturation" );
1365 if ( !hueSaturationElem.isNull() )
1367 hueSaturationFilter->
readXML( hueSaturationElem );
1375 QDomElement resampleElem = pipeNode.firstChildElement(
"rasterresampler" );
1376 if ( !resampleElem.isNull() )
1378 resampleFilter->
readXML( resampleElem );
1382 QDomNode blendModeNode = layer_node.namedItem(
"blendMode" );
1383 if ( !blendModeNode.isNull() )
1385 QDomElement e = blendModeNode.toElement();
1404 QDomNode pkeyNode = layer_node.namedItem(
"provider" );
1406 if ( pkeyNode.isNull() )
1412 QDomElement pkeyElt = pkeyNode.toElement();
1426 QDomNode rpNode = layer_node.namedItem(
"rasterproperties" );
1434 QgsDebugMsg(
"Old WMS URI format detected -> adding params" );
1437 QDomElement layerElement = rpNode.firstChildElement(
"wmsSublayer" );
1438 while ( !layerElement.isNull() )
1443 uri.
setParam(
"layers", layerElement.namedItem(
"name" ).toElement().text() );
1446 uri.
setParam(
"styles", layerElement.namedItem(
"style" ).toElement().text() );
1448 layerElement = layerElement.nextSiblingElement(
"wmsSublayer" );
1452 QDomNode formatNode = rpNode.namedItem(
"wmsFormat" );
1453 uri.
setParam(
"format", rpNode.namedItem(
"wmsFormat" ).toElement().text() );
1464 if ( !
mValid )
return false;
1481 QDomNode stampNode = layer_node.namedItem(
"timestamp" );
1482 if ( !stampNode.isNull() )
1484 QDateTime stamp = QDateTime::fromString( stampNode.toElement().text(), Qt::ISODate );
1486 if ( stamp < mDataProvider->dataTimestamp() )
1492 if ( !
mValid )
return false;
1498 QDomElement noDataElement = layer_node.firstChildElement(
"noData" );
1500 QDomNodeList noDataBandList = noDataElement.elementsByTagName(
"noDataList" );
1502 for (
int i = 0; i < noDataBandList.size(); ++i )
1504 QDomElement bandElement = noDataBandList.at( i ).toElement();
1506 int bandNo = bandElement.attribute(
"bandNo" ).toInt( &ok );
1507 QgsDebugMsg( QString(
"bandNo = %1" ).arg( bandNo ) );
1508 if ( ok && ( bandNo > 0 ) && ( bandNo <= mDataProvider->
bandCount() ) )
1513 QDomNodeList rangeList = bandElement.elementsByTagName(
"noDataRange" );
1515 for (
int j = 0; j < rangeList.size(); ++j )
1517 QDomElement rangeElement = rangeList.at( j ).toElement();
1518 QgsRasterRange myNoDataRange( rangeElement.attribute(
"min" ).toDouble(),
1519 rangeElement.attribute(
"max" ).toDouble() );
1520 QgsDebugMsg( QString(
"min = %1 %2" ).arg( rangeElement.attribute(
"min" ) ).arg( myNoDataRange.min() ) );
1521 myNoDataRangeList << myNoDataRange;
1538 Q_UNUSED( errorMessage );
1539 QDomElement layerElem = layer_node.toElement();
1543 QDomElement pipeElement = document.createElement(
"pipe" );
1545 for (
int i = 1; i <
mPipe.
size(); i++ )
1548 if ( !interface )
continue;
1549 interface->writeXML( document, pipeElement );
1552 layer_node.appendChild( pipeElement );
1555 QDomElement blendModeElement = document.createElement(
"blendMode" );
1557 blendModeElement.appendChild( blendModeText );
1558 layer_node.appendChild( blendModeElement );
1568 QDomDocument & document )
1572 QDomElement mapLayerNode = layer_node.toElement();
1574 if ( mapLayerNode.isNull() ||
"maplayer" != mapLayerNode.nodeName() )
1580 mapLayerNode.setAttribute(
"type",
"raster" );
1584 QDomElement provider = document.createElement(
"provider" );
1585 QDomText providerText = document.createTextNode(
mProviderKey );
1586 provider.appendChild( providerText );
1587 layer_node.appendChild( provider );
1590 QDomElement noData = document.createElement(
"noData" );
1596 QDomElement noDataRangeList = document.createElement(
"noDataList" );
1597 noDataRangeList.setAttribute(
"bandNo", bandNo );
1602 QDomElement noDataRange = document.createElement(
"noDataRange" );
1604 noDataRange.setAttribute(
"min", range.
min() );
1605 noDataRange.setAttribute(
"max", range.
max() );
1606 noDataRangeList.appendChild( noDataRange );
1609 noData.appendChild( noDataRangeList );
1612 if ( noData.hasChildNodes() )
1614 layer_node.appendChild( noData );