Skip to content
Permalink
Browse files

Allow non-square pixels for raster formats with exact resolution

  • Loading branch information
mhugent committed Jul 19, 2012
1 parent 2eade8d commit 47b8d60332f8d4b5c3c4f993c566e95f2e5e948c
Showing with 30 additions and 20 deletions.
  1. +12 −2 src/app/qgisapp.cpp
  2. +12 −15 src/core/raster/qgsrasterfilewriter.cpp
  3. +6 −3 src/core/raster/qgsrasterfilewriter.h
@@ -3785,8 +3785,18 @@ void QgisApp::saveAsRasterFile()

QProgressDialog pd( 0, tr( "Abort..." ), 0, 0 );
pd.setWindowModality( Qt::WindowModal );
QgsRasterIterator iterator( rasterLayer->dataProvider() );
fileWriter.writeRaster( &iterator, d.nColumns(), d.outputRectangle(), rasterLayer->crs(), &pd );
QgsRasterDataProvider* provider = rasterLayer->dataProvider();
if ( !provider )
{
return;
}
QgsRasterIterator iterator( provider );
int nRows = -1; //calculate number of rows such that pixels are squares
if ( provider->capabilities() & QgsRasterDataProvider::ExactResolution )
{
nRows = d.nRows();
}
fileWriter.writeRaster( &iterator, d.nColumns(), nRows, d.outputRectangle(), rasterLayer->crs(), &pd );
}
}

@@ -24,7 +24,7 @@ QgsRasterFileWriter::~QgsRasterFileWriter()

}

QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeRaster( QgsRasterIterator* iter, int nCols, QgsRectangle outputExtent,
QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeRaster( QgsRasterIterator* iter, int nCols, int nRows, QgsRectangle outputExtent,
const QgsCoordinateReferenceSystem& crs, QProgressDialog* p )
{
if ( !iter )
@@ -42,19 +42,19 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeRaster( QgsRasterIter

if ( iface->dataType( 1 ) == QgsRasterInterface::ARGB32 )
{
WriterError e = writeImageRaster( iter, nCols, outputExtent, crs );
WriterError e = writeImageRaster( iter, nCols, nRows, outputExtent, crs );
mProgressDialog = 0;
return e;
}
else
{
mProgressDialog = 0;
WriterError e = writeDataRaster( iter, nCols, outputExtent, crs );
WriterError e = writeDataRaster( iter, nCols, nRows, outputExtent, crs );
return e;
}
}

QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeDataRaster( QgsRasterIterator* iter, int nCols, const QgsRectangle& outputExtent,
QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeDataRaster( QgsRasterIterator* iter, int nCols, int nRows, const QgsRectangle& outputExtent,
const QgsCoordinateReferenceSystem& crs )
{
if ( !iter )
@@ -76,7 +76,6 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeDataRaster( QgsRaster
}

//Get output map units per pixel
double outputMapUnitsPerPixel = outputExtent.width() / nCols;
int iterLeft, iterTop, iterCols, iterRows;

iter->setMaximumTileWidth( mMaxTileWidth );
@@ -90,7 +89,6 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeDataRaster( QgsRaster

//create destProvider for whole dataset here
QgsRasterDataProvider* destProvider = 0;
int nRows;
double pixelSize;
double geoTransform[6];
globalOutputParameters( outputExtent, nCols, nRows, geoTransform, pixelSize );
@@ -160,7 +158,7 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeDataRaster( QgsRaster
}
}

QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeImageRaster( QgsRasterIterator* iter, int nCols, const QgsRectangle& outputExtent,
QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeImageRaster( QgsRasterIterator* iter, int nCols, int nRows, const QgsRectangle& outputExtent,
const QgsCoordinateReferenceSystem& crs )
{
if ( !iter )
@@ -181,10 +179,6 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeImageRaster( QgsRaste
destDir.mkdir( mOutputUrl );
}

//Get output map units per pixel
double outputMapUnitsPerPixel = outputExtent.width() / nCols;


iter->setMaximumTileWidth( mMaxTileWidth );
iter->setMaximumTileHeight( mMaxTileHeight );

@@ -195,12 +189,10 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeImageRaster( QgsRaste
void* alphaData = VSIMalloc( mMaxTileWidth * mMaxTileHeight );
QgsRectangle mapRect;
int iterLeft, iterTop, iterCols, iterRows;
double progress = 0;
int fileIndex = 0;

//create destProvider for whole dataset here
QgsRasterDataProvider* destProvider = 0;
int nRows;
double pixelSize;
double geoTransform[6];
globalOutputParameters( outputExtent, nCols, nRows, geoTransform, pixelSize );
@@ -531,13 +523,18 @@ void QgsRasterFileWriter::globalOutputParameters( const QgsRectangle& extent, in
double* geoTransform, double& pixelSize )
{
pixelSize = extent.width() / nCols;
nRows = ( double )nCols / extent.width() * extent.height() + 0.5;

//calculate nRows automatically for providers without exact resolution
if ( nRows < 0 )
{
nRows = ( double )nCols / extent.width() * extent.height() + 0.5;
}
geoTransform[0] = extent.xMinimum();
geoTransform[1] = pixelSize;
geoTransform[2] = 0.0;
geoTransform[3] = extent.yMaximum();
geoTransform[4] = 0.0;
geoTransform[5] = -pixelSize;
geoTransform[5] = -( extent.height() / nRows );
}


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

WriterError writeRaster( QgsRasterIterator* iter, int nCols, QgsRectangle outputExtent,
/**Write raster file
@param nCols number of output columns
@param nRows number of output rows (or -1 to automatically calculate row number to have square pixels)*/
WriterError writeRaster( QgsRasterIterator* iter, int nCols, int nRows, QgsRectangle outputExtent,
const QgsCoordinateReferenceSystem& crs, QProgressDialog* p = 0 );

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

private:
QgsRasterFileWriter(); //forbidden
WriterError writeDataRaster( QgsRasterIterator* iter, int nCols, const QgsRectangle& outputExtent,
WriterError writeDataRaster( QgsRasterIterator* iter, int nCols, int nRows, const QgsRectangle& outputExtent,
const QgsCoordinateReferenceSystem& crs );
WriterError writeImageRaster( QgsRasterIterator* iter, int nCols, const QgsRectangle& outputExtent,
WriterError writeImageRaster( QgsRasterIterator* iter, int nCols, int nRows, const QgsRectangle& outputExtent,
const QgsCoordinateReferenceSystem& crs );

//initialize vrt member variables

0 comments on commit 47b8d60

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