Skip to content
Permalink
Browse files

Remove deprecated QgsCoordinateTransform ctor ...

... and add a getter for coordinateTransform to QgsMapLayer

The reason is that I think we should reduce the dataProvider API
usage and rely on the higher level QgsMapLayer API whenever it
is possible, QgsMapLayer checks for data provider validity
and returns a default constructed QgsCoordinateTransform instead
of crashing.
  • Loading branch information
elpaso authored and nyalldawson committed Apr 17, 2019
1 parent 7f271d1 commit 5f3dc50cccf021eae19f527db060b431df8f6319
@@ -50,7 +50,6 @@ transforms coordinates from the layer's coordinate system to the map canvas.
Default constructor, creates an invalid QgsCoordinateTransform.
%End


explicit QgsCoordinateTransform( const QgsCoordinateReferenceSystem &source,
const QgsCoordinateReferenceSystem &destination,
const QgsCoordinateTransformContext &context );
@@ -617,6 +617,15 @@ Returns the layer's spatial reference system.
Sets layer's spatial reference system
%End

QgsCoordinateTransformContext transformContext( ) const;
%Docstring
Returns the layer data provider coordinate transform context
or a default transform context if the layer does not have a valid data provider.

.. versionadded:: 3.10
%End


static QString formatLayerName( const QString &name );
%Docstring
A convenience function to capitalize and format a layer ``name``.
@@ -638,9 +638,7 @@ QgsRectangle QgsProcessingUtils::combineLayerExtents( const QList<QgsMapLayer *>
if ( crs.isValid() )
{
//transform layer extent to target CRS
Q_NOWARN_DEPRECATED_PUSH
QgsCoordinateTransform ct( layer->crs(), crs );
Q_NOWARN_DEPRECATED_POP
QgsCoordinateTransform ct( layer->crs(), crs, QgsProject::instance()->transformContext() );
try
{
QgsRectangle reprojExtent = ct.transformBoundingBox( layer->extent() );
@@ -52,20 +52,6 @@ QgsCoordinateTransform::QgsCoordinateTransform()
d = new QgsCoordinateTransformPrivate();
}

QgsCoordinateTransform::QgsCoordinateTransform( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination )
{
d = new QgsCoordinateTransformPrivate( source, destination, QgsCoordinateTransformContext() );

if ( !d->checkValidity() )
return;

if ( !setFromCache( d->mSourceCRS, d->mDestCRS, d->mSourceDatumTransform, d->mDestinationDatumTransform ) )
{
d->initialize();
addToCache();
}
}

QgsCoordinateTransform::QgsCoordinateTransform( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination, const QgsCoordinateTransformContext &context )
{
mContext = context;
@@ -64,19 +64,6 @@ class CORE_EXPORT QgsCoordinateTransform
//! Default constructor, creates an invalid QgsCoordinateTransform.
QgsCoordinateTransform();

/**
* Constructs a QgsCoordinateTransform using QgsCoordinateReferenceSystem objects.
* \param source source CRS, typically of the layer's coordinate system
* \param destination CRS, typically of the map canvas coordinate system
* correctly handle the user's datum transform setup. Instead the constructor
* variant which accepts a QgsCoordinateTransformContext or QgsProject
* argument should be used instead. It is highly likely that this constructor
* will be removed in future QGIS versions.
* \note Not available in Python bindings.
* \deprecated Use of this constructor is strongly discouraged, as it will not
*/
Q_DECL_DEPRECATED explicit QgsCoordinateTransform( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination ) SIP_SKIP;

/**
* Constructs a QgsCoordinateTransform to transform from the \a source
* to \a destination coordinate reference system.
@@ -782,6 +782,11 @@ void QgsMapLayer::setCrs( const QgsCoordinateReferenceSystem &srs, bool emitSign
emit crsChanged();
}

QgsCoordinateTransformContext QgsMapLayer::transformContext() const
{
return dataProvider() ? dataProvider()->transformContext() : QgsCoordinateTransformContext();
}

QString QgsMapLayer::formatLayerName( const QString &name )
{
QString layerName( name );
@@ -612,6 +612,14 @@ class CORE_EXPORT QgsMapLayer : public QObject
//! Sets layer's spatial reference system
void setCrs( const QgsCoordinateReferenceSystem &srs, bool emitSignal = true );

/**
* Returns the layer data provider coordinate transform context
* or a default transform context if the layer does not have a valid data provider.
\since QGIS 3.10
*/
QgsCoordinateTransformContext transformContext( ) const;


/**
* A convenience function to capitalize and format a layer \a name.
*
@@ -2495,9 +2495,7 @@ QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer *layer,
QgsCoordinateTransform ct;
if ( destCRS.isValid() && layer )
{
Q_NOWARN_DEPRECATED_PUSH
ct = QgsCoordinateTransform( layer->crs(), destCRS );
Q_NOWARN_DEPRECATED_POP
ct = QgsCoordinateTransform( layer->crs(), destCRS, layer->transformContext() );
}

QgsVectorFileWriter::WriterError error = writeAsVectorFormat( layer, fileName, fileEncoding, ct, driverName, onlySelected,
@@ -351,9 +351,7 @@ QgsVectorLayerExporter::exportLayer( QgsVectorLayer *layer,
// Create our transform
if ( destCRS.isValid() )
{
Q_NOWARN_DEPRECATED_PUSH
ct = QgsCoordinateTransform( layer->crs(), destCRS );
Q_NOWARN_DEPRECATED_POP
ct = QgsCoordinateTransform( layer->crs(), destCRS, layer->transformContext() );
}

// Check for failure
@@ -231,9 +231,7 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeDataRaster( const Qgs
QgsRasterProjector *projector = pipe->projector();
if ( projector && projector->destinationCrs() != projector->sourceCrs() )
{
Q_NOWARN_DEPRECATED_PUSH
QgsCoordinateTransform ct( projector->destinationCrs(), projector->sourceCrs() );
Q_NOWARN_DEPRECATED_POP
QgsCoordinateTransform ct( projector->destinationCrs(), projector->sourceCrs(), srcProvider->transformContext() );
srcExtent = ct.transformBoundingBox( outputExtent );
}
if ( !srcProvider->extent().contains( srcExtent ) )
@@ -57,6 +57,7 @@
QgsTextFormat,
QgsVectorLayerSelectedFeatureSource,
QgsExpression,
QgsCoordinateTransformContext,
NULL)
from qgis.gui import (QgsAttributeTableModel,
QgsGui
@@ -3166,6 +3167,20 @@ def testMaximumValue(self):
"""
pass


class TestQgsVectorLayerTransformContext(unittest.TestCase):

def testTransformContext(self):
vl = QgsVectorLayer(
'Point?crs=epsg:4326&field=pk:integer&field=cnt:integer&field=name:string(0)&field=name2:string(0)&field=num_char:string&key=pk',
'test', 'memory')
ctx = QgsCoordinateTransformContext()
ctx.addSourceDestinationDatumTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857), 1234, 1235)
vl.dataProvider().setTransformContext(ctx)
self.assertTrue(vl.isValid())
self.assertEqual(vl.transformContext(), vl.dataProvider().transformContext())
self.assertEqual(list(vl.transformContext().sourceDestinationDatumTransforms().keys())[0], ('EPSG:4326', 'EPSG:3857'))

# TODO:
# - fetch rect: feat with changed geometry: 1. in rect, 2. out of rect
# - more join tests

0 comments on commit 5f3dc50

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