Skip to content
Permalink
Browse files

Ensure extent is updated when QgsExtentGroupBox crs is changed

  • Loading branch information
nyalldawson committed May 31, 2017
1 parent 5649c40 commit e2de69d32d9a6263f2c9c9f4d7d92eeec1596968
@@ -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;
@@ -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;
@@ -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.
@@ -435,7 +435,7 @@ void QgsRasterLayerSaveAsDialog::crsChanged()
{
if ( outputCrs() != mPreviousCrs )
{
mExtentGroupBox->setOutputCrs( outputCrs() );
mExtentGroupBox->setOutputCrs( outputCrs(), false );
QgsExtentGroupBox::ExtentState state = mExtentGroupBox->extentState();

// Reset extent
@@ -82,6 +82,23 @@ def test_SettingExtent(self):
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()

0 comments on commit e2de69d

Please sign in to comment.
You can’t perform that action at this time.