@@ -297,6 +297,9 @@ void QgsRasterCalculator::readRasterPart( double* targetGeotransform, int xOffse
297
297
return ;
298
298
}
299
299
300
+ int sourceBandXSize = GDALGetRasterBandXSize ( sourceBand );
301
+ int sourceBandYSize = GDALGetRasterBandYSize ( sourceBand );
302
+
300
303
// pixel calculation needed because of different raster position / resolution
301
304
int nodataSuccess;
302
305
double nodataValue = GDALGetRasterNoDataValue ( sourceBand, &nodataSuccess );
@@ -320,15 +323,34 @@ void QgsRasterCalculator::readRasterPart( double* targetGeotransform, int xOffse
320
323
// do raster io in source resolution
321
324
int sourcePixelOffsetXMin = floor (( intersection.xMinimum () - sourceTransform[0 ] ) / sourceTransform[1 ] );
322
325
int sourcePixelOffsetXMax = ceil (( intersection.xMaximum () - sourceTransform[0 ] ) / sourceTransform[1 ] );
326
+ if ( sourcePixelOffsetXMax > sourceBandXSize )
327
+ {
328
+ sourcePixelOffsetXMax = sourceBandXSize;
329
+ }
323
330
int nSourcePixelsX = sourcePixelOffsetXMax - sourcePixelOffsetXMin;
331
+
324
332
int sourcePixelOffsetYMax = floor (( intersection.yMaximum () - sourceTransform[3 ] ) / sourceTransform[5 ] );
325
333
int sourcePixelOffsetYMin = ceil (( intersection.yMinimum () - sourceTransform[3 ] ) / sourceTransform[5 ] );
334
+ if ( sourcePixelOffsetYMin > sourceBandYSize )
335
+ {
336
+ sourcePixelOffsetYMin = sourceBandYSize;
337
+ }
326
338
int nSourcePixelsY = sourcePixelOffsetYMin - sourcePixelOffsetYMax;
327
339
float * sourceRaster = ( float * ) CPLMalloc ( sizeof ( float ) * nSourcePixelsX * nSourcePixelsY );
328
340
double sourceRasterXMin = sourceRect.xMinimum () + sourcePixelOffsetXMin * sourceTransform[1 ];
329
341
double sourceRasterYMax = sourceRect.yMaximum () + sourcePixelOffsetYMax * sourceTransform[5 ];
330
- GDALRasterIO ( sourceBand, GF_Read, sourcePixelOffsetXMin, sourcePixelOffsetYMax, nSourcePixelsX, nSourcePixelsY,
331
- sourceRaster, nSourcePixelsX, nSourcePixelsY, GDT_Float32, 0 , 0 );
342
+ if ( GDALRasterIO ( sourceBand, GF_Read, sourcePixelOffsetXMin, sourcePixelOffsetYMax, nSourcePixelsX, nSourcePixelsY,
343
+ sourceRaster, nSourcePixelsX, nSourcePixelsY, GDT_Float32, 0 , 0 ) != CE_None )
344
+ {
345
+ // IO error, fill array with nodata values
346
+ CPLFree ( sourceRaster );
347
+ int npixels = nRows * nCols;
348
+ for ( int i = 0 ; i < npixels; ++i )
349
+ {
350
+ rasterBuffer[i] = nodataValue;
351
+ }
352
+ return ;
353
+ }
332
354
333
355
334
356
double targetPixelX;
0 commit comments