1515import qgis # NOQA
1616import os
1717
18- from qgis .core import QgsRectangle , QgsCoordinateReferenceSystem , QgsVectorLayer , QgsProject
18+ from qgis .core import QgsRectangle , QgsCoordinateReferenceSystem , QgsVectorLayer , QgsProject , QgsFeature , QgsGeometry
1919from qgis .gui import QgsExtentGroupBox
2020
2121from qgis .PyQt .QtTest import QSignalSpy
@@ -82,6 +82,8 @@ def test_SettingExtent(self):
8282 self .assertEqual (w .extentState (), QgsExtentGroupBox .ProjectLayerExtent )
8383 self .assertEqual (len (spy ), 4 )
8484
85+ QgsProject .instance ().removeAllMapLayers ()
86+
8587 def testSetOutputCrs (self ):
8688 w = qgis .gui .QgsExtentGroupBox ()
8789
@@ -90,14 +92,66 @@ def testSetOutputCrs(self):
9092 w .setOutputExtentFromCurrent ()
9193 self .assertEqual (w .outputExtent (), QgsRectangle (1 , 2 , 3 , 4 ))
9294
93- # no reprojection
94- w .setOutputCrs (QgsCoordinateReferenceSystem ('epsg:3785' ), False )
95+ # with reprojection
96+ w .setOutputCrs (QgsCoordinateReferenceSystem ('epsg:3785' ))
97+ self .assertEqual (w .outputExtent ().toString (4 ), QgsRectangle (111319.4908 , 222684.2085 , 333958.4724 , 445640.1097 ).toString (4 ))
98+ # change CRS back
99+ w .setOutputCrs (QgsCoordinateReferenceSystem ('epsg:4326' ))
100+ # extent should be back to current - not a reprojection of the reprojected bounds
101+ self .assertEqual (w .outputExtent ().toString (20 ), QgsRectangle (1 , 2 , 3 , 4 ).toString (20 ))
102+
103+ # repeat, this time using original extents
104+ w = qgis .gui .QgsExtentGroupBox ()
105+
106+ w .setOutputCrs (QgsCoordinateReferenceSystem ('epsg:4326' ))
107+ w .setOriginalExtent (QgsRectangle (1 , 2 , 3 , 4 ), QgsCoordinateReferenceSystem ('epsg:4326' ))
108+ w .setOutputExtentFromOriginal ()
95109 self .assertEqual (w .outputExtent (), QgsRectangle (1 , 2 , 3 , 4 ))
96- w .setOutputCrs (QgsCoordinateReferenceSystem ('epsg:4326' ), False )
97110
98111 # with reprojection
99- w .setOutputCrs (QgsCoordinateReferenceSystem ('epsg:3785' ), True )
100- self .assertEqual (w .outputExtent ().toString (4 ), QgsRectangle (111319.4908 , 222684.2085 , 333958.4724 , 445640.1097 ).toString (4 ))
112+ w .setOutputCrs (QgsCoordinateReferenceSystem ('epsg:3785' ))
113+ self .assertEqual (w .outputExtent ().toString (4 ),
114+ QgsRectangle (111319.4908 , 222684.2085 , 333958.4724 , 445640.1097 ).toString (4 ))
115+ # change CRS back
116+ w .setOutputCrs (QgsCoordinateReferenceSystem ('epsg:4326' ))
117+ # extent should be back to original - not a reprojection of the reprojected bounds
118+ self .assertEqual (w .outputExtent ().toString (20 ), QgsRectangle (1 , 2 , 3 , 4 ).toString (20 ))
119+
120+ # repeat, this time using layer extent
121+ layer = QgsVectorLayer ("Polygon?crs=4326" , 'memory' , 'memory' )
122+ self .assertTrue (layer .isValid ())
123+ f = QgsFeature ()
124+ f .setGeometry (QgsGeometry .fromWkt ('Polygon((1 2, 3 2, 3 4, 1 4, 1 2))' ))
125+ layer .dataProvider ().addFeatures ([f ])
126+ QgsProject .instance ().addMapLayer (layer )
127+ w .setOutputCrs (QgsCoordinateReferenceSystem ('epsg:4326' ))
128+ w .setOutputExtentFromLayer (layer )
129+ self .assertEqual (w .outputExtent (), QgsRectangle (1 , 2 , 3 , 4 ))
130+
131+ w .setOutputCrs (QgsCoordinateReferenceSystem ('epsg:3785' ))
132+ self .assertEqual (w .outputExtent ().toString (4 ),
133+ QgsRectangle (111319.4908 , 222684.2085 , 333958.4724 , 445640.1097 ).toString (4 ))
134+ # change CRS back
135+ w .setOutputCrs (QgsCoordinateReferenceSystem ('epsg:4326' ))
136+ # extent should be back to original - not a reprojection of the reprojected bounds
137+ self .assertEqual (w .outputExtent ().toString (20 ), QgsRectangle (1 , 2 , 3 , 4 ).toString (20 ))
138+
139+ # custom extent
140+ w = qgis .gui .QgsExtentGroupBox ()
141+
142+ w .setOutputCrs (QgsCoordinateReferenceSystem ('epsg:4326' ))
143+ w .setOutputExtentFromUser (QgsRectangle (1 , 2 , 3 , 4 ), QgsCoordinateReferenceSystem ('epsg:4326' ))
144+ self .assertEqual (w .outputExtent (), QgsRectangle (1 , 2 , 3 , 4 ))
145+
146+ # with reprojection
147+ w .setOutputCrs (QgsCoordinateReferenceSystem ('epsg:3785' ))
148+ self .assertEqual (w .outputExtent ().toString (4 ),
149+ QgsRectangle (111319.4908 , 222684.2085 , 333958.4724 , 445640.1097 ).toString (4 ))
150+ # change CRS back
151+ w .setOutputCrs (QgsCoordinateReferenceSystem ('epsg:4326' ))
152+ # in this case we can't retrieve the original user extent in 4326, so we have a reprojection of the reprojected bounds
153+ # just test this by restricting the test to 4 decimals
154+ self .assertEqual (w .outputExtent ().toString (4 ), QgsRectangle (1 , 2 , 3 , 4 ).toString (4 ))
101155
102156
103157if __name__ == '__main__' :
0 commit comments