QGIS API Documentation  2.8.6-Wien
qgsrasterblock.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrasterblock.h - Class representing a block of raster data
3  --------------------------------------
4  Date : Oct 9, 2012
5  Copyright : (C) 2012 by Radim Blazek
6  email : radim dot blazek at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSRASTERBLOCK_H
19 #define QGSRASTERBLOCK_H
20 
21 #include <limits>
22 #include <QImage>
23 #include "qgis.h"
24 #include "qgserror.h"
25 #include "qgslogger.h"
26 #include "qgsrasterrange.h"
27 #include "qgsrectangle.h"
28 
32 class CORE_EXPORT QgsRasterBlock
33 {
34  public:
36 
43  QgsRasterBlock( QGis::DataType theDataType, int theWidth, int theHeight );
44 
51  QgsRasterBlock( QGis::DataType theDataType, int theWidth, int theHeight, double theNoDataValue );
52 
53  virtual ~QgsRasterBlock();
54 
62  bool reset( QGis::DataType theDataType, int theWidth, int theHeight );
63 
71  bool reset( QGis::DataType theDataType, int theWidth, int theHeight, double theNoDataValue );
72 
73  // TODO: consider if use isValid() at all, isEmpty() should be sufficient
74  // and works also if block is valid but empty - difference between valid and empty?
79  bool isValid() const { return mValid; }
80 
82  void setValid( bool valid ) { mValid = valid; }
83 
88  bool isEmpty() const;
89 
90  // Return data type size in bytes
91  static int typeSize( int dataType )
92  {
93  // Modified and extended copy from GDAL
94  switch ( dataType )
95  {
96  case QGis::Byte:
97  return 1;
98 
99  case QGis::UInt16:
100  case QGis::Int16:
101  return 2;
102 
103  case QGis::UInt32:
104  case QGis::Int32:
105  case QGis::Float32:
106  case QGis::CInt16:
107  return 4;
108 
109  case QGis::Float64:
110  case QGis::CInt32:
111  case QGis::CFloat32:
112  return 8;
113 
114  case QGis::CFloat64:
115  return 16;
116 
117  case QGis::ARGB32:
119  return 4;
120 
121  default:
122  return 0;
123  }
124  }
125 
126  // Data type in bytes
127  int dataTypeSize() const
128  {
129  return typeSize( mDataType );
130  }
131 
133  static bool typeIsNumeric( QGis::DataType type );
134 
136  static bool typeIsColor( QGis::DataType type );
137 
139  QGis::DataType dataType() const { return mDataType; }
140 
142  static QGis::DataType typeWithNoDataValue( QGis::DataType dataType, double *noDataValue );
143 
146  bool hasNoDataValue() const { return mHasNoDataValue; }
147 
152  bool hasNoData() const;
153 
157  double noDataValue() const { return mNoDataValue; }
158 
163  static QByteArray valueBytes( QGis::DataType theDataType, double theValue );
164 
170  double value( int row, int column ) const;
171 
176  double value( qgssize index ) const;
177 
182  QRgb color( int row, int column ) const;
183 
187  QRgb color( qgssize index ) const;
188 
193  bool isNoData( int row, int column );
194 
198  bool isNoData( qgssize index );
199 
205  bool setValue( int row, int column, double value );
206 
211  bool setValue( qgssize index, double value );
212 
218  bool setColor( int row, int column, QRgb color );
219 
224  bool setColor( qgssize index, QRgb color );
225 
230  bool setIsNoData( int row, int column );
231 
235  bool setIsNoData( qgssize index );
236 
239  bool setIsNoData();
240 
243  bool setIsNoDataExcept( const QRect & theExceptRect );
244 
252  void setIsData( int row, int column );
253 
260  void setIsData( qgssize index );
261 
268  char * bits( int row, int column );
269 
275  char * bits( qgssize index );
276 
281  char * bits();
282 
287  static QString printValue( double value );
288 
292  bool convert( QGis::DataType destDataType );
293 
296  QImage image() const;
297 
301  bool setImage( const QImage * image );
302 
303  // @note not available in python bindings
304  inline static double readValue( void *data, QGis::DataType type, qgssize index );
305 
306  // @note not available in python bindings
307  inline static void writeValue( void *data, QGis::DataType type, qgssize index, double value );
308 
309  void applyNoDataValues( const QgsRasterRangeList & rangeList );
310 
313  void applyScaleOffset( double scale, double offset );
314 
316  QgsError error() const { return mError; }
317 
319  void setError( const QgsError & theError ) { mError = theError;}
320 
330  static QRect subRect( const QgsRectangle &theExtent, int theWidth, int theHeight, const QgsRectangle &theSubExtent );
331 
332  private:
333  static QImage::Format imageFormat( QGis::DataType theDataType );
334  static QGis::DataType dataType( QImage::Format theFormat );
335 
340  static bool isNoDataValue( double value, double noDataValue );
341 
345  bool isNoDataValue( double value ) const;
346 
349  bool createNoDataBitmap();
350 
358  static void * convert( void *srcData, QGis::DataType srcDataType, QGis::DataType destDataType, qgssize size );
359 
360  // Valid
361  bool mValid;
362 
363  // Data type
364  QGis::DataType mDataType;
365 
366  // Data type size in bytes, to make bits() fast
367  int mTypeSize;
368 
369  // Width
370  int mWidth;
371 
372  // Height
373  int mHeight;
374 
375  // Has no data value
376  bool mHasNoDataValue;
377 
378  // No data value
379  double mNoDataValue;
380 
381  static const QRgb mNoDataColor;
382 
383  // Data block for numerical data types, not used with image data types
384  // QByteArray does not seem to be intended for large data blocks, does it?
385  void * mData;
386 
387  // Image for image data types, not used with numerical data types
388  QImage *mImage;
389 
390  // Bitmap of no data. One bit for each pixel. Bit is 1 if a pixels is no data.
391  // Each row is represented by whole number of bytes (last bits may be unused)
392  // to make processing rows easy.
393  char *mNoDataBitmap;
394 
395  // number of bytes in mNoDataBitmap row
396  int mNoDataBitmapWidth;
397 
398  // total size in bytes of mNoDataBitmap
399  qgssize mNoDataBitmapSize;
400 
401  // Error
402  QgsError mError;
403 };
404 
405 inline double QgsRasterBlock::readValue( void *data, QGis::DataType type, qgssize index )
406 {
407  if ( !data )
408  {
409  return std::numeric_limits<double>::quiet_NaN();
410  }
411 
412  switch ( type )
413  {
414  case QGis::Byte:
415  return ( double )(( quint8 * )data )[index];
416  break;
417  case QGis::UInt16:
418  return ( double )(( quint16 * )data )[index];
419  break;
420  case QGis::Int16:
421  return ( double )(( qint16 * )data )[index];
422  break;
423  case QGis::UInt32:
424  return ( double )(( quint32 * )data )[index];
425  break;
426  case QGis::Int32:
427  return ( double )(( qint32 * )data )[index];
428  break;
429  case QGis::Float32:
430  return ( double )(( float * )data )[index];
431  break;
432  case QGis::Float64:
433  return ( double )(( double * )data )[index];
434  break;
435  default:
436  QgsDebugMsg( QString( "Data type %1 is not supported" ).arg( type ) );
437  break;
438  }
439 
440  return std::numeric_limits<double>::quiet_NaN();
441 }
442 
443 inline void QgsRasterBlock::writeValue( void *data, QGis::DataType type, qgssize index, double value )
444 {
445  if ( !data ) return;
446 
447  switch ( type )
448  {
449  case QGis::Byte:
450  (( quint8 * )data )[index] = ( quint8 ) value;
451  break;
452  case QGis::UInt16:
453  (( quint16 * )data )[index] = ( quint16 ) value;
454  break;
455  case QGis::Int16:
456  (( qint16 * )data )[index] = ( qint16 ) value;
457  break;
458  case QGis::UInt32:
459  (( quint32 * )data )[index] = ( quint32 ) value;
460  break;
461  case QGis::Int32:
462  (( qint32 * )data )[index] = ( qint32 ) value;
463  break;
464  case QGis::Float32:
465  (( float * )data )[index] = ( float ) value;
466  break;
467  case QGis::Float64:
468  (( double * )data )[index] = value;
469  break;
470  default:
471  QgsDebugMsg( QString( "Data type %1 is not supported" ).arg( type ) );
472  break;
473  }
474 }
475 
476 inline double QgsRasterBlock::value( qgssize index ) const
477 {
478  if ( !mData )
479  {
480  QgsDebugMsg( "Data block not allocated" );
481  return std::numeric_limits<double>::quiet_NaN();
482  }
483  return readValue( mData, mDataType, index );
484 }
485 
486 inline bool QgsRasterBlock::isNoDataValue( double value ) const
487 {
488  return qIsNaN( value ) || qgsDoubleNear( value, mNoDataValue );
489 }
490 
491 #endif
492 
493 
static unsigned index
A rectangle specified with double values.
Definition: qgsrectangle.h:35
bool isValid() const
Returns true if the block is valid (correctly filled with data).
void setError(const QgsError &theError)
Set error.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
double noDataValue() const
Return no data value.
QGis::DataType dataType() const
Returns data type.
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Definition: qgis.h:330
Raster data container.
double value(int row, int column) const
Read a single value if type of block is numeric.
static int typeSize(int dataType)
unsigned long long qgssize
qgssize is used instead of size_t, because size_t is stdlib type, unknown by SIP, and it would be har...
Definition: qgis.h:437
int dataTypeSize() const
static void writeValue(void *data, QGis::DataType type, qgssize index, double value)
void setValid(bool valid)
Mark block as valid or invalid.
bool hasNoDataValue() const
True if the block has no data value.
QList< QgsRasterRange > QgsRasterRangeList
QgsError is container for error messages (report).
Definition: qgserror.h:77
DataType
Raster data types.
Definition: qgis.h:204
static double readValue(void *data, QGis::DataType type, qgssize index)
QgsError error() const
Get error.