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
initTestCase()46 void TestQgsRasterLayerSaveAsDialog::initTestCase()
47 {
48 QgsApplication::init();
49 QgsApplication::initQgis();
50 }
51
cleanupTestCase()52 void TestQgsRasterLayerSaveAsDialog::cleanupTestCase()
53 {
54 QgsApplication::exitQgis();
55 }
56
init()57 void TestQgsRasterLayerSaveAsDialog::init()
58 {
59 }
60
cleanup()61 void TestQgsRasterLayerSaveAsDialog::cleanup()
62 {
63 }
64
outputLayerExists()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
prepareDb()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"
137