Skip to content

Commit

Permalink
[expression] Avoid paying the price of a QgsCoordinateReferenceSystem…
Browse files Browse the repository at this point in the history
… destruction if we didn't need one

(cherry picked from commit ff3bb1c)
  • Loading branch information
nirvn authored and nyalldawson committed Jan 15, 2021
1 parent b79f618 commit 9d7bdea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/core/expression/qgsexpression.cpp
Expand Up @@ -278,8 +278,8 @@ void QgsExpression::initGeomCalculator( const QgsExpressionContext *context )
// actually don't do it right away, cos it's expensive to create and only a very small number of expression
// functions actually require it. Let's lazily construct it when needed
d->mDaEllipsoid = context->variable( QStringLiteral( "project_ellipsoid" ) ).toString();
d->mDaCrs = context->variable( QStringLiteral( "_layer_crs" ) ).value<QgsCoordinateReferenceSystem>();
d->mDaTransformContext = context->variable( QStringLiteral( "_project_transform_context" ) ).value<QgsCoordinateTransformContext>();
d->mDaCrs = qgis::make_unique<QgsCoordinateReferenceSystem>( context->variable( QStringLiteral( "_layer_crs" ) ).value<QgsCoordinateReferenceSystem>() );
d->mDaTransformContext = qgis::make_unique<QgsCoordinateTransformContext>( context->variable( QStringLiteral( "_project_transform_context" ) ).value<QgsCoordinateTransformContext>() );
}

// Set the distance units from the context if it has not been set by setDistanceUnits()
Expand Down Expand Up @@ -396,12 +396,12 @@ QString QgsExpression::dump() const

QgsDistanceArea *QgsExpression::geomCalculator()
{
if ( !d->mCalc && d->mDaCrs.isValid() )
if ( !d->mCalc && d->mDaCrs && d->mDaCrs->isValid() && d->mDaTransformContext )
{
// calculator IS required, so initialize it now...
d->mCalc = std::shared_ptr<QgsDistanceArea>( new QgsDistanceArea() );
d->mCalc->setEllipsoid( d->mDaEllipsoid.isEmpty() ? geoNone() : d->mDaEllipsoid );
d->mCalc->setSourceCrs( d->mDaCrs, d->mDaTransformContext );
d->mCalc->setSourceCrs( *d->mDaCrs.get(), *d->mDaTransformContext.get() );
}

return d->mCalc.get();
Expand Down
13 changes: 8 additions & 5 deletions src/core/expression/qgsexpression_p.h
Expand Up @@ -47,12 +47,15 @@ class QgsExpressionPrivate
, mParserErrors( other.mParserErrors )
, mExp( other.mExp )
, mDaEllipsoid( other.mDaEllipsoid )
, mDaCrs( other.mDaCrs )
, mDaTransformContext( other.mDaTransformContext )
, mCalc( other.mCalc )
, mDistanceUnit( other.mDistanceUnit )
, mAreaUnit( other.mAreaUnit )
{}
{
if ( other.mDaCrs )
mDaCrs = qgis::make_unique<QgsCoordinateReferenceSystem>( *other.mDaCrs.get() );
if ( other.mDaTransformContext )
mDaTransformContext = qgis::make_unique<QgsCoordinateTransformContext>( *other.mDaTransformContext.get() );
}

~QgsExpressionPrivate()
{
Expand All @@ -71,8 +74,8 @@ class QgsExpressionPrivate
QString mExp;

QString mDaEllipsoid;
QgsCoordinateReferenceSystem mDaCrs;
QgsCoordinateTransformContext mDaTransformContext;
std::unique_ptr<QgsCoordinateReferenceSystem> mDaCrs;
std::unique_ptr<QgsCoordinateTransformContext> mDaTransformContext;

std::shared_ptr<QgsDistanceArea> mCalc;
QgsUnitTypes::DistanceUnit mDistanceUnit = QgsUnitTypes::DistanceUnknownUnit;
Expand Down

0 comments on commit 9d7bdea

Please sign in to comment.