|
| 1 | +/*************************************************************************** |
| 2 | + testqgsgeometry.cpp |
| 3 | + -------------------------------------- |
| 4 | + Date : 20 Jan 2008 |
| 5 | + Copyright : (C) 2008 by Tim Sutton |
| 6 | + Email : tim @ linfiniti.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 <QApplication> |
| 21 | +#include <QFileInfo> |
| 22 | +#include <QDir> |
| 23 | +#include <QDesktopServices> |
| 24 | + |
| 25 | +#include <iostream> |
| 26 | +//qgis includes... |
| 27 | +#include <qgsapplication.h> |
| 28 | +#include <qgsgeometry.h> |
| 29 | + |
| 30 | +/** \ingroup UnitTests |
| 31 | + * This is a unit test for the different geometry operations on vector features. |
| 32 | + */ |
| 33 | +class TestQgsGeometry: public QObject |
| 34 | +{ |
| 35 | + Q_OBJECT; |
| 36 | + private slots: |
| 37 | + QString getQgisPath(); // Gets the path to QGIS installation |
| 38 | + void initTestCase();// will be called before the first testfunction is executed. |
| 39 | + void cleanupTestCase();// will be called after the last testfunction was executed. |
| 40 | + void init();// will be called before each testfunction is executed. |
| 41 | + void cleanup();// will be called after every testfunction. |
| 42 | + |
| 43 | + void intersectionCheck(); |
| 44 | + private: |
| 45 | + QgsPoint mPoint1; /* +1 +A */ |
| 46 | + QgsPoint mPoint2; /* / \ / \ */ |
| 47 | + QgsPoint mPoint3; /* / X \ */ |
| 48 | + QgsPoint mPointA; /* 2+---/-+3 \ */ |
| 49 | + QgsPoint mPointB; /* B+-------+C */ |
| 50 | + QgsPoint mPointC; /* */ |
| 51 | + QgsPoint mPointD; /* +D */ |
| 52 | + QgsPolyline mPolylineA; |
| 53 | + QgsPolyline mPolylineB; |
| 54 | + QgsPolyline mPolylineC; |
| 55 | + QgsPolygon mPolygonA; |
| 56 | + QgsPolygon mPolygonB; |
| 57 | + QgsPolygon mPolygonC; |
| 58 | + QgsGeometry * mpPolygonGeometryA; |
| 59 | + QgsGeometry * mpPolygonGeometryB; |
| 60 | + QgsGeometry * mpPolygonGeometryC; |
| 61 | + |
| 62 | + QString mTestDataDir; |
| 63 | +}; |
| 64 | + |
| 65 | +QString TestQgsGeometry::getQgisPath() |
| 66 | +{ |
| 67 | +#ifdef Q_OS_LINUX |
| 68 | + QString qgisPath = QCoreApplication::applicationDirPath () + "/../"; |
| 69 | +#else //mac and win |
| 70 | + QString qgisPath = QCoreApplication::applicationDirPath () ; |
| 71 | +#endif |
| 72 | + return qgisPath; |
| 73 | +} |
| 74 | + |
| 75 | +void TestQgsGeometry::init() |
| 76 | +{ |
| 77 | + // |
| 78 | + // Reset / reinitialise the geometries before each test is run |
| 79 | + // |
| 80 | + mPoint1 = QgsPoint(20.0,10.0); /* +1 +A */ |
| 81 | + mPoint2 = QgsPoint(10.0,30.0); /* / \ / \ */ |
| 82 | + mPoint3 = QgsPoint(30.0,30.0); /* / X \ */ |
| 83 | + mPointA = QgsPoint(40.0,10.0); /* 2+---/-+3 \ */ |
| 84 | + mPointB = QgsPoint(20.0,40.0); /* B+-------+C */ |
| 85 | + mPointC = QgsPoint(50.0,40.0); /* */ |
| 86 | + mPointD = QgsPoint(20.0,60.0); /* */ |
| 87 | + |
| 88 | + mPolylineA << mPoint1 << mPoint2 << mPoint3 << mPoint1; |
| 89 | + mPolygonA << mPolylineA; |
| 90 | + //Polygon B intersects Polygon A |
| 91 | + mPolylineB << mPointA << mPointB << mPointC << mPointA; |
| 92 | + mPolygonB << mPolylineB; |
| 93 | + // Polygon C should intersect no other polys |
| 94 | + mPolylineC << mPointD << mPointB << mPointC << mPointD; |
| 95 | + mPolygonC << mPolylineC; |
| 96 | + |
| 97 | + //polygon: first item of the list is outer ring, |
| 98 | + // inner rings (if any) start from second item |
| 99 | + mpPolygonGeometryA = QgsGeometry::fromPolygon(mPolygonA); |
| 100 | + mpPolygonGeometryB = QgsGeometry::fromPolygon(mPolygonB); |
| 101 | + mpPolygonGeometryC = QgsGeometry::fromPolygon(mPolygonC); |
| 102 | + |
| 103 | +} |
| 104 | + |
| 105 | +void TestQgsGeometry::cleanup() |
| 106 | +{ |
| 107 | + // will be called after every testfunction. |
| 108 | + delete mpPolygonGeometryA; |
| 109 | + delete mpPolygonGeometryB; |
| 110 | + delete mpPolygonGeometryC; |
| 111 | +} |
| 112 | + |
| 113 | +void TestQgsGeometry::initTestCase() |
| 114 | +{ |
| 115 | + // |
| 116 | + // Runs once before any tests are run |
| 117 | + // |
| 118 | + // init QGIS's paths - true means that all path will be inited from prefix |
| 119 | + //QString qgisPath = QCoreApplication::applicationDirPath (); |
| 120 | + QgsApplication::setPrefixPath(getQgisPath(), TRUE); |
| 121 | +#ifdef Q_OS_LINUX |
| 122 | +// QgsApplication::setPkgDataPath(qgisPath + "/../share/qgis"); |
| 123 | +// QgsApplication::setPluginPath(qgisPath + "/../lib/qgis"); |
| 124 | +#endif |
| 125 | + |
| 126 | + std::cout << "Prefix PATH: " << QgsApplication::prefixPath().toLocal8Bit().data() << std::endl; |
| 127 | + std::cout << "Plugin PATH: " << QgsApplication::pluginPath().toLocal8Bit().data() << std::endl; |
| 128 | + std::cout << "PkgData PATH: " << QgsApplication::pkgDataPath().toLocal8Bit().data() << std::endl; |
| 129 | + std::cout << "User DB PATH: " << QgsApplication::qgisUserDbFilePath().toLocal8Bit().data() << std::endl; |
| 130 | + |
| 131 | +} |
| 132 | +void TestQgsGeometry::cleanupTestCase() |
| 133 | +{ |
| 134 | + // |
| 135 | + // Runs once after all tests are run |
| 136 | + // |
| 137 | + |
| 138 | +} |
| 139 | + |
| 140 | +void TestQgsGeometry::intersectionCheck() |
| 141 | +{ |
| 142 | + QVERIFY ( mpPolygonGeometryA->intersects(mpPolygonGeometryB)); |
| 143 | + QVERIFY ( !mpPolygonGeometryA->intersects(mpPolygonGeometryC)); |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | +QTEST_MAIN(TestQgsGeometry) |
| 148 | +#include "moc_testqgsgeometry.cxx" |
| 149 | + |
0 commit comments