Quantum GIS API Documentation
1.8
|
00001 /*************************************************************************** 00002 qgsrasterlayer.h - description 00003 ------------------- 00004 begin : Fri Jun 28 2002 00005 copyright : (C) 2004 by T.Sutton, Gary E.Sherman, Steve Halasz 00006 email : [email protected] 00007 ***************************************************************************/ 00008 /* 00009 * Peter J. Ersts - contributed to the refactoring and maintenance of this class 00010 * B. Morley - added functions to convert this class to a data provider interface 00011 * Frank Warmerdam - contributed bug fixes and migrated class to use all GDAL_C_API calls 00012 */ 00013 /*************************************************************************** 00014 * * 00015 * This program is free software; you can redistribute it and/or modify * 00016 * it under the terms of the GNU General Public License as published by * 00017 * the Free Software Foundation; either version 2 of the License, or * 00018 * (at your option) any later version. * 00019 * * 00020 ***************************************************************************/ 00021 #ifndef QGSRASTERLAYER_H 00022 #define QGSRASTERLAYER_H 00023 00024 // 00025 // Includes 00026 // 00027 00028 #include <QColor> 00029 #include <QDateTime> 00030 #include <QVector> 00031 #include <QList> 00032 #include <QMap> 00033 #include <QPair> 00034 00035 #include "qgis.h" 00036 #include "qgspoint.h" 00037 #include "qgsmaplayer.h" 00038 #include "qgsrasterviewport.h" 00039 #include "qgscontrastenhancement.h" 00040 #include "qgsrastertransparency.h" 00041 #include "qgsrastershader.h" 00042 #include "qgscolorrampshader.h" 00043 #include "qgsrastershaderfunction.h" 00044 #include "qgsrasterdataprovider.h" 00045 00046 // 00047 // Forward declarations 00048 // 00049 class QgsMapToPixel; 00050 class QgsRectangle; 00051 class QgsRasterBandStats; 00052 class QgsRasterPyramid; 00053 class QImage; 00054 class QPixmap; 00055 class QSlider; 00056 class QLibrary; 00057 00169 class CORE_EXPORT QgsRasterLayer : public QgsMapLayer 00170 { 00171 Q_OBJECT 00172 public: 00189 QgsRasterLayer( const QString & path = QString::null, 00190 const QString & baseName = QString::null, 00191 bool loadDefaultStyleFlag = true ); 00192 00194 QgsRasterLayer( int dummy, 00195 const QString & baseName = QString(), 00196 const QString & path = QString(), 00197 const QString & providerLib = QString(), 00198 const QStringList & layers = QStringList(), 00199 const QStringList & styles = QStringList(), 00200 const QString & format = QString(), 00201 const QString & crs = QString() ); 00202 00203 00205 ~QgsRasterLayer(); 00206 00207 00208 // 00209 // Enums, structs and typedefs 00210 // 00212 enum ColorShadingAlgorithm 00213 { 00214 UndefinedShader, 00215 PseudoColorShader, 00216 FreakOutShader, 00217 ColorRampShader, 00218 UserDefinedShader 00219 }; 00220 00222 enum DrawingStyle 00223 { 00224 UndefinedDrawingStyle, 00225 SingleBandGray, // a single band image drawn as a range of gray colors 00226 SingleBandPseudoColor, // a single band image drawn using a pseudocolor algorithm 00227 PalettedColor, //a "Palette" image drawn using color table 00228 PalettedSingleBandGray, // a "Palette" layer drawn in gray scale 00229 PalettedSingleBandPseudoColor, // a "Palette" layerdrawn using a pseudocolor algorithm 00230 PalettedMultiBandColor, // currently not supported 00231 MultiBandSingleGandGray, // a layer containing 2 or more bands, but a single band drawn as a range of gray colors 00232 //added in 1.6 to fix naming glitch 00233 MultiBandSingleBandGray = MultiBandSingleGandGray, // a layer containing 2 or more bands, but a single band drawn as a range of gray colors 00234 MultiBandSingleBandPseudoColor, //a layer containing 2 or more bands, but a single band drawn using a pseudocolor algorithm 00235 MultiBandColor, //a layer containing 2 or more bands, mapped to RGB color space. In the case of a multiband with only two bands, one band will be mapped to more than one color. 00236 SingleBandColorDataStyle // ARGB values rendered directly 00237 }; 00238 00240 enum LayerType 00241 { 00242 GrayOrUndefined, 00243 Palette, 00244 Multiband, 00245 ColorLayer 00246 } ; 00247 00249 typedef QList<QgsContrastEnhancement> ContrastEnhancementList; 00250 00258 typedef QList<QgsRasterPyramid> RasterPyramidList; 00259 00263 typedef QList<QgsRasterBandStats> RasterStatsList; 00264 00265 // 00266 // Static methods: 00267 // 00268 static void buildSupportedRasterFileFilter( QString & fileFilters ); 00269 00275 static bool isValidRasterFileName( const QString & theFileNameQString, QString &retError ); 00276 static bool isValidRasterFileName( const QString & theFileNameQString ); 00277 //static QStringList subLayers( GDALDatasetH dataset ); 00278 00279 00281 static QDateTime lastModified( const QString & name ); 00282 00283 // Keep this for now, it is used by Python interface!!! 00285 static void registerGdalDrivers(); 00286 00287 // 00288 // Non Static inline methods 00289 // 00290 00292 void init(); 00293 00294 // For backward compatibility (Python) get rid of it once python is updated 00295 void setDataProvider( const QString & provider, 00296 const QStringList & layers, 00297 const QStringList & styles, 00298 const QString & format, 00299 const QString & crs ); 00301 void setDataProvider( const QString & provider, 00302 const QStringList & layers, 00303 const QStringList & styles, 00304 const QString & format, 00305 const QString & crs, 00306 bool loadDefaultStyleFlag ); 00307 00308 static QLibrary* loadProviderLibrary( QString theProviderKey ); 00309 static QgsRasterDataProvider* loadProvider( QString theProviderKey, QString theDataSource = 0 ); 00310 00311 00313 QString blueBandName() const { return mBlueBandName; } 00314 00316 QgsRasterLayer::ColorShadingAlgorithm colorShadingAlgorithm() const { return mColorShadingAlgorithm; } 00317 00319 QgsContrastEnhancement::ContrastEnhancementAlgorithm contrastEnhancementAlgorithm() { return mContrastEnhancementAlgorithm; } 00320 00322 QString contrastEnhancementAlgorithmAsString() const; 00323 00325 DrawingStyle drawingStyle() { return mDrawingStyle; } 00326 00328 QString grayBandName() const { return mGrayBandName; } 00329 00331 QString greenBandName() const { return mGreenBandName; } 00332 00334 bool hasPyramids() { return mHasPyramids; } 00335 00337 bool hasUserDefinedGrayMinimumMaximum() const { return mUserDefinedGrayMinimumMaximum; } 00338 00340 bool hasUserDefinedRGBMinimumMaximum() const { return mUserDefinedRGBMinimumMaximum; } 00341 00343 int height() { return mHeight; } 00344 00346 bool invertHistogram() const { return mInvertColor; } 00347 00349 bool isNoDataValueValid() const { return mValidNoDataValue; } 00350 00352 bool isGrayMinimumMaximumEstimated() const { return mGrayMinimumMaximumEstimated; } 00353 00355 bool isRGBMinimumMaximumEstimated() const { return mRGBMinimumMaximumEstimated; } 00356 00358 double noDataValue( bool* isValid = 0 ) { if ( isValid ) { *isValid = mValidNoDataValue;} return mNoDataValue; } 00359 00361 QgsRasterTransparency* rasterTransparency() { return &mRasterTransparency; } 00362 00364 QgsRasterShader* rasterShader() { return mRasterShader; } 00365 00367 LayerType rasterType() { return mRasterType; } 00368 00370 QString redBandName() const { return mRedBandName; } 00371 00372 00374 void setDrawingStyle( const DrawingStyle & theDrawingStyle ) { mDrawingStyle = theDrawingStyle; } 00375 00377 void setGrayMinimumMaximumEstimated( bool theBool ) { mGrayMinimumMaximumEstimated = theBool; } 00378 00380 void setInvertHistogram( bool theFlag ) { mInvertColor = theFlag; } 00381 00383 void setRGBMinimumMaximumEstimated( bool theBool ) { mRGBMinimumMaximumEstimated = theBool; } 00384 00386 void setStandardDeviations( double theStandardDeviations ) { mStandardDeviations = theStandardDeviations; } 00387 00389 void setUserDefinedGrayMinimumMaximum( bool theBool ) { mUserDefinedGrayMinimumMaximum = theBool; } 00390 00392 void setUserDefinedRGBMinimumMaximum( bool theBool ) { mUserDefinedRGBMinimumMaximum = theBool; } 00393 00395 double standardDeviations() const { return mStandardDeviations; } 00396 00398 QString transparentBandName() const { return mTransparencyBandName; } 00399 00403 Q_DECL_DEPRECATED bool usesProvider(); 00404 00406 int width() { return mWidth; } 00407 00408 // 00409 // Non Static methods 00410 // 00412 unsigned int bandCount() const; 00413 00415 const QString bandName( int theBandNoInt ); 00416 00420 int bandNumber( const QString & theBandName ) const; 00421 00423 const QgsRasterBandStats bandStatistics( int ); 00424 00426 const QgsRasterBandStats bandStatistics( const QString & ); 00427 00433 RasterPyramidList buildPyramidList(); 00434 00436 QString colorShadingAlgorithmAsString() const; 00437 00439 void computeMinimumMaximumEstimates( int theBand, double* theMinMax ); 00440 00442 void computeMinimumMaximumEstimates( QString theBand, double* theMinMax ); 00443 00446 void computeMinimumMaximumEstimates( int theBand, double& theMin, double& theMax ); 00447 00449 void computeMinimumMaximumFromLastExtent( int theBand, double* theMinMax ); 00450 00452 void computeMinimumMaximumFromLastExtent( QString theBand, double* theMinMax ); 00453 00456 void computeMinimumMaximumFromLastExtent( int theBand, double& theMin, double& theMax ); 00457 00459 QgsContrastEnhancement* contrastEnhancement( unsigned int theBand ); 00460 00461 const QgsContrastEnhancement* constContrastEnhancement( unsigned int theBand ) const; 00462 00464 bool copySymbologySettings( const QgsMapLayer& theOther ); 00465 00467 QList<QgsColorRampShader::ColorRampItem>* colorTable( int theBandNoInt ); 00468 00470 QgsRasterDataProvider* dataProvider(); 00471 00473 const QgsRasterDataProvider* dataProvider() const; 00474 00477 virtual void reload(); 00478 00480 bool draw( QgsRenderContext& rendererContext ); 00481 00483 void draw( QPainter * theQPainter, 00484 QgsRasterViewPort * myRasterViewPort, 00485 const QgsMapToPixel* theQgsMapToPixel = 0 ); 00486 00492 QString drawingStyleAsString() const; 00493 00495 bool hasCompatibleSymbology( const QgsMapLayer& theOther ) const; 00496 00498 bool hasStatistics( int theBandNoInt ); 00499 00501 bool identify( const QgsPoint & point, QMap<QString, QString>& results ); 00502 00504 QString identifyAsText( const QgsPoint & point ); 00505 00509 QString identifyAsHtml( const QgsPoint & point ); 00510 00512 bool isEditable() const; 00513 00515 QString lastError(); 00516 00518 QString lastErrorTitle(); 00519 00522 QList< QPair< QString, QColor > > legendSymbologyItems() const; 00523 00525 QPixmap legendAsPixmap(); 00526 00528 QPixmap legendAsPixmap( bool ); 00529 00531 QPixmap legendAsPixmap( int theLabelCount ); 00532 00534 double maximumValue( unsigned int theBand ); 00535 00537 double maximumValue( QString theBand ); 00538 00540 QString metadata(); 00541 00543 double minimumValue( unsigned int theBand ); 00544 00546 double minimumValue( QString theBand ); 00547 00549 QPixmap paletteAsPixmap( int theBandNumber = 1 ); 00550 00554 QString providerType() const; 00555 00559 Q_DECL_DEPRECATED QString providerKey() const { return providerType(); } 00560 00562 double rasterUnitsPerPixel(); 00563 00565 // Keep this for QgsRasterLayerProperties 00566 bool readColorTable( int theBandNumber, QList<QgsColorRampShader::ColorRampItem>* theList ); 00567 00569 void resetNoDataValue(); 00570 00572 void setBlueBandName( const QString & theBandName ); 00573 00575 void setColorShadingAlgorithm( QgsRasterLayer::ColorShadingAlgorithm theShaderAlgorithm ); 00576 00578 void setColorShadingAlgorithm( QString theShaderAlgorithm ); 00579 00581 void setContrastEnhancementAlgorithm( QgsContrastEnhancement::ContrastEnhancementAlgorithm theAlgorithm, 00582 bool theGenerateLookupTableFlag = true ); 00583 00585 void setContrastEnhancementAlgorithm( QString theAlgorithm, bool theGenerateLookupTableFlag = true ); 00586 00588 void setContrastEnhancementFunction( QgsContrastEnhancementFunction* theFunction ); 00589 00591 void setDrawingStyle( const QString & theDrawingStyleQString ); 00592 00594 void setGrayBandName( const QString & theBandName ); 00595 00597 void setGreenBandName( const QString & theBandName ); 00598 00600 void setMaximumValue( unsigned int theBand, double theValue, bool theGenerateLookupTableFlag = true ); 00601 00603 void setMaximumValue( QString theBand, double theValue, bool theGenerateLookupTableFlag = true ); 00604 00608 void setMinimumMaximumUsingLastExtent(); 00609 00612 void setMinimumMaximumUsingDataset(); 00613 00615 void setMinimumValue( unsigned int theBand, double theValue, bool theGenerateLookupTableFlag = true ); 00616 00618 void setMinimumValue( QString theBand, double theValue, bool theGenerateLookupTableFlag = true ); 00619 00621 void setNoDataValue( double theNoData ); 00622 00625 void setRasterShaderFunction( QgsRasterShaderFunction* theFunction ); 00626 00628 void setRedBandName( const QString & theBandName ); 00629 00631 void setTransparentBandName( const QString & theBandName ); 00632 00634 void showProgress( int theValue ); 00635 00637 virtual QStringList subLayers() const; 00638 00640 void thumbnailAsPixmap( QPixmap * theQPixmap ); 00644 void thumbnailAsImage( QImage * thepImage ); 00645 00647 void triggerRepaint(); 00648 00649 // 00650 // Virtual methods 00651 // 00658 virtual void setLayerOrder( const QStringList &layers ); 00659 00663 virtual void setSubLayerVisibility( QString name, bool vis ); 00664 00666 virtual QDateTime timestamp() const { return mDataProvider->timestamp() ; } 00667 00668 public slots: 00670 QString buildPyramids( const RasterPyramidList &, 00671 const QString & theResamplingMethod = "NEAREST", 00672 bool theTryInternalFlag = false ); 00673 00676 void populateHistogram( int theBandNoInt, 00677 int theBinCountInt = 256, 00678 bool theIgnoreOutOfRangeFlag = true, 00679 bool theThoroughBandScanFlag = false ); 00680 00681 void showStatusMessage( const QString & theMessage ); 00682 00684 void updateProgress( int, int ); 00685 00687 void onProgress( int, double, QString ); 00688 00689 signals: 00691 void progressUpdate( int theValue ); 00692 00697 void dataChanged(); 00698 00699 protected: 00700 00702 bool readSymbology( const QDomNode& node, QString& errorMessage ); 00703 00705 bool readXml( const QDomNode& layer_node ); 00706 00708 bool writeSymbology( QDomNode&, QDomDocument& doc, QString& errorMessage ) const; 00709 00711 bool writeXml( QDomNode & layer_node, QDomDocument & doc ); 00712 00713 private: 00714 // 00715 // Private methods 00716 // 00718 void drawSingleBandColorData( QPainter * theQPainter, 00719 QgsRasterViewPort * theRasterViewPort, 00720 const QgsMapToPixel* theQgsMapToPixel, 00721 int theBandNoInt ); 00722 00724 void drawMultiBandColor( QPainter * theQPainter, 00725 QgsRasterViewPort * theRasterViewPort, 00726 const QgsMapToPixel* theQgsMapToPixel ); 00727 00729 void drawMultiBandSingleBandGray( QPainter * theQPainter, 00730 QgsRasterViewPort * theRasterViewPort, 00731 const QgsMapToPixel* theQgsMapToPixel, 00732 int theBandNoInt ); 00733 00735 void drawMultiBandSingleBandPseudoColor( QPainter * theQPainter, 00736 QgsRasterViewPort * theRasterViewPort, 00737 const QgsMapToPixel* theQgsMapToPixel, 00738 int theBandNoInt ); 00739 00741 void drawPalettedSingleBandColor( QPainter * theQPainter, 00742 QgsRasterViewPort * theRasterViewPort, 00743 const QgsMapToPixel* theQgsMapToPixel, 00744 int theBandNoInt ); 00745 00747 void drawPalettedSingleBandGray( QPainter * theQPainter, 00748 QgsRasterViewPort * theRasterViewPort, 00749 const QgsMapToPixel* theQgsMapToPixel, 00750 int theBandNoInt ); 00751 00753 void drawPalettedSingleBandPseudoColor( QPainter * theQPainter, 00754 QgsRasterViewPort * theRasterViewPort, 00755 const QgsMapToPixel* theQgsMapToPixel, 00756 int theBandNoInt ); 00757 00759 void drawPalettedMultiBandColor( QPainter * theQPainter, 00760 QgsRasterViewPort * theRasterViewPort, 00761 const QgsMapToPixel* theQgsMapToPixel, 00762 int theBandNoInt ); 00763 00765 void drawSingleBandGray( QPainter * theQPainter, 00766 QgsRasterViewPort * theRasterViewPort, 00767 const QgsMapToPixel* theQgsMapToPixel, 00768 int theBandNoInt ); 00769 00771 void drawSingleBandPseudoColor( QPainter * theQPainter, 00772 QgsRasterViewPort * theRasterViewPort, 00773 const QgsMapToPixel* theQgsMapToPixel, 00774 int theBandNoInt ); 00775 00777 void closeDataProvider(); 00778 00780 QString generateBandName( int ); 00781 00783 bool hasBand( const QString & theBandName ); 00784 00786 QString projectionWkt(); 00787 00789 //void* readData( GDALRasterBandH gdalBand, QgsRasterViewPort *viewPort ); 00790 void* readData( int bandNo, QgsRasterViewPort *viewPort ); 00791 00793 bool readFile( const QString & fileName ); 00794 00796 //inline double readValue( void *data, GDALDataType type, int index ); 00797 inline double readValue( void *data, int type, int index ); 00798 00800 bool update(); 00801 00803 QString validateBandName( const QString & theBandName ); 00804 00805 // 00806 // Private member vars 00807 // 00809 const QString QSTRING_NOT_SET; 00810 const QString TRSTRING_NOT_SET; 00811 00813 int mBandCount; 00814 00816 QString mBlueBandName; 00817 00819 ColorShadingAlgorithm mColorShadingAlgorithm; 00820 00822 QgsContrastEnhancement::ContrastEnhancementAlgorithm mContrastEnhancementAlgorithm; 00823 00825 ContrastEnhancementList mContrastEnhancementList; 00826 00828 double mStandardDeviations; 00829 00831 QgsRasterDataProvider* mDataProvider; 00832 00833 DrawingStyle mDrawingStyle; 00834 00836 bool mEditable; 00837 00839 QString mError; 00840 00842 QString mErrorCaption; 00843 00845 //GDALDatasetH mGdalBaseDataset; 00846 00848 //GDALDatasetH mGdalDataset; 00849 00851 double mGeoTransform[6]; 00852 00854 QString mGrayBandName; 00855 00857 bool mGrayMinimumMaximumEstimated; 00858 00860 QString mGreenBandName; 00861 00863 bool mHasPyramids; 00864 00866 int mWidth; 00867 00869 int mHeight; 00870 00872 bool mInvertColor; 00873 00875 QDateTime mLastModified; 00876 00877 QgsRasterViewPort mLastViewPort; 00878 00880 //QLibrary* mLib; 00881 00883 bool mModified; 00884 00886 double mNoDataValue; 00887 00889 QString mProviderKey; 00890 00892 RasterPyramidList mPyramidList; 00893 00895 QgsRasterShader* mRasterShader; 00896 00898 RasterStatsList mRasterStatsList; 00899 00901 QgsRasterTransparency mRasterTransparency; 00902 00903 LayerType mRasterType; 00904 00906 QString mRedBandName; 00907 00909 bool mRGBMinimumMaximumEstimated; 00910 00912 QString mTransparencyBandName; 00913 00915 bool mUserDefinedGrayMinimumMaximum; 00916 00918 bool mUserDefinedRGBMinimumMaximum; 00919 00921 bool mValidNoDataValue; 00922 00924 /* TODO: put everything to URI */ 00925 QStringList mLayers; 00926 QStringList mStyles; 00927 QString mFormat; 00928 QString mCrs; 00929 }; 00930 00931 /*#include <QColor> 00932 00933 typedef void* GDALRasterBandH; 00934 class QgsMapToPixel; 00935 struct QgsRasterViewPort; 00936 class QImage; 00937 class QPainter;*/ 00938 00942 class CORE_EXPORT QgsRasterImageBuffer 00943 { 00944 public: 00945 QgsRasterImageBuffer( QgsRasterDataProvider *dataProvider, int bandNo, QPainter* p, 00946 QgsRasterViewPort* viewPort, const QgsMapToPixel* mapToPixel, double* mGeoTransform ); 00947 ~QgsRasterImageBuffer(); 00948 void reset( int maxPixelsInVirtualMemory = 5000000 ); 00950 bool nextScanLine( QRgb** imageScanLine, void** rasterScanLine ); 00951 00952 void setWritingEnabled( bool enabled ) { mWritingEnabled = enabled; } 00953 00954 private: 00955 QgsRasterImageBuffer(); //forbidden 00957 bool createNextPartImage(); 00958 00960 void drawPixelRectangle(); 00961 00962 QgsRasterDataProvider* mDataProvider; 00963 int mBandNo; 00964 QPainter* mPainter; 00965 QgsRasterViewPort* mViewPort; 00966 const QgsMapToPixel* mMapToPixel; 00967 double* mGeoTransform; 00968 00969 bool mValid; 00971 bool mWritingEnabled; 00973 bool mDrawPixelRect; 00974 int mCurrentRow; 00975 int mNumPartImages; //number of part images 00976 int mNumRasterRowsPerPart; //number of (raster source) rows per part 00977 int mCurrentPartRasterMin; //minimum (raster source) row of current image 00978 int mCurrentPartRasterMax; //maximum (raster source) row of current image 00979 int mCurrentPartImageRow; //current image row 00980 int mNumCurrentImageRows; //number of image rows for the current part 00981 00982 int mCurrentPart; 00983 00984 //current memory image and gdal scan data 00985 QImage* mCurrentImage; 00986 void* mCurrentGDALData; 00987 }; 00988 00989 #endif