Skip to content

Commit 60fa8ea

Browse files
committedSep 12, 2018
[FEATURE] Append raster layer to an existing Geopackage in the 'Save Raster Layer As' dialog. Fixes #17926
1 parent 6935027 commit 60fa8ea

File tree

5 files changed

+129
-9
lines changed

5 files changed

+129
-9
lines changed
 

‎python/gui/auto_generated/qgsrasterlayersaveasdialog.sip.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Constructor for QgsRasterLayerSaveAsDialog
5454
bool tileMode() const;
5555
bool addToCanvas() const;
5656
QString outputFileName() const;
57+
QString outputLayerName() const;
5758
QString outputFormat() const;
5859
QgsCoordinateReferenceSystem outputCrs();
5960
QStringList createOptions() const;
@@ -72,6 +73,7 @@ Constructor for QgsRasterLayerSaveAsDialog
7273
public slots:
7374
virtual void accept();
7475

76+
7577
};
7678

7779

‎src/app/qgisapp.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7156,13 +7156,16 @@ void QgisApp::saveAsRasterFile( QgsRasterLayer *rasterLayer )
71567156
bool tileMode = d.tileMode();
71577157
bool addToCanvas = d.addToCanvas();
71587158
QPointer< QgsRasterLayer > rlWeakPointer( rasterLayer );
7159+
QString outputLayerName = d.outputLayerName();
7160+
QString outputFormat = d.outputFormat();
71597161

71607162
QgsRasterFileWriterTask *writerTask = new QgsRasterFileWriterTask( fileWriter, pipe.release(), d.nColumns(), d.nRows(),
71617163
d.outputRectangle(), d.outputCrs() );
71627164

71637165
// when writer is successful:
71647166

7165-
connect( writerTask, &QgsRasterFileWriterTask::writeComplete, this, [this, tileMode, addToCanvas, rlWeakPointer ]( const QString & newFilename )
7167+
connect( writerTask, &QgsRasterFileWriterTask::writeComplete, this,
7168+
[this, tileMode, addToCanvas, rlWeakPointer, outputLayerName, outputFormat]( const QString & newFilename )
71667169
{
71677170
QString fileName = newFilename;
71687171
if ( tileMode )
@@ -7173,7 +7176,14 @@ void QgisApp::saveAsRasterFile( QgsRasterLayer *rasterLayer )
71737176

71747177
if ( addToCanvas )
71757178
{
7176-
addRasterLayers( QStringList( fileName ) );
7179+
if ( outputFormat == QLatin1String( "GPKG" ) && !outputLayerName.isEmpty() )
7180+
{
7181+
addRasterLayers( QStringList( QStringLiteral( "GPKG:%1:%2" ).arg( fileName, outputLayerName ) ) );
7182+
}
7183+
else
7184+
{
7185+
addRasterLayers( QStringList( fileName ) );
7186+
}
71777187
}
71787188
if ( rlWeakPointer )
71797189
emit layerSavedAs( rlWeakPointer, fileName );
@@ -12811,7 +12821,17 @@ bool QgisApp::addRasterLayers( QStringList const &fileNameQStringList, bool guiW
1281112821
QFileInfo myFileInfo( *myIterator );
1281212822

1281312823
// try to create the layer
12814-
QgsRasterLayer *layer = addRasterLayerPrivate( *myIterator, myFileInfo.completeBaseName(),
12824+
// set the layer name to the file base name...
12825+
QString layerName = myFileInfo.completeBaseName();
12826+
12827+
// ...unless the layer uri matches "GPKG:filePath:layerName" and layerName differs from the file base name
12828+
QStringList layerUriSegments = myIterator->split( QLatin1String( ":" ) );
12829+
if ( layerUriSegments.count() == 3 && layerUriSegments[ 0 ] == QLatin1String( "GPKG" ) && layerUriSegments[ 2 ] != layerName )
12830+
{
12831+
layerName = QStringLiteral( "%1 %2" ).arg( layerName, layerUriSegments[ 2 ] );
12832+
}
12833+
12834+
QgsRasterLayer *layer = addRasterLayerPrivate( *myIterator, layerName,
1281512835
QString(), guiWarning, true );
1281612836
if ( layer && layer->isValid() )
1281712837
{

‎src/gui/qgsrasterlayersaveasdialog.cpp

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ QgsRasterLayerSaveAsDialog::QgsRasterLayerSaveAsDialog( QgsRasterLayer *rasterLa
179179
QFileInfo tmplFileInfo( filePath );
180180
settings.setValue( QStringLiteral( "UI/lastRasterFileDir" ), tmplFileInfo.absolutePath() );
181181

182+
if ( !filePath.isEmpty() && mLayerName->isEnabled() )
183+
{
184+
QFileInfo fileInfo( filePath );
185+
mLayerName->setText( fileInfo.baseName() );
186+
}
187+
182188
if ( mTileModeCheckBox->isChecked() )
183189
{
184190
QString fileName = filePath;
@@ -314,6 +320,18 @@ void QgsRasterLayerSaveAsDialog::mFormatComboBox_currentIndexChanged( const QStr
314320
tr( "All files (*.*)" ) );
315321
}
316322
mFilename->setFilter( filter );
323+
324+
mFilename->setConfirmOverwrite( outputFormat() != QStringLiteral( "GPKG" ) );
325+
mLayerName->setEnabled( outputFormat() == QStringLiteral( "GPKG" ) );
326+
if ( mLayerName->isEnabled() )
327+
{
328+
QString layerName = QFileInfo( mFilename->filePath() ).baseName();
329+
mLayerName->setText( layerName );
330+
}
331+
else
332+
{
333+
mLayerName->setText( QString() );
334+
}
317335
}
318336

319337
int QgsRasterLayerSaveAsDialog::nColumns() const
@@ -380,14 +398,55 @@ QString QgsRasterLayerSaveAsDialog::outputFileName() const
380398
return fileName;
381399
}
382400

401+
QString QgsRasterLayerSaveAsDialog::outputLayerName() const
402+
{
403+
if ( mLayerName->text().isEmpty() && outputFormat() == QStringLiteral( "GPKG" ) && !mTileModeCheckBox->isChecked() )
404+
{
405+
// Always return layer name for GeoPackages
406+
return QFileInfo( mFilename->filePath() ).baseName();
407+
}
408+
else
409+
{
410+
return mLayerName->text();
411+
}
412+
}
413+
383414
QString QgsRasterLayerSaveAsDialog::outputFormat() const
384415
{
385416
return mFormatComboBox->currentData().toString();
386417
}
387418

388419
QStringList QgsRasterLayerSaveAsDialog::createOptions() const
389420
{
390-
return mCreateOptionsGroupBox->isChecked() ? mCreateOptionsWidget->options() : QStringList();
421+
QStringList options = mCreateOptionsGroupBox->isChecked() ? mCreateOptionsWidget->options() : QStringList();
422+
if ( outputFormat() == QStringLiteral( "GPKG" ) )
423+
{
424+
// Overwrite the GPKG table options
425+
int indx = options.indexOf( QRegExp( "^RASTER_TABLE=.*" ) );
426+
if ( indx > -1 )
427+
{
428+
options.replace( indx, QStringLiteral( "RASTER_TABLE=%1" ).arg( outputLayerName() ) );
429+
}
430+
else
431+
{
432+
options.append( QStringLiteral( "RASTER_TABLE=%1" ).arg( outputLayerName() ) );
433+
}
434+
435+
// Only enable the append mode if the layer doesn't exist yet. For existing layers a 'confirm overwrite' dialog will be shown.
436+
if ( !outputLayerExistsInGpkg() )
437+
{
438+
indx = options.indexOf( QRegExp( "^APPEND_SUBDATASET=.*", Qt::CaseInsensitive ) );
439+
if ( indx > -1 )
440+
{
441+
options.replace ( indx, QStringLiteral( "APPEND_SUBDATASET=YES" ) );
442+
}
443+
else
444+
{
445+
options.append( QStringLiteral( "APPEND_SUBDATASET=YES" ) );
446+
}
447+
}
448+
}
449+
return options;
391450
}
392451

393452
QgsRectangle QgsRasterLayerSaveAsDialog::outputRectangle() const
@@ -845,6 +904,32 @@ bool QgsRasterLayerSaveAsDialog::validate() const
845904
return true;
846905
}
847906

907+
bool QgsRasterLayerSaveAsDialog::outputLayerExistsInGpkg() const
908+
{
909+
QgsRasterLayer *layer = nullptr;
910+
layer = new QgsRasterLayer( QStringLiteral( "GPKG:%1:%2" ).arg( outputFileName(), outputLayerName() ), "", QStringLiteral( "gdal" ) );
911+
return layer->isValid();
912+
}
913+
914+
void QgsRasterLayerSaveAsDialog::accept()
915+
{
916+
if ( !validate() )
917+
{
918+
return;
919+
}
920+
921+
if ( outputFormat() == QStringLiteral( "GPKG" ) && outputLayerExistsInGpkg() &&
922+
QMessageBox::warning( this, tr( "Save Raster Layer" ),
923+
tr( "The layer %1 already exists in the target file, and overwriting layers in GeoPackage is not supported. "
924+
"Do you want to overwrite the whole file?" ).arg( outputLayerName() ),
925+
QMessageBox::Yes | QMessageBox::No ) == QMessageBox::No )
926+
{
927+
return;
928+
}
929+
930+
QDialog::accept();
931+
}
932+
848933
void QgsRasterLayerSaveAsDialog::showHelp()
849934
{
850935
QgsHelp::openHelp( QStringLiteral( "managing_data_source/create_layers.html#save-layer-from-an-existing-file" ) );

‎src/gui/qgsrasterlayersaveasdialog.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class GUI_EXPORT QgsRasterLayerSaveAsDialog: public QDialog, private Ui::QgsRast
7171
bool tileMode() const;
7272
bool addToCanvas() const;
7373
QString outputFileName() const;
74+
QString outputLayerName() const;
7475
QString outputFormat() const;
7576
QgsCoordinateReferenceSystem outputCrs();
7677
QStringList createOptions() const;
@@ -87,7 +88,7 @@ class GUI_EXPORT QgsRasterLayerSaveAsDialog: public QDialog, private Ui::QgsRast
8788
void hideOutput();
8889

8990
public slots:
90-
void accept() override { if ( validate() ) QDialog::accept(); }
91+
void accept() override;
9192

9293
private slots:
9394
void mRawModeRadioButton_toggled( bool );
@@ -138,6 +139,7 @@ class GUI_EXPORT QgsRasterLayerSaveAsDialog: public QDialog, private Ui::QgsRast
138139
double noDataCellValue( int row, int column ) const;
139140
void adjustNoDataCellWidth( int row, int column );
140141
bool validate() const;
142+
bool outputLayerExistsInGpkg() const;
141143

142144
void insertAvailableOutputFormats();
143145
};

‎src/ui/qgsrasterlayersaveasdialogbase.ui

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<x>0</x>
88
<y>0</y>
99
<width>575</width>
10-
<height>580</height>
10+
<height>610</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
@@ -86,7 +86,7 @@
8686
</property>
8787
</widget>
8888
</item>
89-
<item row="3" column="1">
89+
<item row="4" column="1">
9090
<widget class="QgsProjectionSelectionWidget" name="mCrsSelector" native="true">
9191
<property name="focusPolicy">
9292
<enum>Qt::StrongFocus</enum>
@@ -118,7 +118,7 @@ datasets with maximum width and height specified below.</string>
118118
</item>
119119
</layout>
120120
</item>
121-
<item row="3" column="0">
121+
<item row="4" column="0">
122122
<widget class="QLabel" name="label_2">
123123
<property name="sizePolicy">
124124
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
@@ -147,7 +147,7 @@ datasets with maximum width and height specified below.</string>
147147
</property>
148148
</widget>
149149
</item>
150-
<item row="4" column="0" colspan="2">
150+
<item row="5" column="0" colspan="2">
151151
<widget class="QCheckBox" name="mAddToCanvas">
152152
<property name="text">
153153
<string>Add saved file to map</string>
@@ -157,6 +157,16 @@ datasets with maximum width and height specified below.</string>
157157
</property>
158158
</widget>
159159
</item>
160+
<item row="3" column="0">
161+
<widget class="QLabel" name="label_3">
162+
<property name="text">
163+
<string>Layer name</string>
164+
</property>
165+
</widget>
166+
</item>
167+
<item row="3" column="1">
168+
<widget class="QLineEdit" name="mLayerName"/>
169+
</item>
160170
</layout>
161171
</item>
162172
<item>
@@ -716,6 +726,7 @@ datasets with maximum width and height specified below.</string>
716726
<tabstop>mFormatComboBox</tabstop>
717727
<tabstop>mTileModeCheckBox</tabstop>
718728
<tabstop>mFilename</tabstop>
729+
<tabstop>mLayerName</tabstop>
719730
<tabstop>mCrsSelector</tabstop>
720731
<tabstop>mAddToCanvas</tabstop>
721732
<tabstop>mScrollArea</tabstop>

0 commit comments

Comments
 (0)
Please sign in to comment.