/
testqgsmapsettings.cpp
662 lines (544 loc) · 25.4 KB
/
testqgsmapsettings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/***************************************************************************
testqgsmapsettings.cpp
--------------------------------------
Date : Tue 6 Feb 2015
Copyright : (C) 2014 by Sandro Santilli
Email : strk@keybit.net
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgstest.h"
#include <QObject>
#include <QString>
#include <cmath>
//header for class being tested
#include "qgsexpression.h"
#include "qgsexpressioncontext.h"
#include "qgsrectangle.h"
#include "qgsmapsettings.h"
#include "qgspointxy.h"
#include "qgslogger.h"
#include "qgsapplication.h"
#include "qgsmaplayerlistutils.h"
#include "qgsvectorlayer.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsexpressioncontextutils.h"
#include "qgsrenderedfeaturehandlerinterface.h"
class TestHandler : public QgsRenderedFeatureHandlerInterface
{
public:
void handleRenderedFeature( const QgsFeature &, const QgsGeometry &, const QgsRenderedFeatureHandlerInterface::RenderedFeatureContext & ) override {}
};
class TestQgsMapSettings: public QObject
{
Q_OBJECT
private slots:
void initTestCase();
void cleanupTestCase();
void testDefaults();
void testGettersSetters();
void testLabelingEngineSettings();
void visibleExtent();
void extentBuffer();
void mapUnitsPerPixel();
void testDevicePixelRatio();
void visiblePolygon();
void testIsLayerVisible();
void testMapLayerListUtils();
void testXmlReadWrite();
void testSetLayers();
void testLabelBoundary();
void testExpressionContext();
void testRenderedFeatureHandlers();
void testCustomRenderingFlags();
void testClippingRegions();
void testComputeExtentForScale();
void testComputeScaleForExtent();
private:
QString toString( const QPolygonF &p, int decimalPlaces = 2 ) const;
};
void TestQgsMapSettings::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();
QgsApplication::showSettings();
}
void TestQgsMapSettings::cleanupTestCase()
{
QgsApplication::exitQgis();
}
QString TestQgsMapSettings::toString( const QPolygonF &p, int dec ) const
{
QString s;
const char *sep = "";
double r = std::pow( 10.0, dec );
for ( int i = 0; i < p.size(); ++i )
{
s += QStringLiteral( "%1%2 %3" ).arg( sep )
.arg( int( p[i].x() * r ) / r )
.arg( int( p[i].y() * r ) / r );
sep = ",";
}
return s;
}
void TestQgsMapSettings::testDefaults()
{
QgsMapSettings ms;
QCOMPARE( ms.destinationCrs(), QgsCoordinateReferenceSystem() );
}
void TestQgsMapSettings::testGettersSetters()
{
// basic getter/setter tests
QgsMapSettings ms;
ms.setTextRenderFormat( QgsRenderContext::TextFormatAlwaysText );
QCOMPARE( ms.textRenderFormat(), QgsRenderContext::TextFormatAlwaysText );
ms.setTextRenderFormat( QgsRenderContext::TextFormatAlwaysOutlines );
QCOMPARE( ms.textRenderFormat(), QgsRenderContext::TextFormatAlwaysOutlines );
// must default to no simplification
QCOMPARE( ms.simplifyMethod().simplifyHints(), QgsVectorSimplifyMethod::NoSimplification );
QgsVectorSimplifyMethod simplify;
simplify.setSimplifyHints( QgsVectorSimplifyMethod::GeometrySimplification );
ms.setSimplifyMethod( simplify );
QCOMPARE( ms.simplifyMethod().simplifyHints(), QgsVectorSimplifyMethod::GeometrySimplification );
QVERIFY( ms.zRange().isInfinite() );
ms.setZRange( QgsDoubleRange( 1, 10 ) );
QCOMPARE( ms.zRange(), QgsDoubleRange( 1, 10 ) );
}
void TestQgsMapSettings::testLabelingEngineSettings()
{
// test that setting labeling engine settings for QgsMapSettings works
QgsMapSettings ms;
QgsLabelingEngineSettings les;
les.setMaximumLineCandidatesPerCm( 4 );
les.setMaximumPolygonCandidatesPerCmSquared( 8.0 );
ms.setLabelingEngineSettings( les );
QCOMPARE( ms.labelingEngineSettings().maximumLineCandidatesPerCm(), 4.0 );
QCOMPARE( ms.labelingEngineSettings().maximumPolygonCandidatesPerCmSquared(), 8.0 );
// ensure that setting labeling engine settings also sets text format
les.setDefaultTextRenderFormat( QgsRenderContext::TextFormatAlwaysText );
ms.setLabelingEngineSettings( les );
QCOMPARE( ms.textRenderFormat(), QgsRenderContext::TextFormatAlwaysText );
les.setDefaultTextRenderFormat( QgsRenderContext::TextFormatAlwaysOutlines );
ms.setLabelingEngineSettings( les );
QCOMPARE( ms.textRenderFormat(), QgsRenderContext::TextFormatAlwaysOutlines );
// but we should be able to override this manually
ms.setTextRenderFormat( QgsRenderContext::TextFormatAlwaysText );
QCOMPARE( ms.textRenderFormat(), QgsRenderContext::TextFormatAlwaysText );
QCOMPARE( ms.labelingEngineSettings().defaultTextRenderFormat(), QgsRenderContext::TextFormatAlwaysText );
}
void TestQgsMapSettings::visibleExtent()
{
QgsMapSettings ms;
ms.setExtent( QgsRectangle( 0, 0, 100, 100 ) );
ms.setOutputSize( QSize( 100, 50 ) );
QCOMPARE( ms.visibleExtent().toString( 0 ), QString( "-50,0 : 150,100" ) );
ms.setExtent( QgsRectangle( 0, 0, 100, 100 ) );
ms.setOutputSize( QSize( 100, 100 ) );
QCOMPARE( ms.visibleExtent().toString( 0 ), QString( "0,0 : 100,100" ) );
ms.setExtent( QgsRectangle( 0, 0, 100, 100 ) );
ms.setOutputSize( QSize( 50, 100 ) );
QCOMPARE( ms.visibleExtent().toString( 0 ), QString( "0,-50 : 100,150" ) );
ms.setExtent( QgsRectangle( 0, 0, 100, 100 ) );
ms.setOutputSize( QSize( 50, 100 ) );
ms.setRotation( 90 );
QCOMPARE( ms.visibleExtent().toString( 0 ), QString( "-50,0 : 150,100" ) );
ms.setRotation( -90 );
QCOMPARE( ms.visibleExtent().toString( 0 ), QString( "-50,0 : 150,100" ) );
ms.setExtent( QgsRectangle( 0, 0, 100, 50 ) );
ms.setOutputSize( QSize( 50, 100 ) );
ms.setRotation( 0 );
QCOMPARE( ms.visibleExtent().toString( 0 ), QString( "0,-75 : 100,125" ) );
ms.setRotation( 90 );
QCOMPARE( ms.visibleExtent().toString( 0 ), QString( "-50,-25 : 150,75" ) );
ms.setRotation( -90 );
QCOMPARE( ms.visibleExtent().toString( 0 ), QString( "-50,-25 : 150,75" ) );
ms.setRotation( 45 );
QCOMPARE( ms.visibleExtent().toString( 0 ), QString( "-56,-81 : 156,131" ) );
}
void TestQgsMapSettings::extentBuffer()
{
QgsMapSettings ms;
ms.setExtent( QgsRectangle( 50, 50, 100, 100 ) );
ms.setOutputSize( QSize( 50, 50 ) );
ms.setExtentBuffer( 10 );
QgsRectangle visibleExtent = ms.visibleExtent();
visibleExtent.grow( ms.extentBuffer() );
QCOMPARE( visibleExtent.toString( 0 ), QString( "40,40 : 110,110" ) );
}
void TestQgsMapSettings::mapUnitsPerPixel()
{
QgsMapSettings ms;
ms.setExtent( QgsRectangle( 0, 0, 100, 100 ) );
ms.setOutputSize( QSize( 100, 50 ) );
QCOMPARE( ms.mapUnitsPerPixel(), 2.0 );
ms.setOutputSize( QSize( 100, 100 ) );
QCOMPARE( ms.mapUnitsPerPixel(), 1.0 );
ms.setOutputSize( QSize( 50, 100 ) );
QCOMPARE( ms.mapUnitsPerPixel(), 2.0 );
ms.setOutputSize( QSize( 5000, 1000 ) );
QCOMPARE( ms.mapUnitsPerPixel(), 0.1 );
ms.setOutputSize( QSize( 1000, 500 ) );
QCOMPARE( ms.mapUnitsPerPixel(), 0.2 );
}
void TestQgsMapSettings::testDevicePixelRatio()
{
QgsMapSettings ms;
ms.setOutputSize( QSize( 100, 50 ) );
ms.setExtent( QgsRectangle( 0, 0, 100, 100 ) );
ms.setDevicePixelRatio( 1 );
double scale = ms.scale();
ms.setDevicePixelRatio( 1.5 );
ms.setExtent( QgsRectangle( 0, 0, 100, 100 ) );
QCOMPARE( ms.outputSize() * 1.5, ms.deviceOutputSize() );
QCOMPARE( scale * 1.5, ms.scale() );
}
void TestQgsMapSettings::visiblePolygon()
{
QgsMapSettings ms;
ms.setExtent( QgsRectangle( 0, 0, 100, 100 ) );
ms.setOutputSize( QSize( 100, 50 ) );
QCOMPARE( toString( ms.visiblePolygon() ),
QString( "-50 100,150 100,150 0,-50 0" ) );
ms.setExtent( QgsRectangle( 0, -50, 100, 0 ) );
ms.setOutputSize( QSize( 100, 50 ) );
ms.setRotation( 90 );
QCOMPARE( toString( ms.visiblePolygon() ),
QString( "25 -75,25 25,75 25,75 -75" ) );
ms.setRotation( -90 );
QCOMPARE( toString( ms.visiblePolygon() ),
QString( "75 25,75 -75,25 -75,25 25" ) );
ms.setRotation( 30 );
QCOMPARE( toString( ms.visiblePolygon() ),
QString( "-5.8 -28.34,80.8 21.65,105.8 -21.65,19.19 -71.65" ) );
ms.setRotation( -30 );
QCOMPARE( toString( ms.visiblePolygon() ),
QString( "19.19 21.65,105.8 -28.34,80.8 -71.65,-5.8 -21.65" ) );
ms.setRotation( 45 );
QCOMPARE( toString( ms.visiblePolygon() ),
QString( "-3.03 -42.67,67.67 28.03,103.03 -7.32,32.32 -78.03" ) );
ms.setRotation( -45 );
QCOMPARE( toString( ms.visiblePolygon() ),
QString( "32.32 28.03,103.03 -42.67,67.67 -78.03,-3.03 -7.32" ) );
}
void TestQgsMapSettings::testIsLayerVisible()
{
QgsVectorLayer *vlA = new QgsVectorLayer( QStringLiteral( "Point" ), QStringLiteral( "a" ), QStringLiteral( "memory" ) );
QgsVectorLayer *vlB = new QgsVectorLayer( QStringLiteral( "Point" ), QStringLiteral( "b" ), QStringLiteral( "memory" ) );
QgsVectorLayer *vlC = new QgsVectorLayer( QStringLiteral( "Point" ), QStringLiteral( "c" ), QStringLiteral( "memory" ) );
vlC->setScaleBasedVisibility( true );
vlC->setMinimumScale( 100 );
vlC->setMaximumScale( 0 );
QList<QgsMapLayer *> layers;
layers << vlA << vlB << vlC;
QgsProject::instance()->addMapLayers( layers );
QgsMapSettings ms;
ms.setLayers( layers );
ms.setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3148" ) ) );
ms.setExtent( QgsRectangle( 100, 100, 200, 200 ) );
ms.setOutputSize( QSize( 100, 100 ) ); // this results in a scale roughly equal to 3779
QgsExpressionContext context;
context << QgsExpressionContextUtils::mapSettingsScope( ms );
// test checking for visible layer by id
QgsExpression e( QStringLiteral( "is_layer_visible( '%1' )" ).arg( vlA->id() ) );
QVariant r = e.evaluate( &context );
QCOMPARE( r.toBool(), true );
// test checking for visible layer by direct map layer object
QgsExpression e4( QStringLiteral( "is_layer_visible(array_get( @map_layers, 0 ) )" ) );
r = e4.evaluate( &context );
QCOMPARE( r.toBool(), true );
// test checking for visible layer by name
QgsExpression e2( QStringLiteral( "is_layer_visible( '%1' )" ).arg( vlB->name() ) );
r = e2.evaluate( &context );
QCOMPARE( r.toBool(), true );
// test checking for visible layer taking into account scale-based visibility
QgsExpression e5( QStringLiteral( "is_layer_visible( '%1' )" ).arg( vlC->name() ) );
r = e5.evaluate( &context );
QCOMPARE( r.toBool(), false );
vlC->setMinimumScale( 100000 );
QgsExpressionContext context2;
context2 << QgsExpressionContextUtils::mapSettingsScope( ms );
QgsExpression e6( QStringLiteral( "is_layer_visible( '%1' )" ).arg( vlC->name() ) );
r = e6.evaluate( &context2 );
QCOMPARE( r.toBool(), true );
vlC->setMinimumScale( 0 );
vlC->setMaximumScale( 5000 );
QgsExpressionContext context3;
context3 << QgsExpressionContextUtils::mapSettingsScope( ms );
QgsExpression e7( QStringLiteral( "is_layer_visible( '%1' )" ).arg( vlC->name() ) );
r = e7.evaluate( &context3 );
QCOMPARE( r.toBool(), false );
vlC->setMaximumScale( 200 );
QgsExpressionContext context4;
context4 << QgsExpressionContextUtils::mapSettingsScope( ms );
QgsExpression e8( QStringLiteral( "is_layer_visible( '%1' )" ).arg( vlC->name() ) );
r = e8.evaluate( &context4 );
QCOMPARE( r.toBool(), true );
// test checking for non-existent layer
QgsExpression e3( QStringLiteral( "is_layer_visible( 'non matching name' )" ) );
r = e3.evaluate( &context );
QCOMPARE( r.toBool(), false );
QgsProject::instance()->removeMapLayer( vlA );
e.prepare( &context );
r = e.evaluate( &context );
QCOMPARE( r.toBool(), false ); // layer is deleted
e2.prepare( &context );
r = e2.evaluate( &context );
QCOMPARE( r.toBool(), true ); // layer still exists
QgsProject::instance()->removeMapLayer( vlB );
e2.prepare( &context );
r = e2.evaluate( &context );
QCOMPARE( r.toBool(), false ); // layer is deleted
}
void TestQgsMapSettings::testMapLayerListUtils()
{
QgsVectorLayer *vlA = new QgsVectorLayer( QStringLiteral( "Point" ), QStringLiteral( "a" ), QStringLiteral( "memory" ) );
QgsVectorLayer *vlB = new QgsVectorLayer( QStringLiteral( "Point" ), QStringLiteral( "b" ), QStringLiteral( "memory" ) );
QList<QgsMapLayer *> listRawSource;
listRawSource << vlA << vlB;
QgsMapLayer *l = _qgis_findLayer( listRawSource, QStringLiteral( "a" ) );
QCOMPARE( l, vlA );
l = _qgis_findLayer( listRawSource, QStringLiteral( "z" ) );
QCOMPARE( !l, true );
QgsWeakMapLayerPointerList listQPointer = _qgis_listRawToQPointer( listRawSource );
QCOMPARE( listQPointer.count(), 2 );
QCOMPARE( listQPointer[0].data(), vlA );
QCOMPARE( listQPointer[1].data(), vlB );
QList<QgsMapLayer *> listRaw = _qgis_listQPointerToRaw( listQPointer );
QCOMPARE( listRaw.count(), 2 );
QCOMPARE( listRaw[0], vlA );
QCOMPARE( listRaw[1], vlB );
QStringList listIDs = _qgis_listQPointerToIDs( listQPointer );
QCOMPARE( listIDs.count(), 2 );
QCOMPARE( listIDs[0], vlA->id() );
QCOMPARE( listIDs[1], vlB->id() );
// now delete one layer!
// QPointer to vlA must get invalidated
delete vlA;
QCOMPARE( listQPointer.count(), 2 ); // still two items but one is invalid
QList<QgsMapLayer *> listRaw2 = _qgis_listQPointerToRaw( listQPointer );
QCOMPARE( listRaw2.count(), 1 );
QCOMPARE( listRaw2[0], vlB );
QStringList listIDs2 = _qgis_listQPointerToIDs( listQPointer );
QCOMPARE( listIDs2.count(), 1 );
QCOMPARE( listIDs2[0], vlB->id() );
delete vlB;
}
void TestQgsMapSettings::testXmlReadWrite()
{
//create a test dom element
QDomImplementation DomImplementation;
QDomDocumentType documentType =
DomImplementation.createDocumentType(
QStringLiteral( "qgis" ), QStringLiteral( "http://mrcc.com/qgis.dtd" ), QStringLiteral( "SYSTEM" ) );
QDomDocument doc( documentType );
QDomElement element = doc.createElement( QStringLiteral( "s" ) );
//create a map settings object
QgsMapSettings s1;
s1.setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3111" ) ) );
//write to xml
s1.writeXml( element, doc );
// read a copy from xml
QgsMapSettings s2;
s2.readXml( element );
QCOMPARE( s2.destinationCrs().authid(), QStringLiteral( "EPSG:3111" ) );
// test writing a map settings without a valid CRS
element = doc.createElement( QStringLiteral( "s" ) );
s1.setDestinationCrs( QgsCoordinateReferenceSystem() );
s1.writeXml( element, doc );
s2.readXml( element );
QVERIFY( !s2.destinationCrs().isValid() );
}
void TestQgsMapSettings::testSetLayers()
{
std::unique_ptr< QgsVectorLayer > vlA = std::make_unique< QgsVectorLayer >( QStringLiteral( "Point" ), QStringLiteral( "a" ), QStringLiteral( "memory" ) );
std::unique_ptr< QgsVectorLayer > vlB = std::make_unique< QgsVectorLayer >( QStringLiteral( "Point" ), QStringLiteral( "b" ), QStringLiteral( "memory" ) );
std::unique_ptr< QgsVectorLayer > nonSpatial = std::make_unique< QgsVectorLayer >( QStringLiteral( "none" ), QStringLiteral( "a" ), QStringLiteral( "memory" ) );
QgsMapSettings ms;
ms.setLayers( QList< QgsMapLayer * >() << vlA.get() );
QCOMPARE( ms.layers(), QList< QgsMapLayer * >() << vlA.get() );
ms.setLayers( QList< QgsMapLayer * >() << vlB.get() << vlA.get() );
QCOMPARE( ms.layers(), QList< QgsMapLayer * >() << vlB.get() << vlA.get() );
// non spatial and null layers should be stripped
ms.setLayers( QList< QgsMapLayer * >() << vlA.get() << nonSpatial.get() << nullptr << vlB.get() );
QCOMPARE( ms.layers(), QList< QgsMapLayer * >() << vlA.get() << vlB.get() );
}
void TestQgsMapSettings::testLabelBoundary()
{
QgsMapSettings ms;
QVERIFY( ms.labelBoundaryGeometry().isNull() );
ms.setLabelBoundaryGeometry( QgsGeometry::fromWkt( QStringLiteral( "Polygon(( 0 0, 1 0, 1 1, 0 1, 0 0 ))" ) ) );
QCOMPARE( ms.labelBoundaryGeometry().asWkt(), QStringLiteral( "Polygon ((0 0, 1 0, 1 1, 0 1, 0 0))" ) );
}
void TestQgsMapSettings::testExpressionContext()
{
QgsMapSettings ms;
QgsExpressionContext c;
QVariant r;
ms.setOutputSize( QSize( 5000, 5000 ) );
ms.setExtent( QgsRectangle( -1, 0, 2, 2 ) );
ms.setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
ms.setRotation( -32 );
c << QgsExpressionContextUtils::mapSettingsScope( ms );
QgsExpression e( QStringLiteral( "@map_scale" ) );
r = e.evaluate( &c );
QGSCOMPARENEAR( r.toDouble(), 247990, 10.0 );
// The old $scale function should silently map to @map_scale, so that older projects work without change
e = QgsExpression( QStringLiteral( "$scale" ) );
r = e.evaluate( &c );
QGSCOMPARENEAR( r.toDouble(), 247990, 10.0 );
// no map settings scope -- $scale is meaningless
e = QgsExpression( QStringLiteral( "$scale" ) );
r = e.evaluate( nullptr );
QVERIFY( !r.isValid() );
e = QgsExpression( QStringLiteral( "@map_id" ) );
r = e.evaluate( &c );
QCOMPARE( r.toString(), QStringLiteral( "canvas" ) );
e = QgsExpression( QStringLiteral( "@map_rotation" ) );
r = e.evaluate( &c );
QCOMPARE( r.toDouble(), -32.0 );
ms.setRotation( 0 );
c << QgsExpressionContextUtils::mapSettingsScope( ms );
e = QgsExpression( QStringLiteral( "geom_to_wkt( @map_extent )" ) );
r = e.evaluate( &c );
QCOMPARE( r.toString(), QStringLiteral( "Polygon ((-1 -0.5, 2 -0.5, 2 2.5, -1 2.5, -1 -0.5))" ) );
e = QgsExpression( QStringLiteral( "@map_extent_width" ) );
r = e.evaluate( &c );
QCOMPARE( r.toDouble(), 3.0 );
e = QgsExpression( QStringLiteral( "@map_extent_height" ) );
r = e.evaluate( &c );
QCOMPARE( r.toDouble(), 3.0 );
e = QgsExpression( QStringLiteral( "geom_to_wkt( @map_extent_center )" ) );
r = e.evaluate( &c );
QCOMPARE( r.toString(), QStringLiteral( "Point (0.5 1)" ) );
e = QgsExpression( QStringLiteral( "@map_crs" ) );
r = e.evaluate( &c );
QCOMPARE( r.toString(), QStringLiteral( "EPSG:4326" ) );
e = QgsExpression( QStringLiteral( "@map_crs_definition" ) );
r = e.evaluate( &c );
QCOMPARE( r.toString(), QStringLiteral( "+proj=longlat +datum=WGS84 +no_defs" ) );
e = QgsExpression( QStringLiteral( "@map_units" ) );
r = e.evaluate( &c );
QCOMPARE( r.toString(), QStringLiteral( "degrees" ) );
e = QgsExpression( QStringLiteral( "@map_crs_description" ) );
r = e.evaluate( &c );
QCOMPARE( r.toString(), QStringLiteral( "WGS 84" ) );
e = QgsExpression( QStringLiteral( "@map_crs_acronym" ) );
r = e.evaluate( &c );
QCOMPARE( r.toString(), QStringLiteral( "longlat" ) );
QgsExpression e6a( QStringLiteral( "@map_crs_projection" ) );
r = e6a.evaluate( &c );
QCOMPARE( r.toString(), QString( "Lat/long (Geodetic alias)" ) );
e = QgsExpression( QStringLiteral( "@map_crs_proj4" ) );
r = e.evaluate( &c );
QCOMPARE( r.toString(), QStringLiteral( "+proj=longlat +datum=WGS84 +no_defs" ) );
e = QgsExpression( QStringLiteral( "@map_crs_wkt" ) );
r = e.evaluate( &c );
QVERIFY( r.toString().length() > 15 );
e = QgsExpression( QStringLiteral( "@map_crs_ellipsoid" ) );
r = e.evaluate( &c );
QCOMPARE( r.toString(), QStringLiteral( "EPSG:7030" ) );
e = QgsExpression( QStringLiteral( "@map_start_time" ) );
r = e.evaluate( &c );
QVERIFY( !r.isValid() );
e = QgsExpression( QStringLiteral( "@map_end_time" ) );
r = e.evaluate( &c );
QVERIFY( !r.isValid() );
e = QgsExpression( QStringLiteral( "@map_interval" ) );
r = e.evaluate( &c );
QVERIFY( !r.isValid() );
ms.setTemporalRange( QgsDateTimeRange( QDateTime( QDate( 2002, 3, 4 ), QTime( 0, 0, 0 ) ), QDateTime( QDate( 2010, 6, 7 ), QTime( 0, 0, 0 ) ) ) );
c = QgsExpressionContext();
c << QgsExpressionContextUtils::mapSettingsScope( ms );
e = QgsExpression( QStringLiteral( "@map_start_time" ) );
r = e.evaluate( &c );
QCOMPARE( r.toDateTime(), QDateTime( QDate( 2002, 3, 4 ), QTime( 0, 0, 0 ) ) );
e = QgsExpression( QStringLiteral( "@map_end_time" ) );
r = e.evaluate( &c );
QCOMPARE( r.toDateTime(), QDateTime( QDate( 2010, 6, 7 ), QTime( 0, 0, 0 ) ) );
e = QgsExpression( QStringLiteral( "@map_interval" ) );
r = e.evaluate( &c );
QCOMPARE( r.value< QgsInterval >(), QgsInterval( QDateTime( QDate( 2010, 6, 7 ), QTime( 0, 0, 0 ) ) - QDateTime( QDate( 2002, 3, 4 ), QTime( 0, 0, 0 ) ) ) );
}
void TestQgsMapSettings::testRenderedFeatureHandlers()
{
std::unique_ptr< TestHandler > testHandler = std::make_unique< TestHandler >();
std::unique_ptr< TestHandler > testHandler2 = std::make_unique< TestHandler >();
std::unique_ptr< QgsMapSettings> mapSettings = std::make_unique< QgsMapSettings >();
QVERIFY( mapSettings->renderedFeatureHandlers().isEmpty() );
mapSettings->addRenderedFeatureHandler( testHandler.get() );
mapSettings->addRenderedFeatureHandler( testHandler2.get() );
QCOMPARE( mapSettings->renderedFeatureHandlers(), QList< QgsRenderedFeatureHandlerInterface * >() << testHandler.get() << testHandler2.get() );
//ownership should NOT be transferred, i.e. it won't delete the registered handlers upon QgsMapSettings destruction
mapSettings.reset();
// should be no double-delete here
}
void TestQgsMapSettings::testCustomRenderingFlags()
{
QgsMapSettings settings;
settings.setCustomRenderingFlag( QStringLiteral( "myexport" ), true );
settings.setCustomRenderingFlag( QStringLiteral( "omitgeometries" ), QStringLiteral( "points" ) );
QVERIFY( settings.customRenderingFlags()[ QStringLiteral( "myexport" ) ].toBool() == true );
QVERIFY( settings.customRenderingFlags()[ QStringLiteral( "omitgeometries" ) ].toString() == QLatin1String( "points" ) );
// Test deprecated API
Q_NOWARN_DEPRECATED_PUSH
settings.setCustomRenderFlags( QStringLiteral( "myexport;omitpoints" ) );
QVERIFY( settings.customRenderFlags().split( ";" ).contains( QStringLiteral( "myexport" ) ) );
QVERIFY( settings.customRenderFlags().split( ";" ).contains( QStringLiteral( "omitpoints" ) ) );
Q_NOWARN_DEPRECATED_POP
}
void TestQgsMapSettings::testClippingRegions()
{
QgsMapSettings settings;
QVERIFY( settings.clippingRegions().isEmpty() );
QgsMapClippingRegion region( QgsGeometry::fromWkt( QStringLiteral( "Polygon(( 0 0, 1 0 , 1 1 , 0 1, 0 0 ))" ) ) );
settings.addClippingRegion( region );
QCOMPARE( settings.clippingRegions().size(), 1 );
QCOMPARE( settings.clippingRegions().at( 0 ).geometry().asWkt(), QStringLiteral( "Polygon ((0 0, 1 0, 1 1, 0 1, 0 0))" ) );
QgsMapClippingRegion region2( QgsGeometry::fromWkt( QStringLiteral( "Polygon(( 10 0, 11 0 , 11 1 , 10 1, 10 0 ))" ) ) );
settings.addClippingRegion( region2 );
QCOMPARE( settings.clippingRegions().size(), 2 );
QCOMPARE( settings.clippingRegions().at( 0 ).geometry().asWkt(), QStringLiteral( "Polygon ((0 0, 1 0, 1 1, 0 1, 0 0))" ) );
QCOMPARE( settings.clippingRegions().at( 1 ).geometry().asWkt(), QStringLiteral( "Polygon ((10 0, 11 0, 11 1, 10 1, 10 0))" ) );
QgsMapSettings settings2( settings );
QCOMPARE( settings2.clippingRegions().size(), 2 );
QCOMPARE( settings2.clippingRegions().at( 0 ).geometry().asWkt(), QStringLiteral( "Polygon ((0 0, 1 0, 1 1, 0 1, 0 0))" ) );
QCOMPARE( settings2.clippingRegions().at( 1 ).geometry().asWkt(), QStringLiteral( "Polygon ((10 0, 11 0, 11 1, 10 1, 10 0))" ) );
QgsMapSettings settings3;
settings3 = settings;
QCOMPARE( settings3.clippingRegions().size(), 2 );
QCOMPARE( settings3.clippingRegions().at( 0 ).geometry().asWkt(), QStringLiteral( "Polygon ((0 0, 1 0, 1 1, 0 1, 0 0))" ) );
QCOMPARE( settings3.clippingRegions().at( 1 ).geometry().asWkt(), QStringLiteral( "Polygon ((10 0, 11 0, 11 1, 10 1, 10 0))" ) ) ;
settings.setClippingRegions( QList< QgsMapClippingRegion >() << region2 );
QCOMPARE( settings.clippingRegions().size(), 1 );
QCOMPARE( settings.clippingRegions().at( 0 ).geometry().asWkt(), QStringLiteral( "Polygon ((10 0, 11 0, 11 1, 10 1, 10 0))" ) ) ;
}
void TestQgsMapSettings::testComputeExtentForScale()
{
QgsMapSettings settings;
settings.setExtent( QgsRectangle( -500., -500., 500., 500. ) ); // Just to ensure settings are valid
settings.setDestinationCrs( QgsCoordinateReferenceSystem( "EPSG:3857" ) );
settings.setOutputSize( QSize( 1000, 1000 ) );
QgsRectangle rect = settings.computeExtentForScale( QgsPoint( 0, 0 ), 500 );
// [ output width in inches ] * [scale]
double widthInches = settings.outputSize().width() / double( settings.outputDpi() ) * 500;
double widthMapUnits = widthInches * QgsUnitTypes::fromUnitToUnitFactor( QgsUnitTypes::DistanceFeet, settings.mapUnits() ) / 12;
QGSCOMPARENEARRECTANGLE( rect, QgsRectangle( - 0.5 * widthMapUnits, - 0.5 * widthMapUnits, 0.5 * widthMapUnits, 0.5 * widthMapUnits ), 0.0001 );
}
void TestQgsMapSettings::testComputeScaleForExtent()
{
QgsMapSettings settings;
settings.setExtent( QgsRectangle( -500., -500., 500., 500. ) ); // Just to ensure settings are valid
settings.setDestinationCrs( QgsCoordinateReferenceSystem( "EPSG:3857" ) );
settings.setOutputSize( QSize( 1000, 1000 ) );
double scale = settings.computeScaleForExtent( QgsRectangle( -500., -500., 500., 500. ) );
double widthInches = 1000 * QgsUnitTypes::fromUnitToUnitFactor( settings.mapUnits(), QgsUnitTypes::DistanceFeet ) * 12;
double testScale = widthInches * settings.outputDpi() / double( settings.outputSize().width() );
QGSCOMPARENEAR( scale, testScale, 0.001 );
}
QGSTEST_MAIN( TestQgsMapSettings )
#include "testqgsmapsettings.moc"