📄 qgsrasterlayer.cpp
字号:
} } } } } QgsDebugMsg("last modified = " + t.toString()); return t;}// emit a signal asking for a repaintvoid QgsRasterLayer::triggerRepaint(){ emit repaintRequested();}QString QgsRasterLayer::getDrawingStyleAsQString(){ switch (drawingStyle) { case SINGLE_BAND_GRAY: return QString("SINGLE_BAND_GRAY"); //no need to tr() this its not shown in ui break; case SINGLE_BAND_PSEUDO_COLOR: return QString("SINGLE_BAND_PSEUDO_COLOR");//no need to tr() this its not shown in ui break; case PALETTED_SINGLE_BAND_GRAY: return QString("PALETTED_SINGLE_BAND_GRAY");//no need to tr() this its not shown in ui break; case PALETTED_SINGLE_BAND_PSEUDO_COLOR: return QString("PALETTED_SINGLE_BAND_PSEUDO_COLOR");//no need to tr() this its not shown in ui break; case PALETTED_MULTI_BAND_COLOR: return QString("PALETTED_MULTI_BAND_COLOR");//no need to tr() this its not shown in ui break; case MULTI_BAND_SINGLE_BAND_GRAY: return QString("MULTI_BAND_SINGLE_BAND_GRAY");//no need to tr() this its not shown in ui break; case MULTI_BAND_SINGLE_BAND_PSEUDO_COLOR: return QString("MULTI_BAND_SINGLE_BAND_PSEUDO_COLOR");//no need to tr() this its not shown in ui break; case MULTI_BAND_COLOR: return QString("MULTI_BAND_COLOR");//no need to tr() this its not shown in ui break; default: break; } return QString("UNDEFINED_DRAWING_STYLE");}void QgsRasterLayer::setDrawingStyle(QString const & theDrawingStyleQString){ if (theDrawingStyleQString == "SINGLE_BAND_GRAY")//no need to tr() this its not shown in ui { drawingStyle = SINGLE_BAND_GRAY; } else if (theDrawingStyleQString == "SINGLE_BAND_PSEUDO_COLOR")//no need to tr() this its not shown in ui { drawingStyle = SINGLE_BAND_PSEUDO_COLOR; } else if (theDrawingStyleQString == "PALETTED_SINGLE_BAND_GRAY")//no need to tr() this its not shown in ui { drawingStyle = PALETTED_SINGLE_BAND_GRAY; } else if (theDrawingStyleQString == "PALETTED_SINGLE_BAND_PSEUDO_COLOR")//no need to tr() this its not shown in ui { drawingStyle = PALETTED_SINGLE_BAND_PSEUDO_COLOR; } else if (theDrawingStyleQString == "PALETTED_MULTI_BAND_COLOR")//no need to tr() this its not shown in ui { drawingStyle = PALETTED_MULTI_BAND_COLOR; } else if (theDrawingStyleQString == "MULTI_BAND_SINGLE_BAND_GRAY")//no need to tr() this its not shown in ui { drawingStyle = MULTI_BAND_SINGLE_BAND_GRAY; } else if (theDrawingStyleQString == "MULTI_BAND_SINGLE_BAND_PSEUDO_COLOR")//no need to tr() this its not shown in ui { drawingStyle = MULTI_BAND_SINGLE_BAND_PSEUDO_COLOR; } else if (theDrawingStyleQString == "MULTI_BAND_COLOR")//no need to tr() this its not shown in ui { drawingStyle = MULTI_BAND_COLOR; } else { drawingStyle = UNDEFINED_DRAWING_STYLE; }}/** This method looks to see if a given band name exists. @note muliband layers may have more than one "Undefined" band! */bool QgsRasterLayer::hasBand(QString const & theBandName){ QgsDebugMsg("Looking for band : " + theBandName); for (int i = 1; i <= GDALGetRasterCount(mGdalDataset); i++) { GDALRasterBandH myGdalBand = GDALGetRasterBand(mGdalDataset,i); QString myColorQString = GDALGetColorInterpretationName(GDALGetRasterColorInterpretation(myGdalBand));#ifdef QGISDEBUG QgsLogger::debug("band", i, __FILE__, __FUNCTION__, __LINE__, 2);#endif if (myColorQString == theBandName) {#ifdef QGISDEBUG QgsLogger::debug("band", i, __FILE__, __FUNCTION__, __LINE__, 2); QgsDebugMsgLevel("Found band : " + theBandName, 2);#endif return true; } QgsDebugMsgLevel("Found unmatched band : " + QString::number(i) + " " + myColorQString, 2); } return false;}void QgsRasterLayer::drawThumbnail(QPixmap * theQPixmap){ theQPixmap->fill(); //defaults to white // Raster providers are disabled (for the moment) if (mProviderKey.isEmpty()) { QgsRasterViewPort *myRasterViewPort = new QgsRasterViewPort(); myRasterViewPort->rectXOffset = 0; myRasterViewPort->rectYOffset = 0; myRasterViewPort->clippedXMin = 0; myRasterViewPort->clippedXMax = mRasterXDim; myRasterViewPort->clippedYMin = mRasterYDim; myRasterViewPort->clippedYMax = 0; myRasterViewPort->clippedWidth = mRasterXDim; myRasterViewPort->clippedHeight = mRasterYDim; myRasterViewPort->topLeftPoint = QgsPoint(0,0); myRasterViewPort->bottomRightPoint = QgsPoint(theQPixmap->width(), theQPixmap->height()); myRasterViewPort->drawableAreaXDim = theQPixmap->width(); myRasterViewPort->drawableAreaYDim = theQPixmap->height(); QPainter * myQPainter=new QPainter(theQPixmap); draw(myQPainter,myRasterViewPort); delete myRasterViewPort; myQPainter->end(); delete myQPainter; }}QPixmap QgsRasterLayer::getPaletteAsPixmap(){ QgsDebugMsg("QgsRasterLayer::getPaletteAsPixmap"); // Only do this for the non-provider (hard-coded GDAL) scenario... // Maybe WMS can do this differently using QImage::numColors and QImage::color() if ( (mProviderKey.isEmpty()) && (hasBand("Palette") ) //dont tr() this its a gdal word! ) { QgsDebugMsg("....found paletted image"); QgsColorTable *myColorTable = colorTable ( 1 ); GDALRasterBandH myGdalBand = GDALGetRasterBand(mGdalDataset,1); if( GDALGetRasterColorInterpretation(myGdalBand) == GCI_PaletteIndex && myColorTable->defined() ) { QgsDebugMsg("....found GCI_PaletteIndex"); double myMin = myColorTable->rmin(); double myMax = myColorTable->rmax(); QgsDebugMsg("myMin = " + QString::number(myMin) + " myMax = " + QString::number(myMax)); // Draw image int mySize = 100; QPixmap myPalettePixmap( mySize, mySize); QPainter myQPainter(&myPalettePixmap); QImage myQImage = QImage( mySize, mySize, 32); myQImage.fill(0); myQImage.setAlphaBuffer(false); myPalettePixmap.fill(); double myStep = ( myMax - myMin ) / ( mySize * mySize); for( int myRow = 0; myRow < mySize; myRow++ ) { for( int myCol = 0; myCol < mySize; myCol++ ) { double myValue = myMin + myStep * (myCol + myRow * mySize); int c1, c2, c3; bool found = myColorTable->color ( myValue, &c1, &c2, &c3 ); if ( found ) myQImage.setPixel( myCol, myRow, qRgb(c1, c2, c3)); } } myQPainter.drawImage(0,0,myQImage); return myPalettePixmap; } QPixmap myNullPixmap; return myNullPixmap; } else { //invalid layer was requested QPixmap myNullPixmap; return myNullPixmap; }}bool QgsRasterLayer::draw(QPainter * theQPainter, QgsRect & theViewExtent, QgsMapToPixel * theQgsMapToPixel, QgsCoordinateTransform*, bool drawingToEditingCanvas){ QgsDebugMsg("QgsRasterLayer::draw(4 arguments): entered."); //Dont waste time drawing if transparency is at 0 (completely transparent) if (mTransparencyLevel == 0) return TRUE; QgsDebugMsg("QgsRasterLayer::draw(4 arguments): checking timestamp."); // Check timestamp if ( !update() ) { return FALSE; } // clip raster extent to view extent QgsRect myRasterExtent = theViewExtent.intersect(&mLayerExtent); if (myRasterExtent.isEmpty()) { // nothing to do return TRUE; }#ifdef QGISDEBUG QgsLogger::debug<QgsRect>("QgsRasterLayer::draw(4 arguments): theViewExtent is ", theViewExtent, __FILE__, __FUNCTION__, __LINE__, 1); QgsLogger::debug<QgsRect>("QgsRasterLayer::draw(4 arguments): myRasterExtent is ", myRasterExtent, __FILE__, __FUNCTION__, __LINE__, 1);#endif // // The first thing we do is set up the QgsRasterViewPort. This struct stores all the settings // relating to the size (in pixels and coordinate system units) of the raster part that is // in view in the map window. It also stores the origin. // //this is not a class level member because every time the user pans or zooms //the contents of the rasterViewPort will change QgsRasterViewPort *myRasterViewPort = new QgsRasterViewPort(); // calculate raster pixel offsets from origin to clipped rect // we're only interested in positive offsets where the origin of the raster // is northwest of the origin of the view myRasterViewPort->rectXOffsetFloat = (theViewExtent.xMin() - mLayerExtent.xMin()) / fabs(mGeoTransform[1]); myRasterViewPort->rectYOffsetFloat = (mLayerExtent.yMax() - theViewExtent.yMax()) / fabs(mGeoTransform[5]); if (myRasterViewPort->rectXOffsetFloat < 0 ) { myRasterViewPort->rectXOffsetFloat = 0; } if (myRasterViewPort->rectYOffsetFloat < 0 ) { myRasterViewPort->rectYOffsetFloat = 0; } myRasterViewPort->rectXOffset = static_cast < int >(myRasterViewPort->rectXOffsetFloat); myRasterViewPort->rectYOffset = static_cast < int >(myRasterViewPort->rectYOffsetFloat);#ifdef QGISDEBUG QgsLogger::debug("QgsRasterLayer::draw(4 arguments): mGeoTransform[0] = ", mGeoTransform[0], 1, __FILE__,\ __FUNCTION__, __LINE__); QgsLogger::debug("QgsRasterLayer::draw(4 arguments): mGeoTransform[1] = ", mGeoTransform[1], 1, __FILE__,\ __FUNCTION__, __LINE__); QgsLogger::debug("QgsRasterLayer::draw(4 arguments): mGeoTransform[2] = ", mGeoTransform[2], 1, __FILE__,\ __FUNCTION__, __LINE__); QgsLogger::debug("QgsRasterLayer::draw(4 arguments): mGeoTransform[3] = ", mGeoTransform[3], 1, __FILE__,\ __FUNCTION__, __LINE__); QgsLogger::debug("QgsRasterLayer::draw(4 arguments): mGeoTransform[4] = ", mGeoTransform[4], 1, __FILE__,\ __FUNCTION__, __LINE__); QgsLogger::debug("QgsRasterLayer::draw(4 arguments): mGeoTransform[5] = ", mGeoTransform[5], 1, __FILE__,\ __FUNCTION__, __LINE__);#endif // get dimensions of clipped raster image in raster pixel space/ RasterIO will do the scaling for us. // So for example, if the user is zoomed in a long way, there may only be e.g. 5x5 pixels retrieved from // the raw raster data, but rasterio will seamlessly scale the up to whatever the screen coordinats are // e.g. a 600x800 display window (see next section below) myRasterViewPort->clippedXMin = (myRasterExtent.xMin() - mGeoTransform[0]) / mGeoTransform[1]; myRasterViewPort->clippedXMax = (myRasterExtent.xMax() - mGeoTransform[0]) / mGeoTransform[1]; myRasterViewPort->clippedYMin = (myRasterExtent.yMin() - mGeoTransform[3]) / mGeoTransform[5]; myRasterViewPort->clippedYMax = (myRasterExtent.yMax() - mGeoTransform[3]) / mGeoTransform[5]; // Sometimes the Ymin/Ymax are reversed. if (myRasterViewPort->clippedYMin > myRasterViewPort->clippedYMax) { double t = myRasterViewPort->clippedYMin; myRasterViewPort->clippedYMin = myRasterViewPort->clippedYMax; myRasterViewPort->clippedYMax = t; } // Set the clipped width and height to encompass all of the source pixels // that could end up being displayed. myRasterViewPort->clippedWidth = static_cast<int>(ceil(myRasterViewPort->clippedXMax) - floor(myRasterViewPort->clippedXMin)); myRasterViewPort->clippedHeight = static_cast<int>(ceil(myRasterViewPort->clippedYMax) - floor(myRasterViewPort->clippedYMin)); // but make sure the intended SE corner extent doesn't exceed the SE corner // of the source raster, otherwise GDAL's RasterIO gives an error and returns nothing. // The SE corner = NW origin + dimensions of the image itself. if ( (myRasterViewPort->rectXOffset + myRasterViewPort->clippedWidth) > mRasterXDim) { myRasterViewPort->clippedWidth = mRasterXDim - myRasterViewPort->rectXOffset; } if ( (myRasterViewPort->rectYOffset + myRasterViewPort->clippedHeight) > mRasterYDim) { myRasterViewPort->clippedHeight = mRasterYDim - myRasterViewPort->rectYOffset; } // get dimensions of clipped raster image in device coordinate space (this is the size of the viewport) myRasterViewPort->topLeftPoint = theQgsMapToPixel->transform(myRasterExtent.xMin(), myRasterExtent.yMax()); myRasterViewPort->bottomRightPoint = theQgsMapToPixel->transform(myRasterExtent.xMax(), myRasterExtent.yMin()); myRasterViewPort->drawableAreaXDim = static_cast<int> (fabs( (myRasterViewPort->clippedWidth / theQgsMapToPixel->mapUnitsPerPixel() * mGeoTransform[1])) + 0.5); myRasterViewPort->drawableAreaYDim = static_cast<int> (fabs( (myRasterViewPort->clippedHeight / theQgsMapToPixel->mapUnitsPerPixel() * mGeoTransform[5])) + 0.5);#ifdef QGISDEBUG QgsLogger::debug("QgsRasterLayer::draw: mapUnitsPerPixel", theQgsMapToPixel->mapUnitsPerPixel(), 1, __FILE__,\ __FUNCTION__, __LINE__); QgsLogger::debug("QgsRasterLayer::draw: mRasterXDim", mRasterXDim, 1, __FILE__, __FUNCTION__, __LINE__); QgsLogger::debug("QgsRasterLayer::draw: mRasterYDim", mRasterYDim, 1, __FILE__, __FUNCTION__, __LINE__); QgsLogger::debug("QgsRasterLayer::draw: rectXOffset", myRasterViewPort->rectXOffset, 1, __FILE__,\ __FUNCTION__, __LINE__); QgsLogger::debug("QgsRasterLayer::draw: rectXOffsetFloat", myRasterViewPort->rectXOffsetFloat, 1, __FILE__,\ __FUNCTION__, __LINE__); QgsLogger::debug("QgsRasterLayer::draw: rectYOffset", myRasterViewPort->rectYOffset, 1, __FILE__,\ __FUNCTION__, __LINE__); QgsLogger::debug("QgsRasterLayer::draw: rectYOffsetFloat", myRasterViewPort->rectYOffsetFloat, 1, __FILE__,\ __FUNCTION__, __LINE__); QgsLogger::debug("QgsRasterLayer::draw: myRasterExtent.xMin()", myRasterExtent.xMin(), 1, __FILE__, __FUNCTION__,\ __LINE__); QgsLogger::debug("QgsRasterLayer::draw: myRasterExtent.xMax()", myRasterExtent.xMax(), 1, __FILE__, __FUNCTION__,\ __LINE__); QgsLogger::debug("QgsRasterLayer::draw: myRasterExtent.yMin()", myRasterExtent.yMin(), 1, __FILE__, __FUNCTION__,\ __LINE__); QgsLogger::debug("QgsRasterLayer::draw: myRasterExtent.yMax()", myRasterExtent.yMax(), 1, __FILE__, __FUNCTION__,\ __LINE__); QgsLogger::debug("QgsRasterLayer::draw: topLeftPoint.x()", myRasterViewPort->topLeftPoint.x(), 1, __FILE__,\ __FUNCTION__, __LINE__); QgsLogger::debug("QgsRasterLayer::draw: bottomRightPoint.x()", myRasterViewPort->bottomRightPoint.x(), 1, __FILE__,\ __FUNCTION__, __LINE__); QgsLogger::debug("QgsRasterLayer::draw: topLeftPoint.y()", myRasterViewPort->topLeftPoint.y(), 1, __FILE__,\ __FUNCTION__, __LINE__); QgsLogger::debug("QgsRasterLayer::draw: bottomRightPoint.y()", myRasterViewPort->bottomRightPoint.y(), 1, __FILE__,\ __FUNCTION__, __LINE__); QgsLogger::debug("QgsRasterLayer::draw: clippedXMin", myRasterViewPort->clippedXMin, 1, __FILE__,\ __FUNCTION__, __LINE__); QgsLogger::debug("QgsRasterLayer::draw: clippedXMax", myRasterViewPort->clippedXMax, 1, __FILE__,\ __FUNCTION__, __LINE__); QgsLogger::debug("QgsRasterLayer::draw: clippedYMin", myRasterViewPort->clippedYMin, 1, __FILE__,\ __FUNCTION__, __LINE__);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -