18 #include <gdalwarper.h> 19 #include <ogr_srs_api.h> 33 if ( qAbs( value - qRound( value ) ) < 1e-6 )
34 return qRound( value );
36 return qCeil( value );
41 if ( qAbs( value - qRound( value ) ) < 1e-6 )
42 return qRound( value );
44 return qFloor( value );
57 geotransform[0] + geotransform[1] * xSize,
58 geotransform[3] + geotransform[5] * ySize );
64 static int CPL_STDCALL
_progress(
double dfComplete,
const char* pszMessage,
void* pProgressArg )
66 Q_UNUSED( pszMessage );
70 return handler->
progress( dfComplete );
78 GDALWarpKernel* kern = ( GDALWarpKernel* ) pKern;
79 double cellsize = ((
double* )pArg )[0];
81 for (
int nBand = 0; nBand < kern->nBands; ++nBand )
83 float* bandData = (
float * ) kern->papabySrcImage[nBand];
84 for (
int nLine = 0; nLine < kern->nSrcYSize; ++nLine )
86 for (
int nPixel = 0; nPixel < kern->nSrcXSize; ++nPixel )
88 bandData[nPixel] /= cellsize;
90 bandData += kern->nSrcXSize;
99 GDALWarpKernel* kern = ( GDALWarpKernel* ) pKern;
100 double cellsize = ((
double* )pArg )[1];
102 for (
int nBand = 0; nBand < kern->nBands; ++nBand )
104 float* bandData = (
float * ) kern->papabyDstImage[nBand];
105 for (
int nLine = 0; nLine < kern->nDstYSize; ++nLine )
107 for (
int nPixel = 0; nPixel < kern->nDstXSize; ++nPixel )
109 bandData[nPixel] *= cellsize;
111 bandData += kern->nDstXSize;
120 : mProgressHandler( nullptr )
129 for (
int i = 0; i < 6; ++i )
161 if ( customCRSWkt.
isEmpty() || customCRSWkt == rasterInfo.
crs() )
166 if ( !customCellSize.
isValid() )
177 if ( customGridOffset.
x() < 0 || customGridOffset.
y() < 0 )
179 if ( !customCellSize.
isValid() )
213 if ( !customCellSize.
isValid() )
224 if ( customGridOffset.
x() < 0 || customGridOffset.
y() < 0 )
258 double finalExtent[4] = { 0, 0, 0, 0 };
273 "Source WKT:\n%2\n\nDestination WKT:\n%3" )
282 if ( finalExtent[0] == 0 && finalExtent[1] == 0 && finalExtent[2] == 0 && finalExtent[3] == 0 )
293 if ( extent.
xMinimum() > finalExtent[0] ) finalExtent[0] = extent.
xMinimum();
294 if ( extent.
yMinimum() > finalExtent[1] ) finalExtent[1] = extent.
yMinimum();
295 if ( extent.
xMaximum() < finalExtent[2] ) finalExtent[2] = extent.
xMaximum();
296 if ( extent.
yMaximum() < finalExtent[3] ) finalExtent[3] = extent.
yMaximum();
311 if ( clipX0 > finalExtent[0] ) finalExtent[0] = clipX0;
312 if ( clipY0 > finalExtent[1] ) finalExtent[1] = clipY0;
313 if ( clipX1 < finalExtent[2] ) finalExtent[2] = clipX1;
314 if ( clipY1 < finalExtent[3] ) finalExtent[3] = clipY1;
326 if ( xSize <= 0 || ySize <= 0 )
379 qDebug(
"---ALIGN------------------" );
382 qDebug(
"transform" );
393 double bestCellArea = qInf();
409 if ( cellArea < bestCellArea )
411 bestCellArea = cellArea;
423 GDALDriverH hDriver = GDALGetDriverByName(
"GTiff" );
440 int bandCount = GDALGetRasterCount( hSrcDS );
441 GDALDataType eDT = GDALGetRasterDataType( GDALGetRasterBand( hSrcDS, 1 ) );
446 bandCount, eDT, nullptr );
459 GDALColorTableH hCT = GDALGetRasterColorTable( GDALGetRasterBand( hSrcDS, 1 ) );
461 GDALSetRasterColorTable( GDALGetRasterBand( hDstDS, 1 ), hCT );
466 GDALWarpOptions* psWarpOptions = GDALCreateWarpOptions();
467 psWarpOptions->hSrcDS = hSrcDS;
468 psWarpOptions->hDstDS = hDstDS;
470 psWarpOptions->nBandCount = GDALGetRasterCount( hSrcDS );
471 psWarpOptions->panSrcBands = (
int * ) CPLMalloc(
sizeof(
int ) * psWarpOptions->nBandCount );
472 psWarpOptions->panDstBands = (
int * ) CPLMalloc(
sizeof(
int ) * psWarpOptions->nBandCount );
473 for (
int i = 0; i < psWarpOptions->nBandCount; ++i )
475 psWarpOptions->panSrcBands[i] = i + 1;
476 psWarpOptions->panDstBands[i] = i + 1;
479 psWarpOptions->eResampleAlg =
static_cast< GDALResampleAlg
>( raster.
resampleMethod );
483 psWarpOptions->pProgressArg =
this;
486 psWarpOptions->pTransformerArg =
487 GDALCreateGenImgProjTransformer( hSrcDS, GDALGetProjectionRef( hSrcDS ),
488 hDstDS, GDALGetProjectionRef( hDstDS ),
490 psWarpOptions->pfnTransformer = GDALGenImgProjTransform;
492 double rescaleArg[2];
499 psWarpOptions->pPreWarpProcessorArg = rescaleArg;
500 psWarpOptions->pPostWarpProcessorArg = rescaleArg;
502 psWarpOptions->eWorkingDataType = GDT_Float32;
506 GDALWarpOperation oOperation;
507 oOperation.Initialize( psWarpOptions );
508 oOperation.ChunkAndWarpImage( 0, 0,
mXSize, mYSize );
510 GDALDestroyGenImgProjTransformer( psWarpOptions->pTransformerArg );
511 GDALDestroyWarpOptions( psWarpOptions );
525 if ( !hTransformArg )
529 double adfDstGeoTransform[6];
531 int nPixels = 0, nLines = 0;
533 eErr = GDALSuggestedWarpOutput2( info.
mDataset,
534 GDALGenImgProjTransform, hTransformArg,
535 adfDstGeoTransform, &nPixels, &nLines, extents, 0 );
536 GDALDestroyGenImgProjTransformer( hTransformArg );
538 if ( eErr != CE_None )
541 QSizeF cs( qAbs( adfDstGeoTransform[1] ), qAbs( adfDstGeoTransform[5] ) );
544 *rect =
QgsRectangle( extents[0], extents[1], extents[2], extents[3] );
606 qDebug(
"---RASTER INFO------------------" );
614 qDebug(
"transform" );
621 GDALRasterBandH hBand = GDALGetRasterBand(
mDataset, 1 );
627 float* pafScanline = (
float * ) CPLMalloc(
sizeof(
float ) );
628 CPLErr err = GDALRasterIO( hBand, GF_Read, px, py, 1, 1,
629 pafScanline, 1, 1, GDT_Float32, 0, 0 );
630 double value = err == CE_None ? pafScanline[0] : std::numeric_limits<double>::quiet_NaN();
631 CPLFree( pafScanline );
QString fromAscii(const char *str, int size)
A rectangle specified with double values.
double mCellSizeX
Destination cell size.
QgsRectangle extent() const
Return extent of the raster.
double mGridOffsetX
Destination grid offset - expected to be in interval <0,cellsize)
static bool suggestedWarpOutput(const RasterInfo &info, const QString &destWkt, QSizeF *cellSize=nullptr, QPointF *gridOffset=nullptr, QgsRectangle *rect=nullptr)
Determine suggested output of raster warp to a different CRS. Returns true on success.
bool rescaleValues
rescaling of values according to the change of pixel size
double identify(double mx, double my)
Get raster value at the given coordinates (from the first band)
List mRasters
List of rasters to be aligned (with their output files and other options)
QSizeF cellSize() const
Return cell size in map units.
bool run()
Run the alignment process.
static double ceil_with_tolerance(double value)
bool setParametersFromRaster(const RasterInfo &rasterInfo, const QString &customCRSWkt=QString(), QSizeF customCellSize=QSizeF(), QPointF customGridOffset=QPointF(-1, -1))
Set destination CRS, cell size and grid offset from a raster file.
static double fmod_with_tolerance(double num, double denom)
virtual bool progress(double complete)=0
Method to be overridden for progress reporting.
int mXSize
raster grid size
static CPLErr rescalePreWarpChunkProcessor(void *pKern, void *pArg)
QString tr(const char *sourceText, const char *disambiguation, int n)
QgsRectangle alignedRasterExtent() const
Return expected extent of the resulting aligned raster.
QPointF origin() const
Return origin of the raster.
void setClipExtent(double xmin, double ymin, double xmax, double ymax)
Configure clipping extent (region of interest).
QString outputFilename
filename of the newly created aligned raster (will be overwritten if exists already) ...
static CPLErr rescalePostWarpChunkProcessor(void *pKern, void *pArg)
QString toString(bool automaticPrecision=false) const
returns string representation of form xmin,ymin xmax,ymax
int mBandCnt
number of raster's bands
int count(const T &value) const
QString crs() const
Return CRS in WKT format.
QSize alignedRasterSize() const
Return expected size of the resulting aligned raster.
Definition of one raster layer for alignment.
bool checkInputParameters()
Determine destination extent from the input rasters and calculate derived values. ...
const char * constData() const
QgsAlignRaster takes one or more raster layers and warps (resamples) them so they have the same: ...
Helper struct to be sub-classed for progress reporting.
double srcCellSizeInDestCRS
used for rescaling of values (if necessary)
QPointF gridOffset() const
Return grid offset.
static double floor_with_tolerance(double value)
QString mCrsWkt
CRS stored in WKT format.
void dump() const
write contents of the object to standard error stream - for debugging
double mGeoTransform[6]
geotransform coefficients
QByteArray toLocal8Bit() const
int mXSize
Computed raster grid width/height.
void dump() const
write contents of the object to standard error stream - for debugging
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
double xMaximum() const
Get the x maximum value (right side of rectangle)
static QgsRectangle transform_to_extent(const double *geotransform, double xSize, double ySize)
double mClipExtent[4]
Optional clip extent: sets "requested area" which be extended to fit the raster grid.
RasterInfo(const QString &layerpath)
Construct raster info with a path to a raster file.
Class for storing a coordinate reference system (CRS)
QString toWkt() const
Returns a WKT representation of this CRS.
static int CPL_STDCALL _progress(double dfComplete, const char *pszMessage, void *pProgressArg)
ResampleAlg resampleMethod
resampling method to be used
double xMinimum() const
Get the x minimum value (left side of rectangle)
double yMaximum() const
Get the y maximum value (top side of rectangle)
int suggestedReferenceLayer() const
Return index of the layer which has smallest cell size (returns -1 on error)
QString mErrorMessage
Last error message from run()
QString inputFilename
filename of the source raster
QgsRectangle clipExtent() const
Get clipping extent (region of interest).
void normalize()
Normalize the rectangle so it has non-negative width/height.
double mGeoTransform[6]
Computed geo-transform.
GDALDatasetH mDataset
handle to open GDAL dataset
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
Utility class for gathering information about rasters.
QString mCrsWkt
Destination CRS - stored in well-known text (WKT) format.
QPointF gridOffset() const
QByteArray toAscii() const
bool createAndWarp(const Item &raster)
Internal function for processing of one raster (1. create output, 2. do the alignment) ...
QSizeF cellSize() const
Get output cell size.