Skip to content
Permalink
Browse files
Added unit test for qgsvectorfilewriter
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@7190 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
timlinux committed Sep 12, 2007
1 parent f96840d commit e71ec2620733039e83410fff49930a073776fc4e
Showing with 105 additions and 2 deletions.
  1. +16 −2 tests/src/core/CMakeLists.txt
  2. +89 −0 tests/src/core/testqgsvectorfilewriter.cpp
@@ -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})
@@ -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)



@@ -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 e71ec26

Please sign in to comment.