Skip to content
Permalink
Browse files

Changes to raster iterator

  • Loading branch information
mhugent committed Jul 11, 2012
1 parent 15284eb commit 6002e6ffe24cf9a82f5b6b8e8f313e4eeaf2ab93
@@ -159,6 +159,7 @@
#include "qgsquerybuilder.h"
#include "qgsrastercalcdialog.h"
#include "qgsrasterfilewriter.h"
#include "qgsrasteriterator.h"
#include "qgsrasterlayer.h"
#include "qgsrasterlayerproperties.h"
#include "qgsrasterrenderer.h"
@@ -3784,7 +3785,8 @@ void QgisApp::saveAsRasterFile()

QProgressDialog pd( 0, tr( "Abort..." ), 0, 0 );
pd.setWindowModality( Qt::WindowModal );
fileWriter.writeRaster( rasterLayer->dataProvider(), d.nColumns(), d.outputRectangle(), &pd );
QgsRasterIterator iterator( rasterLayer->pipe()->last() );
fileWriter.writeRaster( &iterator, d.nColumns(), d.outputRectangle(), &pd );
}
}

@@ -41,7 +41,7 @@ void QgsRasterDrawer::draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsM

// last pipe filter has only 1 band
int bandNumber = 1;
mIterator->startRasterRead( bandNumber, viewPort, theQgsMapToPixel );
mIterator->startRasterRead( bandNumber, viewPort->drawableAreaXDim, viewPort->drawableAreaYDim, viewPort->mDrawnExtent );

//number of cols/rows in output pixels
int nCols = 0;
@@ -59,7 +59,7 @@ void QgsRasterDrawer::draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsM
void* rasterData;

// readNextRasterPart calcs and resets nCols, nRows, topLeftCol, topLeftRow
while ( mIterator->readNextRasterPart( bandNumber, viewPort, nCols, nRows,
while ( mIterator->readNextRasterPart( bandNumber, nCols, nRows,
&rasterData, topLeftCol, topLeftRow ) )
{
//create image
@@ -25,26 +25,33 @@ QgsRasterFileWriter::~QgsRasterFileWriter()

}

QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeRaster( QgsRasterDataProvider* sourceProvider, int nCols, QgsRectangle outputExtent, QProgressDialog* p )
QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeRaster( QgsRasterIterator* iter, int nCols, QgsRectangle outputExtent, QProgressDialog* p )
{
return NoError;
#if 0
if ( !sourceProvider || ! sourceProvider->isValid() )
if ( !iter )
{
return SourceProviderError;
}

const QgsRasterInterface* iface = iter->input();
if ( !iface )
{
return SourceProviderError;
}

#if 0
if ( outputExtent.isEmpty() )
{
outputExtent = sourceProvider->extent();
}
#endif //0

mProgressDialog = p;

QgsRasterInterface::DataType inputDataType = ( QgsRasterInterface::DataType )sourceProvider->dataType( 1 );
if ( inputDataType == QgsRasterInterface::::ARGBDataType && sourceProvider->bandCount() == 1 )
QgsRasterInterface::DataType debug = iface->dataType( 1 );

if ( iface->dataType( 1 ) == QgsRasterInterface::ARGB32 )
{
WriterError e = writeARGBRaster( sourceProvider, nCols, outputExtent );
WriterError e = writeARGBRaster( iter, nCols, outputExtent );
mProgressDialog = 0;
return e;
}
@@ -54,10 +61,9 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeRaster( QgsRasterData
mProgressDialog = 0;
return NoError;
}
#endif //0
}

QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeRasterSingleTile( QgsRasterDataProvider* sourceProvider, int nCols )
QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeRasterSingleTile( QgsRasterIterator* iter, int nCols )
{
#if 0
if ( !sourceProvider || ! sourceProvider->isValid() )
@@ -156,22 +162,20 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeRasterSingleTile( Qgs
return NoError;
}

QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeARGBRaster( QgsRasterDataProvider* sourceProvider, int nCols, const QgsRectangle& outputExtent )
QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeARGBRaster( QgsRasterIterator* iter, int nCols, const QgsRectangle& outputExtent )
{
#if 0
if ( !sourceProvider || ! sourceProvider->isValid() )
if ( !iter )
{
return SourceProviderError;
}

QgsRasterInterface::DataType sourceDataType = ( QgsRasterInterface::DataType )sourceProvider->dataType( 1 );
int nInputBands = sourceProvider->bandCount();
if ( sourceDataType != QgsRasterInterface::ARGBDataType || nInputBands != 1 )
const QgsRasterInterface* iface = iter->input();
if ( !iface || iface->dataType( 1 ) != QgsRasterInterface::ARGB32 )
{
return SourceProviderError;
}


//create directory for output files
QDir destDir( mOutputUrl );
if ( mTiledMode )
@@ -185,7 +189,8 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeARGBRaster( QgsRaster
QgsRasterIterator iter( 1, sourceProvider );
iter.setMaximumTileWidth( mMaxTileWidth );
iter.setMaximumTileHeight( mMaxTileHeight );
void* data = VSIMalloc( sourceProvider->dataTypeSize( 1 ) * mMaxTileWidth * mMaxTileHeight );

void* data = VSIMalloc( iface->typeSize( QgsRasterInterface::ARGB32 ) * mMaxTileWidth * mMaxTileHeight );
void* redData = VSIMalloc( mMaxTileWidth * mMaxTileHeight );
void* greenData = VSIMalloc( mMaxTileWidth * mMaxTileHeight );
void* blueData = VSIMalloc( mMaxTileWidth * mMaxTileHeight );
@@ -208,9 +213,13 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeARGBRaster( QgsRaster
geoTransform[4] = 0.0;
geoTransform[5] = -pixelSize;

//where to get CRS from?
QgsCoordinateReferenceSystem fakeCRS;
fakeCRS.createFromEpsg( 21781 );

if ( mTiledMode )
{
createVRT( nCols, nRows, sourceProvider->crs(), geoTransform );
createVRT( nCols, nRows, fakeCRS, geoTransform );
}
else
{
@@ -221,14 +230,21 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeARGBRaster( QgsRaster
}

if ( !destProvider->create( mOutputFormat, 4, QgsRasterInterface::Byte, nCols, nRows, geoTransform,
sourceProvider->crs() ) )
fakeCRS ) )
{
delete destProvider;
return CreateDatasourceError;
}
}

iter.select( outputExtent, outputMapUnitsPerPixel );
QgsRasterViewPort viewPort;
viewPort.drawableAreaXDim = nCols;
viewPort.drawableAreaYDim = nRows;

QgsMapUnitsPerPixel mup;

//iter->select( outputExtent, outputMapUnitsPerPixel );
iter->startRasterRead( 1, &viewPort, &mup );

//initialize progress dialog
int nTiles = iter.nTilesX() * iter.nTilesY();
@@ -239,7 +255,7 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeARGBRaster( QgsRaster
mProgressDialog->show();
}

while ( iter.nextPart( data, mapRect, iterLeft, iterTop, iterCols, iterRows, progress ) )
while ( iter->nextPart( data, mapRect, iterLeft, iterTop, iterCols, iterRows, progress ) )
{
if ( iterCols <= 5 || iterRows <= 5 ) //some wms servers don't like small values
{
@@ -299,7 +315,7 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeARGBRaster( QgsRaster
geoTransform[4] = 0.0;
geoTransform[5] = -outputMapUnitsPerPixel;
if ( !destProvider->create( mOutputFormat, 4, QgsRasterInterface::Byte, iterCols, iterRows, geoTransform,
sourceProvider->crs() ) )
fakeCRS ) )
{
delete destProvider;
return CreateDatasourceError;
@@ -8,7 +8,7 @@
#include <QString>

class QProgressDialog;
class QgsRasterDataProvider;
class QgsRasterIterator;

class CORE_EXPORT QgsRasterFileWriter
{
@@ -25,7 +25,7 @@ class CORE_EXPORT QgsRasterFileWriter
QgsRasterFileWriter( const QString& outputUrl );
~QgsRasterFileWriter();

WriterError writeRaster( QgsRasterDataProvider* sourceProvider, int nCols, QgsRectangle outputExtent = QgsRectangle(), QProgressDialog* p = 0 );
WriterError writeRaster( QgsRasterIterator* iter, int nCols, QgsRectangle outputExtent = QgsRectangle(), QProgressDialog* p = 0 );

void setOutputFormat( const QString& format ) { mOutputFormat = format; }
QString outputFormat() const { return mOutputFormat; }
@@ -46,8 +46,8 @@ class CORE_EXPORT QgsRasterFileWriter

private:
QgsRasterFileWriter(); //forbidden
WriterError writeRasterSingleTile( QgsRasterDataProvider* sourceProvider, int nCols );
WriterError writeARGBRaster( QgsRasterDataProvider* sourceProvider, int nCols, const QgsRectangle& outputExtent );
WriterError writeRasterSingleTile( QgsRasterIterator* iter, int nCols );
WriterError writeARGBRaster( QgsRasterIterator* iter, int nCols, const QgsRectangle& outputExtent );

//initialize vrt member variables
void createVRT( int xSize, int ySize, const QgsCoordinateReferenceSystem& crs, double* geoTransform );
@@ -11,20 +11,22 @@ QgsRasterIterator::~QgsRasterIterator()
{
}

void QgsRasterIterator::startRasterRead( int bandNumber, QgsRasterViewPort* viewPort, const QgsMapToPixel* mapToPixel )
void QgsRasterIterator::startRasterRead( int bandNumber, int nCols, int nRows, const QgsRectangle& extent )
{
if ( !viewPort || !mapToPixel || !mInput )
if ( !mInput )
{
return;
}

mExtent = extent;

//remove any previous part on that band
removePartInfo( bandNumber );

//split raster into small portions if necessary
RasterPartInfo pInfo;
pInfo.nCols = viewPort->drawableAreaXDim;
pInfo.nRows = viewPort->drawableAreaYDim;
pInfo.nCols = nCols;
pInfo.nRows = nRows;

//effective oversampling factors are different to global one because of rounding
//oversamplingX = (( double )pInfo.nCols * oversampling ) / viewPort->drawableAreaXDim;
@@ -44,16 +46,11 @@ void QgsRasterIterator::startRasterRead( int bandNumber, QgsRasterViewPort* view
mRasterPartInfos.insert( bandNumber, pInfo );
}

bool QgsRasterIterator::readNextRasterPart( int bandNumber, QgsRasterViewPort* viewPort,
bool QgsRasterIterator::readNextRasterPart( int bandNumber,
int& nCols, int& nRows,
void** rasterData,
int& topLeftCol, int& topLeftRow )
{
if ( !viewPort )
{
return false;
}

//get partinfo
QMap<int, RasterPartInfo>::iterator partIt = mRasterPartInfos.find( bandNumber );
if ( partIt == mRasterPartInfos.end() )
@@ -81,7 +78,7 @@ bool QgsRasterIterator::readNextRasterPart( int bandNumber, QgsRasterViewPort* v
nRows = qMin( pInfo.nRowsPerPart, pInfo.nRows - pInfo.currentRow );

//get subrectangle
QgsRectangle viewPortExtent = viewPort->mDrawnExtent;
QgsRectangle viewPortExtent = mExtent;
double xmin = viewPortExtent.xMinimum() + pInfo.currentCol / ( double )pInfo.nCols * viewPortExtent.width();
double xmax = viewPortExtent.xMinimum() + ( pInfo.currentCol + nCols ) / ( double )pInfo.nCols * viewPortExtent.width();
double ymin = viewPortExtent.yMaximum() - ( pInfo.currentRow + nRows ) / ( double )pInfo.nRows * viewPortExtent.height();
@@ -1,6 +1,7 @@
#ifndef QGSRASTERITERATOR_H
#define QGSRASTERITERATOR_H

#include "qgsrectangle.h"
#include <QMap>

class QgsMapToPixel;
@@ -31,23 +32,26 @@ class QgsRasterIterator
@param bandNumer number of raster band to read
@param viewPort describes raster position on screen
*/
void startRasterRead( int bandNumber, QgsRasterViewPort* viewPort, const QgsMapToPixel* mapToPixel );
void startRasterRead( int bandNumber, int nCols, int nRows, const QgsRectangle& extent );

/**Fetches next part of raster data
@param nCols number of columns on output device
@param nRows number of rows on output device
@param nColsRaster number of raster columns (different to nCols if oversamplingX != 1.0)
@param nRowsRaster number of raster rows (different to nRows if oversamplingY != 0)*/
bool readNextRasterPart( int bandNumber, QgsRasterViewPort* viewPort,
bool readNextRasterPart( int bandNumber,
int& nCols, int& nRows,
void** rasterData,
int& topLeftCol, int& topLeftRow );

void stopRasterRead( int bandNumber );

const QgsRasterInterface* input() const { return mInput; }

private:
QgsRasterInterface* mInput;
QMap<int, RasterPartInfo> mRasterPartInfos;
QgsRectangle mExtent;

/**Remove part into and release memory*/
void removePartInfo( int bandNumber );

0 comments on commit 6002e6f

Please sign in to comment.
You can’t perform that action at this time.