Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
qgsrasterlayer.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrasterlayer.h - description
3  -------------------
4  begin : Fri Jun 28 2002
5  copyright : (C) 2004 by T.Sutton, Gary E.Sherman, Steve Halasz
6  email : tim@linfiniti.com
7 ***************************************************************************/
8 /*
9  * Peter J. Ersts - contributed to the refactoring and maintenance of this class
10  * B. Morley - added functions to convert this class to a data provider interface
11  * Frank Warmerdam - contributed bug fixes and migrated class to use all GDAL_C_API calls
12  */
13 /***************************************************************************
14  * *
15  * This program is free software; you can redistribute it and/or modify *
16  * it under the terms of the GNU General Public License as published by *
17  * the Free Software Foundation; either version 2 of the License, or *
18  * (at your option) any later version. *
19  * *
20  ***************************************************************************/
21 /* $Id$ */
22 #ifndef QGSRASTERLAYER_H
23 #define QGSRASTERLAYER_H
24 
25 //
26 // Includes
27 //
28 
29 #include <QColor>
30 #include <QDateTime>
31 #include <QVector>
32 #include <QList>
33 #include <QMap>
34 
35 #include "qgis.h"
36 #include "qgspoint.h"
37 #include "qgsmaplayer.h"
38 #include "qgsrasterviewport.h"
39 #include "qgscontrastenhancement.h"
40 #include "qgsrastertransparency.h"
41 #include "qgsrastershader.h"
42 #include "qgscolorrampshader.h"
44 #include "qgsrasterdataprovider.h"
45 
46 //
47 // Forward declarations
48 //
49 class QgsMapToPixel;
50 class QgsRectangle;
51 class QgsRasterBandStats;
52 class QgsRasterPyramid;
53 class QImage;
54 class QPixmap;
55 class QSlider;
56 class QLibrary;
57 
169 class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
170 {
171  Q_OBJECT
172  public:
189  QgsRasterLayer( const QString & path = QString::null,
190  const QString & baseName = QString::null,
191  bool loadDefaultStyleFlag = true );
192 
194  QgsRasterLayer( int dummy,
195  const QString & baseName = QString(),
196  const QString & path = QString(),
197  const QString & providerLib = QString(),
198  const QStringList & layers = QStringList(),
199  const QStringList & styles = QStringList(),
200  const QString & format = QString(),
201  const QString & crs = QString() );
202 
203 
205  ~QgsRasterLayer();
206 
207 
208  //
209  // Enums, structs and typedefs
210  //
213  {
218  UserDefinedShader
219  };
220 
223  {
225  SingleBandGray, // a single band image drawn as a range of gray colors
226  SingleBandPseudoColor, // a single band image drawn using a pseudocolor algorithm
227  PalettedColor, //a "Palette" image drawn using color table
228  PalettedSingleBandGray, // a "Palette" layer drawn in gray scale
229  PalettedSingleBandPseudoColor, // a "Palette" layerdrawn using a pseudocolor algorithm
230  PalettedMultiBandColor, // currently not supported
231  MultiBandSingleGandGray, // a layer containing 2 or more bands, but a single band drawn as a range of gray colors
232  //added in 1.6 to fix naming glitch
233  MultiBandSingleBandGray = MultiBandSingleGandGray, // a layer containing 2 or more bands, but a single band drawn as a range of gray colors
234  MultiBandSingleBandPseudoColor, //a layer containing 2 or more bands, but a single band drawn using a pseudocolor algorithm
235  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.
236  SingleBandColorDataStyle // ARGB values rendered directly
237  };
238 
241  {
245  ColorLayer
246  } ;
247 
249  typedef QList<QgsContrastEnhancement> ContrastEnhancementList;
250 
258  typedef QList<QgsRasterPyramid> RasterPyramidList;
259 
263  typedef QList<QgsRasterBandStats> RasterStatsList;
264 
265  //
266  // Static methods:
267  //
268  static void buildSupportedRasterFileFilter( QString & fileFilters );
269 
275  static bool isValidRasterFileName( const QString & theFileNameQString, QString &retError );
276  static bool isValidRasterFileName( const QString & theFileNameQString );
277  //static QStringList subLayers( GDALDatasetH dataset );
278 
279 
281  static QDateTime lastModified( const QString & name );
282 
283  // Keep this for now, it is used by Python interface!!!
285  static void registerGdalDrivers();
286 
287  //
288  // Non Static inline methods
289  //
290 
292  void init();
293 
294  // For backward compatibility (Python) get rid of it once python is updated
295  void setDataProvider( const QString & provider,
296  const QStringList & layers,
297  const QStringList & styles,
298  const QString & format,
299  const QString & crs );
301  void setDataProvider( const QString & provider,
302  const QStringList & layers,
303  const QStringList & styles,
304  const QString & format,
305  const QString & crs,
306  bool loadDefaultStyleFlag );
307 
308  static QLibrary* loadProviderLibrary( QString theProviderKey );
309  static QgsRasterDataProvider* loadProvider( QString theProviderKey, QString theDataSource = 0 );
310 
311 
313  QString blueBandName() const { return mBlueBandName; }
314 
316  QgsRasterLayer::ColorShadingAlgorithm colorShadingAlgorithm() const { return mColorShadingAlgorithm; }
317 
320 
322  QString contrastEnhancementAlgorithmAsString() const;
323 
325  DrawingStyle drawingStyle() { return mDrawingStyle; }
326 
328  QString grayBandName() const { return mGrayBandName; }
329 
331  QString greenBandName() const { return mGreenBandName; }
332 
334  bool hasPyramids() { return mHasPyramids; }
335 
337  bool hasUserDefinedGrayMinimumMaximum() const { return mUserDefinedGrayMinimumMaximum; }
338 
340  bool hasUserDefinedRGBMinimumMaximum() const { return mUserDefinedRGBMinimumMaximum; }
341 
343  int height() { return mHeight; }
344 
346  bool invertHistogram() const { return mInvertColor; }
347 
349  bool isNoDataValueValid() const { return mValidNoDataValue; }
350 
352  bool isGrayMinimumMaximumEstimated() const { return mGrayMinimumMaximumEstimated; }
353 
355  bool isRGBMinimumMaximumEstimated() const { return mRGBMinimumMaximumEstimated; }
356 
358  double noDataValue( bool* isValid = 0 ) { if ( isValid ) { *isValid = mValidNoDataValue;} return mNoDataValue; }
359 
361  QgsRasterTransparency* rasterTransparency() { return &mRasterTransparency; }
362 
364  QgsRasterShader* rasterShader() { return mRasterShader; }
365 
367  LayerType rasterType() { return mRasterType; }
368 
370  QString redBandName() const { return mRedBandName; }
371 
372 
374  void setDrawingStyle( const DrawingStyle & theDrawingStyle ) { mDrawingStyle = theDrawingStyle; }
375 
377  void setGrayMinimumMaximumEstimated( bool theBool ) { mGrayMinimumMaximumEstimated = theBool; }
378 
380  void setInvertHistogram( bool theFlag ) { mInvertColor = theFlag; }
381 
383  void setRGBMinimumMaximumEstimated( bool theBool ) { mRGBMinimumMaximumEstimated = theBool; }
384 
386  void setStandardDeviations( double theStandardDeviations ) { mStandardDeviations = theStandardDeviations; }
387 
389  void setUserDefinedGrayMinimumMaximum( bool theBool ) { mUserDefinedGrayMinimumMaximum = theBool; }
390 
392  void setUserDefinedRGBMinimumMaximum( bool theBool ) { mUserDefinedRGBMinimumMaximum = theBool; }
393 
395  double standardDeviations() const { return mStandardDeviations; }
396 
398  QString transparentBandName() const { return mTransparencyBandName; }
399 
401  bool usesProvider();
402 
404  int width() { return mWidth; }
405 
406  //
407  // Non Static methods
408  //
410  unsigned int bandCount();
411 
413  const QString bandName( int theBandNoInt );
414 
418  int bandNumber( const QString & theBandName );
419 
421  const QgsRasterBandStats bandStatistics( int );
422 
424  const QgsRasterBandStats bandStatistics( const QString & );
425 
431  RasterPyramidList buildPyramidList();
432 
434  QString colorShadingAlgorithmAsString() const;
435 
437  void computeMinimumMaximumEstimates( int theBand, double* theMinMax );
438 
440  void computeMinimumMaximumEstimates( QString theBand, double* theMinMax );
441 
444  void computeMinimumMaximumEstimates( int theBand, double& theMin, double& theMax );
445 
447  void computeMinimumMaximumFromLastExtent( int theBand, double* theMinMax );
448 
450  void computeMinimumMaximumFromLastExtent( QString theBand, double* theMinMax );
451 
454  void computeMinimumMaximumFromLastExtent( int theBand, double& theMin, double& theMax );
455 
457  QgsContrastEnhancement* contrastEnhancement( unsigned int theBand );
458 
460  bool copySymbologySettings( const QgsMapLayer& theOther );
461 
463  QList<QgsColorRampShader::ColorRampItem>* colorTable( int theBandNoInt );
464 
466  QgsRasterDataProvider* dataProvider();
467 
469  const QgsRasterDataProvider* dataProvider() const;
470 
473  virtual void reload();
474 
476  bool draw( QgsRenderContext& rendererContext );
477 
479  void draw( QPainter * theQPainter,
480  QgsRasterViewPort * myRasterViewPort,
481  const QgsMapToPixel* theQgsMapToPixel = 0 );
482 
488  QString drawingStyleAsString() const;
489 
491  bool hasCompatibleSymbology( const QgsMapLayer& theOther ) const;
492 
494  bool hasStatistics( int theBandNoInt );
495 
497  bool identify( const QgsPoint & point, QMap<QString, QString>& results );
498 
500  QString identifyAsText( const QgsPoint & point );
501 
505  QString identifyAsHtml( const QgsPoint & point );
506 
508  bool isEditable() const;
509 
511  QString lastError();
512 
514  QString lastErrorTitle();
515 
517  QPixmap legendAsPixmap();
518 
520  QPixmap legendAsPixmap( bool );
521 
523  QPixmap legendAsPixmap( int theLabelCount );
524 
526  double maximumValue( unsigned int theBand );
527 
529  double maximumValue( QString theBand );
530 
532  QString metadata();
533 
535  double minimumValue( unsigned int theBand );
536 
538  double minimumValue( QString theBand );
539 
541  QPixmap paletteAsPixmap( int theBand = 1 );
542 
544  QString providerKey() const;
545 
547  double rasterUnitsPerPixel();
548 
550  // Keep this for QgsRasterLayerProperties
551  bool readColorTable( int theBandNumber, QList<QgsColorRampShader::ColorRampItem>* theList );
552 
554  void resetNoDataValue();
555 
557  void setBlueBandName( const QString & theBandName );
558 
560  void setColorShadingAlgorithm( QgsRasterLayer::ColorShadingAlgorithm theShaderAlgorithm );
561 
563  void setColorShadingAlgorithm( QString theShaderAlgorithm );
564 
566  void setContrastEnhancementAlgorithm( QgsContrastEnhancement::ContrastEnhancementAlgorithm theAlgorithm,
567  bool theGenerateLookupTableFlag = true );
568 
570  void setContrastEnhancementAlgorithm( QString theAlgorithm, bool theGenerateLookupTableFlag = true );
571 
573  void setContrastEnhancementFunction( QgsContrastEnhancementFunction* theFunction );
574 
576  void setDrawingStyle( const QString & theDrawingStyleQString );
577 
579  void setGrayBandName( const QString & theBandName );
580 
582  void setGreenBandName( const QString & theBandName );
583 
585  void setMaximumValue( unsigned int theBand, double theValue, bool theGenerateLookupTableFlag = true );
586 
588  void setMaximumValue( QString theBand, double theValue, bool theGenerateLookupTableFlag = true );
589 
593  void setMinimumMaximumUsingLastExtent();
594 
597  void setMinimumMaximumUsingDataset();
598 
600  void setMinimumValue( unsigned int theBand, double theValue, bool theGenerateLookupTableFlag = true );
601 
603  void setMinimumValue( QString theBand, double theValue, bool theGenerateLookupTableFlag = true );
604 
606  void setNoDataValue( double theNoData );
607 
610  void setRasterShaderFunction( QgsRasterShaderFunction* theFunction );
611 
613  void setRedBandName( const QString & theBandName );
614 
616  void setTransparentBandName( const QString & theBandName );
617 
619  void showProgress( int theValue );
620 
622  QStringList subLayers() const;
623 
625  void thumbnailAsPixmap( QPixmap * theQPixmap );
629  void thumbnailAsImage( QImage * thepImage );
630 
632  void triggerRepaint();
633 
634  //
635  // Virtural methods
636  //
643  virtual void setLayerOrder( const QStringList & layers );
644 
648  virtual void setSubLayerVisibility( const QString & name, bool vis );
649 
651  virtual QDateTime timestamp() const { return mDataProvider->timestamp() ; }
652 
653  public slots:
655  QString buildPyramids( const RasterPyramidList &,
656  const QString & theResamplingMethod = "NEAREST",
657  bool theTryInternalFlag = false );
658 
661  void populateHistogram( int theBandNoInt,
662  int theBinCountInt = 256,
663  bool theIgnoreOutOfRangeFlag = true,
664  bool theThoroughBandScanFlag = false );
665 
666  void showStatusMessage( const QString & theMessage );
667 
669  void updateProgress( int, int );
670 
672  void onProgress( int, double, QString );
673 
674  signals:
676  void progressUpdate( int theValue );
677 
682  void dataChanged();
683 
684  protected:
685 
687  bool readSymbology( const QDomNode& node, QString& errorMessage );
688 
690  bool readXml( QDomNode & layer_node );
691 
693  bool writeSymbology( QDomNode&, QDomDocument& doc, QString& errorMessage ) const;
694 
696  bool writeXml( QDomNode & layer_node, QDomDocument & doc );
697 
698  private:
699  //
700  // Private methods
701  //
703  void drawSingleBandColorData( QPainter * theQPainter,
704  QgsRasterViewPort * theRasterViewPort,
705  const QgsMapToPixel* theQgsMapToPixel,
706  int theBandNoInt );
707 
709  void drawMultiBandColor( QPainter * theQPainter,
710  QgsRasterViewPort * theRasterViewPort,
711  const QgsMapToPixel* theQgsMapToPixel );
712 
714  void drawMultiBandSingleBandGray( QPainter * theQPainter,
715  QgsRasterViewPort * theRasterViewPort,
716  const QgsMapToPixel* theQgsMapToPixel,
717  int theBandNoInt );
718 
720  void drawMultiBandSingleBandPseudoColor( QPainter * theQPainter,
721  QgsRasterViewPort * theRasterViewPort,
722  const QgsMapToPixel* theQgsMapToPixel,
723  int theBandNoInt );
724 
726  void drawPalettedSingleBandColor( QPainter * theQPainter,
727  QgsRasterViewPort * theRasterViewPort,
728  const QgsMapToPixel* theQgsMapToPixel,
729  int theBandNoInt );
730 
732  void drawPalettedSingleBandGray( QPainter * theQPainter,
733  QgsRasterViewPort * theRasterViewPort,
734  const QgsMapToPixel* theQgsMapToPixel,
735  int theBandNoInt );
736 
738  void drawPalettedSingleBandPseudoColor( QPainter * theQPainter,
739  QgsRasterViewPort * theRasterViewPort,
740  const QgsMapToPixel* theQgsMapToPixel,
741  int theBandNoInt );
742 
744  void drawPalettedMultiBandColor( QPainter * theQPainter,
745  QgsRasterViewPort * theRasterViewPort,
746  const QgsMapToPixel* theQgsMapToPixel,
747  int theBandNoInt );
748 
750  void drawSingleBandGray( QPainter * theQPainter,
751  QgsRasterViewPort * theRasterViewPort,
752  const QgsMapToPixel* theQgsMapToPixel,
753  int theBandNoInt );
754 
756  void drawSingleBandPseudoColor( QPainter * theQPainter,
757  QgsRasterViewPort * theRasterViewPort,
758  const QgsMapToPixel* theQgsMapToPixel,
759  int theBandNoInt );
760 
762  void closeDataProvider();
763 
765  QString generateBandName( int );
766 
768  bool hasBand( const QString & theBandName );
769 
771  QString projectionWkt();
772 
774  //void* readData( GDALRasterBandH gdalBand, QgsRasterViewPort *viewPort );
775  void* readData( int bandNo, QgsRasterViewPort *viewPort );
776 
778  bool readFile( const QString & fileName );
779 
781  //inline double readValue( void *data, GDALDataType type, int index );
782  inline double readValue( void *data, int type, int index );
783 
785  bool update();
786 
788  QString validateBandName( const QString & theBandName );
789 
790  //
791  // Private member vars
792  //
794  const QString QSTRING_NOT_SET;
795  const QString TRSTRING_NOT_SET;
796 
799 
801  QString mBlueBandName;
802 
805 
808 
811 
814 
817 
819 
821  bool mEditable;
822 
824  QString mError;
825 
827  QString mErrorCaption;
828 
830  //GDALDatasetH mGdalBaseDataset;
831 
833  //GDALDatasetH mGdalDataset;
834 
836  double mGeoTransform[6];
837 
839  QString mGrayBandName;
840 
843 
845  QString mGreenBandName;
846 
849 
851  int mWidth;
852 
854  int mHeight;
855 
858 
860  QDateTime mLastModified;
861 
863 
865  //QLibrary* mLib;
866 
868  bool mModified;
869 
871  double mNoDataValue;
872 
874  QString mProviderKey;
875 
878 
881 
884 
887 
889 
891  QString mRedBandName;
892 
895 
898 
901 
904 
907 
909  /* TODO: put everything to URI */
910  QStringList mLayers;
911  QStringList mStyles;
912  QString mFormat;
913  QString mCrs;
914 };
915 
916 /*#include <QColor>
917 
918 typedef void* GDALRasterBandH;
919 class QgsMapToPixel;
920 struct QgsRasterViewPort;
921 class QImage;
922 class QPainter;*/
923 
927 class CORE_EXPORT QgsRasterImageBuffer
928 {
929  public:
930  QgsRasterImageBuffer( QgsRasterDataProvider *dataProvider, int bandNo, QPainter* p,
931  QgsRasterViewPort* viewPort, const QgsMapToPixel* mapToPixel, double* mGeoTransform );
933  void reset( int maxPixelsInVirtualMemory = 5000000 );
935  bool nextScanLine( QRgb** imageScanLine, void** rasterScanLine );
936 
937  void setWritingEnabled( bool enabled ) { mWritingEnabled = enabled; }
938 
939  private:
940  QgsRasterImageBuffer(); //forbidden
942  bool createNextPartImage();
943 
945  void drawPixelRectangle();
946 
948  int mBandNo;
949  QPainter* mPainter;
952  double* mGeoTransform;
953 
954  bool mValid;
960  int mNumPartImages; //number of part images
961  int mNumRasterRowsPerPart; //number of (raster source) rows per part
962  int mCurrentPartRasterMin; //minimum (raster source) row of current image
963  int mCurrentPartRasterMax; //maximum (raster source) row of current image
964  int mCurrentPartImageRow; //current image row
965  int mNumCurrentImageRows; //number of image rows for the current part
966 
968 
969  //current memory image and gdal scan data
970  QImage* mCurrentImage;
972 };
973 
974 #endif