1 /***************************************************************************
2      testqgsquickidentifykit.cpp
3      --------------------------------------
4   Date                 : May 2018
5   Copyright            : (C) 2018 by Viktor Sklencar
6   Email                : vsklencar at gmail 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 <QObject>
16 #include <QApplication>
17 #include <QDesktopWidget>
18 
19 #include "qgsapplication.h"
20 #include "qgstest.h"
21 #include "qgis.h"
22 
23 #include "qgsvectorlayer.h"
24 #include "qgsfeature.h"
25 #include "qgsgeometry.h"
26 #include "qgsvectordataprovider.h"
27 
28 #include "qgsquickmapcanvasmap.h"
29 #include "qgsquickidentifykit.h"
30 
31 
32 class TestQgsQuickScaleBarKit: public QObject
33 {
34     Q_OBJECT
35   private slots:
init()36     void init() {} // will be called before each testfunction is executed.
cleanup()37     void cleanup() {} // will be called after every testfunction.
38 
39     void identifyOne(); // tests identifyOne function without given layer
40     void identifyOneDefinedVector(); // tests identifyOne function with given layer
41     void identifyInRadius();
42 };
43 
identifyOne()44 void TestQgsQuickScaleBarKit::identifyOne()
45 {
46   QgsCoordinateReferenceSystem crsGPS = QgsCoordinateReferenceSystem::fromEpsgId( 4326 );
47   QVERIFY( crsGPS.authid() == "EPSG:4326" );
48 
49   QgsRectangle extent = QgsRectangle( -120, 23, -82, 47 );
50   QgsQuickMapCanvasMap canvas;
51 
52   QgsVectorLayer *tempLayer = new QgsVectorLayer( QStringLiteral( "Point?crs=epsg:4326" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) );
53   QVERIFY( tempLayer->isValid() );
54 
55   QgsQuickMapSettings *ms = canvas.mapSettings();
56   ms->setDestinationCrs( crsGPS );
57   ms->setExtent( extent );
58   ms->setOutputSize( QSize( 1000, 500 ) );
59   ms->setLayers( QList<QgsMapLayer *>() << tempLayer );
60 
61   QgsQuickIdentifyKit kit;
62   kit.setMapSettings( ms );
63 
64   double pointX = -31.208;
65   double pointY = 20.407999999999998;
66   double pointX2 = pointX + 0.5;
67 
68   // add feature
69   QgsFeature f1( tempLayer->dataProvider()->fields(), 1 );
70   QgsPointXY point( pointX, pointY );
71   QgsGeometry geom = QgsGeometry::fromPointXY( point ) ;
72   f1.setGeometry( geom );
73 
74   // add another feature
75   QgsFeature f2( tempLayer->dataProvider()->fields(), 1 );
76   QgsPointXY point2( pointX2, pointY );
77   QgsGeometry geom2 = QgsGeometry::fromPointXY( point2 ) ;
78   f2.setGeometry( geom2 );
79 
80   tempLayer->dataProvider()->addFeatures( QgsFeatureList() << f1 << f2 );
81 
82   // exactly matches f1 point
83   QgsPointXY screenPoint( 1954.0, 554.0 );
84   QgsQuickFeatureLayerPair identifiedFeature = kit.identifyOne( screenPoint.toQPointF() );
85   QVERIFY( identifiedFeature.isValid() );
86   QVERIFY( identifiedFeature.feature().geometry().asPoint() == point );
87 }
88 
identifyOneDefinedVector()89 void TestQgsQuickScaleBarKit::identifyOneDefinedVector()
90 {
91   QgsCoordinateReferenceSystem crsGPS = QgsCoordinateReferenceSystem::fromEpsgId( 4326 );
92   QVERIFY( crsGPS.authid() == "EPSG:4326" );
93 
94   QgsRectangle extent = QgsRectangle( -120, 23, -82, 47 );
95   QgsQuickMapCanvasMap canvas;
96 
97   QgsVectorLayer *tempLayer = new QgsVectorLayer( QStringLiteral( "Point?crs=epsg:4326" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) );
98   QVERIFY( tempLayer->isValid() );
99 
100   QgsVectorLayer *tempLayer2 = new QgsVectorLayer( QStringLiteral( "Point?crs=epsg:4326" ), QStringLiteral( "vl2" ), QStringLiteral( "memory" ) );
101   QVERIFY( tempLayer->isValid() );
102 
103   QgsQuickMapSettings *ms = canvas.mapSettings();
104   ms->setDestinationCrs( crsGPS );
105   ms->setExtent( extent );
106   ms->setOutputSize( QSize( 1000, 500 ) );
107   ms->setLayers( QList<QgsMapLayer *>() << tempLayer );
108 
109   QgsQuickIdentifyKit kit;
110   kit.setMapSettings( ms );
111 
112   double pointX = -31.208;
113   double pointY = 20.407999999999998;
114   double pointX2 = pointX + 0.5;
115 
116   // add feature
117   QgsFeature f1( tempLayer->dataProvider()->fields(), 1 );
118   QgsPointXY point( pointX, pointY );
119   QgsGeometry geom = QgsGeometry::fromPointXY( point ) ;
120   f1.setGeometry( geom );
121 
122   // add another feature
123   QgsFeature f2( tempLayer2->dataProvider()->fields(), 1 );
124   QgsPointXY point2( pointX2, pointY );
125   QgsGeometry geom2 = QgsGeometry::fromPointXY( point2 ) ;
126   f2.setGeometry( geom2 );
127 
128   tempLayer->dataProvider()->addFeatures( QgsFeatureList() << f1 );
129   tempLayer2->dataProvider()->addFeatures( QgsFeatureList() << f2 );
130 
131   QgsPointXY screenPoint( 1954.0, 554.0 );
132   QgsQuickFeatureLayerPair identifiedFeature = kit.identifyOne( screenPoint.toQPointF(), tempLayer2 );
133   QVERIFY( identifiedFeature.isValid() );
134   QVERIFY( identifiedFeature.feature().geometry().asPoint() == point2 );
135 
136 }
137 
identifyInRadius()138 void TestQgsQuickScaleBarKit::identifyInRadius()
139 {
140   QgsCoordinateReferenceSystem crsGPS = QgsCoordinateReferenceSystem::fromEpsgId( 4326 );
141   QVERIFY( crsGPS.authid() == "EPSG:4326" );
142 
143   QgsRectangle extent = QgsRectangle( -120, 23, -82, 47 );
144   QgsQuickMapCanvasMap canvas;
145 
146   QgsVectorLayer *tempLayer = new QgsVectorLayer( QStringLiteral( "Point?crs=epsg:4326" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) );
147   QVERIFY( tempLayer->isValid() );
148 
149   QgsQuickMapSettings *ms = canvas.mapSettings();
150   ms->setDestinationCrs( crsGPS );
151   ms->setExtent( extent );
152   ms->setOutputSize( QSize( 1000, 500 ) );
153   ms->setLayers( QList<QgsMapLayer *>() << tempLayer );
154 
155   QgsQuickIdentifyKit kit;
156   kit.setMapSettings( ms );
157 
158   double pointX = -31.208;
159   double pointY = 20.407999999999998;
160   double pointX2 = pointX + 5;
161 
162   QgsFeature f1( tempLayer->dataProvider()->fields(), 1 );
163   QgsPointXY point( pointX, pointY );
164   QgsGeometry geom = QgsGeometry::fromPointXY( point ) ;
165   f1.setGeometry( geom );
166 
167   QgsFeature f2( tempLayer->dataProvider()->fields(), 1 );
168   QgsPointXY point2( pointX2, pointY );
169   QgsGeometry geom2 = QgsGeometry::fromPointXY( point2 ) ;
170   f2.setGeometry( geom2 );
171 
172   tempLayer->dataProvider()->addFeatures( QgsFeatureList() << f1 << f2 );
173 
174   kit.setSearchRadiusMm( 1.0 );
175   QgsPointXY screenPoint( 1954.0, 554.0 );
176   QgsQuickFeatureLayerPairs res = kit.identify( screenPoint.toQPointF() );
177   QVERIFY( res.size() == 1 );
178 
179   kit.setSearchRadiusMm( 100.0 );
180   res = kit.identify( screenPoint.toQPointF() );
181   QVERIFY( res.size() == 2 );
182 }
183 
184 QGSTEST_MAIN( TestQgsQuickScaleBarKit )
185 #include "testqgsquickidentifykit.moc"
186