Skip to content

Commit

Permalink
Do not store context in layer (but in provider) ...
Browse files Browse the repository at this point in the history
... also use transform context in raster projector
and deprecate setCrs without context
  • Loading branch information
elpaso authored and nyalldawson committed Apr 17, 2019
1 parent 7f52e29 commit b3a448a
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 56 deletions.
4 changes: 2 additions & 2 deletions python/core/auto_generated/mesh/qgsmeshlayer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ Interpolates the value on the given point from given dataset.

virtual void setTransformContext( const QgsCoordinateTransformContext &transformContext );
%Docstring
Triggered when the coordinate transform context has changed ``transformContext``
Sets the coordinate transform context to ``transformContext``.

.. versionadded:: 3.10
.. versionadded:: 3.8
%End


Expand Down
19 changes: 0 additions & 19 deletions python/core/auto_generated/qgsdataprovider.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -387,26 +387,7 @@ Returns ``True`` if metadata was successfully written to the data provider.
.. versionadded:: 3.0
%End

QgsCoordinateTransformContext transformContext() const;
%Docstring
Returns data provider coordinate transform context

.. seealso:: :py:func:`setTransformContext`

.. versionadded:: 3.10
%End

virtual void setTransformContext( const QgsCoordinateTransformContext &transformContext );
%Docstring
Sets data coordinate transform context to ``transformContext``

The default implementation is a simple setter, subclasses may override to perform
additional actions required by a change of coordinate transform context.

.. seealso:: :py:func:`transformContext`

.. versionadded:: 3.10
%End

signals:

Expand Down
4 changes: 2 additions & 2 deletions python/core/auto_generated/qgsvectorlayer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -2346,9 +2346,9 @@ rollBack().

virtual void setTransformContext( const QgsCoordinateTransformContext &transformContext );
%Docstring
Triggered when the coordinate transform context has changed ``transformContext``
Sets the coordinate transform context to ``transformContext``

.. versionadded:: 3.10
.. versionadded:: 3.8
%End

signals:
Expand Down
4 changes: 2 additions & 2 deletions python/core/auto_generated/raster/qgsrasterlayer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,9 @@ Writes the symbology of the layer into the document provided in SLD 1.0.0 format

virtual void setTransformContext( const QgsCoordinateTransformContext &transformContext );
%Docstring
Triggered when the coordinate transform context has changed ``transformContext``
Sets the coordinate transform context to ``transformContext``

.. versionadded:: 3.10
.. versionadded:: 3.8
%End

protected:
Expand Down
14 changes: 12 additions & 2 deletions python/core/auto_generated/raster/qgsrasterprojector.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,20 @@ which are used to calculate affine transformation matrices.
virtual Qgis::DataType dataType( int bandNo ) const;


void setCrs( const QgsCoordinateReferenceSystem &srcCRS, const QgsCoordinateReferenceSystem &destCRS,
int srcDatumTransform = -1, int destDatumTransform = -1 );
void setCrs( const QgsCoordinateReferenceSystem &srcCRS, const QgsCoordinateReferenceSystem &destCRS,
int srcDatumTransform = -1, int destDatumTransform = -1 ) /Deprecated/;
%Docstring
Sets the source and destination CRS

.. deprecated:: since QGIS 3.8, use transformContext version instead
%End

void setCrs( const QgsCoordinateReferenceSystem &srcCRS, const QgsCoordinateReferenceSystem &destCRS,
QgsCoordinateTransformContext transformContext );
%Docstring
Sets source CRS to ``srcCRS`` and destination CRS to ``destCRS`` and the transformation context to ``transformContext``

.. versionadded:: 3.8
%End

QgsCoordinateReferenceSystem sourceCrs() const;
Expand Down
8 changes: 6 additions & 2 deletions src/core/mesh/qgsmeshlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ QgsMeshLayer::QgsMeshLayer( const QString &meshLayerPath,
const QString &providerKey,
const QgsMeshLayer::LayerOptions &options )
: QgsMapLayer( QgsMapLayerType::MeshLayer, baseName, meshLayerPath )
, mOptions( options )
{
setProviderType( providerKey );
// if we’re given a provider type, try to create and bind one to this layer
Expand Down Expand Up @@ -89,7 +88,12 @@ const QgsMeshDataProvider *QgsMeshLayer::dataProvider() const

QgsMeshLayer *QgsMeshLayer::clone() const
{
QgsMeshLayer *layer = new QgsMeshLayer( source(), name(), mProviderKey, mOptions );
QgsMeshLayer::LayerOptions options;
if ( mDataProvider )
{
options.transformContext = mDataProvider->transformContext();
}
QgsMeshLayer *layer = new QgsMeshLayer( source(), name(), mProviderKey, options );
QgsMapLayer::clone( layer );
return layer;
}
Expand Down
6 changes: 2 additions & 4 deletions src/core/mesh/qgsmeshlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ class CORE_EXPORT QgsMeshLayer : public QgsMapLayer
public slots:

/**
* Triggered when the coordinate transform context has changed \a transformContext
* Sets the coordinate transform context to \a transformContext.
*
* \since QGIS 3.10
* \since QGIS 3.8
*/
virtual void setTransformContext( const QgsCoordinateTransformContext &transformContext ) override;

Expand Down Expand Up @@ -306,8 +306,6 @@ class CORE_EXPORT QgsMeshLayer : public QgsMapLayer
//! Time format configuration
QgsMeshTimeSettings mTimeSettings;

//! Layer options
QgsMeshLayer::LayerOptions mOptions;
};

#endif //QGSMESHLAYER_H
10 changes: 6 additions & 4 deletions src/core/qgsdataprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,10 @@ class CORE_EXPORT QgsDataProvider : public QObject
*
* \see setTransformContext()
*
* \since QGIS 3.10
* \since QGIS 3.8
* \note not available in Python bindings
*/
QgsCoordinateTransformContext transformContext() const;
QgsCoordinateTransformContext transformContext() const SIP_SKIP;

/**
* Sets data coordinate transform context to \a transformContext
Expand All @@ -539,9 +540,10 @@ class CORE_EXPORT QgsDataProvider : public QObject
*
* \see transformContext()
*
* \since QGIS 3.10
* \since QGIS 3.8
* \note not available in Python bindings
*/
virtual void setTransformContext( const QgsCoordinateTransformContext &transformContext );
virtual void setTransformContext( const QgsCoordinateTransformContext &transformContext ) SIP_SKIP;

signals:

Expand Down
8 changes: 6 additions & 2 deletions src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ QgsVectorLayer::QgsVectorLayer( const QString &vectorLayerPath,
, mAuxiliaryLayer( nullptr )
, mAuxiliaryLayerKey( QString() )
, mReadExtentFromXml( options.readExtentFromXml )
, mOptions( options )
{
setProviderType( providerKey );

Expand Down Expand Up @@ -208,7 +207,12 @@ QgsVectorLayer::~QgsVectorLayer()

QgsVectorLayer *QgsVectorLayer::clone() const
{
QgsVectorLayer *layer = new QgsVectorLayer( source(), name(), mProviderKey, mOptions );
QgsVectorLayer::LayerOptions options;
if ( mDataProvider )
{
options.transformContext = mDataProvider->transformContext();
}
QgsVectorLayer *layer = new QgsVectorLayer( source(), name(), mProviderKey, options );
QgsMapLayer::clone( layer );

QList<QgsVectorLayerJoinInfo> joins = vectorJoins();
Expand Down
6 changes: 2 additions & 4 deletions src/core/qgsvectorlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2161,9 +2161,9 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
bool startEditing();

/**
* Triggered when the coordinate transform context has changed \a transformContext
* Sets the coordinate transform context to \a transformContext
*
* \since QGIS 3.10
* \since QGIS 3.8
*/
virtual void setTransformContext( const QgsCoordinateTransformContext &transformContext ) override;

Expand Down Expand Up @@ -2593,8 +2593,6 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte

bool mAllowCommit = true;

QgsVectorLayer::LayerOptions mOptions;

friend class QgsVectorLayerFeatureSource;
};

Expand Down
7 changes: 6 additions & 1 deletion src/core/raster/qgsrasterlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@ QgsRasterLayer::~QgsRasterLayer()

QgsRasterLayer *QgsRasterLayer::clone() const
{
QgsRasterLayer *layer = new QgsRasterLayer( source(), name(), mProviderKey );
QgsRasterLayer::LayerOptions options;
if ( mDataProvider )
{
options.transformContext = mDataProvider->transformContext();
}
QgsRasterLayer *layer = new QgsRasterLayer( source(), name(), mProviderKey, options );
QgsMapLayer::clone( layer );

// do not clone data provider which is the first element in pipe
Expand Down
6 changes: 3 additions & 3 deletions src/core/raster/qgsrasterlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer

/**
* Coordinate transform context
* \since QGIS 3.10
* \since QGIS 3.8
*/
QgsCoordinateTransformContext transformContext = QgsCoordinateTransformContext();

Expand Down Expand Up @@ -439,9 +439,9 @@ class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
void showStatusMessage( const QString &message );

/**
* Triggered when the coordinate transform context has changed \a transformContext
* Sets the coordinate transform context to \a transformContext
*
* \since QGIS 3.10
* \since QGIS 3.8
*/
virtual void setTransformContext( const QgsCoordinateTransformContext &transformContext ) override;

Expand Down
44 changes: 37 additions & 7 deletions src/core/raster/qgsrasterprojector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ QgsRasterProjector *QgsRasterProjector::clone() const
projector->mSrcDatumTransform = mSrcDatumTransform;
projector->mDestDatumTransform = mDestDatumTransform;
projector->mPrecision = mPrecision;
projector->mTransformContext = mTransformContext;
return projector;
}

Expand All @@ -60,12 +61,24 @@ Qgis::DataType QgsRasterProjector::dataType( int bandNo ) const
/// @cond PRIVATE


void QgsRasterProjector::setCrs( const QgsCoordinateReferenceSystem &srcCRS, const QgsCoordinateReferenceSystem &destCRS, int srcDatumTransform, int destDatumTransform )
void QgsRasterProjector::setCrs( const QgsCoordinateReferenceSystem &srcCRS,
const QgsCoordinateReferenceSystem &destCRS,
int srcDatumTransform,
int destDatumTransform )
{
mSrcCRS = srcCRS;
mDestCRS = destCRS;
mSrcDatumTransform = srcDatumTransform;
mDestDatumTransform = destDatumTransform;
mTransformContext = QgsCoordinateTransformContext();
mTransformContext.addSourceDestinationDatumTransform( srcCRS, destCRS, srcDatumTransform, destDatumTransform );
}

void QgsRasterProjector::setCrs( const QgsCoordinateReferenceSystem &srcCRS, const QgsCoordinateReferenceSystem &destCRS, QgsCoordinateTransformContext transformContext )
{
mSrcCRS = srcCRS;
mDestCRS = destCRS;
mTransformContext = transformContext;
}


Expand Down Expand Up @@ -747,13 +760,23 @@ QgsRasterBlock *QgsRasterProjector::block( int bandNo, QgsRectangle const &exte
if ( feedback && feedback->isCanceled() )
return new QgsRasterBlock();

// TODO: check if the last condition always apply to context
if ( ! mSrcCRS.isValid() || ! mDestCRS.isValid() || mSrcCRS == mDestCRS )
{
QgsDebugMsgLevel( QStringLiteral( "No projection necessary" ), 4 );
return mInput->block( bandNo, extent, width, height, feedback );
}

QgsCoordinateTransform inverseCt( mDestCRS, mSrcCRS, mDestDatumTransform, mSrcDatumTransform );
QgsCoordinateTransform inverseCt;

if ( mSrcDatumTransform != -1 && mDestDatumTransform != -1 )
{
inverseCt = QgsCoordinateTransform( mDestCRS, mSrcCRS, mDestDatumTransform, mSrcDatumTransform );
}
else
{
inverseCt = QgsCoordinateTransform( mDestCRS, mSrcCRS, mTransformContext );
}

ProjectorData pd( extent, width, height, mInput, inverseCt, mPrecision );

Expand All @@ -774,7 +797,7 @@ QgsRasterBlock *QgsRasterProjector::block( int bandNo, QgsRectangle const &exte
return new QgsRasterBlock();
}

qgssize pixelSize = QgsRasterBlock::typeSize( mInput->dataType( bandNo ) );
qgssize pixelSize = static_cast<qgssize>( QgsRasterBlock::typeSize( mInput->dataType( bandNo ) ) );

std::unique_ptr< QgsRasterBlock > outputBlock( new QgsRasterBlock( inputBlock->dataType(), width, height ) );
if ( inputBlock->hasNoDataValue() )
Expand Down Expand Up @@ -814,7 +837,7 @@ QgsRasterBlock *QgsRasterProjector::block( int bandNo, QgsRectangle const &exte
bool inside = pd.srcRowCol( i, j, &srcRow, &srcCol );
if ( !inside ) continue; // we have everything set to no data

qgssize srcIndex = static_cast< qgssize >( srcRow ) * pd.srcCols() + srcCol;
qgssize srcIndex = static_cast< qgssize >( srcRow * pd.srcCols() + srcCol );

// isNoData() may be slow so we check doNoData first
if ( doNoData && inputBlock->isNoData( srcRow, srcCol ) )
Expand All @@ -823,7 +846,7 @@ QgsRasterBlock *QgsRasterProjector::block( int bandNo, QgsRectangle const &exte
continue;
}

qgssize destIndex = static_cast< qgssize >( i ) * width + j;
qgssize destIndex = static_cast< qgssize >( i * width + j );
char *srcBits = inputBlock->bits( srcIndex );
char *destBits = outputBlock->bits( destIndex );
if ( !srcBits )
Expand Down Expand Up @@ -851,8 +874,15 @@ bool QgsRasterProjector::destExtentSize( const QgsRectangle &srcExtent, int srcX
{
return false;
}
QgsCoordinateTransform ct( mSrcCRS, mDestCRS, mSrcDatumTransform, mDestDatumTransform );

QgsCoordinateTransform ct;
if ( mSrcDatumTransform != -1 && mDestDatumTransform != -1 )
{
ct = QgsCoordinateTransform( mSrcCRS, mDestCRS, mSrcDatumTransform, mDestDatumTransform );
}
else
{
ct = QgsCoordinateTransform( mSrcCRS, mDestCRS, mTransformContext );
}
return extentSize( ct, srcExtent, srcXSize, srcYSize, destExtent, destXSize, destYSize );
}

Expand Down
17 changes: 15 additions & 2 deletions src/core/raster/qgsrasterprojector.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,19 @@ class CORE_EXPORT QgsRasterProjector : public QgsRasterInterface

Qgis::DataType dataType( int bandNo ) const override;

//! Sets the source and destination CRS
/**
* Sets the source and destination CRS
* \deprecated since QGIS 3.8, use transformContext version instead
*/
Q_DECL_DEPRECATED void setCrs( const QgsCoordinateReferenceSystem &srcCRS, const QgsCoordinateReferenceSystem &destCRS,
int srcDatumTransform = -1, int destDatumTransform = -1 ) SIP_DEPRECATED;

/**
* Sets source CRS to \a srcCRS and destination CRS to \a destCRS and the transformation context to \a transformContext
* \since QGIS 3.8
*/
void setCrs( const QgsCoordinateReferenceSystem &srcCRS, const QgsCoordinateReferenceSystem &destCRS,
int srcDatumTransform = -1, int destDatumTransform = -1 );
QgsCoordinateTransformContext transformContext );

//! Returns the source CRS
QgsCoordinateReferenceSystem sourceCrs() const { return mSrcCRS; }
Expand Down Expand Up @@ -112,6 +122,9 @@ class CORE_EXPORT QgsRasterProjector : public QgsRasterInterface
//! Requested precision
Precision mPrecision = Approximate;

//! Transform context
QgsCoordinateTransformContext mTransformContext;

};


Expand Down

0 comments on commit b3a448a

Please sign in to comment.