Ensure extent is updated when QgsExtentGroupBox crs is changed

This commit is contained in:
Nyall Dawson 2017-05-31 21:18:44 +10:00
parent 5649c405c9
commit e2de69d32d
5 changed files with 41 additions and 5 deletions

View File

@ -87,10 +87,12 @@ class QgsExtentGroupBox : QgsCollapsibleGroupBox
:rtype: QgsCoordinateReferenceSystem
%End
void setOutputCrs( const QgsCoordinateReferenceSystem &outputCrs );
void setOutputCrs( const QgsCoordinateReferenceSystem &outputCrs, bool reprojectCurrentExtent = true );
%Docstring
Sets the output CRS - may need to be used for transformation from original/current extent.
Should be called as part of initialization and whenever the the output CRS is changed.
If ``reprojectCurrentExtent`` is true then the current extent will be reproject into the
new output CRS.
%End
QgsRectangle outputExtent() const;

View File

@ -61,12 +61,27 @@ void QgsExtentGroupBox::setCurrentExtent( const QgsRectangle &currentExtent, con
mCurrentCrs = currentCrs;
}
void QgsExtentGroupBox::setOutputCrs( const QgsCoordinateReferenceSystem &outputCrs )
void QgsExtentGroupBox::setOutputCrs( const QgsCoordinateReferenceSystem &outputCrs, bool reprojectCurrentExtent )
{
if ( reprojectCurrentExtent && mOutputCrs != outputCrs )
{
try
{
QgsCoordinateTransform ct( mOutputCrs, outputCrs );
QgsRectangle extent = ct.transformBoundingBox( outputExtent() );
mXMinLineEdit->setText( QgsRasterBlock::printValue( extent.xMinimum() ) );
mXMaxLineEdit->setText( QgsRasterBlock::printValue( extent.xMaximum() ) );
mYMinLineEdit->setText( QgsRasterBlock::printValue( extent.yMinimum() ) );
mYMaxLineEdit->setText( QgsRasterBlock::printValue( extent.yMaximum() ) );
}
catch ( QgsCsException & )
{
// can't reproject
}
}
mOutputCrs = outputCrs;
}
void QgsExtentGroupBox::setOutputExtent( const QgsRectangle &r, const QgsCoordinateReferenceSystem &srcCrs, ExtentState state )
{
QgsRectangle extent;

View File

@ -104,8 +104,10 @@ class GUI_EXPORT QgsExtentGroupBox : public QgsCollapsibleGroupBox, private Ui::
/**
* Sets the output CRS - may need to be used for transformation from original/current extent.
* Should be called as part of initialization and whenever the the output CRS is changed.
* If \a reprojectCurrentExtent is true then the current extent will be reproject into the
* new output CRS.
*/
void setOutputCrs( const QgsCoordinateReferenceSystem &outputCrs );
void setOutputCrs( const QgsCoordinateReferenceSystem &outputCrs, bool reprojectCurrentExtent = true );
/**
* Returns the extent shown in the widget - in output CRS coordinates.

View File

@ -435,7 +435,7 @@ void QgsRasterLayerSaveAsDialog::crsChanged()
{
if ( outputCrs() != mPreviousCrs )
{
mExtentGroupBox->setOutputCrs( outputCrs() );
mExtentGroupBox->setOutputCrs( outputCrs(), false );
QgsExtentGroupBox::ExtentState state = mExtentGroupBox->extentState();
// Reset extent

View File

@ -82,6 +82,23 @@ class TestQgsExtentGroupBox(unittest.TestCase):
self.assertEqual(w.extentState(), QgsExtentGroupBox.ProjectLayerExtent)
self.assertEqual(len(spy), 4)
def testSetOutputCrs(self):
w = qgis.gui.QgsExtentGroupBox()
w.setOutputCrs(QgsCoordinateReferenceSystem('epsg:4326'))
w.setCurrentExtent(QgsRectangle(1, 2, 3, 4), QgsCoordinateReferenceSystem('epsg:4326'))
w.setOutputExtentFromCurrent()
self.assertEqual(w.outputExtent(), QgsRectangle(1, 2, 3, 4))
# no reprojection
w.setOutputCrs(QgsCoordinateReferenceSystem('epsg:3785'), False)
self.assertEqual(w.outputExtent(), QgsRectangle(1, 2, 3, 4))
w.setOutputCrs(QgsCoordinateReferenceSystem('epsg:4326'), False)
# with reprojection
w.setOutputCrs(QgsCoordinateReferenceSystem('epsg:3785'), True)
self.assertEqual(w.outputExtent().toString(4), QgsRectangle(111319.4908, 222684.2085, 333958.4724, 445640.1097).toString(4))
if __name__ == '__main__':
unittest.main()