50 #include <QApplication>
52 #include <QDomElement>
57 #include <QFontMetrics>
64 #include <QMessageBox>
75 #define ERR(message) QGS_ERROR_MESSAGE(message,"Raster layer")
83 , QSTRING_NOT_SET(
"Not Set" )
84 , TRSTRING_NOT_SET(
tr(
"Not Set" ) )
93 QString
const & baseName,
94 bool loadDefaultStyleFlag )
96 , QSTRING_NOT_SET(
"Not Set" )
97 , TRSTRING_NOT_SET(
tr(
"Not Set" ) )
107 bool defaultLoadedFlag =
false;
108 if (
mValid && loadDefaultStyleFlag )
112 if ( !defaultLoadedFlag )
124 const QString & baseName,
125 const QString & providerKey,
126 bool loadDefaultStyleFlag )
129 , QSTRING_NOT_SET(
"Not Set" )
130 , TRSTRING_NOT_SET(
tr(
"Not Set" ) )
132 , mProviderKey( providerKey )
140 bool defaultLoadedFlag =
false;
141 if (
mValid && loadDefaultStyleFlag )
145 if ( !defaultLoadedFlag )
175 QgsDebugMsg(
"Could not resolve isValidRasterFileName in gdal provider library" );
179 bool myIsValid = pValid( theFileNameQString, retErrMsg );
194 QFileInfo fi( name );
200 t = fi.lastModified();
218 if ( !mDataProvider )
return 0;
237 return mDataProvider;
245 return mDataProvider;
303 drawer.draw( theQPainter, theRasterViewPort, theQgsMapToPixel );
305 QgsDebugMsg( QString(
"total raster draw time (ms): %1" ).arg( time.elapsed(), 5 ) );
310 QList< QPair< QString, QColor > > symbolList;
322 myMetadata +=
"<p class=\"glossy\">" +
tr(
"Driver" ) +
"</p>\n";
325 myMetadata +=
"</p>\n";
329 myMetadata += mDataProvider->
metadata();
331 myMetadata +=
"<p class=\"glossy\">";
332 myMetadata +=
tr(
"No Data Value" );
333 myMetadata +=
"</p>\n";
338 myMetadata += QString::number( mDataProvider->
srcNoDataValue( 1 ) );
342 myMetadata +=
"*" +
tr(
"NoDataValue not set" ) +
"*";
344 myMetadata +=
"</p>\n";
346 myMetadata +=
"</p>\n";
347 myMetadata +=
"<p class=\"glossy\">";
348 myMetadata +=
tr(
"Data Type" );
349 myMetadata +=
"</p>\n";
355 myMetadata +=
tr(
"Byte - Eight bit unsigned integer" );
358 myMetadata +=
tr(
"UInt16 - Sixteen bit unsigned integer " );
361 myMetadata +=
tr(
"Int16 - Sixteen bit signed integer " );
364 myMetadata +=
tr(
"UInt32 - Thirty two bit unsigned integer " );
367 myMetadata +=
tr(
"Int32 - Thirty two bit signed integer " );
370 myMetadata +=
tr(
"Float32 - Thirty two bit floating point " );
373 myMetadata +=
tr(
"Float64 - Sixty four bit floating point " );
376 myMetadata +=
tr(
"CInt16 - Complex Int16 " );
379 myMetadata +=
tr(
"CInt32 - Complex Int32 " );
382 myMetadata +=
tr(
"CFloat32 - Complex Float32 " );
385 myMetadata +=
tr(
"CFloat64 - Complex Float64 " );
388 myMetadata +=
tr(
"Could not determine raster data type." );
390 myMetadata +=
"</p>\n";
392 myMetadata +=
"<p class=\"glossy\">";
393 myMetadata +=
tr(
"Pyramid overviews" );
394 myMetadata +=
"</p>\n";
397 myMetadata +=
"<p class=\"glossy\">";
398 myMetadata +=
tr(
"Layer Spatial Reference System" );
399 myMetadata +=
"</p>\n";
402 myMetadata +=
"</p>\n";
404 myMetadata +=
"<p class=\"glossy\">";
405 myMetadata +=
tr(
"Layer Extent (layer original source projection)" );
406 myMetadata +=
"</p>\n";
409 myMetadata +=
"</p>\n";
414 myMetadata +=
"<tr><td class=\"glossy\">";
415 myMetadata +=
tr(
"Project Spatial Reference System" );
416 myMetadata +=
"</p>\n";
418 myMetadata += mCoordinateTransform->destCRS().toProj4();
419 myMetadata +=
"</p>\n";
426 for (
int myIteratorInt = 1; myIteratorInt <= myBandCountInt; ++myIteratorInt )
428 QgsDebugMsg(
"Raster properties : checking if band " + QString::number( myIteratorInt ) +
" has stats? " );
430 myMetadata +=
"<p class=\"glossy\">\n";
431 myMetadata +=
tr(
"Band" );
432 myMetadata +=
"</p>\n";
434 myMetadata +=
bandName( myIteratorInt );
435 myMetadata +=
"</p>\n";
438 myMetadata +=
tr(
"Band No" );
439 myMetadata +=
"</p>\n";
440 myMetadata +=
"<p>\n";
441 myMetadata += QString::number( myIteratorInt );
442 myMetadata +=
"</p>\n";
450 myMetadata +=
tr(
"No Stats" );
451 myMetadata +=
"</p>\n";
452 myMetadata +=
"<p>\n";
453 myMetadata +=
tr(
"No stats collected yet" );
454 myMetadata +=
"</p>\n";
463 myMetadata +=
tr(
"Min Val" );
464 myMetadata +=
"</p>\n";
465 myMetadata +=
"<p>\n";
466 myMetadata += QString::number( myRasterBandStats.
minimumValue,
'f', 10 );
467 myMetadata +=
"</p>\n";
471 myMetadata +=
tr(
"Max Val" );
472 myMetadata +=
"</p>\n";
473 myMetadata +=
"<p>\n";
474 myMetadata += QString::number( myRasterBandStats.
maximumValue,
'f', 10 );
475 myMetadata +=
"</p>\n";
479 myMetadata +=
tr(
"Range" );
480 myMetadata +=
"</p>\n";
481 myMetadata +=
"<p>\n";
482 myMetadata += QString::number( myRasterBandStats.
range,
'f', 10 );
483 myMetadata +=
"</p>\n";
487 myMetadata +=
tr(
"Mean" );
488 myMetadata +=
"</p>\n";
489 myMetadata +=
"<p>\n";
490 myMetadata += QString::number( myRasterBandStats.
mean,
'f', 10 );
491 myMetadata +=
"</p>\n";
495 myMetadata +=
tr(
"Sum of squares" );
496 myMetadata +=
"</p>\n";
497 myMetadata +=
"<p>\n";
498 myMetadata += QString::number( myRasterBandStats.
sumOfSquares,
'f', 10 );
499 myMetadata +=
"</p>\n";
503 myMetadata +=
tr(
"Standard Deviation" );
504 myMetadata +=
"</p>\n";
505 myMetadata +=
"<p>\n";
506 myMetadata += QString::number( myRasterBandStats.
stdDev,
'f', 10 );
507 myMetadata +=
"</p>\n";
511 myMetadata +=
tr(
"Sum of all cells" );
512 myMetadata +=
"</p>\n";
513 myMetadata +=
"<p>\n";
514 myMetadata += QString::number( myRasterBandStats.
sum,
'f', 10 );
515 myMetadata +=
"</p>\n";
519 myMetadata +=
tr(
"Cell Count" );
520 myMetadata +=
"</p>\n";
521 myMetadata +=
"<p>\n";
522 myMetadata += QString::number( myRasterBandStats.
elementCount );
523 myMetadata +=
"</p>\n";
546 QList<QgsColorRampShader::ColorRampItem> myColorRampItemList = mDataProvider->
colorTable( theBandNumber );
547 if ( myColorRampItemList.size() > 0 )
554 QPixmap myPalettePixmap( mySize, mySize );
555 QPainter myQPainter( &myPalettePixmap );
557 QImage myQImage = QImage( mySize, mySize, QImage::Format_RGB32 );
559 myPalettePixmap.fill();
561 double myStep = (( double )myColorRampItemList.size() - 1 ) / (
double )( mySize * mySize );
562 double myValue = 0.0;
563 for (
int myRow = 0; myRow < mySize; myRow++ )
565 QRgb* myLineBuffer = ( QRgb* )myQImage.scanLine( myRow );
566 for (
int myCol = 0; myCol < mySize; myCol++ )
568 myValue = myStep * ( double )( myCol + myRow * mySize );
570 myShader.
shade( myValue, &c1, &c2, &c3, &c4 );
571 myLineBuffer[ myCol ] = qRgba( c1, c2, c3, c4 );
575 myQPainter.drawImage( 0, 0, myQImage );
576 return myPalettePixmap;
578 QPixmap myNullPixmap;
584 QPixmap myNullPixmap;
621 void QgsRasterLayer::init()
639 mPipe.
remove( mDataProvider );
645 mProviderKey = provider;
655 if ( !mDataProvider )
658 appendError(
ERR(
tr(
"Cannot instantiate the '%1' data provider" ).arg( mProviderKey ) ) );
664 mPipe.
set( mDataProvider );
665 if ( !mDataProvider->
isValid() )
672 if ( provider ==
"gdal" )
696 QString mySourceWkt =
crs().
toWkt();
703 QSettings myQSettings;
731 QgsDebugMsg(
"mRasterType = " + QString::number( mRasterType ) );
745 QList<QgsColorRampShader::ColorRampItem> colorTable = mDataProvider->
colorTable( 1 );
768 for (
int bandNo = 1; bandNo <= mDataProvider->
bandCount(); bandNo++ )
782 mPipe.
set( brightnessFilter );
786 mPipe.
set( hueSaturationFilter );
790 mPipe.
set( resampleFilter );
794 mPipe.
set( projector );
824 mDataProvider, SIGNAL( progress(
int,
double, QString ) ),
825 this, SLOT(
onProgress(
int,
double, QString ) )
840 void QgsRasterLayer::closeDataProvider()
843 mPipe.
remove( mDataProvider );
849 QgsDebugMsg( QString(
"theAlgorithm = %1 theLimits = %2 theExtent.isEmpty() = %3" ).arg( theAlgorithm ).arg( theLimits ).arg( theExtent.
isEmpty() ) );
850 if ( !mPipe.
renderer() || !mDataProvider )
856 QList<QgsContrastEnhancement*> myEnhancements;
860 if ( rendererType ==
"singlebandgray" )
863 if ( !myGrayRenderer )
return;
864 myBands << myGrayRenderer->
grayBand();
866 else if ( rendererType ==
"multibandcolor" )
869 if ( !myMultiBandRenderer )
return;
873 foreach (
int myBand, myBands )
881 double myMin = std::numeric_limits<double>::quiet_NaN();
882 double myMax = std::numeric_limits<double>::quiet_NaN();
894 myMin = myRasterBandStats.
mean - ( myStdDev * myRasterBandStats.
stdDev );
895 myMax = myRasterBandStats.
mean + ( myStdDev * myRasterBandStats.
stdDev );
899 QSettings mySettings;
900 double myLower = mySettings.value(
"/Raster/cumulativeCutLower", QString::number(
CUMULATIVE_CUT_LOWER ) ).toDouble();
901 double myUpper = mySettings.value(
"/Raster/cumulativeCutUpper", QString::number(
CUMULATIVE_CUT_UPPER ) ).toDouble();
902 QgsDebugMsg( QString(
"myLower = %1 myUpper = %2" ).arg( myLower ).arg( myUpper ) );
903 mDataProvider->
cumulativeCut( myBand, myLower, myUpper, myMin, myMax, theExtent, theSampleSize );
906 QgsDebugMsg( QString(
"myBand = %1 myMin = %2 myMax = %3" ).arg( myBand ).arg( myMin ).arg( myMax ) );
909 myEnhancements.append( myEnhancement );
913 myEnhancements.append( 0 );
917 if ( rendererType ==
"singlebandgray" )
921 else if ( rendererType ==
"multibandcolor" )
935 QSettings mySettings;
941 if ( dynamic_cast<QgsSingleBandGrayRenderer*>(
renderer() ) )
943 myKey =
"singleBand";
944 myDefault =
"StretchToMinimumMaximum";
946 else if ( dynamic_cast<QgsMultiBandColorRenderer*>(
renderer() ) )
950 myKey =
"multiBandSingleByte";
951 myDefault =
"NoEnhancement";
955 myKey =
"multiBandMultiByte";
956 myDefault =
"StretchToMinimumMaximum";
960 if ( myKey.isEmpty() )
962 QgsDebugMsg(
"No default contrast enhancement for this drawing style" );
966 QString myAlgorithmString = mySettings.value(
"/Raster/defaultContrastEnhancementAlgorithm/" + myKey, myDefault ).toString();
967 QgsDebugMsg(
"myAlgorithmString = " + myAlgorithmString );
976 QString myLimitsString = mySettings.value(
"/Raster/defaultContrastEnhancementLimits",
"CumulativeCut" ).toString();
989 QgsDebugMsg(
"DrawingStyle = " + theDrawingStyleQString );
991 if ( theDrawingStyleQString ==
"SingleBandGray" )
995 else if ( theDrawingStyleQString ==
"SingleBandPseudoColor" )
999 else if ( theDrawingStyleQString ==
"PalettedColor" )
1003 else if ( theDrawingStyleQString ==
"PalettedSingleBandGray" )
1007 else if ( theDrawingStyleQString ==
"PalettedSingleBandPseudoColor" )
1011 else if ( theDrawingStyleQString ==
"PalettedMultiBandColor" )
1015 else if ( theDrawingStyleQString ==
"MultiBandSingleBandGray" )
1019 else if ( theDrawingStyleQString ==
"MultiBandSingleBandPseudoColor" )
1023 else if ( theDrawingStyleQString ==
"MultiBandColor" )
1027 else if ( theDrawingStyleQString ==
"SingleBandColorDataStyle" )
1031 QgsDebugMsg(
"Setted drawingStyle to " + QString::number( drawingStyle ) );
1037 setRendererForDrawingStyle( drawingStyle );
1044 if ( mDataProvider )
1046 QgsDebugMsg(
"About to mDataProvider->setLayerOrder(layers)." );
1055 if ( mDataProvider )
1057 QgsDebugMsg(
"About to mDataProvider->setSubLayerVisibility(name, vis)." );
1066 if ( !theRenderer ) {
return; }
1067 mPipe.
set( theRenderer );
1093 QPixmap myQPixmap( size );
1095 myQPixmap.fill( bgColor );
1099 double myMapUnitsPerPixel;
1103 if ( myExtent.
width() / myExtent.
height() >= ( double )myQPixmap.width() / myQPixmap.height() )
1105 myMapUnitsPerPixel = myExtent.
width() / myQPixmap.width();
1106 myY = ( myQPixmap.height() - myExtent.
height() / myMapUnitsPerPixel ) / 2;
1110 myMapUnitsPerPixel = myExtent.
height() / myQPixmap.height();
1111 myX = ( myQPixmap.width() - myExtent.
width() / myMapUnitsPerPixel ) / 2;
1114 double myPixelWidth = myExtent.
width() / myMapUnitsPerPixel;
1115 double myPixelHeight = myExtent.
height() / myMapUnitsPerPixel;
1119 myRasterViewPort->
mWidth = myQPixmap.width();
1120 myRasterViewPort->
mHeight = myQPixmap.height();
1130 QPainter * myQPainter =
new QPainter( &myQPixmap );
1131 draw( myQPainter, myRasterViewPort, myMapToPixel );
1132 delete myRasterViewPort;
1133 delete myMapToPixel;
1144 QImage myQImage( size, format );
1146 myQImage.setColor( 0, bgColor.rgba() );
1151 double myMapUnitsPerPixel;
1155 if ( myExtent.
width() / myExtent.
height() >= ( double )myQImage.width() / myQImage.height() )
1157 myMapUnitsPerPixel = myExtent.
width() / myQImage.width();
1158 myY = ( myQImage.height() - myExtent.
height() / myMapUnitsPerPixel ) / 2;
1162 myMapUnitsPerPixel = myExtent.
height() / myQImage.height();
1163 myX = ( myQImage.width() - myExtent.
width() / myMapUnitsPerPixel ) / 2;
1166 double myPixelWidth = myExtent.
width() / myMapUnitsPerPixel;
1167 double myPixelHeight = myExtent.
height() / myMapUnitsPerPixel;
1171 myRasterViewPort->
mWidth = myQImage.width();
1172 myRasterViewPort->
mHeight = myQImage.height();
1182 QPainter * myQPainter =
new QPainter( &myQImage );
1183 draw( myQPainter, myRasterViewPort, myMapToPixel );
1184 delete myRasterViewPort;
1185 delete myMapToPixel;
1194 Q_UNUSED( theProgress );
1200 Q_UNUSED( theType );
1201 Q_UNUSED( theMessage );
1202 QgsDebugMsg( QString(
"theProgress = %1" ).arg( theProgress ) );
1218 Q_UNUSED( errorMessage );
1219 QDomElement rasterRendererElem;
1225 QDomNode pipeNode = layer_node.firstChildElement(
"pipe" );
1226 if ( pipeNode.isNull() )
1228 pipeNode = layer_node;
1232 if ( !layer_node.firstChildElement(
"rasterproperties" ).isNull() )
1235 QDomNode layerNodeCopy = layer_node.cloneNode();
1236 QDomDocument doc = layerNodeCopy.ownerDocument();
1237 QDomElement rasterPropertiesElem = layerNodeCopy.firstChildElement(
"rasterproperties" );
1240 rasterRendererElem = layerNodeCopy.firstChildElement(
"rasterrenderer" );
1245 rasterRendererElem = pipeNode.firstChildElement(
"rasterrenderer" );
1248 if ( !rasterRendererElem.isNull() )
1250 QString rendererType = rasterRendererElem.attribute(
"type" );
1255 mPipe.
set( renderer );
1261 mPipe.
set( brightnessFilter );
1264 QDomElement brightnessElem = pipeNode.firstChildElement(
"brightnesscontrast" );
1265 if ( !brightnessElem.isNull() )
1267 brightnessFilter->
readXML( brightnessElem );
1272 mPipe.
set( hueSaturationFilter );
1275 QDomElement hueSaturationElem = pipeNode.firstChildElement(
"huesaturation" );
1276 if ( !hueSaturationElem.isNull() )
1278 hueSaturationFilter->
readXML( hueSaturationElem );
1283 mPipe.
set( resampleFilter );
1286 QDomElement resampleElem = pipeNode.firstChildElement(
"rasterresampler" );
1287 if ( !resampleElem.isNull() )
1289 resampleFilter->
readXML( resampleElem );
1293 QDomNode blendModeNode = layer_node.namedItem(
"blendMode" );
1294 if ( !blendModeNode.isNull() )
1296 QDomElement e = blendModeNode.toElement();
1315 QDomNode pkeyNode = layer_node.namedItem(
"provider" );
1317 if ( pkeyNode.isNull() )
1319 mProviderKey =
"gdal";
1323 QDomElement pkeyElt = pkeyNode.toElement();
1324 mProviderKey = pkeyElt.text();
1325 if ( mProviderKey.isEmpty() )
1327 mProviderKey =
"gdal";
1337 QDomNode rpNode = layer_node.namedItem(
"rasterproperties" );
1339 if ( mProviderKey ==
"wms" )
1345 QgsDebugMsg(
"Old WMS URI format detected -> adding params" );
1348 QDomElement layerElement = rpNode.firstChildElement(
"wmsSublayer" );
1349 while ( !layerElement.isNull() )
1354 uri.
setParam(
"layers", layerElement.namedItem(
"name" ).toElement().text() );
1357 uri.
setParam(
"styles", layerElement.namedItem(
"style" ).toElement().text() );
1359 layerElement = layerElement.nextSiblingElement(
"wmsSublayer" );
1363 QDomNode formatNode = rpNode.namedItem(
"wmsFormat" );
1364 uri.
setParam(
"format", rpNode.namedItem(
"wmsFormat" ).toElement().text() );
1375 if ( !
mValid )
return false;
1381 if ( res && mProviderKey ==
"wms" && ( !
renderer() ||
renderer()->
type() !=
"singlebandcolordata" ) )
1392 QDomNode stampNode = layer_node.namedItem(
"timestamp" );
1393 if ( !stampNode.isNull() )
1395 QDateTime stamp = QDateTime::fromString( stampNode.toElement().text(), Qt::ISODate );
1397 if ( stamp < mDataProvider->dataTimestamp() )
1400 closeDataProvider();
1403 if ( !
mValid )
return false;
1409 QDomElement noDataElement = layer_node.firstChildElement(
"noData" );
1411 QDomNodeList noDataBandList = noDataElement.elementsByTagName(
"noDataList" );
1413 for (
int i = 0; i < noDataBandList.size(); ++i )
1415 QDomElement bandElement = noDataBandList.at( i ).toElement();
1417 int bandNo = bandElement.attribute(
"bandNo" ).toInt( &ok );
1418 QgsDebugMsg( QString(
"bandNo = %1" ).arg( bandNo ) );
1419 if ( ok && ( bandNo > 0 ) && ( bandNo <= mDataProvider->
bandCount() ) )
1421 mDataProvider->
setUseSrcNoDataValue( bandNo, bandElement.attribute(
"useSrcNoData" ).toInt() );
1424 QDomNodeList rangeList = bandElement.elementsByTagName(
"noDataRange" );
1426 for (
int j = 0; j < rangeList.size(); ++j )
1428 QDomElement rangeElement = rangeList.at( j ).toElement();
1429 QgsRasterRange myNoDataRange( rangeElement.attribute(
"min" ).toDouble(),
1430 rangeElement.attribute(
"max" ).toDouble() );
1431 QgsDebugMsg( QString(
"min = %1 %2" ).arg( rangeElement.attribute(
"min" ) ).arg( myNoDataRange.min() ) );
1432 myNoDataRangeList << myNoDataRange;
1451 Q_UNUSED( errorMessage );
1452 QDomElement layerElem = layer_node.toElement();
1456 QDomElement pipeElement = document.createElement(
"pipe" );
1458 for (
int i = 1; i < mPipe.
size(); i++ )
1461 if ( !interface )
continue;
1462 interface->writeXML( document, pipeElement );
1465 layer_node.appendChild( pipeElement );
1468 QDomElement blendModeElement = document.createElement(
"blendMode" );
1470 blendModeElement.appendChild( blendModeText );
1471 layer_node.appendChild( blendModeElement );
1481 QDomDocument & document )
1485 QDomElement mapLayerNode = layer_node.toElement();
1487 if ( mapLayerNode.isNull() ||
"maplayer" != mapLayerNode.nodeName() )
1493 mapLayerNode.setAttribute(
"type",
"raster" );
1497 QDomElement provider = document.createElement(
"provider" );
1498 QDomText providerText = document.createTextNode( mProviderKey );
1499 provider.appendChild( providerText );
1500 layer_node.appendChild( provider );
1503 QDomElement noData = document.createElement(
"noData" );
1505 for (
int bandNo = 1; bandNo <= mDataProvider->
bandCount(); bandNo++ )
1507 QDomElement noDataRangeList = document.createElement(
"noDataList" );
1508 noDataRangeList.setAttribute(
"bandNo", bandNo );
1509 noDataRangeList.setAttribute(
"useSrcNoData", mDataProvider->
useSrcNoDataValue( bandNo ) );
1513 QDomElement noDataRange = document.createElement(
"noDataRange" );
1515 noDataRange.setAttribute(
"min", range.
min() );
1516 noDataRange.setAttribute(
"max", range.
max() );
1517 noDataRangeList.appendChild( noDataRange );
1520 noData.appendChild( noDataRangeList );
1523 if ( noData.hasChildNodes() )
1525 layer_node.appendChild( noData );
1537 if ( !mDataProvider )
return 0;
1538 return mDataProvider->
xSize();
1543 if ( !mDataProvider )
return 0;
1544 return mDataProvider->
ySize();
1552 bool QgsRasterLayer::update()
1559 closeDataProvider();