Skip to content

Commit

Permalink
Fix export raster as GPKG
Browse files Browse the repository at this point in the history
Fixes #20848

Test added for internal functions because the main functionality
is in app.

Partially reverts 006b130
See: 006b130
  • Loading branch information
elpaso committed May 20, 2019
1 parent 284f5d5 commit 3fd399d
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 41 deletions.
7 changes: 2 additions & 5 deletions src/gui/qgsrasterlayersaveasdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -933,11 +933,8 @@ bool QgsRasterLayerSaveAsDialog::outputLayerExists() const
{
uri = outputFileName();
}

std::unique_ptr< QgsRasterLayer > rastLayer( new QgsRasterLayer( uri, "", QStringLiteral( "gdal" ) ) );
QgsVectorLayer::LayerOptions options { QgsProject::instance()->transformContext() };
std::unique_ptr< QgsVectorLayer > vectLayer = qgis::make_unique<QgsVectorLayer>( uri, QString(), QStringLiteral( "ogr" ), options );
return ( rastLayer->isValid() || vectLayer->isValid() );
std::unique_ptr< QgsRasterLayer > rastLayer( new QgsRasterLayer( uri, QString( ), QStringLiteral( "gdal" ) ) );
return rastLayer->isValid();
}

void QgsRasterLayerSaveAsDialog::accept()
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgsrasterlayersaveasdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ class GUI_EXPORT QgsRasterLayerSaveAsDialog: public QDialog, private Ui::QgsRast
bool outputLayerExists() const;

void insertAvailableOutputFormats();

friend class TestQgsRasterLayerSaveAsDialog;
};


Expand Down
39 changes: 3 additions & 36 deletions src/ui/qgsrasterlayersaveasdialogbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ datasets with maximum width and height specified below.</string>
<rect>
<x>0</x>
<y>0</y>
<width>543</width>
<height>675</height>
<width>541</width>
<height>597</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
Expand Down Expand Up @@ -760,38 +760,5 @@ datasets with maximum width and height specified below.</string>
<resources>
<include location="../../images/images.qrc"/>
</resources>
<connections>
<connection>
<sender>mButtonBox</sender>
<signal>accepted()</signal>
<receiver>QgsRasterLayerSaveAsDialogBase</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>254</x>
<y>575</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>mButtonBox</sender>
<signal>rejected()</signal>
<receiver>QgsRasterLayerSaveAsDialogBase</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>322</x>
<y>575</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
<connections/>
</ui>
1 change: 1 addition & 0 deletions tests/src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ ADD_QGIS_TEST(filedownloader testqgsfiledownloader.cpp)
ADD_QGIS_TEST(layoutgui testqgslayoutgui.cpp)
ADD_QGIS_TEST(layoutview testqgslayoutview.cpp)
ADD_QGIS_TEST(valuemapwidgetwrapper testqgsvaluemapwidgetwrapper.cpp)
ADD_QGIS_TEST(rasterlayersavesdialog testqgsrasterlayersavesdialog.cpp)
ADD_QGIS_TEST(valuerelationwidgetwrapper testqgsvaluerelationwidgetwrapper.cpp)
ADD_QGIS_TEST(relationreferencewidget testqgsrelationreferencewidget.cpp)
ADD_QGIS_TEST(featurelistcombobox testqgsfeaturelistcombobox.cpp)
136 changes: 136 additions & 0 deletions tests/src/gui/testqgsrasterlayersavesdialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/***************************************************************************
testqgsrasterlayersavesdialog.cpp
--------------------------------------
Date : May 2019
Copyright : (C) 2019 Alessandro Pasotti
Email : elpaso at itopen dot it
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/


#include "qgstest.h"

#include "qgsrasterlayersaveasdialog.h"
#include "qgsvectorfilewriter.h"
#include "qgsvectorlayer.h"
#include "qgsrasterlayer.h"
#include "qgsrasterfilewriter.h"

#include "qgsgui.h"

class TestQgsRasterLayerSaveAsDialog : public QObject
{
Q_OBJECT
public:
TestQgsRasterLayerSaveAsDialog() = default;

private slots:
void initTestCase(); // will be called before the first testfunction is executed.
void cleanupTestCase(); // will be called after the last testfunction was executed.
void init(); // will be called before each testfunction is executed.
void cleanup(); // will be called after every testfunction.
void outputLayerExists();

private:

QString prepareDb();

};

void TestQgsRasterLayerSaveAsDialog::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();
}

void TestQgsRasterLayerSaveAsDialog::cleanupTestCase()
{
QgsApplication::exitQgis();
}

void TestQgsRasterLayerSaveAsDialog::init()
{
}

void TestQgsRasterLayerSaveAsDialog::cleanup()
{
}

void TestQgsRasterLayerSaveAsDialog::outputLayerExists()
{
QString fileName { prepareDb() };

// Try to add a raster layer to the DB
QString dataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
QString rasterPath { dataDir + "/landsat.tif" };

QgsRasterLayer rl( rasterPath, QStringLiteral( "my_raster" ) );
QVERIFY( rl.isValid() );

QgsRasterLayerSaveAsDialog d( &rl, rl.dataProvider(), rl.extent(), rl.crs(), rl.crs() );
d.mFormatComboBox->setCurrentIndex( d.mFormatComboBox->findData( QStringLiteral( "GPKG" ) ) );
QCOMPARE( d.mFormatComboBox->currentData().toString(), QString( "GPKG" ) );
QVERIFY( ! d.outputLayerExists() );
d.mFilename->setFilePath( fileName );
d.mLayerName->setText( QStringLiteral( "my_imported_raster" ) );
QVERIFY( ! d.outputLayerExists() );

// Write the raster into the destination file
auto pipe { *rl.pipe() };
auto rasterUri { QStringLiteral( "GPKG:%1:%2" ).arg( d.outputFileName() ).arg( d.outputLayerName() ) };
auto fileWriter { QgsRasterFileWriter( d.outputFileName() ) };
fileWriter.setCreateOptions( d.createOptions() );
fileWriter.setOutputFormat( d.outputFormat() );
fileWriter.setBuildPyramidsFlag( d.buildPyramidsFlag() );
fileWriter.setPyramidsList( d.pyramidsList() );
fileWriter.setPyramidsResampling( d.pyramidsResamplingMethod() );
fileWriter.setPyramidsFormat( d.pyramidsFormat() );
fileWriter.setPyramidsConfigOptions( d.pyramidsConfigOptions() );
fileWriter.writeRaster( &pipe, 10, 10, rl.extent(), rl.crs(), rl.transformContext() );
{
QVERIFY( QgsRasterLayer( rasterUri, QStringLiteral( "my_raster2" ) ).isValid() );
}
QVERIFY( d.outputLayerExists() );
}

QString TestQgsRasterLayerSaveAsDialog::prepareDb()
{
// Preparation: make a test gpk DB with a vector layer in it
QTemporaryFile tmpFile( QDir::tempPath() + QStringLiteral( "/test_qgsrasterlayersavesdialog_XXXXXX.gpkg" ) );
tmpFile.setAutoRemove( false );
tmpFile.open();
QString fileName( tmpFile.fileName( ) );
QgsVectorLayer vl( QStringLiteral( "Point?field=firstfield:string(1024)" ), "test_layer", "memory" );
QgsVectorFileWriter w( fileName,
QStringLiteral( "UTF-8" ),
vl.fields(),
QgsWkbTypes::Point,
vl.crs() );
QgsFeature f { vl.fields() };
f.setAttribute( 0, QString( 1024, 'x' ) );
f.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "point(9 45)" ) ) );
vl.startEditing();
vl.addFeature( f );
QgsVectorFileWriter::SaveVectorOptions options;
options.driverName = QStringLiteral( "GPKG" );
options.layerName = QStringLiteral( "test_layer" );
QString errorMessage;
QgsVectorFileWriter::writeAsVectorFormat(
&vl,
fileName,
options,
&errorMessage );
QgsVectorLayer vl2( QStringLiteral( "%1|layername=test_layer" ).arg( fileName ), "src_test", "ogr" );
Q_ASSERT( vl2.isValid() );
return tmpFile.fileName( );
}

QGSTEST_MAIN( TestQgsRasterLayerSaveAsDialog )

#include "testqgsrasterlayersavesdialog.moc"

0 comments on commit 3fd399d

Please sign in to comment.