|
| 1 | +/*************************************************************************** |
| 2 | + qgsgeopackagerasterwriter.cpp - QgsGeoPackageRasterWriter |
| 3 | +
|
| 4 | + --------------------- |
| 5 | + begin : 23.8.2017 |
| 6 | + copyright : (C) 2017 by Alessandro Pasotti |
| 7 | + email : apasotti at boundlessgeo dot com |
| 8 | + *************************************************************************** |
| 9 | + * * |
| 10 | + * This program is free software; you can redistribute it and/or modify * |
| 11 | + * it under the terms of the GNU General Public License as published by * |
| 12 | + * the Free Software Foundation; either version 2 of the License, or * |
| 13 | + * (at your option) any later version. * |
| 14 | + * * |
| 15 | + ***************************************************************************/ |
| 16 | + |
| 17 | +///@cond PRIVATE |
| 18 | + |
| 19 | +#include "gdal.h" |
| 20 | +#include "gdal_utils.h" |
| 21 | + |
| 22 | +#include "qgsgeopackagerasterwriter.h" |
| 23 | +#include "qgscplerrorhandler.h" |
| 24 | + |
| 25 | +#include <QMessageBox> |
| 26 | + |
| 27 | +QgsGeoPackageRasterWriter::QgsGeoPackageRasterWriter( const QgsMimeDataUtils::Uri sourceUri, const QString outputUrl ): |
| 28 | + mSourceUri( sourceUri ), |
| 29 | + mOutputUrl( outputUrl ) |
| 30 | +{ |
| 31 | + |
| 32 | +} |
| 33 | + |
| 34 | +QgsGeoPackageRasterWriter::WriterError QgsGeoPackageRasterWriter::writeRaster( QgsFeedback *feedback, QString *errorMessage ) |
| 35 | +{ |
| 36 | + const char *args[] = { "-of", "gpkg", "-co", QStringLiteral( "RASTER_TABLE=%1" ).arg( mSourceUri.name ).toUtf8().constData(), "-co", "APPEND_SUBDATASET=YES", nullptr }; |
| 37 | + // This sends OGR/GDAL errors to the message log |
| 38 | + QgsCPLErrorHandler handler; |
| 39 | + GDALTranslateOptions *psOptions = GDALTranslateOptionsNew( ( char ** )args, nullptr ); |
| 40 | + |
| 41 | + GDALTranslateOptionsSetProgress( psOptions, [ ]( double dfComplete, const char *pszMessage, void *pProgressData ) -> int |
| 42 | + { |
| 43 | + Q_UNUSED( pszMessage ); |
| 44 | + QgsFeedback *feedback = static_cast< QgsFeedback * >( pProgressData ); |
| 45 | + feedback->setProgress( dfComplete * 100 ); |
| 46 | + return ! feedback->isCanceled(); |
| 47 | + }, feedback ); |
| 48 | + |
| 49 | + GDALDatasetH hSrcDS = GDALOpen( mSourceUri.uri.toUtf8().constData(), GA_ReadOnly ); |
| 50 | + if ( ! hSrcDS ) |
| 51 | + { |
| 52 | + *errorMessage = QObject::tr( "Failed to open source layer %1! See the OGR panel in the message logs for details.\n\n" ).arg( mSourceUri.name ); |
| 53 | + mHasError = true; |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + CPLErrorReset(); |
| 58 | + GDALDatasetH hOutDS = GDALTranslate( mOutputUrl.toUtf8().constData(), hSrcDS, psOptions, NULL ); |
| 59 | + if ( ! hOutDS ) |
| 60 | + { |
| 61 | + *errorMessage = QObject::tr( "Failed to import layer %1! See the OGR panel in the message logs for details.\n\n" ).arg( mSourceUri.name ); |
| 62 | + mHasError = true; |
| 63 | + } |
| 64 | + else // All good! |
| 65 | + { |
| 66 | + GDALClose( hOutDS ); |
| 67 | + } |
| 68 | + GDALClose( hSrcDS ); |
| 69 | + } |
| 70 | + GDALTranslateOptionsFree( psOptions ); |
| 71 | + return ( feedback && feedback->isCanceled() ) ? ErrUserCanceled : ( mHasError ? WriteError : NoError ) ; |
| 72 | +} |
| 73 | + |
| 74 | +///@endcond |
0 commit comments