Skip to content
Permalink
Browse files

Add API to auto set QgsCoordinateOperationWidget selection to match

a QgsCoordinateTransformContext's state
  • Loading branch information
nyalldawson committed Dec 20, 2019
1 parent afc24b7 commit 521080cd1321da0ebb0b5cfcfd0a7fe8908969ed
@@ -120,6 +120,14 @@ Returns the details of the operation currently selected within the widget.
Sets the details of the ``operation`` currently selected within the widget.

.. seealso:: :py:func:`selectedOperation`
%End

void setSelectedOperationUsingContext( const QgsCoordinateTransformContext &context );
%Docstring
Automatically sets the selected operation using the settings encapsulated in a transform ``context``.

If no matching operations are found within the context then the defaultOperation() will be
selected.
%End

signals:
@@ -602,6 +602,39 @@ void QgsCoordinateOperationWidget::setSelectedOperation( const QgsCoordinateOper
}
}

void QgsCoordinateOperationWidget::setSelectedOperationUsingContext( const QgsCoordinateTransformContext &context )
{
#if PROJ_VERSION_MAJOR>=6
const QString op = context.calculateCoordinateOperation( mSourceCrs, mDestinationCrs );
if ( !op.isEmpty() )
{
OperationDetails deets;
deets.proj = op;
setSelectedOperation( deets );
}
else
{
setSelectedOperation( defaultOperation() );
}

#else
if ( context.hasTransform( mSourceCrs, mDestinationCrs ) )
{
Q_NOWARN_DEPRECATED_PUSH
const QgsDatumTransform::TransformPair op = context.calculateDatumTransforms( mSourceCrs, mDestinationCrs );
Q_NOWARN_DEPRECATED_POP
OperationDetails deets;
deets.sourceTransformId = op.sourceTransformId;
deets.destinationTransformId = op.destinationTransformId;
setSelectedOperation( deets );
}
else
{
setSelectedOperation( defaultOperation() );
}
#endif
}

bool QgsCoordinateOperationWidget::gridShiftTransformation( const QString &itemText ) const
{
return !itemText.isEmpty() && !itemText.contains( QLatin1String( "towgs84" ), Qt::CaseInsensitive );
@@ -133,6 +133,14 @@ class GUI_EXPORT QgsCoordinateOperationWidget : public QWidget, private Ui::QgsC
*/
void setSelectedOperation( const QgsCoordinateOperationWidget::OperationDetails &operation ) const;

/**
* Automatically sets the selected operation using the settings encapsulated in a transform \a context.
*
* If no matching operations are found within the context then the defaultOperation() will be
* selected.
*/
void setSelectedOperationUsingContext( const QgsCoordinateTransformContext &context );

signals:

/**
@@ -17,10 +17,7 @@
QgsProjUtils,
QgsDatumTransform,
QgsCoordinateReferenceSystem,
QgsVectorLayer,
QgsProject,
QgsFeature,
QgsGeometry)
QgsCoordinateTransformContext)
from qgis.gui import QgsCoordinateOperationWidget

from qgis.PyQt.QtTest import QSignalSpy
@@ -83,6 +80,22 @@ def testOperations(self):
'+proj=pipeline +step +inv +proj=utm +zone=55 +south +ellps=GRS80 +step +proj=push +v_3 +step +proj=cart +ellps=GRS80 +step +proj=helmert +x=0.06155 +y=-0.01087 +z=-0.04019 +rx=-0.0394924 +ry=-0.0327221 +rz=-0.0328979 +s=-0.009994 +convention=coordinate_frame +step +inv +proj=cart +ellps=GRS80 +step +proj=pop +v_3 +step +proj=utm +zone=55 +south +ellps=GRS80')
self.assertEqual(len(spy), 3)

context = QgsCoordinateTransformContext()
op.proj = '+proj=pipeline +step +inv +proj=utm +zone=55 +south +ellps=GRS80 +step +proj=hgridshift +grids=GDA94_GDA2020_conformal_and_distortion.gsb +step +proj=utm +zone=55 +south +ellps=GRS80'
w.setSelectedOperation(op)
w.setSelectedOperationUsingContext(context)
# should go to default, because there's nothing in the context matching these crs
self.assertEqual(w.selectedOperation().proj,
'+proj=pipeline +step +inv +proj=utm +zone=55 +south +ellps=GRS80 +step +proj=push +v_3 +step +proj=cart +ellps=GRS80 +step +proj=helmert +x=0.06155 +y=-0.01087 +z=-0.04019 +rx=-0.0394924 +ry=-0.0327221 +rz=-0.0328979 +s=-0.009994 +convention=coordinate_frame +step +inv +proj=cart +ellps=GRS80 +step +proj=pop +v_3 +step +proj=utm +zone=55 +south +ellps=GRS80')
self.assertEqual(len(spy), 5)

# put something in the context
context.addCoordinateOperation(w.sourceCrs(), w.destinationCrs(), '+proj=pipeline +step +inv +proj=utm +zone=55 +south +ellps=GRS80 +step +proj=hgridshift +grids=GDA94_GDA2020_conformal_and_distortion.gsb +step +proj=utm +zone=55 +south +ellps=GRS80')
w.setSelectedOperationUsingContext(context)
self.assertEqual(w.selectedOperation().proj,
'+proj=pipeline +step +inv +proj=utm +zone=55 +south +ellps=GRS80 +step +proj=hgridshift +grids=GDA94_GDA2020_conformal_and_distortion.gsb +step +proj=utm +zone=55 +south +ellps=GRS80')
self.assertEqual(len(spy), 6)

@unittest.skipIf(QgsProjUtils.projVersionMajor() >= 6, 'Not a crufty proj build')
def testOperationsCruftyProj(self):
w = QgsCoordinateOperationWidget()

0 comments on commit 521080c

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