Skip to content

Commit

Permalink
raster cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Apr 16, 2013
1 parent 1208051 commit c000ded
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 226 deletions.
8 changes: 2 additions & 6 deletions python/core/raster/qgsrasterinterface.sip
Expand Up @@ -44,15 +44,11 @@ class QgsRasterInterface
enum Capability
{
NoCapabilities,
Identify,
ExactMinimumMaximum,
ExactResolution,
EstimatedMinimumMaximum,
BuildPyramids,
Histogram,
Size,
Create,
Remove,
BuildPyramids,
Identify,
IdentifyValue,
IdentifyText,
IdentifyHtml,
Expand Down
7 changes: 5 additions & 2 deletions src/app/qgsrasterlayerproperties.cpp
Expand Up @@ -195,7 +195,10 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer* lyr, QgsMapCanv
mOptsPage_Pyramids->setEnabled( false );
}

if ( !( provider->capabilities() & QgsRasterDataProvider::Histogram ) )
// We can calculate histogram for all data sources but estimated only if
// size is unknown - could also be enabled if well supported (estimated histogram
// and and let user know that it is estimated)
if ( !( provider->capabilities() & QgsRasterDataProvider::Size ) )
{
// disable Histogram tab completely
mOptsPage_Histogram->setEnabled( false );
Expand Down Expand Up @@ -572,7 +575,7 @@ void QgsRasterLayerProperties::sync()
}
}

if ( !( mRasterLayer->dataProvider()->capabilities() & QgsRasterDataProvider::Histogram ) )
if ( !( mRasterLayer->dataProvider()->capabilities() & QgsRasterDataProvider::Size ) )
{
if ( mOptsPage_Histogram != NULL )
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/raster/qgsrasterdataprovider.cpp
Expand Up @@ -106,7 +106,7 @@ QgsRasterBlock * QgsRasterDataProvider::block( int theBandNo, QgsRectangle cons
double tmpXRes, tmpYRes;
double providerXRes = 0;
double providerYRes = 0;
if ( capabilities() & ExactResolution )
if ( capabilities() & Size )
{
providerXRes = extent().width() / xSize();
providerYRes = extent().height() / ySize();
Expand Down
8 changes: 0 additions & 8 deletions src/core/raster/qgsrasterdataprovider.h
Expand Up @@ -230,14 +230,6 @@ class CORE_EXPORT QgsRasterDataProvider : public QgsDataProvider, public QgsRast
return colorName( colorInterpretation( theBandNo ) );
}

/** Get block size */
//virtual int xBlockSize() const { return 0; }
//virtual int yBlockSize() const { return 0; }

/** Get raster size */
//virtual int xSize() const { return 0; }
//virtual int ySize() const { return 0; }

// TODO: remove or make protected all readBlock working with void*

/** read block of data */
Expand Down
119 changes: 11 additions & 108 deletions src/core/raster/qgsrasterinterface.cpp
Expand Up @@ -24,29 +24,21 @@
#include <qmath.h>

#include "qgslogger.h"
#include "qgsrasterinterface.h"
#include "qgsrasterbandstats.h"
#include "qgsrasterhistogram.h"
#include "qgsrasterinterface.h"
#include "qgsrectangle.h"

//#include "cpl_conv.h"

QgsRasterInterface::QgsRasterInterface( QgsRasterInterface * input )
: mInput( input )
, mOn( true )
//, mStatsOn( false )
{
}

QgsRasterInterface::~QgsRasterInterface()
{
}

//bool QgsRasterInterface::isNoDataValue( int bandNo, double value ) const
//{
// return QgsRasterBlock::isNoDataValue( value, noDataValue( bandNo ) );
//}

void QgsRasterInterface::initStatistics( QgsRasterBandStats &theStatistics,
int theBandNo,
int theStats,
Expand Down Expand Up @@ -168,8 +160,6 @@ QgsRasterBandStats QgsRasterInterface::bandStatistics( int theBandNo,
int myNXBlocks = ( myWidth + myXBlockSize - 1 ) / myXBlockSize;
int myNYBlocks = ( myHeight + myYBlockSize - 1 ) / myYBlockSize;

// void *myData = qgsMalloc( myXBlockSize * myYBlockSize * ( QgsRasterBlock::typeSize( dataType( theBandNo ) ) ) );

double myXRes = myExtent.width() / myWidth;
double myYRes = myExtent.height() / myHeight;
// TODO: progress signals
Expand All @@ -194,20 +184,14 @@ QgsRasterBandStats QgsRasterInterface::bandStatistics( int theBandNo,

QgsRectangle myPartExtent( xmin, ymin, xmax, ymax );

//readBlock( theBandNo, myPartExtent, myBlockWidth, myBlockHeight, myData );
QgsRasterBlock* blk = block( theBandNo, myPartExtent, myBlockWidth, myBlockHeight );

// Collect the histogram counts.
for ( size_t i = 0; i < (( size_t ) myBlockHeight ) * myBlockWidth; i++ )
{
if ( blk->isNoData( i ) )
{
continue; // NULL
}
if ( blk->isNoData( i ) ) continue; // NULL

//double myValue = readValue( myData, myDataType, myX + ( myY * myBlockWidth ) );
double myValue = blk->value( i );
//QgsDebugMsg ( QString ( "%1 %2 value %3" ).arg (myX).arg(myY).arg( myValue ) );

myRasterBandStats.sum += myValue;
myRasterBandStats.elementCount++;
Expand All @@ -230,9 +214,6 @@ QgsRasterBandStats QgsRasterInterface::bandStatistics( int theBandNo,
}
}

//myRasterBandStats.sumOfSquares += static_cast < double >
// ( qPow( myValue - myRasterBandStats.mean, 2 ) );

// Single pass stdev
double myDelta = myValue - myMean;
myMean += myDelta / myRasterBandStats.elementCount;
Expand All @@ -258,8 +239,6 @@ QgsRasterBandStats QgsRasterInterface::bandStatistics( int theBandNo,
QgsDebugMsg( QString( "MEAN %1" ).arg( myRasterBandStats.mean ) );
QgsDebugMsg( QString( "STDDEV %1" ).arg( myRasterBandStats.stdDev ) );

//qgsFree( myData );

myRasterBandStats.statsGathered = QgsRasterBandStats::All;
mStatistics.append( myRasterBandStats );

Expand Down Expand Up @@ -291,7 +270,7 @@ void QgsRasterInterface::initHistogram( QgsRasterHistogram &theHistogram,
}
else
{
// We need statistcs -> avoid histogramDefaults in hasHistogram if possible
// We need statistics -> avoid histogramDefaults in hasHistogram if possible
// TODO: use approximated statistics if aproximated histogram is requested
// (theSampleSize > 0)
QgsRasterBandStats stats = bandStatistics( theBandNo, QgsRasterBandStats::Min, theExtent, theSampleSize );
Expand Down Expand Up @@ -431,8 +410,6 @@ QgsRasterHistogram QgsRasterInterface::histogram( int theBandNo,
QgsRectangle myExtent = myHistogram.extent;
myHistogram.histogramVector.resize( myBinCount );

//int myDataType = dataType( theBandNo );

int myXBlockSize = xBlockSize();
int myYBlockSize = yBlockSize();
if ( myXBlockSize == 0 ) // should not happen, but happens
Expand All @@ -447,8 +424,6 @@ QgsRasterHistogram QgsRasterInterface::histogram( int theBandNo,
int myNXBlocks = ( myWidth + myXBlockSize - 1 ) / myXBlockSize;
int myNYBlocks = ( myHeight + myYBlockSize - 1 ) / myYBlockSize;

//void *myData = qgsMalloc( myXBlockSize * myYBlockSize * ( QgsRasterBlock::typeSize( dataType( theBandNo ) ) ) );

double myXRes = myExtent.width() / myWidth;
double myYRes = myExtent.height() / myHeight;

Expand Down Expand Up @@ -480,7 +455,6 @@ QgsRasterHistogram QgsRasterInterface::histogram( int theBandNo,

QgsRectangle myPartExtent( xmin, ymin, xmax, ymax );

//readBlock( theBandNo, myPartExtent, myBlockWidth, myBlockHeight, myData );
QgsRasterBlock* blk = block( theBandNo, myPartExtent, myBlockWidth, myBlockHeight );

// Collect the histogram counts.
Expand All @@ -492,10 +466,7 @@ QgsRasterHistogram QgsRasterInterface::histogram( int theBandNo,
}
double myValue = blk->value( i );

//QgsDebugMsg ( QString ( "%1 %2 value %3" ).arg (myX).arg(myY).arg( myValue ) );

int myBinIndex = static_cast <int>( qFloor(( myValue - myMinimum ) / myBinSize ) ) ;
//QgsDebugMsg( QString( "myValue = %1 myBinIndex = %2" ).arg( myValue ).arg( myBinIndex ) );

if (( myBinIndex < 0 || myBinIndex > ( myBinCount - 1 ) ) && !theIncludeOutOfRange )
{
Expand All @@ -510,8 +481,6 @@ QgsRasterHistogram QgsRasterInterface::histogram( int theBandNo,
}
}

//qgsFree( myData );

myHistogram.valid = true;
mHistograms.append( myHistogram );

Expand Down Expand Up @@ -572,16 +541,15 @@ QString QgsRasterInterface::capabilitiesString() const

int abilities = capabilities();

// Not all all capabilities are here (Size, IdentifyValue, IdentifyText,
// IdentifyHtml, IdentifyFeature) because those are quite technical and probably
// would be confusing for users

if ( abilities & QgsRasterInterface::Identify )
{
abilitiesList += tr( "Identify" );
}

if ( abilities & QgsRasterInterface::BuildPyramids )
{
abilitiesList += tr( "Build Pyramids" );
}

if ( abilities & QgsRasterInterface::Create )
{
abilitiesList += tr( "Create Datasources" );
Expand All @@ -592,77 +560,12 @@ QString QgsRasterInterface::capabilitiesString() const
abilitiesList += tr( "Remove Datasources" );
}

QgsDebugMsg( "Capability: " + abilitiesList.join( ", " ) );

return abilitiesList.join( ", " );
}

#if 0
// version with time counting
void * QgsRasterInterface::block( int bandNo, QgsRectangle const & extent, int width, int height )
{
QTime time;
time.start();

void * b = 0;

if ( !mOn )
{
// Switched off, pass input data unchanged
if ( mInput )
{
b = mInput->block( bandNo, extent, width, height );
}
}
else
{
b = readBlock( bandNo, extent, width, height );
}

if ( mStatsOn )
{
if ( mTime.size() <= bandNo )
{
mTime.resize( bandNo + 1 );
}
// QTime counts only in miliseconds
// We are adding time until next clear, this way the time may be collected
// for the whole QgsRasterLayer::draw() for example, not just for a single part
mTime[bandNo] += time.elapsed();
QgsDebugMsg( QString( "bandNo = %2 time = %3" ).arg( bandNo ).arg( mTime[bandNo] ) );
}
return b;
}
#endif

#if 0
void QgsRasterInterface::setStatsOn( bool on )
{
if ( on )
if ( abilities & QgsRasterInterface::BuildPyramids )
{
mTime.clear();
abilitiesList += tr( "Build Pyramids" );
}
if ( mInput ) mInput->setStatsOn( on );
mStatsOn = on;
}

double QgsRasterInterface::time( bool cumulative )
{
// We can calculate total time only, because we have to subtract time of previous
// interface(s) and we don't know how to assign bands to each other
double t = 0;
for ( int i = 1; i < mTime.size(); i++ )
{
t += mTime[i];
}
if ( cumulative ) return t;
QgsDebugMsg( "Capability: " + abilitiesList.join( ", " ) );

if ( mInput )
{
QgsDebugMsgLevel( QString( "%1 cumulative time = %2 time = %3" ).arg( typeid( *( this ) ).name() ).arg( t ).arg( t - mInput->time( true ) ), 3 );
t -= mInput->time( true );
}
return t;
return abilitiesList.join( ", " );
}
#endif

0 comments on commit c000ded

Please sign in to comment.