Skip to content

Commit d2353e7

Browse files
committed
Add method to determine whether transform context has a valid transform
for a specific src/dest CRS pair
1 parent f55da4a commit d2353e7

5 files changed

+29
-3
lines changed

python/core/qgscoordinatetransformcontext.sip

+8
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ class QgsCoordinateTransformContext
110110
.. seealso:: addSourceDestinationDatumTransform()
111111
%End
112112

113+
bool hasTransform( const QgsCoordinateReferenceSystem &source,
114+
const QgsCoordinateReferenceSystem &destination ) const;
115+
%Docstring
116+
Returns true if the context has a valid datum transform to use
117+
when transforming from the specified ``source`` CRS to ``destination`` CRS.
118+
:rtype: bool
119+
%End
120+
113121
QPair< int, int > calculateDatumTransforms( const QgsCoordinateReferenceSystem &source,
114122
const QgsCoordinateReferenceSystem &destination ) const;
115123
%Docstring

src/app/qgisapp.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -12535,10 +12535,8 @@ bool QgisApp::askForDatumTransform( QgsCoordinateReferenceSystem sourceCrs, QgsC
1253512535
bool ok = false;
1253612536

1253712537
QgsCoordinateTransformContext context = QgsProject::instance()->transformContext();
12538-
QPair<int, int> dt = context.calculateDatumTransforms( sourceCrs, destinationCrs );
12539-
if ( dt != qMakePair( -1, -1 ) )
12538+
if ( context.hasTransform( sourceCrs, destinationCrs ) )
1254012539
{
12541-
// already defined by user
1254212540
ok = true;
1254312541
}
1254412542
else

src/core/qgscoordinatetransformcontext.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ void QgsCoordinateTransformContext::removeSourceDestinationDatumTransform( const
124124
d->mSourceDestDatumTransforms.remove( qMakePair( sourceCrs.authid(), destinationCrs.authid() ) );
125125
}
126126

127+
bool QgsCoordinateTransformContext::hasTransform( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination ) const
128+
{
129+
QPair<int, int> t = calculateDatumTransforms( source, destination );
130+
return t.first != -1 || t.second != -1;
131+
}
132+
127133
QPair<int, int> QgsCoordinateTransformContext::calculateDatumTransforms( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination ) const
128134
{
129135
QString srcKey = source.authid();

src/core/qgscoordinatetransformcontext.h

+7
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ class CORE_EXPORT QgsCoordinateTransformContext
209209
void removeSourceDestinationDatumTransform( const QgsCoordinateReferenceSystem &sourceCrs,
210210
const QgsCoordinateReferenceSystem &destinationCrs );
211211

212+
/**
213+
* Returns true if the context has a valid datum transform to use
214+
* when transforming from the specified \a source CRS to \a destination CRS.
215+
*/
216+
bool hasTransform( const QgsCoordinateReferenceSystem &source,
217+
const QgsCoordinateReferenceSystem &destination ) const;
218+
212219
/**
213220
* Returns the pair of source and destination datum transforms to use
214221
* for a transform from the specified \a source CRS to \a destination CRS.

tests/src/python/test_qgscoordinatetransformcontext.py

+7
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,15 @@ def testDestDatumTransforms(self):
9494
def testSourceDestinationDatumTransforms(self):
9595
context = QgsCoordinateTransformContext()
9696
self.assertEqual(context.sourceDestinationDatumTransforms(), {})
97+
self.assertFalse(context.hasTransform(QgsCoordinateReferenceSystem('EPSG:3111'), QgsCoordinateReferenceSystem('EPSG:4283')))
9798
self.assertTrue(context.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem('EPSG:3111'),
9899
QgsCoordinateReferenceSystem('EPSG:4283'), 1, 2))
100+
self.assertTrue(
101+
context.hasTransform(QgsCoordinateReferenceSystem('EPSG:3111'), QgsCoordinateReferenceSystem('EPSG:4283')))
102+
self.assertFalse(
103+
context.hasTransform(QgsCoordinateReferenceSystem('EPSG:3111'), QgsCoordinateReferenceSystem('EPSG:4326')))
104+
self.assertFalse(
105+
context.hasTransform(QgsCoordinateReferenceSystem('EPSG:3113'), QgsCoordinateReferenceSystem('EPSG:4283')))
99106
self.assertEqual(context.sourceDestinationDatumTransforms(), {('EPSG:3111', 'EPSG:4283'): (1, 2)})
100107
self.assertTrue(context.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem('EPSG:28356'),
101108
QgsCoordinateReferenceSystem(4283), 3, 4))

0 commit comments

Comments
 (0)