Skip to content
Permalink
Browse files

[rastercalc] Remove some redundant private methods

  • Loading branch information
nyalldawson committed Jun 10, 2015
1 parent f28755f commit efde7ad475afd55bbc40a16013ce58b36b202f56
Showing with 0 additions and 131 deletions.
  1. +0 −110 src/analysis/raster/qgsrastercalculator.cpp
  2. +0 −21 src/analysis/raster/qgsrastercalculator.h
@@ -25,7 +25,6 @@

#include <cpl_string.h>
#include <gdalwarper.h>
#include <ogr_srs_api.h>

#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
#define TO8(x) (x).toUtf8().constData()
@@ -231,115 +230,6 @@ GDALDatasetH QgsRasterCalculator::openOutputFile( GDALDriverH outputDriver )
return outputDataset;
}

void QgsRasterCalculator::readRasterPart( double* targetGeotransform, int xOffset, int yOffset, int nCols, int nRows, double* sourceTransform, GDALRasterBandH sourceBand, float* rasterBuffer )
{
//If dataset transform is the same as the requested transform, do a normal GDAL raster io
if ( transformationsEqual( targetGeotransform, sourceTransform ) )
{
GDALRasterIO( sourceBand, GF_Read, xOffset, yOffset, nCols, nRows, rasterBuffer, nCols, nRows, GDT_Float32, 0, 0 );
return;
}

int sourceBandXSize = GDALGetRasterBandXSize( sourceBand );
int sourceBandYSize = GDALGetRasterBandYSize( sourceBand );

//pixel calculation needed because of different raster position / resolution
int nodataSuccess;
double nodataValue = GDALGetRasterNoDataValue( sourceBand, &nodataSuccess );
QgsRectangle targetRect( targetGeotransform[0] + targetGeotransform[1] * xOffset, targetGeotransform[3] + yOffset * targetGeotransform[5] + nRows * targetGeotransform[5]
, targetGeotransform[0] + targetGeotransform[1] * xOffset + targetGeotransform[1] * nCols, targetGeotransform[3] + yOffset * targetGeotransform[5] );
QgsRectangle sourceRect( sourceTransform[0], sourceTransform[3] + GDALGetRasterBandYSize( sourceBand ) * sourceTransform[5],
sourceTransform[0] + GDALGetRasterBandXSize( sourceBand )* sourceTransform[1], sourceTransform[3] );
QgsRectangle intersection = targetRect.intersect( &sourceRect );

//no intersection, fill all the pixels with nodata values
if ( intersection.isEmpty() )
{
int nPixels = nCols * nRows;
for ( int i = 0; i < nPixels; ++i )
{
rasterBuffer[i] = nodataValue;
}
return;
}

//do raster io in source resolution
int sourcePixelOffsetXMin = floor(( intersection.xMinimum() - sourceTransform[0] ) / sourceTransform[1] );
int sourcePixelOffsetXMax = ceil(( intersection.xMaximum() - sourceTransform[0] ) / sourceTransform[1] );
if ( sourcePixelOffsetXMax > sourceBandXSize )
{
sourcePixelOffsetXMax = sourceBandXSize;
}
int nSourcePixelsX = sourcePixelOffsetXMax - sourcePixelOffsetXMin;

int sourcePixelOffsetYMax = floor(( intersection.yMaximum() - sourceTransform[3] ) / sourceTransform[5] );
int sourcePixelOffsetYMin = ceil(( intersection.yMinimum() - sourceTransform[3] ) / sourceTransform[5] );
if ( sourcePixelOffsetYMin > sourceBandYSize )
{
sourcePixelOffsetYMin = sourceBandYSize;
}
int nSourcePixelsY = sourcePixelOffsetYMin - sourcePixelOffsetYMax;
float* sourceRaster = ( float * ) CPLMalloc( sizeof( float ) * nSourcePixelsX * nSourcePixelsY );
double sourceRasterXMin = sourceRect.xMinimum() + sourcePixelOffsetXMin * sourceTransform[1];
double sourceRasterYMax = sourceRect.yMaximum() + sourcePixelOffsetYMax * sourceTransform[5];
if ( GDALRasterIO( sourceBand, GF_Read, sourcePixelOffsetXMin, sourcePixelOffsetYMax, nSourcePixelsX, nSourcePixelsY,
sourceRaster, nSourcePixelsX, nSourcePixelsY, GDT_Float32, 0, 0 ) != CE_None )
{
//IO error, fill array with nodata values
CPLFree( sourceRaster );
int npixels = nRows * nCols;
for ( int i = 0; i < npixels; ++i )
{
rasterBuffer[i] = nodataValue;
}
return;
}


double targetPixelX;
double targetPixelXMin = targetGeotransform[0] + targetGeotransform[1] * xOffset + targetGeotransform[1] / 2.0;
double targetPixelY = targetGeotransform[3] + targetGeotransform[5] * yOffset + targetGeotransform[5] / 2.0; //coordinates of current target pixel
int sourceIndexX, sourceIndexY; //current raster index in source pixels
double sx, sy;
for ( int i = 0; i < nRows; ++i )
{
targetPixelX = targetPixelXMin;
for ( int j = 0; j < nCols; ++j )
{
sx = ( targetPixelX - sourceRasterXMin ) / sourceTransform[1];
sourceIndexX = sx > 0 ? sx : floor( sx );
sy = ( targetPixelY - sourceRasterYMax ) / sourceTransform[5];
sourceIndexY = sy > 0 ? sy : floor( sy );
if ( sourceIndexX >= 0 && sourceIndexX < nSourcePixelsX
&& sourceIndexY >= 0 && sourceIndexY < nSourcePixelsY )
{
rasterBuffer[j + i*nRows] = sourceRaster[ sourceIndexX + nSourcePixelsX * sourceIndexY ];
}
else
{
rasterBuffer[j + i*j] = nodataValue;
}
targetPixelX += targetGeotransform[1];
}
targetPixelY += targetGeotransform[5];
}

CPLFree( sourceRaster );
return;
}

bool QgsRasterCalculator::transformationsEqual( double* t1, double* t2 ) const
{
for ( int i = 0; i < 6; ++i )
{
if ( !qgsDoubleNear( t1[i], t2[i], 0.00001 ) )
{
return false;
}
}
return true;
}

void QgsRasterCalculator::outputGeoTransform( double* transform ) const
{
transform[0] = mOutputRectangle.xMinimum();
@@ -87,27 +87,6 @@ class ANALYSIS_EXPORT QgsRasterCalculator
@return the output dataset or NULL in case of error*/
GDALDatasetH openOutputFile( GDALDriverH outputDriver );

/**Reads raster pixels from a dataset/band
@param targetGeotransform transformation parameters of the requested raster array
(not necessarily the same as the transform of the source dataset)
@param xOffset x offset
@param yOffset y offset
@param nCols number of columns
@param nRows number of rows
@param sourceTransform source transformation
@param sourceBand source band
@param rasterBuffer raster buffer
*/
void readRasterPart( double* targetGeotransform,
int xOffset, int yOffset,
int nCols, int nRows,
double* sourceTransform,
GDALRasterBandH sourceBand,
float* rasterBuffer );

/**Compares two geotransformations (six parameter double arrays*/
bool transformationsEqual( double* t1, double* t2 ) const;

/**Sets gdal 6 parameters array from mOutputRectangle, mNumOutputColumns, mNumOutputRows
@param transform double[6] array that receives the GDAL parameters*/
void outputGeoTransform( double* transform ) const;

0 comments on commit efde7ad

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