Skip to content
Permalink
Browse files
qgsexpression: lazy initialization of geometry calculator
  • Loading branch information
jef-n committed Jul 27, 2013
1 parent f854288 commit a261812d0f64819f0433233efe7a4522c546808e
Showing with 18 additions and 11 deletions.
  1. +15 −8 src/core/qgsexpression.cpp
  2. +3 −3 src/core/qgsexpression.h
@@ -1612,10 +1612,8 @@ QgsExpression::QgsExpression( const QString& expr )
: mExpression( expr )
, mRowNumber( 0 )
, mScale( 0 )

, mCalc( 0 )
{
initGeomCalculator();

mRootNode = ::parseExpression( mExpression, mParserErrorString );

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

QgsExpression::~QgsExpression()
{
if ( mCalc )
delete mCalc;
delete mRootNode;
}

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

void QgsExpression::initGeomCalculator()
{
if( mCalc )
return;

// Use planimetric as default
mCalc.setEllipsoidalMode( false );
mCalc = new QgsDistanceArea();
mCalc->setEllipsoidalMode( false );
}

void QgsExpression::setGeomCalculator( QgsDistanceArea& calc )
void QgsExpression::setGeomCalculator( QgsDistanceArea &calc )
{
if( !mCalc )
mCalc = new QgsDistanceArea();

// Copy from supplied calculator
mCalc.setEllipsoid( calc.ellipsoid() );
mCalc.setEllipsoidalMode( calc.ellipsoidalEnabled() );
mCalc.setSourceCrs( calc.sourceCrs() );
mCalc->setEllipsoid( calc.ellipsoid() );
mCalc->setEllipsoidalMode( calc.ellipsoidalEnabled() );
mCalc->setSourceCrs( calc.sourceCrs() );
}

bool QgsExpression::prepare( const QgsFields& fields )
@@ -157,11 +157,11 @@ class CORE_EXPORT QgsExpression

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

//! Sets the geometry calculator used in evaluation of expressions,
// instead of the default.
void setGeomCalculator( QgsDistanceArea& calc );
void setGeomCalculator( QgsDistanceArea &calc );

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

static QMap<QString, QVariant> gmSpecialColumns;
QgsDistanceArea mCalc;
QgsDistanceArea *mCalc;

friend class QgsOgcUtils;

0 comments on commit a261812

Please sign in to comment.