|
22 | 22 | from qgis.testing import start_app, unittest
|
23 | 23 | from utilities import unitTestDataPath
|
24 | 24 | from qgis.PyQt.QtCore import QFileInfo, QRectF, qWarning
|
25 |
| -from qgis.core import QgsVectorLayer, QgsProject, QgsCoordinateReferenceSystem, \ |
26 |
| - QgsComposition, QgsFillSymbol, QgsSingleSymbolRenderer, QgsComposerLabel, QgsComposerMap, QgsFontUtils, \ |
27 |
| - QgsRectangle, QgsComposerLegend, QgsFeature, QgsGeometry, QgsPointXY, QgsRendererCategory, QgsCategorizedSymbolRenderer, QgsMarkerSymbol |
| 25 | +from qgis.core import ( |
| 26 | + QgsCategorizedSymbolRenderer, |
| 27 | + QgsComposerLabel, |
| 28 | + QgsComposerLegend, |
| 29 | + QgsComposerMap, |
| 30 | + QgsComposition, |
| 31 | + QgsCoordinateReferenceSystem, |
| 32 | + QgsFeature, |
| 33 | + QgsFillSymbol, |
| 34 | + QgsFontUtils, |
| 35 | + QgsGeometry, |
| 36 | + QgsMarkerSymbol, |
| 37 | + QgsPointXY, |
| 38 | + QgsProject, |
| 39 | + QgsRectangle, |
| 40 | + QgsRendererCategory, |
| 41 | + QgsSingleSymbolRenderer, |
| 42 | + QgsVectorLayer, |
| 43 | +) |
28 | 44 | from qgscompositionchecker import QgsCompositionChecker
|
29 | 45 |
|
30 | 46 | start_app()
|
@@ -113,6 +129,7 @@ def testCase(self):
|
113 | 129 | self.predefinedscales_render_test()
|
114 | 130 | self.hidden_render_test()
|
115 | 131 | self.legend_test()
|
| 132 | + self.rotation_test() |
116 | 133 |
|
117 | 134 | shutil.rmtree(tmppath, True)
|
118 | 135 |
|
@@ -311,6 +328,53 @@ def legend_test(self):
|
311 | 328 | self.mComposition.removeComposerItem(legend)
|
312 | 329 | QgsProject.instance().removeMapLayer(ptLayer.id())
|
313 | 330 |
|
| 331 | + def rotation_test(self): |
| 332 | + # We will create a polygon layer with a rotated rectangle. |
| 333 | + # Then we will make it the object layer for the atlas, |
| 334 | + # rotate the map and test that the bounding rectangle |
| 335 | + # is smaller than the bounds without rotation. |
| 336 | + polygonLayer = QgsVectorLayer('Polygon', 'test_polygon', 'memory') |
| 337 | + poly = QgsFeature(polygonLayer.pendingFields()) |
| 338 | + points = [(10, 15), (15, 10), (45, 40), (40, 45)] |
| 339 | + poly.setGeometry(QgsGeometry.fromPolygon([[QgsPointXY(x[0], x[1]) for x in points]])) |
| 340 | + polygonLayer.dataProvider().addFeatures([poly]) |
| 341 | + QgsProject.instance().addMapLayer(polygonLayer) |
| 342 | + |
| 343 | + # Recreating the composer locally |
| 344 | + composition = QgsComposition(QgsProject.instance()) |
| 345 | + composition.setPaperSize(297, 210) |
| 346 | + |
| 347 | + # the atlas map |
| 348 | + atlasMap = QgsComposerMap(composition, 20, 20, 130, 130) |
| 349 | + atlasMap.setFrameEnabled(True) |
| 350 | + atlasMap.setLayers([polygonLayer]) |
| 351 | + atlasMap.setNewExtent(QgsRectangle(0, 0, 100, 50)) |
| 352 | + composition.addComposerMap(atlasMap) |
| 353 | + |
| 354 | + # the atlas |
| 355 | + atlas = composition.atlasComposition() |
| 356 | + atlas.setCoverageLayer(polygonLayer) |
| 357 | + atlas.setEnabled(True) |
| 358 | + composition.setAtlasMode(QgsComposition.ExportAtlas) |
| 359 | + |
| 360 | + atlasMap.setAtlasDriven(True) |
| 361 | + atlasMap.setAtlasScalingMode(QgsComposerMap.Auto) |
| 362 | + atlasMap.setAtlasMargin(0.0) |
| 363 | + |
| 364 | + # Testing |
| 365 | + atlasMap.setMapRotation(0.0) |
| 366 | + atlas.firstFeature() |
| 367 | + nonRotatedExtent = QgsRectangle(atlasMap.currentMapExtent()) |
| 368 | + |
| 369 | + atlasMap.setMapRotation(45.0) |
| 370 | + atlas.firstFeature() |
| 371 | + rotatedExtent = QgsRectangle(atlasMap.currentMapExtent()) |
| 372 | + |
| 373 | + assert rotatedExtent.width() < nonRotatedExtent.width() * 0.9 |
| 374 | + assert rotatedExtent.height() < nonRotatedExtent.height() * 0.9 |
| 375 | + |
| 376 | + QgsProject.instance().removeMapLayer(polygonLayer) |
| 377 | + |
314 | 378 |
|
315 | 379 | if __name__ == '__main__':
|
316 | 380 | unittest.main()
|
0 commit comments