19 #define CPL_SUPRESS_CPLUSPLUS //#spellok 21 #include "cpl_string.h" 28 QString driverShortName = GDALGetDriverShortName( driver );
29 if ( driverShortName == QLatin1String(
"SQLite" ) )
34 char **driverMetadata = GDALGetMetadata( driver,
nullptr );
35 return CSLFetchBoolean( driverMetadata, GDAL_DCAP_CREATE,
false ) &&
36 CSLFetchBoolean( driverMetadata, GDAL_DCAP_RASTER,
false );
46 GDALDriverH hDriverMem = GDALGetDriverByName(
"MEM" );
54 double cellSizeX = extent.
width() / width;
55 double cellSizeY = extent.
height() / height;
56 double geoTransform[6];
58 geoTransform[1] = cellSizeX;
60 geoTransform[3] = extent.
yMinimum() + ( cellSizeY * height );
62 geoTransform[5] = -cellSizeY;
65 GDALSetGeoTransform( hSrcDS.get(), geoTransform );
71 double cellSizeX = extent.
width() / width;
72 double cellSizeY = extent.
height() / height;
73 double geoTransform[6];
75 geoTransform[1] = cellSizeX;
77 geoTransform[3] = extent.
yMinimum() + ( cellSizeY * height );
79 geoTransform[5] = -cellSizeY;
81 GDALDriverH hDriver = GDALGetDriverByName(
"GTiff" );
88 gdal::dataset_unique_ptr hDstDS( GDALCreate( hDriver, filename.toLocal8Bit().constData(), width, height, 1, dataType, nullptr ) );
96 GDALSetGeoTransform( hDstDS.get(), geoTransform );
102 if ( image.isNull() )
105 const QRgb *rgb =
reinterpret_cast<const QRgb *
>( image.constBits() );
106 GDALDriverH hDriverMem = GDALGetDriverByName(
"MEM" );
111 gdal::dataset_unique_ptr hSrcDS( GDALCreate( hDriverMem,
"", image.width(), image.height(), 0, GDT_Byte, nullptr ) );
114 << QStringLiteral(
"PIXELOFFSET=%1" ).arg(
sizeof( QRgb ) )
115 << QStringLiteral(
"LINEOFFSET=%1" ).arg( image.bytesPerLine() )
116 << QStringLiteral(
"DATAPOINTER=%1" ).arg( reinterpret_cast< qulonglong >( rgb ) + 2 ) );
117 GDALAddBand( hSrcDS.get(), GDT_Byte, papszOptions );
118 CSLDestroy( papszOptions );
121 << QStringLiteral(
"PIXELOFFSET=%1" ).arg(
sizeof( QRgb ) )
122 << QStringLiteral(
"LINEOFFSET=%1" ).arg( image.bytesPerLine() )
123 << QStringLiteral(
"DATAPOINTER=%1" ).arg( reinterpret_cast< qulonglong >( rgb ) + 1 ) );
124 GDALAddBand( hSrcDS.get(), GDT_Byte, papszOptions );
125 CSLDestroy( papszOptions );
128 << QStringLiteral(
"PIXELOFFSET=%1" ).arg(
sizeof( QRgb ) )
129 << QStringLiteral(
"LINEOFFSET=%1" ).arg( image.bytesPerLine() )
130 << QStringLiteral(
"DATAPOINTER=%1" ).arg( reinterpret_cast< qulonglong >( rgb ) ) );
131 GDALAddBand( hSrcDS.get(), GDT_Byte, papszOptions );
132 CSLDestroy( papszOptions );
135 << QStringLiteral(
"PIXELOFFSET=%1" ).arg(
sizeof( QRgb ) )
136 << QStringLiteral(
"LINEOFFSET=%1" ).arg( image.bytesPerLine() )
137 << QStringLiteral(
"DATAPOINTER=%1" ).arg( reinterpret_cast< qulonglong >( rgb ) + 3 ) );
138 GDALAddBand( hSrcDS.get(), GDT_Byte, papszOptions );
139 CSLDestroy( papszOptions );
147 psWarpOptions->hSrcDS = hSrcDS;
148 psWarpOptions->hDstDS = hDstDS;
150 psWarpOptions->nBandCount = 1;
151 psWarpOptions->panSrcBands =
reinterpret_cast< int *
>( CPLMalloc(
sizeof(
int ) * 1 ) );
152 psWarpOptions->panDstBands =
reinterpret_cast< int *
>( CPLMalloc(
sizeof(
int ) * 1 ) );
153 psWarpOptions->panSrcBands[0] = 1;
154 psWarpOptions->panDstBands[0] = 1;
156 psWarpOptions->eResampleAlg = resampleAlg;
159 psWarpOptions->pTransformerArg =
160 GDALCreateGenImgProjTransformer( hSrcDS, GDALGetProjectionRef( hSrcDS ),
161 hDstDS, GDALGetProjectionRef( hDstDS ),
163 psWarpOptions->pfnTransformer = GDALGenImgProjTransform;
166 GDALWarpOperation oOperation;
167 oOperation.Initialize( psWarpOptions.get() );
169 oOperation.ChunkAndWarpImage( 0, 0, GDALGetRasterXSize( hDstDS ), GDALGetRasterYSize( hDstDS ) );
171 GDALDestroyGenImgProjTransformer( psWarpOptions->pTransformerArg );
180 GDALRasterIOExtraArg extra;
181 INIT_RASTERIO_EXTRA_ARG( extra );
182 extra.eResampleAlg = resampleAlg;
184 QImage res( outputSize, image.format() );
188 GByte *rgb =
reinterpret_cast<GByte *
>( res.bits() );
190 CPLErr err = GDALRasterIOEx( GDALGetRasterBand( srcDS.get(), 1 ), GF_Read, 0, 0, image.width(), image.height(), rgb + 2, outputSize.width(),
191 outputSize.height(), GDT_Byte,
sizeof( QRgb ), res.bytesPerLine(), &extra );
192 if ( err != CE_None )
194 QgsDebugMsg( QStringLiteral(
"failed to read red band" ) );
198 err = GDALRasterIOEx( GDALGetRasterBand( srcDS.get(), 2 ), GF_Read, 0, 0, image.width(), image.height(), rgb + 1, outputSize.width(),
199 outputSize.height(), GDT_Byte,
sizeof( QRgb ), res.bytesPerLine(), &extra );
200 if ( err != CE_None )
202 QgsDebugMsg( QStringLiteral(
"failed to read green band" ) );
206 err = GDALRasterIOEx( GDALGetRasterBand( srcDS.get(), 3 ), GF_Read, 0, 0, image.width(), image.height(), rgb, outputSize.width(),
207 outputSize.height(), GDT_Byte,
sizeof( QRgb ), res.bytesPerLine(), &extra );
208 if ( err != CE_None )
210 QgsDebugMsg( QStringLiteral(
"failed to read blue band" ) );
214 err = GDALRasterIOEx( GDALGetRasterBand( srcDS.get(), 4 ), GF_Read, 0, 0, image.width(), image.height(), rgb + 3, outputSize.width(),
215 outputSize.height(), GDT_Byte,
sizeof( QRgb ), res.bytesPerLine(), &extra );
216 if ( err != CE_None )
218 QgsDebugMsg( QStringLiteral(
"failed to read alpha band" ) );
228 GDALDriverH myGdalDriver = GDALGetDriverByName( format.toLocal8Bit().constData() );
232 char **GDALmetadata = GDALGetMetadata( myGdalDriver,
nullptr );
233 message += QLatin1String(
"Format Details:\n" );
234 message += QStringLiteral(
" Extension: %1\n" ).arg( CSLFetchNameValue( GDALmetadata, GDAL_DMD_EXTENSION ) );
235 message += QStringLiteral(
" Short Name: %1" ).arg( GDALGetDriverShortName( myGdalDriver ) );
236 message += QStringLiteral(
" / Long Name: %1\n" ).arg( GDALGetDriverLongName( myGdalDriver ) );
237 message += QStringLiteral(
" Help page: http://www.gdal.org/%1\n\n" ).arg( CSLFetchNameValue( GDALmetadata, GDAL_DMD_HELPTOPIC ) );
241 CPLXMLNode *psCOL = CPLParseXMLString( GDALGetMetadataItem( myGdalDriver,
242 GDAL_DMD_CREATIONOPTIONLIST,
"" ) );
243 char *pszFormattedXML = CPLSerializeXMLTree( psCOL );
244 if ( pszFormattedXML )
245 message += QString( pszFormattedXML );
247 CPLDestroyXMLNode( psCOL );
248 if ( pszFormattedXML )
249 CPLFree( pszFormattedXML );
256 char **papszRetList =
nullptr;
257 const auto constList = list;
258 for (
const QString &elem : constList )
260 papszRetList = CSLAddString( papszRetList, elem.toLocal8Bit().constData() );
267 GDALDriverH myGdalDriver = GDALGetDriverByName( format.toLocal8Bit().constData() );
268 if ( ! myGdalDriver )
269 return QStringLiteral(
"invalid GDAL driver" );
273 int ok = GDALValidateCreationOptions( myGdalDriver, papszOptions );
274 CSLDestroy( papszOptions );
277 return QStringLiteral(
"Failed GDALValidateCreationOptions() test" );
A rectangle specified with double values.
static gdal::dataset_unique_ptr createMultiBandMemoryDataset(GDALDataType dataType, int bands, const QgsRectangle &extent, int width, int height, const QgsCoordinateReferenceSystem &crs)
Creates a new multi band memory dataset with given parameters.
static gdal::dataset_unique_ptr imageToMemoryDataset(const QImage &image)
Converts an image to a GDAL memory dataset by borrowing image data.
const QgsCoordinateReferenceSystem & crs
std::unique_ptr< GDALWarpOptions, GDALWarpOptionsDeleter > warp_options_unique_ptr
Scoped GDAL warp options.
static bool supportsRasterCreate(GDALDriverH driver)
Reads whether a driver supports GDALCreate() for raster purposes.
static gdal::dataset_unique_ptr createSingleBandTiffDataset(const QString &filename, GDALDataType dataType, const QgsRectangle &extent, int width, int height, const QgsCoordinateReferenceSystem &crs)
Creates a new single band TIFF dataset with given parameters.
double width() const
Returns the width of the rectangle.
static char ** papszFromStringList(const QStringList &list)
Helper function.
static gdal::dataset_unique_ptr createSingleBandMemoryDataset(GDALDataType dataType, const QgsRectangle &extent, int width, int height, const QgsCoordinateReferenceSystem &crs)
Creates a new single band memory dataset with given parameters.
static QString helpCreationOptionsFormat(const QString &format)
Gets creation options metadata for a given format.
Full WKT2 string, conforming to ISO 19162:2018 / OGC 18-010, with all possible nodes and new keyword ...
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
This class represents a coordinate reference system (CRS).
static QString validateCreationOptionsFormat(const QStringList &createOptions, const QString &format)
Validates creation options for a given format, regardless of layer.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
std::unique_ptr< std::remove_pointer< GDALDatasetH >::type, GDALDatasetCloser > dataset_unique_ptr
Scoped GDAL dataset.
QString toWkt(WktVariant variant=WKT1_GDAL, bool multiline=false, int indentationWidth=4) const
Returns a WKT representation of this CRS.
static void resampleSingleBandRaster(GDALDatasetH hSrcDS, GDALDatasetH hDstDS, GDALResampleAlg resampleAlg)
Resamples a single band raster to the destination dataset with different resolution (and possibly wit...
double height() const
Returns the height of the rectangle.
static QImage resampleImage(const QImage &image, QSize outputSize, GDALRIOResampleAlg resampleAlg)
Resamples a QImage image using GDAL resampler.