Skip to content

Commit

Permalink
adapt existing methods instead of creating overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy authored and nyalldawson committed Jan 30, 2025
1 parent 6dae454 commit 03226d9
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 165 deletions.
147 changes: 0 additions & 147 deletions src/app/georeferencer/qgsimagewarper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,68 +61,6 @@ bool QgsImageWarper::openSrcDSAndGetWarpOpt( const QString &input, ResamplingMet
return true;
}

bool QgsImageWarper::createDestinationDataset( const QString &outputName, GDALDatasetH hSrcDS, gdal::dataset_unique_ptr &hDstDS, uint resX, uint resY, double *adfGeoTransform, bool useZeroAsTrans, const QString &compression, const QgsCoordinateReferenceSystem &crs )
{
// create the output file
GDALDriverH driver = GDALGetDriverByName( "GTiff" );
if ( !driver )
{
return false;
}
char **papszOptions = nullptr;
papszOptions = CSLSetNameValue( papszOptions, "COMPRESS", compression.toLatin1() );
hDstDS.reset( GDALCreate( driver, outputName.toUtf8().constData(), resX, resY, GDALGetRasterCount( hSrcDS ), GDALGetRasterDataType( GDALGetRasterBand( hSrcDS, 1 ) ), papszOptions ) );
if ( !hDstDS )
{
return false;
}

if ( CE_None != GDALSetGeoTransform( hDstDS.get(), adfGeoTransform ) )
{
return false;
}

if ( crs.isValid() )
{
OGRSpatialReference oTargetSRS;
oTargetSRS.importFromWkt( crs.toWkt( Qgis::CrsWktVariant::PreferredGdal ).toUtf8().data() );

char *wkt = nullptr;
const OGRErr err = oTargetSRS.exportToWkt( &wkt );
if ( err != CE_None || GDALSetProjection( hDstDS.get(), wkt ) != CE_None )
{
CPLFree( wkt );
return false;
}
CPLFree( wkt );
}

for ( int i = 0; i < GDALGetRasterCount( hSrcDS ); ++i )
{
GDALRasterBandH hSrcBand = GDALGetRasterBand( hSrcDS, i + 1 );
GDALRasterBandH hDstBand = GDALGetRasterBand( hDstDS.get(), i + 1 );
GDALColorTableH cTable = GDALGetRasterColorTable( hSrcBand );
GDALSetRasterColorInterpretation( hDstBand, GDALGetRasterColorInterpretation( hSrcBand ) );
if ( cTable )
{
GDALSetRasterColorTable( hDstBand, cTable );
}

int success;
const double noData = GDALGetRasterNoDataValue( hSrcBand, &success );
if ( success )
{
GDALSetRasterNoDataValue( hDstBand, noData );
}
else if ( useZeroAsTrans )
{
GDALSetRasterNoDataValue( hDstBand, 0 );
}
}

return true;
}

bool QgsImageWarper::createDestinationDataset( const QString &outputName, GDALDatasetH hSrcDS, gdal::dataset_unique_ptr &hDstDS, uint resX, uint resY, double *adfGeoTransform, bool useZeroAsTrans, const QStringList &options, const QgsCoordinateReferenceSystem &crs )
{
// create the output file
Expand Down Expand Up @@ -189,91 +127,6 @@ bool QgsImageWarper::createDestinationDataset( const QString &outputName, GDALDa
return true;
}

QgsImageWarper::Result QgsImageWarper::warpFile( const QString &input, const QString &output, const QgsGeorefTransform &georefTransform, ResamplingMethod resampling, bool useZeroAsTrans, const QString &compression, const QgsCoordinateReferenceSystem &crs, QgsFeedback *feedback, double destResX, double destResY )
{
if ( !georefTransform.parametersInitialized() )
return QgsImageWarper::Result::InvalidParameters;

gdal::dataset_unique_ptr hSrcDS;
gdal::dataset_unique_ptr hDstDS;
gdal::warp_options_unique_ptr psWarpOptions;
if ( !openSrcDSAndGetWarpOpt( input, resampling, georefTransform.GDALTransformer(), hSrcDS, psWarpOptions ) )
{
return QgsImageWarper::Result::SourceError;
}

double adfGeoTransform[6];
int destPixels, destLines;
CPLErr eErr = GDALSuggestedWarpOutput( hSrcDS.get(), georefTransform.GDALTransformer(), georefTransform.GDALTransformerArgs(), adfGeoTransform, &destPixels, &destLines );
if ( eErr != CE_None )
{
return QgsImageWarper::Result::TransformError;
}

// If specified, override the suggested resolution with user values
if ( destResX != 0.0 || destResY != 0.0 )
{
// If only one scale has been specified, fill in the other from the GDAL suggestion
if ( destResX == 0.0 )
destResX = adfGeoTransform[1];
if ( destResY == 0.0 )
destResY = adfGeoTransform[5];

// Make sure user-specified coordinate system has canonical orientation
if ( destResX < 0.0 )
destResX = -destResX;
if ( destResY > 0.0 )
destResY = -destResY;

// Assert that the north-up convention is fulfilled by GDALSuggestedWarpOutput (should always be the case)
// Asserts are bad as they just crash out, changed to just return false. TS
if ( adfGeoTransform[0] <= 0.0 || adfGeoTransform[5] >= 0.0 )
{
QgsDebugError( QStringLiteral( "Image is not north up after GDALSuggestedWarpOutput, bailing out." ) );
return QgsImageWarper::Result::InvalidParameters;
}
// Find suggested output image extent (in georeferenced units)
const double minX = adfGeoTransform[0];
const double maxX = adfGeoTransform[0] + adfGeoTransform[1] * destPixels;
const double maxY = adfGeoTransform[3];
const double minY = adfGeoTransform[3] + adfGeoTransform[5] * destLines;

// Update line and pixel count to match extent at user-specified resolution
destPixels = ( int ) ( ( ( maxX - minX ) / destResX ) + 0.5 );
destLines = ( int ) ( ( ( minY - maxY ) / destResY ) + 0.5 );
adfGeoTransform[0] = minX;
adfGeoTransform[3] = maxY;
adfGeoTransform[1] = destResX;
adfGeoTransform[5] = destResY;
}

if ( !createDestinationDataset( output, hSrcDS.get(), hDstDS, destPixels, destLines, adfGeoTransform, useZeroAsTrans, compression, crs ) )
{
return QgsImageWarper::Result::DestinationCreationError;
}

// Set GDAL callbacks for the progress dialog
psWarpOptions->pProgressArg = reinterpret_cast<void *>( feedback );
psWarpOptions->pfnProgress = updateWarpProgress;

psWarpOptions->hSrcDS = hSrcDS.get();
psWarpOptions->hDstDS = hDstDS.get();

// Create a transformer which transforms from source to destination pixels (and vice versa)
psWarpOptions->pfnTransformer = GeoToPixelTransform;
psWarpOptions->pTransformerArg = addGeoToPixelTransform( georefTransform.GDALTransformer(), georefTransform.GDALTransformerArgs(), adfGeoTransform );

// Initialize and execute the warp operation.
GDALWarpOperation oOperation;
oOperation.Initialize( psWarpOptions.get() );

eErr = oOperation.ChunkAndWarpImage( 0, 0, destPixels, destLines );

destroyGeoToPixelTransform( psWarpOptions->pTransformerArg );
return feedback->isCanceled() ? QgsImageWarper::Result::Canceled : eErr == CE_None ? QgsImageWarper::Result::Success
: QgsImageWarper::Result::WarpFailure;
}

QgsImageWarper::Result QgsImageWarper::warpFile( const QString &input, const QString &output, const QgsGeorefTransform &georefTransform, ResamplingMethod resampling, bool useZeroAsTrans, const QStringList &options, const QgsCoordinateReferenceSystem &crs, QgsFeedback *feedback, double destResX, double destResY )
{
if ( !georefTransform.parametersInitialized() )
Expand Down
18 changes: 0 additions & 18 deletions src/app/georeferencer/qgsimagewarper.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,6 @@ class APP_EXPORT QgsImageWarper
};
Q_ENUM( Result )

/**
* Warp the file specified by \a input and write the resulting raster to the file \a output.
* \param input input file name
* \param output output file name
* \param georefTransform specifies the warp transformation which should be applied to \a input.
* \param resampling specifies image resampling algorithm to use.
* \param useZeroAsTrans specifies whether to mark transparent areas with a value of "zero".
* \param compression image compression method
* \param crs output file CRS
* \param feedback optional feedback object
* \param destResX The desired horizontal resolution of the output file, in target georeferenced units. A value of zero means automatic selection.
* \param destResY The desired vertical resolution of the output file, in target georeferenced units. A value of zero means automatic selection.
*/
Result warpFile( const QString &input, const QString &output, const QgsGeorefTransform &georefTransform, ResamplingMethod resampling, bool useZeroAsTrans, const QString &compression, const QgsCoordinateReferenceSystem &crs, QgsFeedback *feedback, double destResX = 0.0, double destResY = 0.0 );

/**
* Warp the file specified by \a input and write the resulting raster to the file \a output.
* \param input input file name
Expand All @@ -88,8 +73,6 @@ class APP_EXPORT QgsImageWarper
* \param feedback optional feedback object
* \param destResX The desired horizontal resolution of the output file, in target georeferenced units. A value of zero means automatic selection.
* \param destResY The desired vertical resolution of the output file, in target georeferenced units. A value of zero means automatic selection.
*
* \since QGIS 3.42
*/
Result warpFile( const QString &input, const QString &output, const QgsGeorefTransform &georefTransform, ResamplingMethod resampling, bool useZeroAsTrans, const QStringList &options, const QgsCoordinateReferenceSystem &crs, QgsFeedback *feedback, double destResX = 0.0, double destResY = 0.0 );

Expand Down Expand Up @@ -118,7 +101,6 @@ class APP_EXPORT QgsImageWarper

bool openSrcDSAndGetWarpOpt( const QString &input, ResamplingMethod resampling, const GDALTransformerFunc &pfnTransform, gdal::dataset_unique_ptr &hSrcDS, gdal::warp_options_unique_ptr &psWarpOptions ) const;

bool createDestinationDataset( const QString &outputName, GDALDatasetH hSrcDS, gdal::dataset_unique_ptr &hDstDS, uint resX, uint resY, double *adfGeoTransform, bool useZeroAsTrans, const QString &compression, const QgsCoordinateReferenceSystem &crs );
bool createDestinationDataset( const QString &outputName, GDALDatasetH hSrcDS, gdal::dataset_unique_ptr &hDstDS, uint resX, uint resY, double *adfGeoTransform, bool useZeroAsTrans, const QStringList &options, const QgsCoordinateReferenceSystem &crs );

//! \brief GDAL progress callback, used to display warping progress via a QProgressDialog
Expand Down

0 comments on commit 03226d9

Please sign in to comment.