|
| 1 | +/*************************************************************************** |
| 2 | + testqgsvectorfilewriter.cpp |
| 3 | + -------------------------------------- |
| 4 | + Date : Sun Sep 16 12:22:54 AKDT 2007 |
| 5 | + Copyright : (C) 2007 by Gary E. Sherman |
| 6 | + Email : sherman at mrcc dot com |
| 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 | +#include <QtTest> |
| 16 | +#include <QObject> |
| 17 | +#include <QString> |
| 18 | +#include <QStringList> |
| 19 | +#include <QObject> |
| 20 | +#include <QPainter> |
| 21 | +#include <QSettings> |
| 22 | +#include <QTime> |
| 23 | +#include <iostream> |
| 24 | + |
| 25 | +#include <QApplication> |
| 26 | + |
| 27 | +//qgis includes... |
| 28 | +#include <qgsvectorlayer.h> //defines QgsFieldMap |
| 29 | +#include <qgsvectorfilewriter.h> //logic for writing shpfiles |
| 30 | +#include <qgsfeature.h> //we will need to pass a bunch of these for each rec |
| 31 | +#include <qgsgeometry.h> //each feature needs a geometry |
| 32 | +#include <qgspoint.h> //we will use point geometry |
| 33 | +#include <qgsspatialrefsys.h> //needed for creating a srs |
| 34 | +#include <qgsapplication.h> //search path for srs.db |
| 35 | +#include <qgsfield.h> |
| 36 | +#include <qgis.h> //defines GEOWKT |
| 37 | +#include <qgsmaprender.h> |
| 38 | +#include <qgsmaplayer.h> |
| 39 | +#include <qgsvectorlayer.h> |
| 40 | +#include <qgsapplication.h> |
| 41 | +#include <qgsproviderregistry.h> |
| 42 | +#include <qgsmaplayerregistry.h> |
| 43 | + |
| 44 | +/** \ingroup UnitTests |
| 45 | + * This is a unit test for the QgsMapRender class. |
| 46 | + * It will do some performance testing too |
| 47 | + * |
| 48 | + */ |
| 49 | +class TestQgsMapRender: public QObject |
| 50 | +{ |
| 51 | + Q_OBJECT; |
| 52 | + private slots: |
| 53 | + void initTestCase();// will be called before the first testfunction is executed. |
| 54 | + void init(){};// will be called before each testfunction is executed. |
| 55 | + void cleanup(){};// will be called after every testfunction. |
| 56 | + |
| 57 | + /** This method tests render perfomance */ |
| 58 | + void performanceTest(); |
| 59 | + |
| 60 | + private: |
| 61 | + QString mEncoding; |
| 62 | + QgsVectorFileWriter::WriterError mError; |
| 63 | + QgsSpatialRefSys mSRS; |
| 64 | + QgsFieldMap mFields; |
| 65 | + QgsMapRender * mpMapRenderer; |
| 66 | + QgsMapLayer * mpPolysLayer; |
| 67 | +}; |
| 68 | + |
| 69 | +void TestQgsMapRender::initTestCase() |
| 70 | +{ |
| 71 | + // init QGIS's paths - true means that all path will be inited from prefix |
| 72 | + QString qgisPath = QCoreApplication::applicationDirPath (); |
| 73 | + QgsApplication::setPrefixPath(qgisPath, TRUE); |
| 74 | +#ifdef Q_OS_LINUX |
| 75 | + QgsApplication::setPkgDataPath(qgisPath + "/../share/qgis"); |
| 76 | + QgsApplication::setPluginPath(qgisPath + "/../lib/qgis"); |
| 77 | +#endif |
| 78 | + // Instantiate the plugin directory so that providers are loaded |
| 79 | + QgsProviderRegistry::instance(QgsApplication::pluginPath()); |
| 80 | + std::cout << "Prefix PATH: " << QgsApplication::prefixPath().toLocal8Bit().data() << std::endl; |
| 81 | + std::cout << "Plugin PATH: " << QgsApplication::pluginPath().toLocal8Bit().data() << std::endl; |
| 82 | + std::cout << "PkgData PATH: " << QgsApplication::pkgDataPath().toLocal8Bit().data() << std::endl; |
| 83 | + std::cout << "User DB PATH: " << QgsApplication::qgisUserDbFilePath().toLocal8Bit().data() << std::endl; |
| 84 | + |
| 85 | + //create some objects that will be used in all tests... |
| 86 | + mEncoding = "UTF-8"; |
| 87 | + QgsField myField1("Field1",QVariant::String,"String",10,0,"Field 1 comment"); |
| 88 | + mFields.insert(0, myField1); |
| 89 | + mSRS = QgsSpatialRefSys(GEOWKT); |
| 90 | + // |
| 91 | + // Create the test dataset if it doesnt exist |
| 92 | + // |
| 93 | + QString myDataDir (TEST_DATA_DIR); //defined in CmakeLists.txt |
| 94 | + QString myTestDataDir = myDataDir + QDir::separator(); |
| 95 | + QString myTmpDir = QDir::tempPath() + QDir::separator() ; |
| 96 | + QString myFileName = myTmpDir + "maprender_testdata.shp"; |
| 97 | + qDebug ( "Checking test dataset exists..."); |
| 98 | + qDebug ( myFileName ); |
| 99 | + if (!QFile::exists(myFileName)) |
| 100 | + { |
| 101 | + qDebug ( "Creating test dataset: "); |
| 102 | + |
| 103 | + QgsVectorFileWriter myWriter (myFileName, |
| 104 | + mEncoding, |
| 105 | + mFields, |
| 106 | + QGis::WKBPolygon, |
| 107 | + &mSRS); |
| 108 | + double myInterval=0.5; |
| 109 | + for (double i=-180.0;i<=180.0;i+=myInterval) |
| 110 | + { |
| 111 | + for (double j=-90.0;j<=90.0;j+=myInterval) |
| 112 | + { |
| 113 | + // |
| 114 | + // Create a polygon feature |
| 115 | + // |
| 116 | + QgsPolyline myPolyline; |
| 117 | + QgsPoint myPoint1 = QgsPoint(i,j); |
| 118 | + QgsPoint myPoint2 = QgsPoint(i+myInterval,j); |
| 119 | + QgsPoint myPoint3 = QgsPoint(i+myInterval,j+myInterval); |
| 120 | + QgsPoint myPoint4 = QgsPoint(i,j+myInterval); |
| 121 | + myPolyline << myPoint1 << myPoint2 << myPoint3 << myPoint4 << myPoint1; |
| 122 | + QgsPolygon myPolygon; |
| 123 | + myPolygon << myPolyline; |
| 124 | + //polygon: first item of the list is outer ring, |
| 125 | + // inner rings (if any) start from second item |
| 126 | + // |
| 127 | + // NOTE: dont delete this pointer again - |
| 128 | + // ownership is passed to the feature which will |
| 129 | + // delete it in its dtor! |
| 130 | + QgsGeometry * mypPolygonGeometry = QgsGeometry::fromPolygon(myPolygon); |
| 131 | + QgsFeature myFeature; |
| 132 | + myFeature.setTypeName("WKBPolygon"); |
| 133 | + myFeature.setGeometry(mypPolygonGeometry); |
| 134 | + myFeature.addAttribute(0,"HelloWorld"); |
| 135 | + // |
| 136 | + // Write the feature to the filewriter |
| 137 | + // and check for errors |
| 138 | + // |
| 139 | + QVERIFY(myWriter.addFeature(myFeature)); |
| 140 | + mError = myWriter.hasError(); |
| 141 | + if(mError==QgsVectorFileWriter::ErrDriverNotFound) |
| 142 | + { |
| 143 | + std::cout << "Driver not found error" << std::endl; |
| 144 | + } |
| 145 | + else if (mError==QgsVectorFileWriter::ErrCreateDataSource) |
| 146 | + { |
| 147 | + std::cout << "Create data source error" << std::endl; |
| 148 | + } |
| 149 | + else if (mError==QgsVectorFileWriter::ErrCreateLayer) |
| 150 | + { |
| 151 | + std::cout << "Create layer error" << std::endl; |
| 152 | + } |
| 153 | + QVERIFY(mError==QgsVectorFileWriter::NoError); |
| 154 | + } |
| 155 | + } |
| 156 | + } //file exists |
| 157 | + // |
| 158 | + //create a poly layer that will be used in all tests... |
| 159 | + // |
| 160 | + QFileInfo myPolyFileInfo ( myFileName ); |
| 161 | + mpPolysLayer = new QgsVectorLayer ( myPolyFileInfo.filePath(), |
| 162 | + myPolyFileInfo.completeBaseName(), "ogr" ); |
| 163 | + // Register the layer with the registry |
| 164 | + QgsMapLayerRegistry::instance()->addMapLayer(mpPolysLayer); |
| 165 | + // add the test layer to the maprender |
| 166 | + mpMapRenderer = new QgsMapRender(); |
| 167 | + QStringList myLayers; |
| 168 | + myLayers << mpPolysLayer->getLayerID(); |
| 169 | + mpMapRenderer->setLayerSet(myLayers); |
| 170 | +} |
| 171 | + |
| 172 | +void TestQgsMapRender::performanceTest() |
| 173 | +{ |
| 174 | + |
| 175 | + // |
| 176 | + // Now render our layers onto a pixmap |
| 177 | + // |
| 178 | + QPixmap myPixmap( 1800,900 ); |
| 179 | + myPixmap.fill ( QColor ( "#98dbf9" ) ); |
| 180 | + QPainter myPainter( &myPixmap ); |
| 181 | + mpMapRenderer->setOutputSize( QSize ( 1800,900 ),72 ); |
| 182 | + mpMapRenderer->setExtent(mpPolysLayer->extent()); |
| 183 | + qDebug ("Extents set to:"); |
| 184 | + qDebug (mpPolysLayer->extent().stringRep()); |
| 185 | + QTime myTime; |
| 186 | + myTime.start(); |
| 187 | + mpMapRenderer->render( &myPainter ); |
| 188 | + qDebug ("Elapsed time in ms for render job: " + |
| 189 | + QString::number ( myTime.elapsed() ).toLocal8Bit()); |
| 190 | + myPainter.end(); |
| 191 | + // |
| 192 | + // Save the pixmap to disk so the user can make a |
| 193 | + // visual assessment if needed |
| 194 | + // |
| 195 | + myPixmap.save (QDir::tempPath() + QDir::separator() + "maprender_result.png"); |
| 196 | +} |
| 197 | + |
| 198 | + |
| 199 | +QTEST_MAIN(TestQgsMapRender) |
| 200 | +#include "moc_testqgsmaprender.cxx" |
| 201 | + |
| 202 | + |
0 commit comments