Skip to content
Permalink
Browse files
Fix export raster as GPKG
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 3fd399dc59bfca2526757de9ff1feb7fd8c84825
@@ -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()
@@ -163,6 +163,8 @@ class GUI_EXPORT QgsRasterLayerSaveAsDialog: public QDialog, private Ui::QgsRast
bool outputLayerExists() const;

void insertAvailableOutputFormats();

friend class TestQgsRasterLayerSaveAsDialog;
};


@@ -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">
@@ -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>
@@ -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)
@@ -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.