-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathtestqgslayoutmap.cpp
2062 lines (1834 loc) · 99.2 KB
/
testqgslayoutmap.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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
testqgslayoutmap.cpp
----------------------
begin : October 2017
copyright : (C) 2017 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* 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 "qgsapplication.h"
#include "qgslayout.h"
#include "qgslayoutitemmap.h"
#include "qgsmultibandcolorrenderer.h"
#include "qgsrasterlayer.h"
#include "qgsrasterdataprovider.h"
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"
#include "qgsproject.h"
#include "qgsmapthemecollection.h"
#include "qgsproperty.h"
#include "qgslayoutpagecollection.h"
#include "qgslayoutitempolyline.h"
#include "qgsreadwritecontext.h"
#include "qgsrenderedfeaturehandlerinterface.h"
#include "qgspallabeling.h"
#include "qgsvectorlayerlabeling.h"
#include "qgstemporalrangeobject.h"
#include "qgsfontutils.h"
#include "qgsannotationlayer.h"
#include "qgsannotationmarkeritem.h"
#include "qgslabelingresults.h"
#include "qgslayoutexporter.h"
#include <QObject>
#include "qgstest.h"
class TestQgsLayoutMap : public QgsTest
{
Q_OBJECT
public:
TestQgsLayoutMap() : QgsTest( QStringLiteral( "Layout Map Tests" ), QStringLiteral( "composer_map" ) ) {}
private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void init();// will be called before each testfunction is executed.
void construct(); //test constructor of QgsLayoutItemMap
void id();
void render();
void uniqueId(); //test if map id is adapted when doing copy paste
void worldFileGeneration(); // test world file generation
void zoomToExtent(); // test zoomToExtent method
void mapPolygonVertices(); // test mapPolygon function with no map rotation
void dataDefinedLayers(); //test data defined layer string
void dataDefinedStyles(); //test data defined styles
void dataDefinedCrs(); //test data defined crs
void dataDefinedTemporalRange(); //test data defined temporal range's start and end values
void rasterized();
void layersToRender();
void mapRotation();
void mapItemRotation();
void expressionContext();
void layoutToMapCoordsTransform();
void labelBlockingRegions();
void testSimplificationMethod();
void testRenderedFeatureHandler();
void testLayeredExport();
void testLayeredExportLabelsByLayer();
void testTemporal();
void testLabelResults();
private:
QgsRasterLayer *mRasterLayer = nullptr;
QgsVectorLayer *mPointsLayer = nullptr;
QgsVectorLayer *mPolysLayer = nullptr;
QgsVectorLayer *mLinesLayer = nullptr;
};
void TestQgsLayoutMap::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();
QgsFontUtils::loadStandardTestFonts( QStringList() << QStringLiteral( "Bold" ) );
//create maplayers from testdata and add to layer registry
const QFileInfo rasterFileInfo( QStringLiteral( TEST_DATA_DIR ) + "/landsat.tif" );
mRasterLayer = new QgsRasterLayer( rasterFileInfo.filePath(),
rasterFileInfo.completeBaseName() );
QgsMultiBandColorRenderer *rasterRenderer = new QgsMultiBandColorRenderer( mRasterLayer->dataProvider(), 2, 3, 4 );
mRasterLayer->setRenderer( rasterRenderer );
const QFileInfo pointFileInfo( QStringLiteral( TEST_DATA_DIR ) + "/points.shp" );
mPointsLayer = new QgsVectorLayer( pointFileInfo.filePath(),
pointFileInfo.completeBaseName(), QStringLiteral( "ogr" ) );
const QFileInfo polyFileInfo( QStringLiteral( TEST_DATA_DIR ) + "/polys.shp" );
mPolysLayer = new QgsVectorLayer( polyFileInfo.filePath(),
polyFileInfo.completeBaseName(), QStringLiteral( "ogr" ) );
const QFileInfo lineFileInfo( QStringLiteral( TEST_DATA_DIR ) + "/lines.shp" );
mLinesLayer = new QgsVectorLayer( lineFileInfo.filePath(),
lineFileInfo.completeBaseName(), QStringLiteral( "ogr" ) );
// some layers need to be in project for data-defined layers functionality
QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << mRasterLayer << mPointsLayer << mPolysLayer << mLinesLayer );
}
void TestQgsLayoutMap::cleanupTestCase()
{
QgsApplication::exitQgis();
}
void TestQgsLayoutMap::init()
{
#if 0
//create composition with composer map
mComposition = new QgsComposition( QgsProject::instance() );
mComposition->setPaperSize( 297, 210 ); //A4 landscape
mComposerMap = new QgsComposerMap( mComposition, 20, 20, 200, 100 );
mComposerMap->setFrameEnabled( true );
mComposerMap->setLayers( QList<QgsMapLayer *>() << mRasterLayer );
mComposition->addComposerMap( mComposerMap );
#endif
}
void TestQgsLayoutMap::construct()
{
QgsLayout l( QgsProject::instance( ) );
QgsLayoutItemMap mi( &l );
QCOMPARE( mi.type(), QgsLayoutItemRegistry::LayoutMap );
QVERIFY( mi.extent().isNull() ); // is this correct to expect ?
// WARNING: default values are mostly undefined behavior at
// the time of writing, see
// https://github.com/qgis/QGIS/pull/54827#discussion_r1346928192
//QVERIFY( mi.rect().isNull() ); // is this correct to expect ?
//QCOMPARE( mi.scale(), 0 ); // is this correct ?
// TODO: verify defined state
}
void TestQgsLayoutMap::id()
{
QgsLayout l( QgsProject::instance( ) );
QgsLayoutItemMap *map1 = new QgsLayoutItemMap( &l );
QCOMPARE( map1->displayName(), QStringLiteral( "Map 1" ) );
l.addLayoutItem( map1 );
QgsLayoutItemMap *map2 = new QgsLayoutItemMap( &l );
QCOMPARE( map2->displayName(), QStringLiteral( "Map 2" ) );
l.addLayoutItem( map2 );
map1->setId( "my map" );
QCOMPARE( map1->displayName(), QStringLiteral( "my map" ) );
// existing name should be recycled
l.removeLayoutItem( map1 );
QgsLayoutItemMap *map3 = new QgsLayoutItemMap( &l );
QCOMPARE( map3->displayName(), QStringLiteral( "Map 1" ) );
l.addLayoutItem( map3 );
}
void TestQgsLayoutMap::render()
{
QgsLayout l( QgsProject::instance() );
l.initializeDefaults();
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
map->attemptSetSceneRect( QRectF( 20, 20, 200, 100 ) );
map->setFrameEnabled( true );
map->setLayers( QList<QgsMapLayer *>() << mRasterLayer );
l.addLayoutItem( map );
map->setExtent( QgsRectangle( 781662.375, 3339523.125, 793062.375, 3345223.125 ) );
QGSVERIFYLAYOUTCHECK( QStringLiteral( "composermap_render" ), &l );
}
void TestQgsLayoutMap::uniqueId()
{
QgsLayout l( QgsProject::instance() );
l.initializeDefaults();
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
l.addLayoutItem( map );
QDomDocument doc;
QDomElement documentElement = doc.createElement( QStringLiteral( "ComposerItemClipboard" ) );
map->writeXml( documentElement, doc, QgsReadWriteContext() );
l.addItemsFromXml( documentElement, doc, QgsReadWriteContext() );
//test if both composer maps have different ids
QgsLayoutItemMap *newMap = nullptr;
QList<QgsLayoutItemMap *> mapList;
l.layoutItems( mapList );
for ( auto mapIt = mapList.constBegin() ; mapIt != mapList.constEnd(); ++mapIt )
{
if ( *mapIt != map )
{
newMap = *mapIt;
break;
}
}
QVERIFY( newMap );
const QString oldId = map->displayName();
const QString newId = newMap->displayName();
QVERIFY( oldId != newId );
}
void TestQgsLayoutMap::worldFileGeneration()
{
QgsLayout l( QgsProject::instance() );
l.initializeDefaults();
QgsLayoutItemPage *page2 = new QgsLayoutItemPage( &l );
page2->setPageSize( "A4", QgsLayoutItemPage::Landscape );
l.pageCollection()->addPage( page2 );
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
map->attemptSetSceneRect( QRectF( 20, 20, 200, 100 ) );
map->setFrameEnabled( true );
map->setLayers( QList<QgsMapLayer *>() << mRasterLayer );
l.addLayoutItem( map );
map->setExtent( QgsRectangle( 781662.375, 3339523.125, 793062.375, 3345223.125 ) );
map->setMapRotation( 30.0 );
l.setReferenceMap( map );
const QgsLayoutExporter exporter( &l );
double a, b, c, d, e, f;
exporter.computeWorldFileParameters( a, b, c, d, e, f );
QGSCOMPARENEAR( a, 4.18048, 0.001 );
QGSCOMPARENEAR( b, 2.41331, 0.001 );
QGSCOMPARENEAR( c, 779444, 1 );
QGSCOMPARENEAR( d, 2.4136, 0.001 );
QGSCOMPARENEAR( e, -4.17997, 0.001 );
QGSCOMPARENEAR( f, 3.34241e+06, 1e+03 );
//test with map on second page. Parameters should be the same
map->attemptMove( QgsLayoutPoint( 20, 20 ), true, false, 1 );
exporter.computeWorldFileParameters( a, b, c, d, e, f );
QGSCOMPARENEAR( a, 4.18048, 0.001 );
QGSCOMPARENEAR( b, 2.41331, 0.001 );
QGSCOMPARENEAR( c, 779444, 1 );
QGSCOMPARENEAR( d, 2.4136, 0.001 );
QGSCOMPARENEAR( e, -4.17997, 0.001 );
QGSCOMPARENEAR( f, 3.34241e+06, 1e+03 );
//test computing parameters for specific region
map->attemptMove( QgsLayoutPoint( 20, 20 ), true, false, 1 );
exporter.computeWorldFileParameters( QRectF( 10, 5, 260, 200 ), a, b, c, d, e, f );
QGSCOMPARENEAR( a, 4.18061, 0.001 );
QGSCOMPARENEAR( b, 2.41321, 0.001 );
QGSCOMPARENEAR( c, 773810, 1 );
QGSCOMPARENEAR( d, 2.4137, 0.001 );
QGSCOMPARENEAR( e, -4.1798, 0.001 );
QGSCOMPARENEAR( f, 3.35331e+06, 1e+03 );
}
void TestQgsLayoutMap::zoomToExtent()
{
QgsLayout l( QgsProject::instance( ) );
QgsLayoutItemMap mi( &l );
QVERIFY( mi.extent().isEmpty() );
// Sets extent to new extent, current being empty
mi.zoomToExtent( QgsRectangle( 0, 0, 10, 10 ) );
QCOMPARE( mi.extent(), QgsRectangle( 0, 0, 10, 10 ) );
// Sets extent to new extent but keeps current aspect ratio (1)
mi.zoomToExtent( QgsRectangle( 1, 2, 9, 8 ) );
QCOMPARE( mi.extent(), QgsRectangle( 1, 1, 9, 9 ) );
// Sets extent to new extent but keeps current aspect ratio (1)
mi.zoomToExtent( QgsRectangle( 0, 0, 10, 12 ) );
QCOMPARE( mi.extent(), QgsRectangle( -1, 0, 11, 12 ) );
}
void TestQgsLayoutMap::mapPolygonVertices()
{
QgsLayout l( QgsProject::instance() );
l.initializeDefaults();
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
map->attemptSetSceneRect( QRectF( 20, 20, 200, 100 ) );
map->setFrameEnabled( true );
map->setLayers( QList<QgsMapLayer *>() << mRasterLayer );
l.addLayoutItem( map );
map->setExtent( QgsRectangle( 781662.375, 3339523.125, 793062.375, 3345223.125 ) );
QPolygonF visibleExtent = map->visibleExtentPolygon();
//vertices should be returned in clockwise order starting at the top-left point
QVERIFY( std::fabs( visibleExtent[0].x() - 781662.375 ) < 0.001 );
QVERIFY( std::fabs( visibleExtent[0].y() - 3345223.125 ) < 0.001 );
QVERIFY( std::fabs( visibleExtent[1].x() - 793062.375 ) < 0.001 );
QVERIFY( std::fabs( visibleExtent[1].y() - 3345223.125 ) < 0.001 );
QVERIFY( std::fabs( visibleExtent[2].x() - 793062.375 ) < 0.001 );
QVERIFY( std::fabs( visibleExtent[2].y() - 3339523.125 ) < 0.001 );
QVERIFY( std::fabs( visibleExtent[3].x() - 781662.375 ) < 0.001 );
QVERIFY( std::fabs( visibleExtent[3].y() - 3339523.125 ) < 0.001 );
//polygon should be closed
QVERIFY( visibleExtent.isClosed() );
//now test with rotated map
map->setMapRotation( 10 );
visibleExtent = map->visibleExtentPolygon();
//vertices should be returned in clockwise order starting at the top-left point
QVERIFY( std::fabs( visibleExtent[0].x() - 781254.0735015 ) < 0.001 );
QVERIFY( std::fabs( visibleExtent[0].y() - 3344190.0324834 ) < 0.001 );
QVERIFY( std::fabs( visibleExtent[1].x() - 792480.881886 ) < 0.001 );
QVERIFY( std::fabs( visibleExtent[1].y() - 3346169.62171 ) < 0.001 );
QVERIFY( std::fabs( visibleExtent[2].x() - 793470.676499 ) < 0.001 );
QVERIFY( std::fabs( visibleExtent[2].y() - 3340556.21752 ) < 0.001 );
QVERIFY( std::fabs( visibleExtent[3].x() - 782243.868114 ) < 0.001 );
QVERIFY( std::fabs( visibleExtent[3].y() - 3338576.62829 ) < 0.001 );
//polygon should be closed
QVERIFY( visibleExtent.isClosed() );
map->setMapRotation( 0 );
}
void TestQgsLayoutMap::dataDefinedLayers()
{
QgsLayout l( QgsProject::instance() );
l.initializeDefaults();
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
map->attemptMove( QgsLayoutPoint( 20, 20 ) );
map->attemptResize( QgsLayoutSize( 200, 100 ) );
map->setFrameEnabled( true );
l.addLayoutItem( map );
//test malformed layer set string
map->dataDefinedProperties().setProperty( QgsLayoutObject::MapLayers, QgsProperty::fromExpression( QStringLiteral( "'x'" ) ) );
QList<QgsMapLayer *> result = map->layersToRender();
QVERIFY( result.isEmpty() );
map->dataDefinedProperties().setProperty( QgsLayoutObject::MapLayers, QgsProperty::fromExpression( QStringLiteral( "'x|'" ) ) );
result = map->layersToRender();
QVERIFY( result.isEmpty() );
//test subset of valid layers
map->dataDefinedProperties().setProperty( QgsLayoutObject::MapLayers, QgsProperty::fromExpression(
QStringLiteral( "'%1|%2'" ).arg( mPolysLayer->name(), mRasterLayer->name() ) ) );
result = map->layersToRender();
QCOMPARE( result.count(), 2 );
QVERIFY( result.contains( mPolysLayer ) );
QVERIFY( result.contains( mRasterLayer ) );
//test non-existent layer
map->dataDefinedProperties().setProperty( QgsLayoutObject::MapLayers, QgsProperty::fromExpression(
QStringLiteral( "'x|%1|%2'" ).arg( mLinesLayer->name(), mPointsLayer->name() ) ) );
result = map->layersToRender();
QCOMPARE( result.count(), 2 );
QVERIFY( result.contains( mLinesLayer ) );
QVERIFY( result.contains( mPointsLayer ) );
//test no layers
map->dataDefinedProperties().setProperty( QgsLayoutObject::MapLayers, QgsProperty::fromExpression(
QStringLiteral( "''" ) ) );
result = map->layersToRender();
QVERIFY( result.isEmpty() );
//test with atlas feature evaluation
QgsVectorLayer *atlasLayer = new QgsVectorLayer( QStringLiteral( "Point?field=col1:string" ), QStringLiteral( "atlas" ), QStringLiteral( "memory" ) );
QVERIFY( atlasLayer->isValid() );
QgsFeature f1( atlasLayer->dataProvider()->fields(), 1 );
f1.setAttribute( QStringLiteral( "col1" ), mLinesLayer->name() );
QgsFeature f2( atlasLayer->dataProvider()->fields(), 1 );
f2.setAttribute( QStringLiteral( "col1" ), mPointsLayer->name() );
atlasLayer->dataProvider()->addFeatures( QgsFeatureList() << f1 << f2 );
l.reportContext().setLayer( atlasLayer );
QgsFeature f;
QgsFeatureIterator it = atlasLayer->getFeatures();
it.nextFeature( f );
l.reportContext().setFeature( f );
map->dataDefinedProperties().setProperty( QgsLayoutObject::MapLayers, QgsProperty::fromField( QStringLiteral( "col1" ) ) );
result = map->layersToRender();
QCOMPARE( result.count(), 1 );
QCOMPARE( result.at( 0 ), mLinesLayer );
it.nextFeature( f );
l.reportContext().setFeature( f );
result = map->layersToRender();
QCOMPARE( result.count(), 1 );
QCOMPARE( result.at( 0 ), mPointsLayer );
it.nextFeature( f );
l.reportContext().setFeature( f );
delete atlasLayer;
//render test
map->dataDefinedProperties().setProperty( QgsLayoutObject::MapLayers, QgsProperty::fromExpression(
QStringLiteral( "'%1|%2'" ).arg( mPolysLayer->name(), mPointsLayer->name() ) ) );
map->setExtent( QgsRectangle( -110.0, 25.0, -90, 40.0 ) );
QGSVERIFYLAYOUTCHECK( QStringLiteral( "composermap_ddlayers" ), &l );
}
void TestQgsLayoutMap::dataDefinedStyles()
{
const QList<QgsMapLayer *> layers = QList<QgsMapLayer *>() << mRasterLayer << mPolysLayer << mPointsLayer << mLinesLayer;
QgsLayout l( QgsProject::instance() );
l.initializeDefaults();
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
map->attemptMove( QgsLayoutPoint( 20, 20 ) );
map->attemptResize( QgsLayoutSize( 200, 100 ) );
map->setFrameEnabled( true );
map->setLayers( layers );
l.addLayoutItem( map );
QgsMapThemeCollection::MapThemeRecord rec;
rec.setLayerRecords( QList<QgsMapThemeCollection::MapThemeLayerRecord>()
<< QgsMapThemeCollection::MapThemeLayerRecord( mPointsLayer )
<< QgsMapThemeCollection::MapThemeLayerRecord( mLinesLayer )
);
QgsProject::instance()->mapThemeCollection()->insert( QStringLiteral( "test preset" ), rec );
// test following of preset
map->setFollowVisibilityPreset( true );
map->setFollowVisibilityPresetName( QStringLiteral( "test preset" ) );
QSet<QgsMapLayer *> result = qgis::listToSet( map->layersToRender() );
QCOMPARE( result.count(), 2 );
map->setFollowVisibilityPresetName( QString() );
//test malformed style string
map->dataDefinedProperties().setProperty( QgsLayoutObject::MapStylePreset, QgsProperty::fromExpression( QStringLiteral( "5" ) ) );
result = qgis::listToSet( map->layersToRender() );
QCOMPARE( result, qgis::listToSet( layers ) );
//test valid preset
map->dataDefinedProperties().setProperty( QgsLayoutObject::MapStylePreset, QgsProperty::fromExpression( QStringLiteral( "'test preset'" ) ) );
result = qgis::listToSet( map->layersToRender() );
QCOMPARE( result.count(), 2 );
QVERIFY( result.contains( mLinesLayer ) );
QVERIFY( result.contains( mPointsLayer ) );
//test non-existent preset
map->dataDefinedProperties().setProperty( QgsLayoutObject::MapStylePreset, QgsProperty::fromExpression( QStringLiteral( "'bad preset'" ) ) );
result = qgis::listToSet( map->layersToRender() );
QCOMPARE( result, qgis::listToSet( layers ) );
//test that dd layer set overrides style layers
map->dataDefinedProperties().setProperty( QgsLayoutObject::MapStylePreset, QgsProperty::fromExpression( QStringLiteral( "'test preset'" ) ) );
map->dataDefinedProperties().setProperty( QgsLayoutObject::MapLayers, QgsProperty::fromExpression(
QStringLiteral( "'%1'" ).arg( mPolysLayer->name() ) ) );
result = qgis::listToSet( map->layersToRender() );
QCOMPARE( result.count(), 1 );
QVERIFY( result.contains( mPolysLayer ) );
map->dataDefinedProperties().setProperty( QgsLayoutObject::MapLayers, QgsProperty() );
//render test
map->dataDefinedProperties().setProperty( QgsLayoutObject::MapStylePreset, QgsProperty::fromExpression( QStringLiteral( "'test preset'" ) ) );
map->setExtent( QgsRectangle( -110.0, 25.0, -90, 40.0 ) );
QGSVERIFYLAYOUTCHECK( QStringLiteral( "composermap_ddstyles" ), &l );
}
void TestQgsLayoutMap::dataDefinedCrs()
{
const QList<QgsMapLayer *> layers = QList<QgsMapLayer *>() << mRasterLayer << mPolysLayer << mPointsLayer << mLinesLayer;
QgsLayout l( QgsProject::instance() );
l.initializeDefaults();
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
map->attemptMove( QgsLayoutPoint( 20, 20 ) );
map->attemptResize( QgsLayoutSize( 200, 100 ) );
map->setFrameEnabled( true );
map->setLayers( layers );
l.addLayoutItem( map );
//test epsg variable
map->dataDefinedProperties().setProperty( QgsLayoutObject::MapCrs, QgsProperty::fromValue( QStringLiteral( "EPSG:2192" ) ) );
map->refreshDataDefinedProperty( QgsLayoutObject::MapCrs );
QCOMPARE( map->crs().authid(), QStringLiteral( "EPSG:2192" ) );
//test proj string variable
map->dataDefinedProperties().setProperty( QgsLayoutObject::MapCrs, QgsProperty::fromValue( QStringLiteral( "PROJ4: +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs" ) ) );
map->refreshDataDefinedProperty( QgsLayoutObject::MapCrs );
QCOMPARE( map->crs().toProj(), QStringLiteral( "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs" ) );
}
void TestQgsLayoutMap::dataDefinedTemporalRange()
{
const QList<QgsMapLayer *> layers = QList<QgsMapLayer *>() << mRasterLayer << mPolysLayer << mPointsLayer << mLinesLayer;
QgsLayout l( QgsProject::instance() );
l.initializeDefaults();
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
map->attemptMove( QgsLayoutPoint( 20, 20 ) );
map->attemptResize( QgsLayoutSize( 200, 100 ) );
map->setFrameEnabled( true );
map->setLayers( layers );
l.addLayoutItem( map );
const QDateTime dt1 = QDateTime( QDate( 2010, 1, 1 ), QTime( 0, 0, 0 ) );
const QDateTime dt2 = QDateTime( QDate( 2020, 1, 1 ), QTime( 0, 0, 0 ) );
map->setIsTemporal( true );
map->dataDefinedProperties().setProperty( QgsLayoutObject::StartDateTime, QgsProperty::fromValue( dt1 ) );
map->dataDefinedProperties().setProperty( QgsLayoutObject::EndDateTime, QgsProperty::fromValue( dt2 ) );
map->refreshDataDefinedProperty( QgsLayoutObject::StartDateTime );
map->refreshDataDefinedProperty( QgsLayoutObject::EndDateTime );
QCOMPARE( map->temporalRange(), QgsDateTimeRange( dt1, dt2, true, false ) );
const QgsMapSettings ms = map->mapSettings( map->extent(), map->rect().size(), 300, false );
QCOMPARE( ms.temporalRange(), QgsDateTimeRange( dt1, dt2, true, false ) );
}
void TestQgsLayoutMap::rasterized()
{
// test a map which must be rasterized
QgsLayout l( QgsProject::instance() );
l.initializeDefaults();
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
map->attemptMove( QgsLayoutPoint( 20, 30 ) );
map->attemptResize( QgsLayoutSize( 200, 100 ) );
map->setFrameEnabled( true );
map->setExtent( QgsRectangle( -110.0, 25.0, -90, 40.0 ) );
const QList<QgsMapLayer *> layers = QList<QgsMapLayer *>() << mLinesLayer;
map->setLayers( layers );
map->setBackgroundColor( Qt::yellow );
l.addLayoutItem( map );
// add some guide lines, just for reference
QPolygonF points;
points << QPointF( 0, 30 ) << QPointF( 10, 30 );
QgsLayoutItemPolyline *line1 = new QgsLayoutItemPolyline( points, &l );
l.addLayoutItem( line1 );
points.clear();
points << QPointF( 0, 30 + map->rect().height() ) << QPointF( 10, 30 + map->rect().height() );
QgsLayoutItemPolyline *line2 = new QgsLayoutItemPolyline( points, &l );
l.addLayoutItem( line2 );
points.clear();
points << QPointF( 20, 0 ) << QPointF( 20, 20 );
QgsLayoutItemPolyline *line3 = new QgsLayoutItemPolyline( points, &l );
l.addLayoutItem( line3 );
points.clear();
points << QPointF( 220, 0 ) << QPointF( 220, 20 );
QgsLayoutItemPolyline *line4 = new QgsLayoutItemPolyline( points, &l );
l.addLayoutItem( line4 );
// force rasterization
QgsLayoutItemMapGrid *grid = new QgsLayoutItemMapGrid( "test", map );
grid->setIntervalX( 10 );
grid->setIntervalY( 10 );
grid->setAnnotationTextFormat( QgsTextFormat::fromQFont( QgsFontUtils::getStandardTestFont( QStringLiteral( "Bold" ) ) ) );
grid->setBlendMode( QPainter::CompositionMode_Darken );
grid->setAnnotationEnabled( true );
grid->setAnnotationDisplay( QgsLayoutItemMapGrid::ShowAll, QgsLayoutItemMapGrid::Left );
grid->setAnnotationDisplay( QgsLayoutItemMapGrid::ShowAll, QgsLayoutItemMapGrid::Top );
grid->setAnnotationDisplay( QgsLayoutItemMapGrid::ShowAll, QgsLayoutItemMapGrid::Right );
grid->setAnnotationDisplay( QgsLayoutItemMapGrid::ShowAll, QgsLayoutItemMapGrid::Bottom );
map->grids()->addGrid( grid );
map->updateBoundingRect();
QVERIFY( map->containsAdvancedEffects() );
QGSVERIFYLAYOUTCHECK( QStringLiteral( "layoutmap_rasterized" ), &l );
// try rendering again, without requiring rasterization, for comparison
// (we can use the same test image, because CompositionMode_Darken doesn't actually have any noticeable
// rendering differences for the black grid!)
grid->setBlendMode( QPainter::CompositionMode_SourceOver );
QVERIFY( !map->containsAdvancedEffects() );
QGSVERIFYLAYOUTCHECK( QStringLiteral( "layoutmap_rasterized" ), &l );
}
void TestQgsLayoutMap::layersToRender()
{
const QList<QgsMapLayer *> layers = QList<QgsMapLayer *>() << mRasterLayer << mPolysLayer << mPointsLayer << mLinesLayer;
const QList<QgsMapLayer *> layers2 = QList<QgsMapLayer *>() << mRasterLayer << mPolysLayer << mLinesLayer;
QgsLayout l( QgsProject::instance() );
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
map->setLayers( layers );
l.addLayoutItem( map );
QCOMPARE( map->layersToRender(), layers );
// hide coverage layer
l.reportContext().setLayer( mPointsLayer );
l.renderContext().setFlag( QgsLayoutRenderContext::FlagHideCoverageLayer, true );
QCOMPARE( map->layersToRender(), layers2 );
l.renderContext().setFlag( QgsLayoutRenderContext::FlagHideCoverageLayer, false );
QCOMPARE( map->layersToRender(), layers );
}
void TestQgsLayoutMap::mapRotation()
{
QgsProject p;
const QFileInfo rasterFileInfo( QStringLiteral( TEST_DATA_DIR ) + "/rgb256x256.png" );
QgsRasterLayer *layer = new QgsRasterLayer( rasterFileInfo.filePath(),
rasterFileInfo.completeBaseName() );
QgsMultiBandColorRenderer *rasterRenderer = new QgsMultiBandColorRenderer( mRasterLayer->dataProvider(), 1, 2, 3 );
layer->setRenderer( rasterRenderer );
p.addMapLayer( layer );
QgsLayout l( &p );
l.initializeDefaults();
//test map rotation
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
map->attemptSetSceneRect( QRectF( 20, 20, 100, 50 ) );
map->setFrameEnabled( true );
l.addLayoutItem( map );
map->setExtent( QgsRectangle( 0, -192, 256, -64 ) );
map->setMapRotation( 90 );
map->setLayers( QList<QgsMapLayer *>() << layer );
mControlPathPrefix = QStringLiteral( "composer_items" );
QGSVERIFYLAYOUTCHECK( QStringLiteral( "composerrotation_maprotation" ), &l, 0, 200 );
mControlPathPrefix = QStringLiteral( "composer_map" );
// test that rotation correctly applies to restored items
QDomDocument doc;
QDomElement documentElement = doc.createElement( QStringLiteral( "ComposerItemClipboard" ) );
map->writeXml( documentElement, doc, QgsReadWriteContext() );
QgsLayoutItemMap map2( &l );
QVERIFY( map2.readXml( documentElement.firstChildElement(), doc, QgsReadWriteContext() ) );
QCOMPARE( map2.mapRotation(), 90.0 );
}
void TestQgsLayoutMap::mapItemRotation()
{
QgsProject p;
const QFileInfo rasterFileInfo( QStringLiteral( TEST_DATA_DIR ) + "/rgb256x256.png" );
QgsRasterLayer *layer = new QgsRasterLayer( rasterFileInfo.filePath(),
rasterFileInfo.completeBaseName() );
QgsMultiBandColorRenderer *rasterRenderer = new QgsMultiBandColorRenderer( mRasterLayer->dataProvider(), 1, 2, 3 );
layer->setRenderer( rasterRenderer );
p.addMapLayer( layer );
QgsLayout l( &p );
l.initializeDefaults();
//test map rotation
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
map->attemptSetSceneRect( QRectF( 20, 50, 100, 50 ) );
map->setFrameEnabled( true );
l.addLayoutItem( map );
map->setExtent( QgsRectangle( 0, -192, 256, -64 ) );
map->setItemRotation( 90 );
map->setLayers( QList<QgsMapLayer *>() << layer );
mControlPathPrefix = QStringLiteral( "composer_items" );
QGSVERIFYLAYOUTCHECK( QStringLiteral( "composerrotation_mapitemrotation" ), &l, 0, 200 );
mControlPathPrefix = QStringLiteral( "composer_map" );
}
void TestQgsLayoutMap::expressionContext()
{
const QgsRectangle extent( 2000, 2800, 2500, 2900 );
QgsLayout l( QgsProject::instance() );
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
map->setCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
map->attemptSetSceneRect( QRectF( 30, 60, 200, 100 ) );
map->setExtent( extent );
l.addLayoutItem( map );
map->setId( QStringLiteral( "Map_id" ) );
QgsExpression e( QStringLiteral( "@map_scale" ) );
QgsExpressionContext c = map->createExpressionContext();
QVariant r = e.evaluate( &c );
QGSCOMPARENEAR( r.toDouble(), 184764103, 100 );
QgsExpression e2( QStringLiteral( "@map_crs" ) );
r = e2.evaluate( &c );
QCOMPARE( r.toString(), QString( "EPSG:4326" ) );
QgsExpression e3( QStringLiteral( "@map_crs_definition" ) );
r = e3.evaluate( &c );
QCOMPARE( r.toString(), QString( "+proj=longlat +datum=WGS84 +no_defs" ) );
QgsExpression e4( QStringLiteral( "@map_units" ) );
r = e4.evaluate( &c );
QCOMPARE( r.toString(), QString( "degrees" ) );
QgsExpression e5( QStringLiteral( "@map_crs_description" ) );
r = e5.evaluate( &c );
QCOMPARE( r.toString(), QString( "WGS 84" ) );
QgsExpression e6( QStringLiteral( "@map_crs_acronym" ) );
r = e6.evaluate( &c );
QCOMPARE( r.toString(), QString( "longlat" ) );
QgsExpression e6a( QStringLiteral( "@map_crs_projection" ) );
r = e6a.evaluate( &c );
QCOMPARE( r.toString(), QString( "Lat/long (Geodetic alias)" ) );
QgsExpression e7( QStringLiteral( "@map_crs_proj4" ) );
r = e7.evaluate( &c );
QCOMPARE( r.toString(), QString( "+proj=longlat +datum=WGS84 +no_defs" ) );
QgsExpression e8( QStringLiteral( "@map_crs_wkt" ) );
r = e8.evaluate( &c );
QVERIFY( r.toString().length() >= 15 );
QgsExpression e9( QStringLiteral( "@map_crs_ellipsoid" ) );
r = e9.evaluate( &c );
QCOMPARE( r.toString(), QString( "EPSG:7030" ) );
QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "Point?field=id_a:integer" ), QStringLiteral( "A" ), QStringLiteral( "memory" ) );
QgsVectorLayer *layer2 = new QgsVectorLayer( QStringLiteral( "Point?field=id_a:integer" ), QStringLiteral( "B" ), QStringLiteral( "memory" ) );
map->setLayers( QList<QgsMapLayer *>() << layer << layer2 );
QgsProject::instance()->addMapLayers( map->layers() );
c = map->createExpressionContext();
QgsExpression e10( QStringLiteral( "@map_layer_ids" ) );
r = e10.evaluate( &c );
QCOMPARE( r.toStringList().join( ',' ), QStringLiteral( "%1,%2" ).arg( layer->id(), layer2->id() ) );
e10 = QgsExpression( QStringLiteral( "array_foreach(@map_layers, layer_property(@element, 'name'))" ) );
r = e10.evaluate( &c );
QCOMPARE( r.toStringList().join( ',' ), QStringLiteral( "A,B" ) );
QgsExpression e11( QStringLiteral( "is_layer_visible( '%1' )" ).arg( layer->id() ) );
r = e11.evaluate( &c );
QCOMPARE( r.toBool(), true );
QgsExpression e12( QStringLiteral( "is_layer_visible( 'aaaaaa' )" ) );
r = e12.evaluate( &c );
QCOMPARE( r.toBool(), false );
}
void TestQgsLayoutMap::layoutToMapCoordsTransform()
{
const QgsRectangle extent( 2000, 2800, 2500, 2900 );
QgsLayout l( QgsProject::instance() );
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
map->setCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
map->attemptSetSceneRect( QRectF( 30, 60, 200, 100 ) );
map->setExtent( extent );
l.addLayoutItem( map );
QTransform t = map->layoutToMapCoordsTransform();
QGSCOMPARENEAR( t.map( QPointF( 30, 60 ) ).x(), 2000, 1 );
QGSCOMPARENEAR( t.map( QPointF( 30, 60 ) ).y(), 2900, 1 );
QGSCOMPARENEAR( t.map( QPointF( 230, 60 ) ).x(), 2500, 1 );
QGSCOMPARENEAR( t.map( QPointF( 230, 60 ) ).y(), 2900, 1 );
QGSCOMPARENEAR( t.map( QPointF( 30, 100 ) ).x(), 2000, 1 );
QGSCOMPARENEAR( t.map( QPointF( 30, 100 ) ).y(), 2800, 1 );
QGSCOMPARENEAR( t.map( QPointF( 230, 100 ) ).x(), 2500, 1 );
QGSCOMPARENEAR( t.map( QPointF( 230, 100 ) ).y(), 2800, 1 );
// with map rotation
map->setMapRotation( 75 );
t = map->layoutToMapCoordsTransform();
QGSCOMPARENEAR( t.map( QPointF( 30, 60 ) ).x(), 2136.998947, 1 );
QGSCOMPARENEAR( t.map( QPointF( 30, 60 ) ).y(), 2621.459496, 1 );
QGSCOMPARENEAR( t.map( QPointF( 230, 60 ) ).x(), 2266.408470, 1 );
QGSCOMPARENEAR( t.map( QPointF( 230, 60 ) ).y(), 3104.422409, 1 );
QGSCOMPARENEAR( t.map( QPointF( 30, 100 ) ).x(), 2233.591530, 1 );
QGSCOMPARENEAR( t.map( QPointF( 30, 100 ) ).y(), 2595.577591, 1 );
QGSCOMPARENEAR( t.map( QPointF( 230, 100 ) ).x(), 2363.001053, 1 );
QGSCOMPARENEAR( t.map( QPointF( 230, 100 ) ).y(), 3078.540504, 1 );
// with item rotation
map->setItemRotation( -30 );
t = map->layoutToMapCoordsTransform();
QGSCOMPARENEAR( t.map( QPointF( 30, 60 ) ).x(), 2037.867966, 1 );
QGSCOMPARENEAR( t.map( QPointF( 30, 60 ) ).y(), 2708.578644, 1 );
QGSCOMPARENEAR( t.map( QPointF( 230, 60 ) ).x(), 2391.421356, 1 );
QGSCOMPARENEAR( t.map( QPointF( 230, 60 ) ).y(), 3062.132034, 1 );
QGSCOMPARENEAR( t.map( QPointF( 30, 100 ) ).x(), 2108.578644, 1 );
QGSCOMPARENEAR( t.map( QPointF( 30, 100 ) ).y(), 2637.867966, 1 );
QGSCOMPARENEAR( t.map( QPointF( 230, 100 ) ).x(), 2462.132034, 1 );
QGSCOMPARENEAR( t.map( QPointF( 230, 100 ) ).y(), 2991.421356, 1 );
}
void TestQgsLayoutMap::labelBlockingRegions()
{
const QgsRectangle extent( 2000, 2800, 2500, 2900 );
QgsLayout l( QgsProject::instance() );
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
map->setCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
map->attemptSetSceneRect( QRectF( 30, 60, 200, 100 ) );
map->setExtent( extent );
l.addLayoutItem( map );
QgsLayoutItemMap *map2 = new QgsLayoutItemMap( &l );
map2->attemptSetSceneRect( QRectF( 10, 30, 100, 200 ) );
l.addLayoutItem( map2 );
QgsLayoutItemMap *map3 = new QgsLayoutItemMap( &l );
map3->attemptSetSceneRect( QRectF( 210, 70, 100, 200 ) );
l.addLayoutItem( map3 );
QgsLayoutItemMap *map4 = new QgsLayoutItemMap( &l );
map4->attemptSetSceneRect( QRectF( 210, 70, 100, 200 ) );
l.addLayoutItem( map4 );
QVERIFY( !map->isLabelBlockingItem( map2 ) );
QVERIFY( !map->isLabelBlockingItem( map3 ) );
QVERIFY( !map->isLabelBlockingItem( map4 ) );
map->addLabelBlockingItem( map2 );
QVERIFY( map->isLabelBlockingItem( map2 ) );
map->addLabelBlockingItem( map3 );
QVERIFY( map->isLabelBlockingItem( map3 ) );
map->addLabelBlockingItem( map4 );
QVERIFY( map->isLabelBlockingItem( map4 ) );
map->removeLabelBlockingItem( map4 );
QVERIFY( !map->isLabelBlockingItem( map4 ) );
QList<QgsLabelBlockingRegion> regions = map->createLabelBlockingRegions( map->mapSettings( map->extent(), map->rect().size(), 300, false ) );
QCOMPARE( regions.count(), 2 );
QCOMPARE( regions.at( 0 ).geometry.asWkt( 0 ), QStringLiteral( "Polygon ((1950 2975, 2200 2975, 2200 2475, 1950 2475, 1950 2975, 1950 2975))" ) );
QCOMPARE( regions.at( 1 ).geometry.asWkt( 0 ), QStringLiteral( "Polygon ((2450 2875, 2700 2875, 2700 2375, 2450 2375, 2450 2875, 2450 2875))" ) );
map->setLabelMargin( QgsLayoutMeasurement( 2, Qgis::LayoutUnit::Centimeters ) );
regions = map->createLabelBlockingRegions( map->mapSettings( map->extent(), map->rect().size(), 300, false ) );
QCOMPARE( regions.count(), 2 );
QCOMPARE( regions.at( 0 ).geometry.asWkt( 0 ), QStringLiteral( "Polygon ((1950 2975, 2200 2975, 2200 2475, 1950 2475, 1950 2975, 1950 2975))" ) );
QCOMPARE( regions.at( 1 ).geometry.asWkt( 0 ), QStringLiteral( "Polygon ((2450 2875, 2700 2875, 2700 2375, 2450 2375, 2450 2875, 2450 2875))" ) );
map2->setRotation( 45 );
regions = map->createLabelBlockingRegions( map->mapSettings( map->extent(), map->rect().size(), 300, false ) );
QCOMPARE( regions.count(), 2 );
QCOMPARE( regions.at( 0 ).geometry.asWkt( 0 ), QStringLiteral( "Polygon ((1950 2975, 2127 2798, 1773 2445, 1596 2621, 1950 2975, 1950 2975))" ) );
QCOMPARE( regions.at( 1 ).geometry.asWkt( 0 ), QStringLiteral( "Polygon ((2450 2875, 2700 2875, 2700 2375, 2450 2375, 2450 2875, 2450 2875))" ) );
regions = map2->createLabelBlockingRegions( map2->mapSettings( map2->extent(), map2->rect().size(), 300, false ) );
QVERIFY( regions.isEmpty() );
// invisible items don't block
map2->setVisibility( false );
regions = map->createLabelBlockingRegions( map->mapSettings( map->extent(), map->rect().size(), 300, false ) );
QCOMPARE( regions.count(), 1 );
QCOMPARE( regions.at( 0 ).geometry.asWkt( 0 ), QStringLiteral( "Polygon ((2450 2875, 2700 2875, 2700 2375, 2450 2375, 2450 2875, 2450 2875))" ) );
// but they do if they were previously visible, and just temporarily hidden due to layered export
map2->setProperty( "wasVisible", true );
regions = map->createLabelBlockingRegions( map->mapSettings( map->extent(), map->rect().size(), 300, false ) );
QCOMPARE( regions.count(), 2 );
QCOMPARE( regions.at( 0 ).geometry.asWkt( 0 ), QStringLiteral( "Polygon ((1950 2975, 2127 2798, 1773 2445, 1596 2621, 1950 2975, 1950 2975))" ) );
QCOMPARE( regions.at( 1 ).geometry.asWkt( 0 ), QStringLiteral( "Polygon ((2450 2875, 2700 2875, 2700 2375, 2450 2375, 2450 2875, 2450 2875))" ) );
}
void TestQgsLayoutMap::testSimplificationMethod()
{
const QgsRectangle extent( 2000, 2800, 2500, 2900 );
QgsLayout l( QgsProject::instance() );
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
map->setCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
map->attemptSetSceneRect( QRectF( 30, 60, 200, 100 ) );
map->setExtent( extent );
l.addLayoutItem( map );
l.renderContext().mIsPreviewRender = false;
QgsMapSettings settings = map->mapSettings( map->extent(), map->rect().size(), 300, false );
// should default to no simplification during exports
QCOMPARE( settings.simplifyMethod().simplifyHints(), QgsVectorSimplifyMethod::NoSimplification );
QVERIFY( !( settings.flags() & Qgis::MapSettingsFlag::UseRenderingOptimization ) );
// set a simplification method to use
QgsVectorSimplifyMethod method;
method.setSimplifyHints( QgsVectorSimplifyMethod::GeometrySimplification );
l.renderContext().setSimplifyMethod( method );
// should still have no simplification override for preview renders
l.renderContext().mIsPreviewRender = true;
settings = map->mapSettings( map->extent(), map->rect().size(), 300, false );
QCOMPARE( settings.simplifyMethod().simplifyHints(), QgsVectorSimplifyMethod::NoSimplification );
QVERIFY( settings.flags() & Qgis::MapSettingsFlag::UseRenderingOptimization );
// for exports, we respect the layout context's simplify method
l.renderContext().mIsPreviewRender = false;
settings = map->mapSettings( map->extent(), map->rect().size(), 300, false );
QCOMPARE( settings.simplifyMethod().simplifyHints(), QgsVectorSimplifyMethod::GeometrySimplification );
QVERIFY( settings.flags() & Qgis::MapSettingsFlag::UseRenderingOptimization );
}
class TestHandler : public QgsRenderedFeatureHandlerInterface
{
public:
TestHandler( QList< QgsFeature > &features, QList< QgsGeometry > &geometries )
: features( features )
, geometries( geometries )
{}
void handleRenderedFeature( const QgsFeature &feature, const QgsGeometry &geom, const QgsRenderedFeatureHandlerInterface::RenderedFeatureContext & ) override
{
features.append( feature );
geometries.append( geom );
}
QList< QgsFeature > &features;
QList< QgsGeometry > &geometries;
};
void TestQgsLayoutMap::testRenderedFeatureHandler()
{
QgsVectorLayer *linesLayer = new QgsVectorLayer( TEST_DATA_DIR + QStringLiteral( "/lines.shp" ),
QStringLiteral( "lines" ), QStringLiteral( "ogr" ) );
QVERIFY( linesLayer->isValid() );
QgsProject p;
p.addMapLayer( linesLayer );
QgsLayout l( &p );
l.initializeDefaults();
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
map->attemptSetSceneRect( QRectF( 20, 20, 200, 100 ) );
map->setFrameEnabled( true );
map->setLayers( QList<QgsMapLayer *>() << linesLayer );
map->setCrs( linesLayer->crs() );
map->zoomToExtent( linesLayer->extent() );
l.addLayoutItem( map );
// register a handler
QList< QgsFeature > features1;
QList< QgsGeometry > geometries1;
TestHandler handler1( features1, geometries1 );
// not added yet, no crash
map->removeRenderedFeatureHandler( nullptr );
map->removeRenderedFeatureHandler( &handler1 );
map->addRenderedFeatureHandler( &handler1 );
// trigger render
const QgsLayoutExporter exporter( &l );
exporter.renderPageToImage( 0 );
QCOMPARE( features1.count(), 6 );
QCOMPARE( geometries1.count(), 6 );
// remove handler
map->removeRenderedFeatureHandler( &handler1 );
features1.clear();
// shouldn't be used anymore
const QgsLayoutExporter exporter2( &l );
exporter2.renderPageToImage( 0 );
QVERIFY( features1.isEmpty() );
}
void TestQgsLayoutMap::testLayeredExport()
{
QgsVectorLayer *linesLayer = new QgsVectorLayer( TEST_DATA_DIR + QStringLiteral( "/lines.shp" ),
QStringLiteral( "lines" ), QStringLiteral( "ogr" ) );
QVERIFY( linesLayer->isValid() );
QgsVectorLayer *pointsLayer = new QgsVectorLayer( TEST_DATA_DIR + QStringLiteral( "/points.shp" ),
QStringLiteral( "points" ), QStringLiteral( "ogr" ) );
QVERIFY( pointsLayer->isValid() );
QgsProject p;
p.addMapLayer( linesLayer );
p.addMapLayer( pointsLayer );
QgsLayout l( &p );
l.initializeDefaults();
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
map->attemptSetSceneRect( QRectF( 20, 20, 200, 100 ) );
map->setFrameEnabled( false );
map->setBackgroundEnabled( false );
map->setCrs( linesLayer->crs() );
map->zoomToExtent( linesLayer->extent() );
map->setLayers( QList<QgsMapLayer *>() << linesLayer );
l.addLayoutItem( map );
QVERIFY( !map->nextExportPart() );
QVERIFY( map->shouldDrawPart( QgsLayoutItemMap::Grid ) );
QVERIFY( map->shouldDrawPart( QgsLayoutItemMap::OverviewMapExtent ) );
QVERIFY( map->shouldDrawPart( QgsLayoutItemMap::Frame ) );
QVERIFY( map->shouldDrawPart( QgsLayoutItemMap::Background ) );
QVERIFY( map->shouldDrawPart( QgsLayoutItemMap::Layer ) );
map->startLayeredExport();