Skip to content

Commit

Permalink
Remove deprecated QgsCoordinateTransform ctor ...
Browse files Browse the repository at this point in the history
... 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 5f3dc50
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 40 deletions.
1 change: 0 additions & 1 deletion python/core/auto_generated/qgscoordinatetransform.sip.in
Expand Up @@ -50,7 +50,6 @@ transforms coordinates from the layer's coordinate system to the map canvas.
Default constructor, creates an invalid QgsCoordinateTransform. Default constructor, creates an invalid QgsCoordinateTransform.
%End %End



explicit QgsCoordinateTransform( const QgsCoordinateReferenceSystem &source, explicit QgsCoordinateTransform( const QgsCoordinateReferenceSystem &source,
const QgsCoordinateReferenceSystem &destination, const QgsCoordinateReferenceSystem &destination,
const QgsCoordinateTransformContext &context ); const QgsCoordinateTransformContext &context );
Expand Down
9 changes: 9 additions & 0 deletions python/core/auto_generated/qgsmaplayer.sip.in
Expand Up @@ -617,6 +617,15 @@ Returns the layer's spatial reference system.
Sets layer's spatial reference system Sets layer's spatial reference system
%End %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 ); static QString formatLayerName( const QString &name );
%Docstring %Docstring
A convenience function to capitalize and format a layer ``name``. A convenience function to capitalize and format a layer ``name``.
Expand Down
4 changes: 1 addition & 3 deletions src/core/processing/qgsprocessingutils.cpp
Expand Up @@ -638,9 +638,7 @@ QgsRectangle QgsProcessingUtils::combineLayerExtents( const QList<QgsMapLayer *>
if ( crs.isValid() ) if ( crs.isValid() )
{ {
//transform layer extent to target CRS //transform layer extent to target CRS
Q_NOWARN_DEPRECATED_PUSH QgsCoordinateTransform ct( layer->crs(), crs, QgsProject::instance()->transformContext() );
QgsCoordinateTransform ct( layer->crs(), crs );
Q_NOWARN_DEPRECATED_POP
try try
{ {
QgsRectangle reprojExtent = ct.transformBoundingBox( layer->extent() ); QgsRectangle reprojExtent = ct.transformBoundingBox( layer->extent() );
Expand Down
14 changes: 0 additions & 14 deletions src/core/qgscoordinatetransform.cpp
Expand Up @@ -52,20 +52,6 @@ QgsCoordinateTransform::QgsCoordinateTransform()
d = new QgsCoordinateTransformPrivate(); 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 ) QgsCoordinateTransform::QgsCoordinateTransform( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination, const QgsCoordinateTransformContext &context )
{ {
mContext = context; mContext = context;
Expand Down
13 changes: 0 additions & 13 deletions src/core/qgscoordinatetransform.h
Expand Up @@ -64,19 +64,6 @@ class CORE_EXPORT QgsCoordinateTransform
//! Default constructor, creates an invalid QgsCoordinateTransform. //! Default constructor, creates an invalid QgsCoordinateTransform.
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 * Constructs a QgsCoordinateTransform to transform from the \a source
* to \a destination coordinate reference system. * to \a destination coordinate reference system.
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -782,6 +782,11 @@ void QgsMapLayer::setCrs( const QgsCoordinateReferenceSystem &srs, bool emitSign
emit crsChanged(); emit crsChanged();
} }


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

QString QgsMapLayer::formatLayerName( const QString &name ) QString QgsMapLayer::formatLayerName( const QString &name )
{ {
QString layerName( name ); QString layerName( name );
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsmaplayer.h
Expand Up @@ -612,6 +612,14 @@ class CORE_EXPORT QgsMapLayer : public QObject
//! Sets layer's spatial reference system //! Sets layer's spatial reference system
void setCrs( const QgsCoordinateReferenceSystem &srs, bool emitSignal = true ); 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. * A convenience function to capitalize and format a layer \a name.
* *
Expand Down
4 changes: 1 addition & 3 deletions src/core/qgsvectorfilewriter.cpp
Expand Up @@ -2495,9 +2495,7 @@ QgsVectorFileWriter::writeAsVectorFormat( QgsVectorLayer *layer,
QgsCoordinateTransform ct; QgsCoordinateTransform ct;
if ( destCRS.isValid() && layer ) if ( destCRS.isValid() && layer )
{ {
Q_NOWARN_DEPRECATED_PUSH ct = QgsCoordinateTransform( layer->crs(), destCRS, layer->transformContext() );
ct = QgsCoordinateTransform( layer->crs(), destCRS );
Q_NOWARN_DEPRECATED_POP
} }


QgsVectorFileWriter::WriterError error = writeAsVectorFormat( layer, fileName, fileEncoding, ct, driverName, onlySelected, QgsVectorFileWriter::WriterError error = writeAsVectorFormat( layer, fileName, fileEncoding, ct, driverName, onlySelected,
Expand Down
4 changes: 1 addition & 3 deletions src/core/qgsvectorlayerexporter.cpp
Expand Up @@ -351,9 +351,7 @@ QgsVectorLayerExporter::exportLayer( QgsVectorLayer *layer,
// Create our transform // Create our transform
if ( destCRS.isValid() ) if ( destCRS.isValid() )
{ {
Q_NOWARN_DEPRECATED_PUSH ct = QgsCoordinateTransform( layer->crs(), destCRS, layer->transformContext() );
ct = QgsCoordinateTransform( layer->crs(), destCRS );
Q_NOWARN_DEPRECATED_POP
} }


// Check for failure // Check for failure
Expand Down
4 changes: 1 addition & 3 deletions src/core/raster/qgsrasterfilewriter.cpp
Expand Up @@ -231,9 +231,7 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeDataRaster( const Qgs
QgsRasterProjector *projector = pipe->projector(); QgsRasterProjector *projector = pipe->projector();
if ( projector && projector->destinationCrs() != projector->sourceCrs() ) if ( projector && projector->destinationCrs() != projector->sourceCrs() )
{ {
Q_NOWARN_DEPRECATED_PUSH QgsCoordinateTransform ct( projector->destinationCrs(), projector->sourceCrs(), srcProvider->transformContext() );
QgsCoordinateTransform ct( projector->destinationCrs(), projector->sourceCrs() );
Q_NOWARN_DEPRECATED_POP
srcExtent = ct.transformBoundingBox( outputExtent ); srcExtent = ct.transformBoundingBox( outputExtent );
} }
if ( !srcProvider->extent().contains( srcExtent ) ) if ( !srcProvider->extent().contains( srcExtent ) )
Expand Down
15 changes: 15 additions & 0 deletions tests/src/python/test_qgsvectorlayer.py
Expand Up @@ -57,6 +57,7 @@
QgsTextFormat, QgsTextFormat,
QgsVectorLayerSelectedFeatureSource, QgsVectorLayerSelectedFeatureSource,
QgsExpression, QgsExpression,
QgsCoordinateTransformContext,
NULL) NULL)
from qgis.gui import (QgsAttributeTableModel, from qgis.gui import (QgsAttributeTableModel,
QgsGui QgsGui
Expand Down Expand Up @@ -3166,6 +3167,20 @@ def testMaximumValue(self):
""" """
pass 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: # TODO:
# - fetch rect: feat with changed geometry: 1. in rect, 2. out of rect # - fetch rect: feat with changed geometry: 1. in rect, 2. out of rect
# - more join tests # - more join tests
Expand Down

0 comments on commit 5f3dc50

Please sign in to comment.