-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add QgsScaleExpression subclass for sized based expressions
- Loading branch information
1 parent
9b56618
commit 70b91b6
Showing
5 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
|
||
/*************************************************************************** | ||
qgsscaleexpression.cpp | ||
--------------------- | ||
begin : November 2015 | ||
copyright : (C) 2015 by Vincent Mora | ||
email : vincent dor mora at oslandia dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsscaleexpression.h" | ||
|
||
#include <QStringList> | ||
#include <qmath.h> | ||
|
||
void QgsScaleExpression::init() | ||
{ | ||
bool ok; | ||
if ( rootNode() ) | ||
{ | ||
const NodeFunction * f = dynamic_cast<const NodeFunction*>( rootNode() ); | ||
if ( f ) | ||
{ | ||
QList<Node*> args = f->args()->list(); | ||
if ( "scale_linear" == Functions()[f->fnIndex()]->name() ) | ||
{ | ||
mType = Linear; | ||
} | ||
if ( "scale_exp" == Functions()[f->fnIndex()]->name() ) | ||
{ | ||
const double exp = QgsExpression( args[5]->dump() ).evaluate().toDouble( &ok ); | ||
if ( ! ok ) return; | ||
if ( qAbs( exp - .57 ) < .001 ) mType = Flannery; | ||
else if ( qAbs( exp - .5 ) < .001 ) mType = Area; | ||
else return; | ||
} | ||
mMinValue = QgsExpression( args[1]->dump() ).evaluate().toDouble( &ok ); | ||
if ( ! ok ) return; | ||
mMaxValue = QgsExpression( args[2]->dump() ).evaluate().toDouble( &ok ); | ||
if ( ! ok ) return; | ||
mMinSize = QgsExpression( args[3]->dump() ).evaluate().toDouble( &ok ); | ||
if ( ! ok ) return; | ||
mMaxSize = QgsExpression( args[4]->dump() ).evaluate().toDouble( &ok ); | ||
if ( ! ok ) return; | ||
mExpression = args[0]->dump(); | ||
} | ||
} | ||
} | ||
|
||
QgsScaleExpression::QgsScaleExpression( const QString & expr ) | ||
: QgsExpression( expr ) | ||
{ | ||
init(); | ||
} | ||
|
||
QgsScaleExpression::QgsScaleExpression( Type type, const QString & baseExpr, double minValue, double maxValue, double minSize, double maxSize ) | ||
: QgsExpression( createExpression( type, baseExpr, minValue, maxValue, minSize, maxSize ) ) | ||
{ | ||
init(); | ||
} | ||
|
||
QString QgsScaleExpression::createExpression( Type type, const QString & baseExpr, double minValue, double maxValue, double minSize, double maxSize ) | ||
{ | ||
switch ( type ) | ||
{ | ||
case Linear: | ||
return "scale_linear(" + baseExpr | ||
+ ", " + QString::number( minValue ) | ||
+ ", " + QString::number( maxValue ) | ||
+ ", " + QString::number( minSize ) | ||
+ ", " + QString::number( maxSize ) + ")"; | ||
case Area: | ||
return "scale_exp(" + baseExpr | ||
+ ", " + QString::number( minValue ) | ||
+ ", " + QString::number( maxValue ) | ||
+ ", " + QString::number( minSize ) | ||
+ ", " + QString::number( maxSize ) + ", .5)"; | ||
case Flannery: | ||
return "scale_exp(" + baseExpr | ||
+ ", " + QString::number( minValue ) | ||
+ ", " + QString::number( maxValue ) | ||
+ ", " + QString::number( minSize ) | ||
+ ", " + QString::number( maxSize ) + ", .57)"; | ||
} | ||
return ""; | ||
} | ||
|
||
|
||
double QgsScaleExpression::size( double value ) const | ||
{ | ||
switch ( mType ) | ||
{ | ||
case Linear: return mMinSize + ( qBound( mMinValue, value, mMaxValue ) - mMinValue ) * ( mMaxSize - mMinSize ) / ( mMaxValue - mMinValue ); | ||
case Area: return qPow( qBound( mMinValue, value, mMaxValue ) - mMinValue, .5 ) * ( mMaxSize - mMinSize ) / qPow( mMaxValue - mMinValue, .5 ); | ||
case Flannery: return qPow( qBound( mMinValue, value, mMaxValue ) - mMinValue, .57 ) * ( mMaxSize - mMinSize ) / qPow( mMaxValue - mMinValue, .57 ); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
|
||
/*************************************************************************** | ||
qgsscaleexpression.h | ||
--------------------- | ||
begin : November 2015 | ||
copyright : (C) 2015 by Vincent Mora | ||
email : vincent dor mora at oslandia dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
#ifndef QGSSCALEEXPRESSION_H | ||
#define QGSSCALEEXPRESSION_H | ||
|
||
#include "qgsexpression.h" | ||
|
||
//! Class to retrieve parameters of a | ||
//! scale expression (size or width) | ||
//! | ||
//! It derives from QgsExpression, | ||
class QgsScaleExpression: public QgsExpression | ||
{ | ||
public: | ||
enum Type {Linear, Area, Flannery}; | ||
|
||
//! parse string like an expression and | ||
//! detemine if it's a size expression | ||
QgsScaleExpression( const QString & expr ); | ||
|
||
//! setup expression from parameters | ||
QgsScaleExpression( Type type, const QString & baseExpr, double minValue, double maxValue, double minSize, double maxSize ); | ||
|
||
operator bool() const { return ! mExpression.isEmpty(); } | ||
double size( double value ) const; | ||
|
||
double minSize() const { return mMinSize; } | ||
double maxSize() const { return mMaxSize; } | ||
double minValue() const { return mMinValue; } | ||
double maxValue() const { return mMaxValue; } | ||
QString baseExpression() const { return mExpression; } | ||
Type type() const { return mType; } | ||
|
||
private: | ||
QString mExpression; | ||
Type mType; | ||
double mMinSize; | ||
double mMaxSize; | ||
double mMinValue; | ||
double mMaxValue; | ||
|
||
void init(); | ||
static QString createExpression( Type type, const QString & baseExpr, double minValue, double maxValue, double minSize, double maxSize ); | ||
|
||
}; | ||
|
||
#endif | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/*************************************************************************** | ||
test_template.cpp | ||
-------------------------------------- | ||
Date : Sun Sep 16 12:22:23 AKDT 2007 | ||
Copyright : (C) 2007 by Gary E. Sherman | ||
Email : sherman at mrcc dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
#include <QtTest/QtTest> | ||
#include <QObject> | ||
#include <QString> | ||
#include <QObject> | ||
#include <QtConcurrentMap> | ||
#include <QSharedPointer> | ||
|
||
#include <qgsapplication.h> | ||
//header for class being tested | ||
#include <qgsscaleexpression.h> | ||
#include <qgsfeature.h> | ||
#include <qgsfeaturerequest.h> | ||
#include <qgsgeometry.h> | ||
#include <qgsrenderchecker.h> | ||
|
||
#if QT_VERSION < 0x40701 | ||
// See http://hub.qgis.org/issues/4284 | ||
Q_DECLARE_METATYPE( QVariant ) | ||
#endif | ||
|
||
class TestQgsScaleExpression: public QObject | ||
{ | ||
Q_OBJECT | ||
private slots: | ||
|
||
void initTestCase() | ||
{ | ||
// | ||
// Runs once before any tests are run | ||
// | ||
// init QGIS's paths - true means that all path will be inited from prefix | ||
QgsApplication::init(); | ||
QgsApplication::initQgis(); | ||
// Will make sure the settings dir with the style file for color ramp is created | ||
QgsApplication::createDB(); | ||
QgsApplication::showSettings(); | ||
} | ||
|
||
void cleanupTestCase() | ||
{ | ||
QgsApplication::exitQgis(); | ||
} | ||
|
||
void parsing() | ||
{ | ||
{ | ||
QgsScaleExpression exp( "scale_linear(column, 1, 7, 2, 10)" ); | ||
QCOMPARE( bool(exp), true ); | ||
QCOMPARE( exp.type(), QgsScaleExpression::Linear ); | ||
QCOMPARE( exp.baseExpression(), QString("column") ); | ||
QCOMPARE( exp.minValue(), 1. ); | ||
QCOMPARE( exp.maxValue(), 7. ); | ||
QCOMPARE( exp.minSize(), 2. ); | ||
QCOMPARE( exp.maxSize(), 10. ); | ||
} | ||
{ | ||
QgsScaleExpression exp( "scale_exp(column, 1, 7, 2, 10, 0.5)" ); | ||
QCOMPARE( bool(exp), true ); | ||
QCOMPARE( exp.type(), QgsScaleExpression::Area ); | ||
} | ||
{ | ||
QgsScaleExpression exp( "scale_exp(column, 1, 7, 2, 10, 0.57)" ); | ||
QCOMPARE( bool(exp), true ); | ||
QCOMPARE( exp.type(), QgsScaleExpression::Flannery ); | ||
} | ||
{ | ||
QgsScaleExpression exp( "scale_exp(column, 1, 7, 2, 10, 0.51)" ); | ||
QCOMPARE( bool(exp), false ); | ||
} | ||
{ | ||
QgsScaleExpression exp( "scale_exp(column, 1, 7, a, 10, 0.5)" ); | ||
QCOMPARE( bool(exp), false ); | ||
} | ||
{ | ||
QgsScaleExpression exp( QgsScaleExpression::Linear, "column", 1, 7, 2, 10 ); | ||
QCOMPARE( bool(exp), true ); | ||
QCOMPARE( exp.type(), QgsScaleExpression::Linear ); | ||
QCOMPARE( exp.baseExpression(), QString("column") ); | ||
QCOMPARE( exp.minValue(), 1. ); | ||
QCOMPARE( exp.maxValue(), 7. ); | ||
QCOMPARE( exp.minSize(), 2. ); | ||
QCOMPARE( exp.maxSize(), 10. ); | ||
} | ||
|
||
} | ||
}; | ||
|
||
QTEST_MAIN( TestQgsScaleExpression ) | ||
|
||
#include "testqgsscaleexpression.moc" | ||
|