|
34 | 34 | #include "qgsrasterfilewriter.h"
|
35 | 35 | #include "qgsreclassifyutils.h"
|
36 | 36 | #include "qgsalgorithmrasterlogicalop.h"
|
| 37 | +#include "qgsprintlayout.h" |
| 38 | +#include "qgslayoutmanager.h" |
| 39 | +#include "qgslayoutitemmap.h" |
37 | 40 |
|
38 | 41 | class TestQgsProcessingAlgs: public QObject
|
39 | 42 | {
|
@@ -72,6 +75,8 @@ class TestQgsProcessingAlgs: public QObject
|
72 | 75 | void rasterLogicOp_data();
|
73 | 76 | void rasterLogicOp();
|
74 | 77 |
|
| 78 | + void layoutMapExtent(); |
| 79 | + |
75 | 80 | private:
|
76 | 81 |
|
77 | 82 | QString mPointLayerPath;
|
@@ -1116,5 +1121,131 @@ void TestQgsProcessingAlgs::rasterLogicOp()
|
1116 | 1121 | }
|
1117 | 1122 | }
|
1118 | 1123 |
|
| 1124 | +void TestQgsProcessingAlgs::layoutMapExtent() |
| 1125 | +{ |
| 1126 | + std::unique_ptr< QgsProcessingAlgorithm > alg( QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:printlayoutmapextenttolayer" ) ) ); |
| 1127 | + QVERIFY( alg != nullptr ); |
| 1128 | + |
| 1129 | + std::unique_ptr< QgsProcessingContext > context = qgis::make_unique< QgsProcessingContext >(); |
| 1130 | + QgsProject p; |
| 1131 | + context->setProject( &p ); |
| 1132 | + |
| 1133 | + QVariantMap parameters; |
| 1134 | + parameters.insert( QStringLiteral( "LAYOUT" ), QStringLiteral( "l" ) ); |
| 1135 | + parameters.insert( QStringLiteral( "MAP" ), QStringLiteral( "m" ) ); |
| 1136 | + parameters.insert( QStringLiteral( "OUTPUT" ), QgsProcessing::TEMPORARY_OUTPUT ); |
| 1137 | + |
| 1138 | + bool ok = false; |
| 1139 | + QgsProcessingFeedback feedback; |
| 1140 | + QVariantMap results; |
| 1141 | + // no layout |
| 1142 | + results = alg->run( parameters, *context, &feedback, &ok ); |
| 1143 | + QVERIFY( !ok ); |
| 1144 | + |
| 1145 | + QgsPrintLayout *layout = new QgsPrintLayout( &p ); |
| 1146 | + layout->setName( QStringLiteral( "l" ) ); |
| 1147 | + p.layoutManager()->addLayout( layout ); |
| 1148 | + |
| 1149 | + // no matching map |
| 1150 | + results = alg->run( parameters, *context, &feedback, &ok ); |
| 1151 | + QVERIFY( !ok ); |
| 1152 | + |
| 1153 | + QgsLayoutItemMap *map = new QgsLayoutItemMap( layout ); |
| 1154 | + layout->addLayoutItem( map ); |
| 1155 | + map->setId( QStringLiteral( "m" ) ); |
| 1156 | + map->setCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3111" ) ) ); |
| 1157 | + map->attemptSetSceneRect( QRectF( 100, 100, 150, 180 ) ); |
| 1158 | + map->zoomToExtent( QgsRectangle( 10000, 100000, 60000, 180000 ) ); |
| 1159 | + map->setMapRotation( 45 ); |
| 1160 | + map->setScale( 10000 ); |
| 1161 | + QgsLayoutItemMap *map2 = new QgsLayoutItemMap( layout ); |
| 1162 | + layout->addLayoutItem( map2 ); |
| 1163 | + map2->setId( QStringLiteral( "m2" ) ); |
| 1164 | + map2->setCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3785" ) ) ); |
| 1165 | + map2->attemptSetSceneRect( QRectF( 100, 100, 50, 80 ) ); |
| 1166 | + map2->zoomToExtent( QgsRectangle( 10000, 100000, 5000, 8000 ) ); |
| 1167 | + map2->setMapRotation( 0 ); |
| 1168 | + map2->setScale( 1000 ); |
| 1169 | + |
| 1170 | + results = alg->run( parameters, *context, &feedback, &ok ); |
| 1171 | + QVERIFY( ok ); |
| 1172 | + |
| 1173 | + QCOMPARE( results.value( QStringLiteral( "WIDTH" ) ).toDouble(), 150.0 ); |
| 1174 | + QCOMPARE( results.value( QStringLiteral( "HEIGHT" ) ).toDouble(), 180.0 ); |
| 1175 | + QCOMPARE( results.value( QStringLiteral( "SCALE" ) ).toDouble(), 10000.0 ); |
| 1176 | + QCOMPARE( results.value( QStringLiteral( "ROTATION" ) ).toDouble(), 45.0 ); |
| 1177 | + |
| 1178 | + QgsFeature f; |
| 1179 | + QCOMPARE( context->getMapLayer( results.value( QStringLiteral( "OUTPUT" ) ).toString() )->crs().authid(), QStringLiteral( "EPSG:3111" ) ); |
| 1180 | + QVERIFY( qobject_cast< QgsVectorLayer * >( context->getMapLayer( results.value( QStringLiteral( "OUTPUT" ) ).toString() ) )->getFeatures().nextFeature( f ) ); |
| 1181 | + QCOMPARE( f.attribute( 0 ).toString(), QStringLiteral( "m" ) ); |
| 1182 | + QCOMPARE( f.attribute( 1 ).toDouble(), 150.0 ); |
| 1183 | + QCOMPARE( f.attribute( 2 ).toDouble(), 180.0 ); |
| 1184 | + QCOMPARE( f.attribute( 3 ).toDouble(), 10000.0 ); |
| 1185 | + QCOMPARE( f.attribute( 4 ).toDouble(), 45.0 ); |
| 1186 | + QCOMPARE( f.geometry().asWkt( 0 ), QStringLiteral( "Polygon ((33833 140106, 34894 141167, 36167 139894, 35106 138833, 33833 140106))" ) ); |
| 1187 | + |
| 1188 | + // all maps |
| 1189 | + parameters.remove( QStringLiteral( "MAP" ) ); |
| 1190 | + results = alg->run( parameters, *context, &feedback, &ok ); |
| 1191 | + QVERIFY( ok ); |
| 1192 | + |
| 1193 | + QVERIFY( !results.value( QStringLiteral( "WIDTH" ) ).isValid() ); |
| 1194 | + QVERIFY( !results.value( QStringLiteral( "HEIGHT" ) ).isValid() ); |
| 1195 | + QVERIFY( !results.value( QStringLiteral( "SCALE" ) ).isValid() ); |
| 1196 | + QVERIFY( !results.value( QStringLiteral( "ROTATION" ) ).isValid() ); |
| 1197 | + |
| 1198 | + QCOMPARE( context->getMapLayer( results.value( QStringLiteral( "OUTPUT" ) ).toString() )->crs().authid(), QStringLiteral( "EPSG:3785" ) ); |
| 1199 | + QgsFeatureIterator it = qobject_cast< QgsVectorLayer * >( context->getMapLayer( results.value( QStringLiteral( "OUTPUT" ) ).toString() ) )->getFeatures(); |
| 1200 | + QgsFeature f1; |
| 1201 | + QVERIFY( it.nextFeature( f1 ) ); |
| 1202 | + QgsFeature f2; |
| 1203 | + QVERIFY( it.nextFeature( f2 ) ); |
| 1204 | + f = f1.attribute( 0 ).toString() == QStringLiteral( "m" ) ? f1 : f2; |
| 1205 | + QCOMPARE( f.attribute( 0 ).toString(), QStringLiteral( "m" ) ); |
| 1206 | + QCOMPARE( f.attribute( 1 ).toDouble(), 150.0 ); |
| 1207 | + QCOMPARE( f.attribute( 2 ).toDouble(), 180.0 ); |
| 1208 | + QCOMPARE( f.attribute( 3 ).toDouble(), 10000.0 ); |
| 1209 | + QCOMPARE( f.attribute( 4 ).toDouble(), 45.0 ); |
| 1210 | + QCOMPARE( f.geometry().asWkt( 0 ), QStringLiteral( "Polygon ((12077408 -7108521, 12079627 -7107575, 12080760 -7110245, 12078540 -7111191, 12077408 -7108521))" ) ); |
| 1211 | + f = f1.attribute( 0 ).toString() == QStringLiteral( "m" ) ? f2 : f1; |
| 1212 | + QCOMPARE( f.attribute( 0 ).toString(), QStringLiteral( "m2" ) ); |
| 1213 | + QCOMPARE( f.attribute( 1 ).toDouble(), 50.0 ); |
| 1214 | + QCOMPARE( f.attribute( 2 ).toDouble(), 80.0 ); |
| 1215 | + QGSCOMPARENEAR( f.attribute( 3 ).toDouble(), 1000.0, 0.0001 ); |
| 1216 | + QCOMPARE( f.attribute( 4 ).toDouble(), 0.0 ); |
| 1217 | + QCOMPARE( f.geometry().asWkt( 0 ), QStringLiteral( "Polygon ((7475 54040, 7525 54040, 7525 53960, 7475 53960, 7475 54040))" ) ); |
| 1218 | + |
| 1219 | + // crs override |
| 1220 | + parameters.insert( QStringLiteral( "CRS" ), QStringLiteral( "EPSG:3111" ) ); |
| 1221 | + results = alg->run( parameters, *context, &feedback, &ok ); |
| 1222 | + QVERIFY( ok ); |
| 1223 | + |
| 1224 | + QVERIFY( !results.value( QStringLiteral( "WIDTH" ) ).isValid() ); |
| 1225 | + QVERIFY( !results.value( QStringLiteral( "HEIGHT" ) ).isValid() ); |
| 1226 | + QVERIFY( !results.value( QStringLiteral( "SCALE" ) ).isValid() ); |
| 1227 | + QVERIFY( !results.value( QStringLiteral( "ROTATION" ) ).isValid() ); |
| 1228 | + |
| 1229 | + QCOMPARE( context->getMapLayer( results.value( QStringLiteral( "OUTPUT" ) ).toString() )->crs().authid(), QStringLiteral( "EPSG:3111" ) ); |
| 1230 | + it = qobject_cast< QgsVectorLayer * >( context->getMapLayer( results.value( QStringLiteral( "OUTPUT" ) ).toString() ) )->getFeatures(); |
| 1231 | + QVERIFY( it.nextFeature( f1 ) ); |
| 1232 | + QVERIFY( it.nextFeature( f2 ) ); |
| 1233 | + f = f1.attribute( 0 ).toString() == QStringLiteral( "m" ) ? f1 : f2; |
| 1234 | + QCOMPARE( f.attribute( 0 ).toString(), QStringLiteral( "m" ) ); |
| 1235 | + QCOMPARE( f.attribute( 1 ).toDouble(), 150.0 ); |
| 1236 | + QCOMPARE( f.attribute( 2 ).toDouble(), 180.0 ); |
| 1237 | + QCOMPARE( f.attribute( 3 ).toDouble(), 10000.0 ); |
| 1238 | + QCOMPARE( f.attribute( 4 ).toDouble(), 45.0 ); |
| 1239 | + QCOMPARE( f.geometry().asWkt( 0 ), QStringLiteral( "Polygon ((33833 140106, 34894 141167, 36167 139894, 35106 138833, 33833 140106))" ) ); |
| 1240 | + f = f1.attribute( 0 ).toString() == QStringLiteral( "m" ) ? f2 : f1; |
| 1241 | + QCOMPARE( f.attribute( 0 ).toString(), QStringLiteral( "m2" ) ); |
| 1242 | + QCOMPARE( f.attribute( 1 ).toDouble(), 50.0 ); |
| 1243 | + QCOMPARE( f.attribute( 2 ).toDouble(), 80.0 ); |
| 1244 | + QGSCOMPARENEAR( f.attribute( 3 ).toDouble(), 1000.0, 0.0001 ); |
| 1245 | + QCOMPARE( f.attribute( 4 ).toDouble(), 0.0 ); |
| 1246 | + QCOMPARE( f.geometry().asWkt( 0 ), QStringLiteral( "Polygon ((-10399464 -5347896, -10399461 -5347835, -10399364 -5347840, -10399367 -5347901, -10399464 -5347896))" ) ); |
| 1247 | + |
| 1248 | +} |
| 1249 | + |
1119 | 1250 | QGSTEST_MAIN( TestQgsProcessingAlgs )
|
1120 | 1251 | #include "testqgsprocessingalgs.moc"
|
0 commit comments