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();
273 QgsDebugMsg(
"coordinateTransform set -> project extents." );
297 myProjectedViewExtent = rendererContext.
extent();
298 myProjectedLayerExtent =
extent();
301 QPainter* theQPainter = rendererContext.
painter();
310 if ( myRasterExtent.
isEmpty() )
312 QgsDebugMsg(
"draw request outside view extent." );
331 myRasterViewPort->mDrawnExtent = myRasterExtent;
334 myRasterViewPort->mSrcCRS =
crs();
343 myRasterViewPort->mSrcDatumTransform = -1;
344 myRasterViewPort->mDestDatumTransform = -1;
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; }
1232 QPixmap myQPixmap( size );
1234 myQPixmap.fill( bgColor );
1238 double myMapUnitsPerPixel;
1242 if ( myExtent.
width() / myExtent.
height() >= myQPixmap.width() / myQPixmap.height() )
1244 myMapUnitsPerPixel = myExtent.
width() / myQPixmap.width();
1245 myY = ( myQPixmap.height() - myExtent.
height() / myMapUnitsPerPixel ) / 2;
1249 myMapUnitsPerPixel = myExtent.
height() / myQPixmap.height();
1250 myX = ( myQPixmap.width() - myExtent.
width() / myMapUnitsPerPixel ) / 2;
1253 double myPixelWidth = myExtent.
width() / myMapUnitsPerPixel;
1254 double myPixelHeight = myExtent.
height() / myMapUnitsPerPixel;
1258 myRasterViewPort->
mWidth = myQPixmap.width();
1259 myRasterViewPort->
mHeight = myQPixmap.height();
1269 QPainter * myQPainter =
new QPainter( &myQPixmap );
1270 draw( myQPainter, myRasterViewPort, myMapToPixel );
1271 delete myRasterViewPort;
1272 delete myMapToPixel;
1292 Q_UNUSED( theType );
1293 Q_UNUSED( theMessage );
1294 QgsDebugMsg( QString(
"theProgress = %1" ).arg( theProgress ) );
1310 Q_UNUSED( errorMessage );
1311 QDomElement rasterRendererElem;
1317 QDomNode pipeNode = layer_node.firstChildElement(
"pipe" );
1318 if ( pipeNode.isNull() )
1320 pipeNode = layer_node;
1324 if ( !layer_node.firstChildElement(
"rasterproperties" ).isNull() )
1327 QDomNode layerNodeCopy = layer_node.cloneNode();
1328 QDomDocument doc = layerNodeCopy.ownerDocument();
1329 QDomElement rasterPropertiesElem = layerNodeCopy.firstChildElement(
"rasterproperties" );
1332 rasterRendererElem = layerNodeCopy.firstChildElement(
"rasterrenderer" );
1337 rasterRendererElem = pipeNode.firstChildElement(
"rasterrenderer" );
1340 if ( !rasterRendererElem.isNull() )
1342 QString rendererType = rasterRendererElem.attribute(
"type" );
1356 QDomElement brightnessElem = pipeNode.firstChildElement(
"brightnesscontrast" );
1357 if ( !brightnessElem.isNull() )
1359 brightnessFilter->
readXML( brightnessElem );
1367 QDomElement hueSaturationElem = pipeNode.firstChildElement(
"huesaturation" );
1368 if ( !hueSaturationElem.isNull() )
1370 hueSaturationFilter->
readXML( hueSaturationElem );
1378 QDomElement resampleElem = pipeNode.firstChildElement(
"rasterresampler" );
1379 if ( !resampleElem.isNull() )
1381 resampleFilter->
readXML( resampleElem );
1385 QDomNode blendModeNode = layer_node.namedItem(
"blendMode" );
1386 if ( !blendModeNode.isNull() )
1388 QDomElement e = blendModeNode.toElement();
1407 QDomNode pkeyNode = layer_node.namedItem(
"provider" );
1409 if ( pkeyNode.isNull() )
1415 QDomElement pkeyElt = pkeyNode.toElement();
1429 QDomNode rpNode = layer_node.namedItem(
"rasterproperties" );
1437 QgsDebugMsg(
"Old WMS URI format detected -> adding params" );
1440 QDomElement layerElement = rpNode.firstChildElement(
"wmsSublayer" );
1441 while ( !layerElement.isNull() )
1446 uri.
setParam(
"layers", layerElement.namedItem(
"name" ).toElement().text() );
1449 uri.
setParam(
"styles", layerElement.namedItem(
"style" ).toElement().text() );
1451 layerElement = layerElement.nextSiblingElement(
"wmsSublayer" );
1455 QDomNode formatNode = rpNode.namedItem(
"wmsFormat" );
1456 uri.
setParam(
"format", rpNode.namedItem(
"wmsFormat" ).toElement().text() );
1467 if ( !
mValid )
return false;
1484 QDomNode stampNode = layer_node.namedItem(
"timestamp" );
1485 if ( !stampNode.isNull() )
1487 QDateTime stamp = QDateTime::fromString( stampNode.toElement().text(), Qt::ISODate );
1489 if ( stamp < mDataProvider->dataTimestamp() )
1495 if ( !
mValid )
return false;
1501 QDomElement noDataElement = layer_node.firstChildElement(
"noData" );
1503 QDomNodeList noDataBandList = noDataElement.elementsByTagName(
"noDataList" );
1505 for (
int i = 0; i < noDataBandList.size(); ++i )
1507 QDomElement bandElement = noDataBandList.at( i ).toElement();
1509 int bandNo = bandElement.attribute(
"bandNo" ).toInt( &ok );
1510 QgsDebugMsg( QString(
"bandNo = %1" ).arg( bandNo ) );
1511 if ( ok && ( bandNo > 0 ) && ( bandNo <= mDataProvider->
bandCount() ) )
1516 QDomNodeList rangeList = bandElement.elementsByTagName(
"noDataRange" );
1518 for (
int j = 0; j < rangeList.size(); ++j )
1520 QDomElement rangeElement = rangeList.at( j ).toElement();
1521 QgsRasterRange myNoDataRange( rangeElement.attribute(
"min" ).toDouble(),
1522 rangeElement.attribute(
"max" ).toDouble() );
1523 QgsDebugMsg( QString(
"min = %1 %2" ).arg( rangeElement.attribute(
"min" ) ).arg( myNoDataRange.min() ) );
1524 myNoDataRangeList << myNoDataRange;
1541 Q_UNUSED( errorMessage );
1542 QDomElement layerElem = layer_node.toElement();
1546 QDomElement pipeElement = document.createElement(
"pipe" );
1548 for (
int i = 1; i <
mPipe.
size(); i++ )
1551 if ( !interface )
continue;
1552 interface->writeXML( document, pipeElement );
1555 layer_node.appendChild( pipeElement );
1558 QDomElement blendModeElement = document.createElement(
"blendMode" );
1560 blendModeElement.appendChild( blendModeText );
1561 layer_node.appendChild( blendModeElement );
1571 QDomDocument & document )
1575 QDomElement mapLayerNode = layer_node.toElement();
1577 if ( mapLayerNode.isNull() ||
"maplayer" != mapLayerNode.nodeName() )
1583 mapLayerNode.setAttribute(
"type",
"raster" );
1587 QDomElement provider = document.createElement(
"provider" );
1588 QDomText providerText = document.createTextNode(
mProviderKey );
1589 provider.appendChild( providerText );
1590 layer_node.appendChild( provider );
1593 QDomElement noData = document.createElement(
"noData" );
1599 QDomElement noDataRangeList = document.createElement(
"noDataList" );
1600 noDataRangeList.setAttribute(
"bandNo", bandNo );
1605 QDomElement noDataRange = document.createElement(
"noDataRange" );
1607 noDataRange.setAttribute(
"min", range.
min() );
1608 noDataRange.setAttribute(
"max", range.
max() );
1609 noDataRangeList.appendChild( noDataRange );
1612 noData.appendChild( noDataRangeList );
1615 if ( noData.hasChildNodes() )
1617 layer_node.appendChild( noData );