23
23
#include " qgsvectorlayer.h"
24
24
#include " qgsrasterlayer.h"
25
25
#include " qgsogrprovider.h"
26
+ #include " qgscplerrorhandler.h"
26
27
#include " qgsnewgeopackagelayerdialog.h"
27
28
#include " qgsmessageoutput.h"
28
29
#include " qgsvectorlayerexporter.h"
29
30
#include " gdal.h"
31
+ #include " gdal_utils.h"
30
32
31
33
#include < QAction>
32
34
#include < QMessageBox>
@@ -322,62 +324,75 @@ bool QgsGeoPackageConnectionItem::handleDrop( const QMimeData *data, Qt::DropAct
322
324
return false ;
323
325
324
326
QString uri;
327
+ // This sends OGR/GDAL errors to the message log
328
+ QgsCPLErrorHandler handler;
325
329
326
330
QStringList importResults;
327
331
bool hasError = false ;
328
332
329
333
QgsMimeDataUtils::UriList lst = QgsMimeDataUtils::decodeUriList ( data );
330
334
Q_FOREACH ( const QgsMimeDataUtils::Uri &u, lst )
331
335
{
332
- if ( u.layerType == QStringLiteral ( " vector" ) )
336
+ // Check that we are not copying over self
337
+ if ( u.uri .startsWith ( mPath ) )
333
338
{
334
- // Check that we are not copying over self
335
- if ( u.uri .startsWith ( mPath ) )
336
- {
337
- importResults.append ( tr ( " You cannot import layer %1 over itself!" ).arg ( u.name ) );
338
- hasError = true ;
339
+ importResults.append ( tr ( " You cannot import layer %1 over itself!" ).arg ( u.name ) );
340
+ hasError = true ;
339
341
342
+ }
343
+ else
344
+ {
345
+ QgsMapLayer *srcLayer;
346
+ bool owner;
347
+ bool isVector = false ;
348
+ QString error;
349
+ // Common checks for raster and vector
350
+ // aspatial is treated like vector
351
+ if ( u.layerType == QStringLiteral ( " vector" ) )
352
+ {
353
+ // open the source layer
354
+ srcLayer = u.vectorLayer ( owner, error );
355
+ isVector = true ;
340
356
}
341
357
else
342
358
{
343
- // open the source layer
344
- bool owner;
345
- QString error;
346
- QgsVectorLayer *srcLayer = u.vectorLayer ( owner, error );
347
- if ( !srcLayer )
348
- {
349
- importResults.append ( tr ( " %1: %2" ).arg ( u.name ).arg ( error ) );
350
- hasError = true ;
351
- continue ;
352
- }
359
+ srcLayer = u.rasterLayer ( owner, error );
360
+ }
361
+ if ( !srcLayer )
362
+ {
363
+ importResults.append ( tr ( " %1: %2" ).arg ( u.name ).arg ( error ) );
364
+ hasError = true ;
365
+ continue ;
366
+ }
353
367
354
- if ( srcLayer->isValid () )
355
- {
356
- uri = mPath ;
357
- QgsDebugMsgLevel ( " URI " + uri, 3 );
368
+ if ( srcLayer->isValid () )
369
+ {
370
+ uri = mPath ;
371
+ QgsDebugMsgLevel ( " URI " + uri, 3 );
358
372
359
- // check if the destination layer already exists
360
- bool exists = false ;
361
- // Q_FOREACH won't detach ...
362
- for ( const auto child : children () )
373
+ // check if the destination layer already exists
374
+ bool exists = false ;
375
+ // Q_FOREACH won't detach ...
376
+ for ( const auto child : children () )
377
+ {
378
+ if ( child->name () == u.name )
363
379
{
364
- if ( child->name () == u.name )
365
- {
366
- exists = true ;
367
- }
380
+ exists = true ;
368
381
}
369
- if ( ! exists || QMessageBox::question ( nullptr , tr ( " Overwrite Layer" ),
370
- tr ( " Destination layer <b>%1</b> already exists. Do you want to overwrite it?" ).arg ( u.name ), QMessageBox::Yes | QMessageBox::No ) == QMessageBox::Yes )
382
+ }
383
+ if ( ! exists || QMessageBox::question ( nullptr , tr ( " Overwrite Layer" ),
384
+ tr ( " Destination layer <b>%1</b> already exists. Do you want to overwrite it?" ).arg ( u.name ), QMessageBox::Yes | QMessageBox::No ) == QMessageBox::Yes )
385
+ {
386
+ if ( isVector )
371
387
{
372
-
388
+ QgsVectorLayer *vectorSrcLayer = dynamic_cast < QgsVectorLayer * >( srcLayer );
373
389
QVariantMap options;
374
390
options.insert ( QStringLiteral ( " driverName" ), QStringLiteral ( " GPKG" ) );
375
391
options.insert ( QStringLiteral ( " update" ), true );
376
392
options.insert ( QStringLiteral ( " overwrite" ), true );
377
393
options.insert ( QStringLiteral ( " layerName" ), u.name );
378
394
379
- std::unique_ptr< QgsVectorLayerExporterTask > exportTask ( new QgsVectorLayerExporterTask ( srcLayer, uri, QStringLiteral ( " ogr" ), srcLayer->crs (), options, owner ) );
380
-
395
+ std::unique_ptr< QgsVectorLayerExporterTask > exportTask ( new QgsVectorLayerExporterTask ( vectorSrcLayer, uri, QStringLiteral ( " ogr" ), vectorSrcLayer->crs (), options, owner ) );
381
396
// when export is successful:
382
397
connect ( exportTask.get (), &QgsVectorLayerExporterTask::exportComplete, this , [ = ]()
383
398
{
@@ -400,24 +415,34 @@ bool QgsGeoPackageConnectionItem::handleDrop( const QMimeData *data, Qt::DropAct
400
415
401
416
QgsApplication::taskManager ()->addTask ( exportTask.release () );
402
417
}
403
- }
404
- else
405
- {
406
- importResults.append ( tr ( " %1: Not a valid layer!" ).arg ( u.name ) );
407
- hasError = true ;
408
- }
409
- }
410
- }
411
- else
412
- {
413
- // TODO: implement raster import
414
- QgsMessageOutput *output = QgsMessageOutput::createMessageOutput ();
415
- output->setTitle ( tr ( " Import to GeoPackage database failed" ) );
416
- output->setMessage ( tr ( " Failed to import some layers!\n\n " ) + QStringLiteral ( " Raster import is not yet implemented!\n " ), QgsMessageOutput::MessageText );
417
- output->showMessage ();
418
- }
418
+ else // Import raster
419
+ {
420
+ // In case we need it
421
+ // QgsRasterLayer* rasterSrcLayer = dynamic_cast < QgsRasterLayer* > ( srcLayer );
422
+
423
+ const char *args[] = { " -of" , " gpkg" , " -co" , QStringLiteral ( " RASTER_TABLE=%1" ).arg ( u.name ).toUtf8 ().constData (), " -co" , " APPEND_SUBDATASET=YES" , nullptr };
424
+ GDALTranslateOptions *psOptions = GDALTranslateOptionsNew ( ( char ** )args, nullptr );
425
+ GDALDatasetH hSrcDS = GDALOpen ( u.uri .toUtf8 ().constData (), GA_ReadOnly );
426
+ CPLErrorReset ();
427
+ GDALDatasetH hOutDS = GDALTranslate ( mPath .toUtf8 ().constData (), hSrcDS, psOptions, NULL );
428
+ if ( ! hOutDS )
429
+ {
430
+ importResults.append ( tr ( " Failed to import layer %1! See the message logs for details.\n\n " ).arg ( u.name ) );
431
+ hasError = true ;
419
432
420
- }
433
+ }
434
+ GDALClose ( hSrcDS );
435
+ GDALTranslateOptionsFree ( psOptions );
436
+ }
437
+ } // do not overwrite
438
+ }
439
+ else
440
+ {
441
+ importResults.append ( tr ( " %1: Not a valid layer!" ).arg ( u.name ) );
442
+ hasError = true ;
443
+ }
444
+ } // check for self copy
445
+ } // for each
421
446
422
447
if ( hasError )
423
448
{
@@ -426,7 +451,6 @@ bool QgsGeoPackageConnectionItem::handleDrop( const QMimeData *data, Qt::DropAct
426
451
output->setMessage ( tr ( " Failed to import some layers!\n\n " ) + importResults.join ( QStringLiteral ( " \n " ) ), QgsMessageOutput::MessageText );
427
452
output->showMessage ();
428
453
}
429
-
430
454
return true ;
431
455
}
432
456
0 commit comments