Quantum GIS API Documentation  1.7.4
src/core/raster/qgsrasterlayer.h
Go to the documentation of this file.
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 /* $Id$ */
00022 #ifndef QGSRASTERLAYER_H
00023 #define QGSRASTERLAYER_H
00024 
00025 //
00026 // Includes
00027 //
00028 
00029 #include <QColor>
00030 #include <QDateTime>
00031 #include <QVector>
00032 #include <QList>
00033 #include <QMap>
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 
00401     bool usesProvider();
00402 
00404     int width() { return mWidth; }
00405 
00406     //
00407     // Non Static methods
00408     //
00410     unsigned int bandCount();
00411 
00413     const  QString bandName( int theBandNoInt );
00414 
00418     int bandNumber( const QString & theBandName );
00419 
00421     const  QgsRasterBandStats bandStatistics( int );
00422 
00424     const  QgsRasterBandStats bandStatistics( const QString & );
00425 
00431     RasterPyramidList buildPyramidList();
00432 
00434     QString colorShadingAlgorithmAsString() const;
00435 
00437     void computeMinimumMaximumEstimates( int theBand, double* theMinMax );
00438 
00440     void computeMinimumMaximumEstimates( QString theBand, double* theMinMax );
00441 
00444     void computeMinimumMaximumEstimates( int theBand, double& theMin, double& theMax );
00445 
00447     void computeMinimumMaximumFromLastExtent( int theBand, double* theMinMax );
00448 
00450     void computeMinimumMaximumFromLastExtent( QString theBand, double* theMinMax );
00451 
00454     void computeMinimumMaximumFromLastExtent( int theBand, double& theMin, double& theMax );
00455 
00457     QgsContrastEnhancement* contrastEnhancement( unsigned int theBand );
00458 
00460     bool copySymbologySettings( const QgsMapLayer& theOther );
00461 
00463     QList<QgsColorRampShader::ColorRampItem>* colorTable( int theBandNoInt );
00464 
00466     QgsRasterDataProvider* dataProvider();
00467 
00469     const QgsRasterDataProvider* dataProvider() const;
00470 
00473     virtual void reload();
00474 
00476     bool draw( QgsRenderContext& rendererContext );
00477 
00479     void draw( QPainter * theQPainter,
00480                QgsRasterViewPort * myRasterViewPort,
00481                const QgsMapToPixel* theQgsMapToPixel = 0 );
00482 
00488     QString drawingStyleAsString() const;
00489 
00491     bool hasCompatibleSymbology( const QgsMapLayer& theOther ) const;
00492 
00494     bool hasStatistics( int theBandNoInt );
00495 
00497     bool identify( const QgsPoint & point, QMap<QString, QString>& results );
00498 
00500     QString identifyAsText( const QgsPoint & point );
00501 
00505     QString identifyAsHtml( const QgsPoint & point );
00506 
00508     bool isEditable() const;
00509 
00511     QString lastError();
00512 
00514     QString lastErrorTitle();
00515 
00517     QPixmap legendAsPixmap();
00518 
00520     QPixmap legendAsPixmap( bool );
00521 
00523     QPixmap legendAsPixmap( int theLabelCount );
00524 
00526     double maximumValue( unsigned int theBand );
00527 
00529     double maximumValue( QString theBand );
00530 
00532     QString metadata();
00533 
00535     double minimumValue( unsigned int theBand );
00536 
00538     double minimumValue( QString theBand );
00539 
00541     QPixmap paletteAsPixmap( int theBand = 1 );
00542 
00544     QString providerKey() const;
00545 
00547     double rasterUnitsPerPixel();
00548 
00550     // Keep this for QgsRasterLayerProperties
00551     bool readColorTable( int theBandNumber, QList<QgsColorRampShader::ColorRampItem>* theList );
00552 
00554     void resetNoDataValue();
00555 
00557     void setBlueBandName( const QString & theBandName );
00558 
00560     void setColorShadingAlgorithm( QgsRasterLayer::ColorShadingAlgorithm theShaderAlgorithm );
00561 
00563     void setColorShadingAlgorithm( QString theShaderAlgorithm );
00564 
00566     void setContrastEnhancementAlgorithm( QgsContrastEnhancement::ContrastEnhancementAlgorithm theAlgorithm,
00567                                           bool theGenerateLookupTableFlag = true );
00568 
00570     void setContrastEnhancementAlgorithm( QString theAlgorithm, bool theGenerateLookupTableFlag = true );
00571 
00573     void setContrastEnhancementFunction( QgsContrastEnhancementFunction* theFunction );
00574 
00576     void setDrawingStyle( const QString & theDrawingStyleQString );
00577 
00579     void setGrayBandName( const QString & theBandName );
00580 
00582     void setGreenBandName( const QString & theBandName );
00583 
00585     void setMaximumValue( unsigned int theBand, double theValue, bool theGenerateLookupTableFlag = true );
00586 
00588     void setMaximumValue( QString theBand, double theValue, bool theGenerateLookupTableFlag = true );
00589 
00593     void setMinimumMaximumUsingLastExtent();
00594 
00597     void setMinimumMaximumUsingDataset();
00598 
00600     void setMinimumValue( unsigned int theBand, double theValue, bool theGenerateLookupTableFlag = true );
00601 
00603     void setMinimumValue( QString theBand, double theValue, bool theGenerateLookupTableFlag = true );
00604 
00606     void setNoDataValue( double theNoData );
00607 
00610     void setRasterShaderFunction( QgsRasterShaderFunction* theFunction );
00611 
00613     void setRedBandName( const QString & theBandName );
00614 
00616     void setTransparentBandName( const QString & theBandName );
00617 
00619     void showProgress( int theValue );
00620 
00622     QStringList subLayers() const;
00623 
00625     void thumbnailAsPixmap( QPixmap * theQPixmap );
00629     void thumbnailAsImage( QImage * thepImage );
00630 
00632     void triggerRepaint();
00633 
00634     //
00635     // Virtural methods
00636     //
00643     virtual void setLayerOrder( const QStringList & layers );
00644 
00648     virtual void setSubLayerVisibility( const QString & name, bool vis );
00649 
00651     virtual QDateTime timestamp() const { return mDataProvider->timestamp() ; }
00652 
00653   public slots:
00655     QString buildPyramids( const RasterPyramidList &,
00656                            const QString &  theResamplingMethod = "NEAREST",
00657                            bool theTryInternalFlag = false );
00658 
00661     void populateHistogram( int theBandNoInt,
00662                             int theBinCountInt = 256,
00663                             bool theIgnoreOutOfRangeFlag = true,
00664                             bool theThoroughBandScanFlag = false );
00665 
00666     void showStatusMessage( const QString & theMessage );
00667 
00669     void updateProgress( int, int );
00670 
00672     void onProgress( int, double, QString );
00673 
00674   signals:
00676     void progressUpdate( int theValue );
00677 
00682     void dataChanged();
00683 
00684   protected:
00685 
00687     bool readSymbology( const QDomNode& node, QString& errorMessage );
00688 
00690     bool readXml( QDomNode & layer_node );
00691 
00693     bool writeSymbology( QDomNode&, QDomDocument& doc, QString& errorMessage ) const;
00694 
00696     bool writeXml( QDomNode & layer_node, QDomDocument & doc );
00697 
00698   private:
00699     //
00700     // Private methods
00701     //
00703     void drawSingleBandColorData( QPainter * theQPainter,
00704                                   QgsRasterViewPort * theRasterViewPort,
00705                                   const QgsMapToPixel* theQgsMapToPixel,
00706                                   int theBandNoInt );
00707 
00709     void drawMultiBandColor( QPainter * theQPainter,
00710                              QgsRasterViewPort * theRasterViewPort,
00711                              const QgsMapToPixel* theQgsMapToPixel );
00712 
00714     void drawMultiBandSingleBandGray( QPainter * theQPainter,
00715                                       QgsRasterViewPort * theRasterViewPort,
00716                                       const QgsMapToPixel* theQgsMapToPixel,
00717                                       int theBandNoInt );
00718 
00720     void drawMultiBandSingleBandPseudoColor( QPainter * theQPainter,
00721         QgsRasterViewPort * theRasterViewPort,
00722         const QgsMapToPixel* theQgsMapToPixel,
00723         int theBandNoInt );
00724 
00726     void drawPalettedSingleBandColor( QPainter * theQPainter,
00727                                       QgsRasterViewPort * theRasterViewPort,
00728                                       const QgsMapToPixel* theQgsMapToPixel,
00729                                       int theBandNoInt );
00730 
00732     void drawPalettedSingleBandGray( QPainter * theQPainter,
00733                                      QgsRasterViewPort * theRasterViewPort,
00734                                      const QgsMapToPixel* theQgsMapToPixel,
00735                                      int theBandNoInt );
00736 
00738     void drawPalettedSingleBandPseudoColor( QPainter * theQPainter,
00739                                             QgsRasterViewPort * theRasterViewPort,
00740                                             const QgsMapToPixel* theQgsMapToPixel,
00741                                             int theBandNoInt );
00742 
00744     void drawPalettedMultiBandColor( QPainter * theQPainter,
00745                                      QgsRasterViewPort * theRasterViewPort,
00746                                      const QgsMapToPixel* theQgsMapToPixel,
00747                                      int theBandNoInt );
00748 
00750     void drawSingleBandGray( QPainter * theQPainter,
00751                              QgsRasterViewPort * theRasterViewPort,
00752                              const QgsMapToPixel* theQgsMapToPixel,
00753                              int theBandNoInt );
00754 
00756     void drawSingleBandPseudoColor( QPainter * theQPainter,
00757                                     QgsRasterViewPort * theRasterViewPort,
00758                                     const QgsMapToPixel* theQgsMapToPixel,
00759                                     int theBandNoInt );
00760 
00762     void closeDataProvider();
00763 
00765     QString  generateBandName( int );
00766 
00768     bool hasBand( const QString &  theBandName );
00769 
00771     QString projectionWkt();
00772 
00774     //void* readData( GDALRasterBandH gdalBand, QgsRasterViewPort *viewPort );
00775     void* readData( int bandNo, QgsRasterViewPort *viewPort );
00776 
00778     bool readFile( const QString & fileName );
00779 
00781     //inline double readValue( void *data, GDALDataType type, int index );
00782     inline double readValue( void *data, int type, int index );
00783 
00785     bool update();
00786 
00788     QString validateBandName( const QString & theBandName );
00789 
00790     //
00791     // Private member vars
00792     //
00794     const QString QSTRING_NOT_SET;
00795     const QString TRSTRING_NOT_SET;
00796 
00798     int mBandCount;
00799 
00801     QString mBlueBandName;
00802 
00804     ColorShadingAlgorithm mColorShadingAlgorithm;
00805 
00807     QgsContrastEnhancement::ContrastEnhancementAlgorithm mContrastEnhancementAlgorithm;
00808 
00810     ContrastEnhancementList mContrastEnhancementList;
00811 
00813     double mStandardDeviations;
00814 
00816     QgsRasterDataProvider* mDataProvider;
00817 
00818     DrawingStyle mDrawingStyle;
00819 
00821     bool mEditable;
00822 
00824     QString mError;
00825 
00827     QString mErrorCaption;
00828 
00830     //GDALDatasetH mGdalBaseDataset;
00831 
00833     //GDALDatasetH mGdalDataset;
00834 
00836     double mGeoTransform[6];
00837 
00839     QString mGrayBandName;
00840 
00842     bool mGrayMinimumMaximumEstimated;
00843 
00845     QString mGreenBandName;
00846 
00848     bool mHasPyramids;
00849 
00851     int mWidth;
00852 
00854     int mHeight;
00855 
00857     bool mInvertColor;
00858 
00860     QDateTime mLastModified;
00861 
00862     QgsRasterViewPort mLastViewPort;
00863 
00865     //QLibrary* mLib;
00866 
00868     bool mModified;
00869 
00871     double mNoDataValue;
00872 
00874     QString mProviderKey;
00875 
00877     RasterPyramidList mPyramidList;
00878 
00880     QgsRasterShader* mRasterShader;
00881 
00883     RasterStatsList mRasterStatsList;
00884 
00886     QgsRasterTransparency mRasterTransparency;
00887 
00888     LayerType mRasterType;
00889 
00891     QString mRedBandName;
00892 
00894     bool mRGBMinimumMaximumEstimated;
00895 
00897     QString mTransparencyBandName;
00898 
00900     bool mUserDefinedGrayMinimumMaximum;
00901 
00903     bool mUserDefinedRGBMinimumMaximum;
00904 
00906     bool mValidNoDataValue;
00907 
00909     /* TODO: put everything to URI */
00910     QStringList mLayers;
00911     QStringList mStyles;
00912     QString mFormat;
00913     QString mCrs;
00914 };
00915 
00916 /*#include <QColor>
00917 
00918 typedef void* GDALRasterBandH;
00919 class QgsMapToPixel;
00920 struct QgsRasterViewPort;
00921 class QImage;
00922 class QPainter;*/
00923 
00927 class CORE_EXPORT QgsRasterImageBuffer
00928 {
00929   public:
00930     QgsRasterImageBuffer( QgsRasterDataProvider *dataProvider, int bandNo, QPainter* p,
00931                           QgsRasterViewPort* viewPort, const QgsMapToPixel* mapToPixel, double* mGeoTransform );
00932     ~QgsRasterImageBuffer();
00933     void reset( int maxPixelsInVirtualMemory = 5000000 );
00935     bool nextScanLine( QRgb** imageScanLine, void** rasterScanLine );
00936 
00937     void setWritingEnabled( bool enabled ) { mWritingEnabled = enabled; }
00938 
00939   private:
00940     QgsRasterImageBuffer(); //forbidden
00942     bool createNextPartImage();
00943 
00945     void drawPixelRectangle();
00946 
00947     QgsRasterDataProvider* mDataProvider;
00948     int mBandNo;
00949     QPainter* mPainter;
00950     QgsRasterViewPort* mViewPort;
00951     const QgsMapToPixel* mMapToPixel;
00952     double* mGeoTransform;
00953 
00954     bool mValid;
00956     bool mWritingEnabled;
00958     bool mDrawPixelRect;
00959     int mCurrentRow;
00960     int mNumPartImages; //number of part images
00961     int mNumRasterRowsPerPart; //number of (raster source) rows per part
00962     int mCurrentPartRasterMin; //minimum (raster source) row of current image
00963     int mCurrentPartRasterMax; //maximum (raster source) row of current image
00964     int mCurrentPartImageRow; //current image row
00965     int mNumCurrentImageRows; //number of image rows for the current part
00966 
00967     int mCurrentPart;
00968 
00969     //current memory image and gdal scan data
00970     QImage* mCurrentImage;
00971     void* mCurrentGDALData;
00972 };
00973 
00974 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines