Skip to content

Commit

Permalink
Ensure extent is updated when QgsExtentGroupBox crs is changed
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 31, 2017
1 parent 5649c40 commit e2de69d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
4 changes: 3 additions & 1 deletion python/gui/qgsextentgroupbox.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 17 additions & 2 deletions src/gui/qgsextentgroupbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/gui/qgsextentgroupbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsrasterlayersaveasdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ void QgsRasterLayerSaveAsDialog::crsChanged()
{
if ( outputCrs() != mPreviousCrs )
{
mExtentGroupBox->setOutputCrs( outputCrs() );
mExtentGroupBox->setOutputCrs( outputCrs(), false );
QgsExtentGroupBox::ExtentState state = mExtentGroupBox->extentState();

// Reset extent
Expand Down
17 changes: 17 additions & 0 deletions tests/src/python/test_qgsextentgroupbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.