Skip to content

Commit 3fd399d

Browse files
committedMay 20, 2019
Fix export raster as GPKG
Fixes #20848 Test added for internal functions because the main functionality is in app. Partially reverts 006b130 See: 006b130
1 parent 284f5d5 commit 3fd399d

File tree

5 files changed

+144
-41
lines changed

5 files changed

+144
-41
lines changed
 

‎src/gui/qgsrasterlayersaveasdialog.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -933,11 +933,8 @@ bool QgsRasterLayerSaveAsDialog::outputLayerExists() const
933933
{
934934
uri = outputFileName();
935935
}
936-
937-
std::unique_ptr< QgsRasterLayer > rastLayer( new QgsRasterLayer( uri, "", QStringLiteral( "gdal" ) ) );
938-
QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() };
939-
std::unique_ptr< QgsVectorLayer > vectLayer = qgis::make_unique<QgsVectorLayer>( uri, QString(), QStringLiteral( "ogr" ), options );
940-
return ( rastLayer->isValid() || vectLayer->isValid() );
936+
std::unique_ptr< QgsRasterLayer > rastLayer( new QgsRasterLayer( uri, QString( ), QStringLiteral( "gdal" ) ) );
937+
return rastLayer->isValid();
941938
}
942939

943940
void QgsRasterLayerSaveAsDialog::accept()

‎src/gui/qgsrasterlayersaveasdialog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ class GUI_EXPORT QgsRasterLayerSaveAsDialog: public QDialog, private Ui::QgsRast
163163
bool outputLayerExists() const;
164164

165165
void insertAvailableOutputFormats();
166+
167+
friend class TestQgsRasterLayerSaveAsDialog;
166168
};
167169

168170

‎src/ui/qgsrasterlayersaveasdialogbase.ui

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ datasets with maximum width and height specified below.</string>
169169
<rect>
170170
<x>0</x>
171171
<y>0</y>
172-
<width>543</width>
173-
<height>675</height>
172+
<width>541</width>
173+
<height>597</height>
174174
</rect>
175175
</property>
176176
<layout class="QVBoxLayout" name="verticalLayout_5">
@@ -760,38 +760,5 @@ datasets with maximum width and height specified below.</string>
760760
<resources>
761761
<include location="../../images/images.qrc"/>
762762
</resources>
763-
<connections>
764-
<connection>
765-
<sender>mButtonBox</sender>
766-
<signal>accepted()</signal>
767-
<receiver>QgsRasterLayerSaveAsDialogBase</receiver>
768-
<slot>accept()</slot>
769-
<hints>
770-
<hint type="sourcelabel">
771-
<x>254</x>
772-
<y>575</y>
773-
</hint>
774-
<hint type="destinationlabel">
775-
<x>157</x>
776-
<y>274</y>
777-
</hint>
778-
</hints>
779-
</connection>
780-
<connection>
781-
<sender>mButtonBox</sender>
782-
<signal>rejected()</signal>
783-
<receiver>QgsRasterLayerSaveAsDialogBase</receiver>
784-
<slot>reject()</slot>
785-
<hints>
786-
<hint type="sourcelabel">
787-
<x>322</x>
788-
<y>575</y>
789-
</hint>
790-
<hint type="destinationlabel">
791-
<x>286</x>
792-
<y>274</y>
793-
</hint>
794-
</hints>
795-
</connection>
796-
</connections>
763+
<connections/>
797764
</ui>

‎tests/src/gui/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ ADD_QGIS_TEST(filedownloader testqgsfiledownloader.cpp)
149149
ADD_QGIS_TEST(layoutgui testqgslayoutgui.cpp)
150150
ADD_QGIS_TEST(layoutview testqgslayoutview.cpp)
151151
ADD_QGIS_TEST(valuemapwidgetwrapper testqgsvaluemapwidgetwrapper.cpp)
152+
ADD_QGIS_TEST(rasterlayersavesdialog testqgsrasterlayersavesdialog.cpp)
152153
ADD_QGIS_TEST(valuerelationwidgetwrapper testqgsvaluerelationwidgetwrapper.cpp)
153154
ADD_QGIS_TEST(relationreferencewidget testqgsrelationreferencewidget.cpp)
154155
ADD_QGIS_TEST(featurelistcombobox testqgsfeaturelistcombobox.cpp)
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/***************************************************************************
2+
testqgsrasterlayersavesdialog.cpp
3+
--------------------------------------
4+
Date : May 2019
5+
Copyright : (C) 2019 Alessandro Pasotti
6+
Email : elpaso at itopen dot it
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
17+
#include "qgstest.h"
18+
19+
#include "qgsrasterlayersaveasdialog.h"
20+
#include "qgsvectorfilewriter.h"
21+
#include "qgsvectorlayer.h"
22+
#include "qgsrasterlayer.h"
23+
#include "qgsrasterfilewriter.h"
24+
25+
#include "qgsgui.h"
26+
27+
class TestQgsRasterLayerSaveAsDialog : public QObject
28+
{
29+
Q_OBJECT
30+
public:
31+
TestQgsRasterLayerSaveAsDialog() = default;
32+
33+
private slots:
34+
void initTestCase(); // will be called before the first testfunction is executed.
35+
void cleanupTestCase(); // will be called after the last testfunction was executed.
36+
void init(); // will be called before each testfunction is executed.
37+
void cleanup(); // will be called after every testfunction.
38+
void outputLayerExists();
39+
40+
private:
41+
42+
QString prepareDb();
43+
44+
};
45+
46+
void TestQgsRasterLayerSaveAsDialog::initTestCase()
47+
{
48+
QgsApplication::init();
49+
QgsApplication::initQgis();
50+
}
51+
52+
void TestQgsRasterLayerSaveAsDialog::cleanupTestCase()
53+
{
54+
QgsApplication::exitQgis();
55+
}
56+
57+
void TestQgsRasterLayerSaveAsDialog::init()
58+
{
59+
}
60+
61+
void TestQgsRasterLayerSaveAsDialog::cleanup()
62+
{
63+
}
64+
65+
void TestQgsRasterLayerSaveAsDialog::outputLayerExists()
66+
{
67+
QString fileName { prepareDb() };
68+
69+
// Try to add a raster layer to the DB
70+
QString dataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
71+
QString rasterPath { dataDir + "/landsat.tif" };
72+
73+
QgsRasterLayer rl( rasterPath, QStringLiteral( "my_raster" ) );
74+
QVERIFY( rl.isValid() );
75+
76+
QgsRasterLayerSaveAsDialog d( &rl, rl.dataProvider(), rl.extent(), rl.crs(), rl.crs() );
77+
d.mFormatComboBox->setCurrentIndex( d.mFormatComboBox->findData( QStringLiteral( "GPKG" ) ) );
78+
QCOMPARE( d.mFormatComboBox->currentData().toString(), QString( "GPKG" ) );
79+
QVERIFY( ! d.outputLayerExists() );
80+
d.mFilename->setFilePath( fileName );
81+
d.mLayerName->setText( QStringLiteral( "my_imported_raster" ) );
82+
QVERIFY( ! d.outputLayerExists() );
83+
84+
// Write the raster into the destination file
85+
auto pipe { *rl.pipe() };
86+
auto rasterUri { QStringLiteral( "GPKG:%1:%2" ).arg( d.outputFileName() ).arg( d.outputLayerName() ) };
87+
auto fileWriter { QgsRasterFileWriter( d.outputFileName() ) };
88+
fileWriter.setCreateOptions( d.createOptions() );
89+
fileWriter.setOutputFormat( d.outputFormat() );
90+
fileWriter.setBuildPyramidsFlag( d.buildPyramidsFlag() );
91+
fileWriter.setPyramidsList( d.pyramidsList() );
92+
fileWriter.setPyramidsResampling( d.pyramidsResamplingMethod() );
93+
fileWriter.setPyramidsFormat( d.pyramidsFormat() );
94+
fileWriter.setPyramidsConfigOptions( d.pyramidsConfigOptions() );
95+
fileWriter.writeRaster( &pipe, 10, 10, rl.extent(), rl.crs(), rl.transformContext() );
96+
{
97+
QVERIFY( QgsRasterLayer( rasterUri, QStringLiteral( "my_raster2" ) ).isValid() );
98+
}
99+
QVERIFY( d.outputLayerExists() );
100+
}
101+
102+
QString TestQgsRasterLayerSaveAsDialog::prepareDb()
103+
{
104+
// Preparation: make a test gpk DB with a vector layer in it
105+
QTemporaryFile tmpFile( QDir::tempPath() + QStringLiteral( "/test_qgsrasterlayersavesdialog_XXXXXX.gpkg" ) );
106+
tmpFile.setAutoRemove( false );
107+
tmpFile.open();
108+
QString fileName( tmpFile.fileName( ) );
109+
QgsVectorLayer vl( QStringLiteral( "Point?field=firstfield:string(1024)" ), "test_layer", "memory" );
110+
QgsVectorFileWriter w( fileName,
111+
QStringLiteral( "UTF-8" ),
112+
vl.fields(),
113+
QgsWkbTypes::Point,
114+
vl.crs() );
115+
QgsFeature f { vl.fields() };
116+
f.setAttribute( 0, QString( 1024, 'x' ) );
117+
f.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "point(9 45)" ) ) );
118+
vl.startEditing();
119+
vl.addFeature( f );
120+
QgsVectorFileWriter::SaveVectorOptions options;
121+
options.driverName = QStringLiteral( "GPKG" );
122+
options.layerName = QStringLiteral( "test_layer" );
123+
QString errorMessage;
124+
QgsVectorFileWriter::writeAsVectorFormat(
125+
&vl,
126+
fileName,
127+
options,
128+
&errorMessage );
129+
QgsVectorLayer vl2( QStringLiteral( "%1|layername=test_layer" ).arg( fileName ), "src_test", "ogr" );
130+
Q_ASSERT( vl2.isValid() );
131+
return tmpFile.fileName( );
132+
}
133+
134+
QGSTEST_MAIN( TestQgsRasterLayerSaveAsDialog )
135+
136+
#include "testqgsrasterlayersavesdialog.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.