Skip to content

Commit a261812

Browse files
committed
qgsexpression: lazy initialization of geometry calculator
1 parent f854288 commit a261812

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/core/qgsexpression.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,10 +1612,8 @@ QgsExpression::QgsExpression( const QString& expr )
16121612
: mExpression( expr )
16131613
, mRowNumber( 0 )
16141614
, mScale( 0 )
1615-
1615+
, mCalc( 0 )
16161616
{
1617-
initGeomCalculator();
1618-
16191617
mRootNode = ::parseExpression( mExpression, mParserErrorString );
16201618

16211619
if ( mParserErrorString.isNull() )
@@ -1626,6 +1624,8 @@ QgsExpression::QgsExpression( const QString& expr )
16261624

16271625
QgsExpression::~QgsExpression()
16281626
{
1627+
if ( mCalc )
1628+
delete mCalc;
16291629
delete mRootNode;
16301630
}
16311631

@@ -1661,16 +1661,23 @@ bool QgsExpression::needsGeometry()
16611661

16621662
void QgsExpression::initGeomCalculator()
16631663
{
1664+
if( mCalc )
1665+
return;
1666+
16641667
// Use planimetric as default
1665-
mCalc.setEllipsoidalMode( false );
1668+
mCalc = new QgsDistanceArea();
1669+
mCalc->setEllipsoidalMode( false );
16661670
}
16671671

1668-
void QgsExpression::setGeomCalculator( QgsDistanceArea& calc )
1672+
void QgsExpression::setGeomCalculator( QgsDistanceArea &calc )
16691673
{
1674+
if( !mCalc )
1675+
mCalc = new QgsDistanceArea();
1676+
16701677
// Copy from supplied calculator
1671-
mCalc.setEllipsoid( calc.ellipsoid() );
1672-
mCalc.setEllipsoidalMode( calc.ellipsoidalEnabled() );
1673-
mCalc.setSourceCrs( calc.sourceCrs() );
1678+
mCalc->setEllipsoid( calc.ellipsoid() );
1679+
mCalc->setEllipsoidalMode( calc.ellipsoidalEnabled() );
1680+
mCalc->setSourceCrs( calc.sourceCrs() );
16741681
}
16751682

16761683
bool QgsExpression::prepare( const QgsFields& fields )

src/core/qgsexpression.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,11 @@ class CORE_EXPORT QgsExpression
157157

158158
//! Return calculator used for distance and area calculations
159159
//! (used by internal functions)
160-
QgsDistanceArea* geomCalculator() { return & mCalc; }
160+
QgsDistanceArea *geomCalculator() { initGeomCalculator(); return mCalc; }
161161

162162
//! Sets the geometry calculator used in evaluation of expressions,
163163
// instead of the default.
164-
void setGeomCalculator( QgsDistanceArea& calc );
164+
void setGeomCalculator( QgsDistanceArea &calc );
165165

166166
/** This function currently replaces each expression between [% and %]
167167
in the string with the result of its evaluation on the feature
@@ -613,7 +613,7 @@ class CORE_EXPORT QgsExpression
613613
double mScale;
614614

615615
static QMap<QString, QVariant> gmSpecialColumns;
616-
QgsDistanceArea mCalc;
616+
QgsDistanceArea *mCalc;
617617

618618
friend class QgsOgcUtils;
619619

0 commit comments

Comments
 (0)