Skip to content

Commit

Permalink
Added unit test for qgsvectorfilewriter
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@7190 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
timlinux committed Sep 12, 2007
1 parent ad843df commit e7be602
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
18 changes: 16 additions & 2 deletions tests/src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ ENDIF (APPLE)
#directly included in the sources
#and should not be compiled twice. Trying to include
#them in will cause an error at build time

#############################################################
# Tests:

#
# QgsApplication test
#
SET(applicationtest_SRCS testqgsapplication.cpp)
SET(applicationtest_MOC_CPPS testqgsapplication.cpp)
QT4_WRAP_CPP(applicationtest_MOC_SRCS ${applicationtest_MOC_CPPS})
Expand All @@ -64,5 +66,17 @@ INSTALL(TARGETS applicationtest RUNTIME DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
ADD_TEST(application ${CMAKE_CURRENT_BINARY_DIR}/applicationtest)

#
##
# QgsFileWriter test
#
SET(filewritertest_SRCS testqgsvectorfilewriter.cpp)
SET(filewritertest_MOC_CPPS testqgsvectorfilewriter.cpp)
QT4_WRAP_CPP(filewritertest_MOC_SRCS ${filewritertest_MOC_CPPS})
ADD_CUSTOM_TARGET(filewritertestmoc ALL DEPENDS ${filewritertest_MOC_SRCS})
ADD_EXECUTABLE(filewritertest ${filewritertest_SRCS})
ADD_DEPENDENCIES(filewritertest filewritertestmoc)
TARGET_LINK_LIBRARIES(filewritertest ${QT_LIBRARIES} qgis_core)
INSTALL(TARGETS filewritertest RUNTIME DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
ADD_TEST(filewriter ${CMAKE_CURRENT_BINARY_DIR}/filewritertest)



89 changes: 89 additions & 0 deletions tests/src/core/testqgsvectorfilewriter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <QtTest>
#include <QObject>
#include <QString>
#include <QStringList>
#include <QObject>
#include <iostream>

#include <QApplication>

#include <qgsvectorlayer.h> //defines QgsFieldMap
#include <qgsvectorfilewriter.h> //logic for writing shpfiles
#include <qgsfeature.h> //we will need to pass a bunch of these for each rec
#include <qgsgeometry.h> //each feature needs a geometry
#include <qgspoint.h> //we will use point geometry
#include <qgsspatialrefsys.h> //needed for creating a srs
#include <qgsapplication.h> //search path for srs.db
#include <qgsfield.h>
#include <qgis.h> //defines GEOWKT

class TestQgsVectorFileWriter: public QObject
{
Q_OBJECT;
private slots:
void createVectorFile()
{
// init QGIS's paths - true means that all path will be inited from prefix
QString qgisPath = QCoreApplication::applicationDirPath ();
QgsApplication::setPrefixPath(qgisPath, TRUE);

std::cout << "Prefix PATH: " << QgsApplication::prefixPath().toLocal8Bit().data() << std::endl;
std::cout << "Plugin PATH: " << QgsApplication::pluginPath().toLocal8Bit().data() << std::endl;
std::cout << "PkgData PATH: " << QgsApplication::pkgDataPath().toLocal8Bit().data() << std::endl;
std::cout << "User DB PATH: " << QgsApplication::qgisUserDbFilePath().toLocal8Bit().data() << std::endl;

QString myEncoding("UTF-8");
QString myFileName("/tmp/testshp.shp");
QgsVectorFileWriter::WriterError myError;

// Possible QVariant::Type s
// QVariant::String
// QVariant::Int
// QVariant::Double
//
// Allowed ogr prvider typeNames:
// Integer
// Real
// String

// Constructor for QgsField:
// QgsField::QgsField(QString name,
// QVariant::Type type,
// QString typeName,
// int len,
// int prec,
// QString comment)
QgsField myField1("Field1",QVariant::String,"String",10,0,"Field 1 comment");
QgsFieldMap myFields;
myFields.insert(0, myField1);
QgsSpatialRefSys mySRS(GEOWKT);
QgsVectorFileWriter myWriter (myFileName,
myEncoding,
myFields,
QGis::WKBPoint,
&mySRS);
//
// Create a feature
//
QgsPoint myPoint(10.0,10.0);
QgsGeometry * mypGeometry = QgsGeometry::fromPoint(myPoint);
QgsFeature myFeature;
myFeature.setGeometry(mypGeometry);
myFeature.addAttribute(0,"HelloWorld");
//
// Write the featyre to the filewriter
//
myWriter.addFeature(myFeature);
myError = myWriter.hasError();
Q_ASSERT(myError==QgsVectorFileWriter::NoError);
// other possible outcomes...
//QgsVectorFileWriter::ErrDriverNotFound:
//QgsVectorFileWriter::ErrCreateDataSource:
//QgsVectorFileWriter::ErrCreateLayer:
delete mypGeometry;
}
};

QTEST_MAIN(TestQgsVectorFileWriter)
#include "moc_testqgsvectorfilewriter.cxx"

0 comments on commit e7be602

Please sign in to comment.