|
| 1 | +/*************************************************************************** |
| 2 | + testqgsmaptoolreshape.cpp |
| 3 | + -------------------------------- |
| 4 | +Date : 2017-12-14 |
| 5 | +Copyright : (C) 2017 by Paul Blottiere |
| 6 | +Email : paul.blottiere@oslandia.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 | + |
| 16 | +#include <QtTest/QtTest> |
| 17 | +#include "qgsapplication.h" |
| 18 | +#include "qgsmapcanvas.h" |
| 19 | +#include "qgsvectorlayer.h" |
| 20 | +#include "qgslinestringv2.h" |
| 21 | +#include "qgsmaptoolreshape.h" |
| 22 | +#include "qgsvectordataprovider.h" |
| 23 | +#include "qgsmaplayerregistry.h" |
| 24 | +#include "qgisapp.h" |
| 25 | + |
| 26 | +class TestQgsMapToolReshape : public QObject |
| 27 | +{ |
| 28 | + Q_OBJECT |
| 29 | + public: |
| 30 | + TestQgsMapToolReshape() = default; |
| 31 | + |
| 32 | + private slots: |
| 33 | + void initTestCase(); // will be called before the first testfunction is executed. |
| 34 | + void cleanupTestCase(); // will be called after the last testfunction was executed. |
| 35 | + void init(); // will be called before each testfunction is executed. |
| 36 | + void cleanup(); // will be called after every testfunction. |
| 37 | + |
| 38 | + void reshapeWithBindingLine(); |
| 39 | + |
| 40 | + private: |
| 41 | + QgisApp *mQgisApp = nullptr; |
| 42 | +}; |
| 43 | + |
| 44 | +void TestQgsMapToolReshape::initTestCase() |
| 45 | +{ |
| 46 | + QgsApplication::init(); |
| 47 | + QgsApplication::initQgis(); |
| 48 | + |
| 49 | + // Set up the QgsSettings environment |
| 50 | + QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) ); |
| 51 | + QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) ); |
| 52 | + QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) ); |
| 53 | + |
| 54 | + QgsApplication::showSettings(); |
| 55 | + |
| 56 | + // enforce C locale because the tests expect it |
| 57 | + // (decimal separators / thousand separators) |
| 58 | + QLocale::setDefault( QLocale::c() ); |
| 59 | + |
| 60 | + mQgisApp = new QgisApp(); |
| 61 | +} |
| 62 | + |
| 63 | +void TestQgsMapToolReshape::cleanupTestCase() |
| 64 | +{ |
| 65 | + QgsApplication::exitQgis(); |
| 66 | +} |
| 67 | + |
| 68 | +void TestQgsMapToolReshape::init() |
| 69 | +{ |
| 70 | +} |
| 71 | + |
| 72 | +void TestQgsMapToolReshape::cleanup() |
| 73 | +{ |
| 74 | +} |
| 75 | + |
| 76 | +void TestQgsMapToolReshape::reshapeWithBindingLine() |
| 77 | +{ |
| 78 | + // prepare vector layer |
| 79 | + QgsVectorLayer *vl = new QgsVectorLayer( QStringLiteral( "LineString?crs=epsg:4326&field=name:string(20)" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) ); |
| 80 | + |
| 81 | + QgsGeometry *g0 = QgsGeometry::fromWkt( "LineString (0 0, 1 1, 1 2)" ); |
| 82 | + QgsFeature f0; |
| 83 | + f0.setGeometry( g0 ); |
| 84 | + f0.setAttribute( 0, "polyline0" ); |
| 85 | + |
| 86 | + QgsGeometry *g1 = QgsGeometry::fromWkt( "LineString (2 1, 3 2, 3 3, 2 2)" ); |
| 87 | + QgsFeature f1; |
| 88 | + f1.setGeometry( g1 ); |
| 89 | + f1.setAttribute( 0, "polyline1" ); |
| 90 | + |
| 91 | + vl->dataProvider()->addFeatures( QgsFeatureList() << f0 << f1 ); |
| 92 | + |
| 93 | + std::cout << "Feature count: " << vl->featureCount() << std::endl; |
| 94 | + |
| 95 | + // prepare canvas |
| 96 | + QList<QgsMapLayer *> layers; |
| 97 | + layers.append( vl ); |
| 98 | + |
| 99 | + QgsCoordinateReferenceSystem srs( 4326, QgsCoordinateReferenceSystem::EpsgCrsId ); |
| 100 | + QgsMapLayerRegistry::instance()->addMapLayer( vl ); |
| 101 | + mQgisApp->mapCanvas()->setDestinationCrs( srs ); |
| 102 | + mQgisApp->mapCanvas()->setCurrentLayer( vl ); |
| 103 | + |
| 104 | + // reshape to add line to polyline0 |
| 105 | + QgsLineStringV2 cl0; |
| 106 | + cl0.setPoints( QgsPointSequenceV2() << QgsPointV2( 1, 2 ) << QgsPointV2( 2, 1 ) ); |
| 107 | + |
| 108 | + QgsAbstractGeometryV2 *curveGgeom0 = cl0.toCurveType(); |
| 109 | + QgsCompoundCurveV2 curve0; |
| 110 | + curve0.fromWkt( curveGgeom0->asWkt() ); |
| 111 | + |
| 112 | + QgsMapToolReshape tool0( mQgisApp->mapCanvas() ); |
| 113 | + tool0.mCaptureCurve = curve0; |
| 114 | + |
| 115 | + vl->startEditing(); |
| 116 | + tool0.reshape( vl ); |
| 117 | + |
| 118 | + vl->getFeatures( QgsFeatureRequest().setFilterFid( 1 ) ).nextFeature( f0 ); |
| 119 | + QCOMPARE( f0.geometry()->exportToWkt(), QStringLiteral( "LineString (0 0, 1 1, 1 2, 2 1)" ) ); |
| 120 | + |
| 121 | + vl->getFeatures( QgsFeatureRequest().setFilterFid( 2 ) ).nextFeature( f1 ); |
| 122 | + QCOMPARE( f1.geometry()->exportToWkt(), QStringLiteral( "LineString (2 1, 3 2, 3 3, 2 2)" ) ); |
| 123 | + |
| 124 | + vl->rollBack(); |
| 125 | + |
| 126 | + // reshape to add line to polyline1 |
| 127 | + QgsLineStringV2 cl1; |
| 128 | + cl1.setPoints( QgsPointSequenceV2() << QgsPointV2( 2, 1 ) << QgsPointV2( 1, 2 ) ); |
| 129 | + |
| 130 | + QgsAbstractGeometryV2 *curveGeom1 = cl1.toCurveType(); |
| 131 | + QgsCompoundCurveV2 curve1; |
| 132 | + curve1.fromWkt( curveGeom1->asWkt() ); |
| 133 | + |
| 134 | + QgsMapToolReshape tool1( mQgisApp->mapCanvas() ); |
| 135 | + tool1.mCaptureCurve = curve1; |
| 136 | + |
| 137 | + vl->startEditing(); |
| 138 | + tool1.reshape( vl ); |
| 139 | + |
| 140 | + vl->getFeatures( QgsFeatureRequest().setFilterFid( 1 ) ).nextFeature( f0 ); |
| 141 | + QCOMPARE( f0.geometry()->exportToWkt(), QStringLiteral( "LineString (0 0, 1 1, 1 2)" ) ); |
| 142 | + |
| 143 | + vl->getFeatures( QgsFeatureRequest().setFilterFid( 2 ) ).nextFeature( f1 ); |
| 144 | + QCOMPARE( f1.geometry()->exportToWkt(), QStringLiteral( "LineString (1 2, 2 1, 3 2, 3 3, 2 2)" ) ); |
| 145 | + |
| 146 | + vl->rollBack(); |
| 147 | +} |
| 148 | + |
| 149 | +QTEST_MAIN( TestQgsMapToolReshape ) |
| 150 | +#include "testqgsmaptoolreshape.moc" |
0 commit comments