Skip to content

Commit 391f76b

Browse files
committed
A test for atlas feature extent after rotation
1 parent 1aa2a4e commit 391f76b

File tree

1 file changed

+67
-3
lines changed

1 file changed

+67
-3
lines changed

tests/src/python/test_qgsatlascomposition.py

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,25 @@
2222
from qgis.testing import start_app, unittest
2323
from utilities import unitTestDataPath
2424
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+
)
2844
from qgscompositionchecker import QgsCompositionChecker
2945

3046
start_app()
@@ -113,6 +129,7 @@ def testCase(self):
113129
self.predefinedscales_render_test()
114130
self.hidden_render_test()
115131
self.legend_test()
132+
self.rotation_test()
116133

117134
shutil.rmtree(tmppath, True)
118135

@@ -311,6 +328,53 @@ def legend_test(self):
311328
self.mComposition.removeComposerItem(legend)
312329
QgsProject.instance().removeMapLayer(ptLayer.id())
313330

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+
314378

315379
if __name__ == '__main__':
316380
unittest.main()

0 commit comments

Comments
 (0)