Skip to content

Commit bd9e02b

Browse files
committedMay 3, 2015
Add alias support for expression functions
Standardise naming of all internal functions to lowercase with underscore convention. Add aliases for old names to avoid breakage.
1 parent bb64830 commit bd9e02b

29 files changed

+198
-128
lines changed
 

‎python/core/qgsexpression.sip

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ class QgsExpression
202202
/** Does this function use a geometry object. */
203203
bool usesgeometry();
204204

205+
/** Returns a list of possible aliases for the function. These include
206+
* other permissible names for the function, eg deprecated names.
207+
* @return list of known aliases
208+
* @note added in QGIS 2.9
209+
*/
210+
virtual QStringList aliases() const;
211+
205212
/** True if this function should use lazy evaluation. Lazy evaluation functions take QgsExpression::Node objects
206213
* rather than the node results when called. You can use node->eval(parent, feature) to evaluate the node and return the result
207214
* Functions are non lazy default and will be given the node return value when called **/
@@ -231,7 +238,7 @@ class QgsExpression
231238
static bool isFunctionName( QString name );
232239

233240
// return index of the function in Functions array
234-
static int functionIndex( QString name );
241+
static int functionIndex( const QString& name );
235242

236243
/** Returns the number of functions defined in the parser
237244
* @return The number of function defined in the parser.

‎resources/function_help/bbox

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<h3>convexHull function</h3>
1+
<h3>convex_hull function</h3>
22
Returns the convex hull of a geometry. It represents the minimum convex geometry that encloses all geometries within the set.
33

44
<h4>Syntax</h4>
5-
<pre>convexHull( a, b )</pre>
5+
<pre>convex_hull( a, b )</pre>
66

77
<h4>Arguments</h4>
88
a &rarr; geometry
99
a &rarr; geometry
1010

1111
<h4>Example</h4>
12-
<pre> geomToWKT( convexHull( geomFromWKT( 'LINESTRING(3 3 , 4 4 , 4 10)' )) ) &rarr; returns POLYGON((3 3,4 10,4 4,3 3)) </pre>
12+
<pre> geom_to_wkt( convex_hull( geom_from_wkt( 'LINESTRING(3 3 , 4 4 , 4 10)' )) ) &rarr; returns POLYGON((3 3,4 10,4 4,3 3)) </pre>

‎resources/function_help/geomFromGML

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎resources/function_help/geom_from_gml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<h3>geom_from_gml function</h3>
2+
Returns a geometry from a GML representation of geometry
3+
4+
<h4>Syntax</h4>
5+
<pre>geom_from_gml(text)</pre>
6+
7+
<h4>Arguments</h4>
8+
text &rarr; GML representation of a geometry
9+
10+
<h4>Example</h4>
11+
<pre> geom_from_gml( '<gml:LineString srsName="EPSG:4326"><gml:coordinates>4,4 5,5 6,6</gml:coordinates></gml:LineString>') &rarr; returns a geometry</pre>
12+
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<h3>geomFromWKT function</h3>
1+
<h3>geom_from_wkt function</h3>
22
Returns a geometry created from a Well-Known Text (WKT) representation.
33

44
<h4>Syntax</h4>
5-
<pre>geomFromWKT(text)</pre>
5+
<pre>geom_from_wkt(text)</pre>
66

77
<h4>Arguments</h4>
88
text &rarr; Well-Known Text (WKT) representation of a geometry
99

1010
<h4>Example</h4>
11-
<pre> geomFromWKT( 'POINT(4 5)' ) &rarr; returns a geometry</pre>
11+
<pre> geom_from_wkt( 'POINT(4 5)' ) &rarr; returns a geometry</pre>
1212

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
<h3>geomToWKT function</h3>
1+
<h3>geom_to_wkt function</h3>
22
Returns the Well-Known Text (WKT) representation of the geometry without SRID metadata.
33

44
<h4>Syntax</h4>
5-
<pre>geomToWKT(a)</pre>
5+
<pre>geom_to_wkt(a)</pre>
66

77
<h4>Arguments</h4>
88
a &rarr; geometry
99

1010
<h4>Example</h4>
11-
<pre> geomToWKT( $geometry ) &rarr; POINT(6 50)</pre>
11+
<pre> geom_to_wkt( $geometry ) &rarr; POINT(6 50)</pre>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<h3>getFeature function</h3>
1+
<h3>get_feature function</h3>
22
Returns the first feature of a layer matching a given attribute value
33

44
<h4>Syntax</h4>
5-
<pre>getFeature( layer, attributeField, value )</pre>
5+
<pre>get_feature( layer, attributeField, value )</pre>
66

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<h3>intersects_bbox function</h3>
2+
Returns 1 if the geometries spatially intersect the bounding box defined and 0 if they don't.
3+
4+
<h4>Syntax</h4>
5+
<pre>intersects_bbox( a, b )</pre>
6+
7+
<h4>Arguments</h4>
8+
a &rarr; geometry
9+
b &rarr; geometry
10+
11+
<h4>Example</h4>
12+
<pre>intersects_bbox( geomFromWKT( 'POINT(4 5)' ) , geom_from_wkt( 'LINESTRING(3 3 , 4 4 , 5 5)' )) &rarr; returns 1</pre>
13+
<pre>intersects_bbox( geomFromWKT( 'POINT(6 5)' ) , geom_from_wkt( 'POLYGON((3 3 , 4 4 , 5 5, 3 3))' )) &rarr; returns 0</pre>

‎resources/function_help/symDifference

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<h3>sym_difference function</h3>
2+
Returns a geometry that represents the portions of a and b that do not intersect.
3+
4+
<h4>Syntax</h4>
5+
<pre>sym_difference( geometry a, geometry b)</pre>
6+
7+
<h4>Arguments</h4>
8+
geometry &rarr; geometry
9+
geometry &rarr; geometry
10+
11+
<h4>Example</h4>
12+
<pre> geom_to_wkt( sym_difference( geom_from_wkt( 'LINESTRING(3 3 , 4 4 , 5 5)' ) , geom_from_wkt( 'LINESTRING(3 3 , 8 8)' )) ) &rarr; returns LINESTRING(5 5, 8 8)</pre>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
<h3>todate() function</h3>
1+
<h3>to_date() function</h3>
22
Convert a string into Qt data type.
33

44
<h4>Syntax</h4>
5-
<code>todate('string')</code><br>
5+
<code>to_date('string')</code><br>
66

77
<h4>Arguments</h4>
88
<code>string</code> - is string in Qt date format.
99
<br>
1010

1111
<h4>Example</h4>
1212
<!-- Show example of function.-->
13-
<code>todate('2012-05-04') &rarr; 2012-05-04</code><br>
13+
<code>to_date('2012-05-04') &rarr; 2012-05-04</code><br>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
<h3>todatetime() function</h3>
1+
<h3>to_datetime() function</h3>
22
Convert a string into Qt data time type.
33

44
<h4>Syntax</h4>
5-
<code>todatetime('string')</code><br>
5+
<code>to_datetime('string')</code><br>
66

77
<h4>Arguments</h4>
88
<code>string</code> - is string in Qt date time format.
99
<br>
1010

1111
<h4>Example</h4>
1212
<!-- Show example of function.-->
13-
<code>todatetime('2012-05-04 12:50:00') &rarr; 2012-05-04T12:50:00</code><br>
13+
<code>to_datetime('2012-05-04 12:50:00') &rarr; 2012-05-04T12:50:00</code><br>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
<h3>toint() function</h3>
1+
<h3>to_int() function</h3>
22
Converts a string to integer number. Nothing changed if a value cannot be converted to integer (e.g '123asd' is invalid).
33

44
<p><h4>Syntax</h4>
5-
toint(<i>string</i>)</p>
5+
to_int(<i>string</i>)</p>
66

77
<p><h4>Arguments</h4>
88
<!-- List args for functions here-->
99
<i> string</i> &rarr; is string. The String to convert to integer number.</p>
1010

1111
<p><h4>Example</h4>
1212
<!-- Show example of function.-->
13-
toint('123') &rarr; 123</p>
13+
to_int('123') &rarr; 123</p>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
<h3>tointerval() function</h3>
1+
<h3>to_interval() function</h3>
22
Converts a string to a interval type. Can be used to take days, hours, month, etc off a date.
33

44
<h4>Syntax</h4>
5-
<code>tointerval('string')</code><br>
5+
<code>to_interval('string')</code><br>
66

77
<h4>Arguments</h4>
88
<code>string</code> - is string. Format {n} days {n} hours {n} months
99
<br>
1010

1111
<h4>Example</h4>
1212
<!-- Show example of function.-->
13-
<code>todatetime('2012-05-05 12:00:00') - tointerval('1 day 2 hours') &rarr; 2012-05-04T10:00:00</code><br>
13+
<code>todatetime('2012-05-05 12:00:00') - to_interval('1 day 2 hours') &rarr; 2012-05-04T10:00:00</code><br>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
<h3>toreal() function</h3>
1+
<h3>to_real() function</h3>
22
Converts a string to real number. Nothing changed if a value cannot be converted to real (e.g '123.56asd' is invalid). Numbers are rounded after saving changes if the precision is smaller than the result of the conversion.
33

44
<p><h4>Syntax</h4>
5-
toreal(<i>string</i>)</p>
5+
to_real(<i>string</i>)</p>
66

77
<p><h4>Arguments</h4>
88
<!-- List args for functions here-->
99
<i> string</i> &rarr; is string. The String to convert to real number.</p>
1010

1111
<p><h4>Example</h4>
1212
<!-- Show example of function.-->
13-
toreal('123.45') &rarr; 123.45</p>
13+
to_real('123.45') &rarr; 123.45</p>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
<h3>tostring() function</h3>
1+
<h3>to_string() function</h3>
22
Converts a number to string.
33

44
<p><h4>Syntax</h4>
5-
tostring(<i>number</i>)</p>
5+
to_string(<i>number</i>)</p>
66

77
<p><h4>Arguments</h4>
88
<!-- List args for functions here-->
99
<i>number</i> &rarr; is integer or real. The number to convert to string.</p>
1010

1111
<p><h4>Example</h4>
1212
<!-- Show example of function.-->
13-
tostring(123) &rarr; '123'</p>
13+
to_string(123) &rarr; '123'</p>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
<h3>totime() function</h3>
1+
<h3>to_time() function</h3>
22
Convert a string into Qt time type.
33

44
<h4>Syntax</h4>
5-
<code>totime('string')</code><br>
5+
<code>to_time('string')</code><br>
66

77
<h4>Arguments</h4>
88
<code>string</code> - is string in Qt time format.
99
<br>
1010

1111
<h4>Example</h4>
1212
<!-- Show example of function.-->
13-
<code>totime('12:30:01') &rarr; 12:30:01</code><br>
13+
<code>to_time('12:30:01') &rarr; 12:30:01</code><br>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
<h3>xat() function</h3>
1+
<h3>x_at() function</h3>
22
Retrieves a x coordinate of the current feature
33

44
<h4>Syntax</h4>
5-
<code>xat(i)</code>
5+
<code>x_at(i)</code>
66

77
<h4>Arguments</h4>
88
<code>i<code> - is int. index of point of a line (indices start at 0; negative values apply to the last index).
99

1010
<h4>Example</h4>
11-
<pre>xat(1) &rarr; 5</pre>
11+
<pre>x_at(1) &rarr; 5</pre>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<h3>xmax function</h3>
1+
<h3>x_max function</h3>
22
Returns the maximum x coordinate of a geometry. Calculations are in the Spatial Reference System of this Geometry.
33

44
<h4>Syntax</h4>
5-
<pre>xmax(geom)</pre>
5+
<pre>x_max(geom)</pre>
66

77
<h4>Arguments</h4>
88
geom &rarr; a geometry
99

1010
<h4>Example</h4>
11-
<pre> xmax($geometry) &rarr; returns maximum x coordinate of $geometry</pre>
11+
<pre> x_max($geometry) &rarr; returns maximum x coordinate of $geometry</pre>
1212

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<h3>xmin function</h3>
1+
<h3>x_min function</h3>
22
Returns the minimum x coordinate of a geometry. Calculations are in the Spatial Reference System of this Geometry.
33

44
<h4>Syntax</h4>
5-
<pre>xmin(geom)</pre>
5+
<pre>x_min(geom)</pre>
66

77
<h4>Arguments</h4>
88
geom &rarr; a geometry
99

1010
<h4>Example</h4>
11-
<pre> xmin($geometry) &rarr; returns minimum x coordinate of $geometry</pre>
11+
<pre> x_min($geometry) &rarr; returns minimum x coordinate of $geometry</pre>
1212

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
<h3>yat() function</h3>
1+
<h3>y_at() function</h3>
22
Retrieves a y coordinate of the current feature
33

44
<h4>Syntax</h4>
5-
<code>yat(i)</code>
5+
<code>y_at(i)</code>
66

77
<h4>Arguments</h4>
88
<code>i<code> - is int. index of point of a line (indices start at 0; negative values apply to the last index).
99

1010
<h4>Example</h4>
11-
<pre>yat(1) &rarr; 5</pre>
11+
<pre>y_at(1) &rarr; 5</pre>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<h3>ymax function</h3>
1+
<h3>y_max function</h3>
22
Returns the maximum y coordinate of a geometry. Calculations are in the Spatial Reference System of this Geometry.
33

44
<h4>Syntax</h4>
5-
<pre>ymax(geom)</pre>
5+
<pre>y_max(geom)</pre>
66

77
<h4>Arguments</h4>
88
geom &rarr; a geometry
99

1010
<h4>Example</h4>
11-
<pre> ymax($geometry) &rarr; returns maximum y coordinate of $geometry</pre>
11+
<pre> y_max($geometry) &rarr; returns maximum y coordinate of $geometry</pre>
1212

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<h3>ymin function</h3>
1+
<h3>y_min function</h3>
22
Returns the minimum y coordinate of a geometry. Calculations are in the Spatial Reference System of this Geometry.
33

44
<h4>Syntax</h4>
5-
<pre>ymin(geom)</pre>
5+
<pre>y_min(geom)</pre>
66

77
<h4>Arguments</h4>
88
geom &rarr; a geometry
99

1010
<h4>Example</h4>
11-
<pre> ymin($geometry) &rarr; returns minimum y coordinate of $geometry</pre>
11+
<pre> y_min($geometry) &rarr; returns minimum y coordinate of $geometry</pre>
1212

‎src/core/qgsexpression.cpp

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,22 +1666,33 @@ const QStringList &QgsExpression::BuiltinFunctions()
16661666
<< "asin" << "acos" << "atan" << "atan2"
16671667
<< "exp" << "ln" << "log10" << "log"
16681668
<< "round" << "rand" << "randf" << "max" << "min" << "clamp"
1669-
<< "scale_linear" << "scale_exp" << "floor" << "ceil"
1670-
<< "toint" << "toreal" << "tostring"
1671-
<< "todatetime" << "todate" << "totime" << "tointerval"
1672-
<< "coalesce" << "regexp_match" << "$now" << "age" << "year"
1669+
<< "scale_linear" << "scale_exp" << "floor" << "ceil" << "$pi"
1670+
<< "toint" << "to_int" << "toreal" << "to_real" << "tostring" << "to_string"
1671+
<< "todatetime" << "to_datetime" << "todate" << "to_date"
1672+
<< "totime" << "to_time" << "tointerval" << "to_interval"
1673+
<< "coalesce" << "if" << "regexp_match" << "age" << "year"
16731674
<< "month" << "week" << "day" << "hour"
16741675
<< "minute" << "second" << "lower" << "upper"
16751676
<< "title" << "length" << "replace" << "trim" << "wordwrap"
16761677
<< "regexp_replace" << "regexp_substr"
16771678
<< "substr" << "concat" << "strpos" << "left"
1678-
<< "right" << "rpad" << "lpad"
1679+
<< "right" << "rpad" << "lpad" << "format"
16791680
<< "format_number" << "format_date"
16801681
<< "color_rgb" << "color_rgba" << "ramp_color"
16811682
<< "color_hsl" << "color_hsla" << "color_hsv" << "color_hsva"
16821683
<< "color_cymk" << "color_cymka"
16831684
<< "xat" << "yat" << "$area"
16841685
<< "$length" << "$perimeter" << "$x" << "$y"
1686+
<< "x_at" << "xat" << "y_at" << "yat" << "x_min" << "xmin" << "x_max" << "xmax"
1687+
<< "y_min" << "ymin" << "y_max" << "ymax" << "geom_from_wkt" << "geomFromWKT"
1688+
<< "geom_from_gml" << "geomFromGML" << "intersects_bbox" << "bbox"
1689+
<< "disjoint" << "intersects" << "touches" << "crosses" << "contains"
1690+
<< "overlaps" << "within" << "buffer" << "centroid" << "bounds"
1691+
<< "bounds_width" << "bounds_height" << "convex_hull" << "difference"
1692+
<< "distance" << "intersection" << "sym_difference" << "combine"
1693+
<< "union" << "geom_to_wkt" << "geomToWKT" << "geometry"
1694+
<< "transform" << "get_feature" << "getFeature"
1695+
<< "attribute"
16851696
<< "$rownum" << "$id" << "$scale" << "_specialcol_";
16861697
}
16871698
return gmBuiltinFunctions;
@@ -1718,13 +1729,13 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
17181729
<< new StaticFunction( "floor", 1, fcnFloor, "Math" )
17191730
<< new StaticFunction( "ceil", 1, fcnCeil, "Math" )
17201731
<< new StaticFunction( "$pi", 0, fcnPi, "Math" )
1721-
<< new StaticFunction( "toint", 1, fcnToInt, "Conversions" )
1722-
<< new StaticFunction( "toreal", 1, fcnToReal, "Conversions" )
1723-
<< new StaticFunction( "tostring", 1, fcnToString, "Conversions" )
1724-
<< new StaticFunction( "todatetime", 1, fcnToDateTime, "Conversions" )
1725-
<< new StaticFunction( "todate", 1, fcnToDate, "Conversions" )
1726-
<< new StaticFunction( "totime", 1, fcnToTime, "Conversions" )
1727-
<< new StaticFunction( "tointerval", 1, fcnToInterval, "Conversions" )
1732+
<< new StaticFunction( "to_int", 1, fcnToInt, "Conversions", QString(), false, QStringList(), false, QStringList() << "toint" )
1733+
<< new StaticFunction( "to_real", 1, fcnToReal, "Conversions", QString(), false, QStringList(), false, QStringList() << "toreal" )
1734+
<< new StaticFunction( "to_string", 1, fcnToString, "Conversions", QString(), false, QStringList(), false, QStringList() << "tostring" )
1735+
<< new StaticFunction( "to_datetime", 1, fcnToDateTime, "Conversions", QString(), false, QStringList(), false, QStringList() << "todatetime" )
1736+
<< new StaticFunction( "to_date", 1, fcnToDate, "Conversions", QString(), false, QStringList(), false, QStringList() << "todate" )
1737+
<< new StaticFunction( "to_time", 1, fcnToTime, "Conversions", QString(), false, QStringList(), false, QStringList() << "totime" )
1738+
<< new StaticFunction( "to_interval", 1, fcnToInterval, "Conversions", QString(), false, QStringList(), false, QStringList() << "tointerval" )
17281739
<< new StaticFunction( "coalesce", -1, fcnCoalesce, "Conditionals" )
17291740
<< new StaticFunction( "if", 3, fcnIf, "Conditionals", "", False, QStringList(), true )
17301741
<< new StaticFunction( "regexp_match", 2, fcnRegexpMatch, "Conditionals" )
@@ -1771,15 +1782,15 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
17711782
<< new StaticFunction( "$perimeter", 0, fcnGeomPerimeter, "GeometryGroup", "", true )
17721783
<< new StaticFunction( "$x", 0, fcnX, "GeometryGroup", "", true )
17731784
<< new StaticFunction( "$y", 0, fcnY, "GeometryGroup", "", true )
1774-
<< new StaticFunction( "xat", 1, fcnXat, "GeometryGroup", "", true )
1775-
<< new StaticFunction( "yat", 1, fcnYat, "GeometryGroup", "", true )
1776-
<< new StaticFunction( "xmin", 1, fcnXMin, "GeometryGroup", "", true )
1777-
<< new StaticFunction( "xmax", 1, fcnXMax, "GeometryGroup", "", true )
1778-
<< new StaticFunction( "ymin", 1, fcnYMin, "GeometryGroup", "", true )
1779-
<< new StaticFunction( "ymax", 1, fcnYMax, "GeometryGroup", "", true )
1780-
<< new StaticFunction( "geomFromWKT", 1, fcnGeomFromWKT, "GeometryGroup" )
1781-
<< new StaticFunction( "geomFromGML", 1, fcnGeomFromGML, "GeometryGroup" )
1782-
<< new StaticFunction( "bbox", 2, fcnBbox, "GeometryGroup" )
1785+
<< new StaticFunction( "x_at", 1, fcnXat, "GeometryGroup", "", true, QStringList(), false, QStringList() << "xat" )
1786+
<< new StaticFunction( "y_at", 1, fcnYat, "GeometryGroup", "", true, QStringList(), false, QStringList() << "yat" )
1787+
<< new StaticFunction( "x_min", 1, fcnXMin, "GeometryGroup", "", true, QStringList(), false, QStringList() << "xmin" )
1788+
<< new StaticFunction( "x_max", 1, fcnXMax, "GeometryGroup", "", true, QStringList(), false, QStringList() << "xmax" )
1789+
<< new StaticFunction( "y_min", 1, fcnYMin, "GeometryGroup", "", true, QStringList(), false, QStringList() << "ymin" )
1790+
<< new StaticFunction( "y_max", 1, fcnYMax, "GeometryGroup", "", true, QStringList(), false, QStringList() << "ymax" )
1791+
<< new StaticFunction( "geom_from_wkt", 1, fcnGeomFromWKT, "GeometryGroup", QString(), false, QStringList(), false, QStringList() << "geomFromWKT" )
1792+
<< new StaticFunction( "geom_from_gml", 1, fcnGeomFromGML, "GeometryGroup", QString(), false, QStringList(), false, QStringList() << "geomFromGML" )
1793+
<< new StaticFunction( "intersects_bbox", 2, fcnBbox, "GeometryGroup", QString(), false, QStringList(), false, QStringList() << "bbox" )
17831794
<< new StaticFunction( "disjoint", 2, fcnDisjoint, "GeometryGroup" )
17841795
<< new StaticFunction( "intersects", 2, fcnIntersects, "GeometryGroup" )
17851796
<< new StaticFunction( "touches", 2, fcnTouches, "GeometryGroup" )
@@ -1792,22 +1803,22 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
17921803
<< new StaticFunction( "bounds", 1, fcnBounds, "GeometryGroup", "", true )
17931804
<< new StaticFunction( "bounds_width", 1, fcnBoundsWidth, "GeometryGroup", "", true )
17941805
<< new StaticFunction( "bounds_height", 1, fcnBoundsHeight, "GeometryGroup", "", true )
1795-
<< new StaticFunction( "convexHull", 1, fcnConvexHull, "GeometryGroup" )
1806+
<< new StaticFunction( "convex_hull", 1, fcnConvexHull, "GeometryGroup", QString(), false, QStringList(), false, QStringList() << "convexHull" )
17961807
<< new StaticFunction( "difference", 2, fcnDifference, "GeometryGroup" )
17971808
<< new StaticFunction( "distance", 2, fcnDistance, "GeometryGroup" )
17981809
<< new StaticFunction( "intersection", 2, fcnIntersection, "GeometryGroup" )
1799-
<< new StaticFunction( "symDifference", 2, fcnSymDifference, "GeometryGroup" )
1810+
<< new StaticFunction( "sym_difference", 2, fcnSymDifference, "GeometryGroup", QString(), false, QStringList(), false, QStringList() << "symDifference" )
18001811
<< new StaticFunction( "combine", 2, fcnCombine, "GeometryGroup" )
18011812
<< new StaticFunction( "union", 2, fcnCombine, "GeometryGroup" )
1802-
<< new StaticFunction( "geomToWKT", -1, fcnGeomToWKT, "GeometryGroup" )
1813+
<< new StaticFunction( "geom_to_wkt", -1, fcnGeomToWKT, "GeometryGroup", QString(), false, QStringList(), false, QStringList() << "geomToWKT" )
18031814
<< new StaticFunction( "geometry", 1, fcnGetGeometry, "GeometryGroup" )
18041815
<< new StaticFunction( "transform", 3, fcnTransformGeometry, "GeometryGroup" )
18051816
<< new StaticFunction( "$rownum", 0, fcnRowNumber, "Record" )
18061817
<< new StaticFunction( "$id", 0, fcnFeatureId, "Record" )
18071818
<< new StaticFunction( "$currentfeature", 0, fcnFeature, "Record" )
18081819
<< new StaticFunction( "$scale", 0, fcnScale, "Record" )
18091820
<< new StaticFunction( "$uuid", 0, fcnUuid, "Record" )
1810-
<< new StaticFunction( "getFeature", 3, fcnGetFeature, "Record" )
1821+
<< new StaticFunction( "get_feature", 3, fcnGetFeature, "Record", QString(), false, QStringList(), false, QStringList() << "getFeature" )
18111822

18121823
//return all attributes string for referencedColumns - this is caught by
18131824
// QgsFeatureRequest::setSubsetOfAttributes and causes all attributes to be fetched by the
@@ -1933,13 +1944,18 @@ bool QgsExpression::isFunctionName( QString name )
19331944
return functionIndex( name ) != -1;
19341945
}
19351946

1936-
int QgsExpression::functionIndex( QString name )
1947+
int QgsExpression::functionIndex( const QString &name )
19371948
{
19381949
int count = functionCount();
19391950
for ( int i = 0; i < count; i++ )
19401951
{
19411952
if ( QString::compare( name, Functions()[i]->name(), Qt::CaseInsensitive ) == 0 )
19421953
return i;
1954+
foreach ( QString alias, Functions()[i]->aliases() )
1955+
{
1956+
if ( QString::compare( name, alias, Qt::CaseInsensitive ) == 0 )
1957+
return i;
1958+
}
19431959
}
19441960
return -1;
19451961
}

‎src/core/qgsexpression.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,13 @@ class CORE_EXPORT QgsExpression
295295
/** Does this function use a geometry object. */
296296
bool usesgeometry() { return mUsesGeometry; }
297297

298+
/** Returns a list of possible aliases for the function. These include
299+
* other permissible names for the function, eg deprecated names.
300+
* @return list of known aliases
301+
* @note added in QGIS 2.9
302+
*/
303+
virtual QStringList aliases() const { return QStringList(); }
304+
298305
/** True if this function should use lazy evaluation. Lazy evaluation functions take QgsExpression::Node objects
299306
* rather than the node results when called. You can use node->eval(parent, feature) to evaluate the node and return the result
300307
* Functions are non lazy default and will be given the node return value when called **/
@@ -330,16 +337,19 @@ class CORE_EXPORT QgsExpression
330337
class StaticFunction : public Function
331338
{
332339
public:
333-
StaticFunction( QString fnname, int params, FcnEval fcn, QString group, QString helpText = QString(), bool usesGeometry = false, QStringList referencedColumns = QStringList(), bool lazyEval = false )
334-
: Function( fnname, params, group, helpText, usesGeometry, referencedColumns, lazyEval ), mFnc( fcn ) {}
340+
StaticFunction( QString fnname, int params, FcnEval fcn, QString group, QString helpText = QString(), bool usesGeometry = false, QStringList referencedColumns = QStringList(), bool lazyEval = false, const QStringList& aliases = QStringList() )
341+
: Function( fnname, params, group, helpText, usesGeometry, referencedColumns, lazyEval ), mFnc( fcn ), mAliases( aliases ) {}
335342

336343
virtual QVariant func( const QVariantList& values, const QgsFeature* f, QgsExpression* parent ) override
337344
{
338345
return mFnc( values, f, parent );
339346
}
340347

348+
virtual QStringList aliases() const override { return mAliases; }
349+
341350
private:
342351
FcnEval mFnc;
352+
QStringList mAliases;
343353
};
344354

345355
static const QList<Function*> &Functions();
@@ -355,7 +365,7 @@ class CORE_EXPORT QgsExpression
355365
static bool isFunctionName( QString name );
356366

357367
// return index of the function in Functions array
358-
static int functionIndex( QString name );
368+
static int functionIndex( const QString& name );
359369

360370
/** Returns the number of functions defined in the parser
361371
* @return The number of function defined in the parser.

‎src/core/qgsogcutils.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,7 +2128,7 @@ static QgsGeometry* geometryFromConstExpr( const QgsExpression::Node* node )
21282128
{
21292129
const QgsExpression::NodeFunction* fnNode = static_cast<const QgsExpression::NodeFunction*>( node );
21302130
QgsExpression::Function* fnDef = QgsExpression::Functions()[fnNode->fnIndex()];
2131-
if ( fnDef->name() == "geomFromWKT" )
2131+
if ( fnDef->name() == "geom_from_wkt" )
21322132
{
21332133
const QList<QgsExpression::Node*>& args = fnNode->args()->list();
21342134
if ( args[0]->nodeType() == QgsExpression::ntLiteral )
@@ -2146,7 +2146,7 @@ QDomElement QgsOgcUtils::expressionFunctionToOgcFilter( const QgsExpression::Nod
21462146
{
21472147
QgsExpression::Function* fd = QgsExpression::Functions()[node->fnIndex()];
21482148

2149-
if ( fd->name() == "bbox" )
2149+
if ( fd->name() == "intersects_bbox" )
21502150
{
21512151
QList<QgsExpression::Node*> argNodes = node->args()->list();
21522152
Q_ASSERT( argNodes.count() == 2 ); // binary spatial ops must have two args
@@ -2203,33 +2203,33 @@ QDomElement QgsOgcUtils::expressionFunctionToOgcFilter( const QgsExpression::Nod
22032203

22042204
const QgsExpression::NodeFunction* otherFn = static_cast<const QgsExpression::NodeFunction*>( otherNode );
22052205
QgsExpression::Function* otherFnDef = QgsExpression::Functions()[otherFn->fnIndex()];
2206-
if ( otherFnDef->name() == "geomFromWKT" )
2206+
if ( otherFnDef->name() == "geom_from_wkt" )
22072207
{
22082208
QgsExpression::Node* firstFnArg = otherFn->args()->list()[0];
22092209
if ( firstFnArg->nodeType() != QgsExpression::ntLiteral )
22102210
{
2211-
errorMessage = "geomFromWKT: argument must be string literal";
2211+
errorMessage = "geom_from_wkt: argument must be string literal";
22122212
return QDomElement();
22132213
}
22142214
QString wkt = static_cast<const QgsExpression::NodeLiteral*>( firstFnArg )->value().toString();
22152215
QgsGeometry* geom = QgsGeometry::fromWkt( wkt );
22162216
otherGeomElem = QgsOgcUtils::geometryToGML( geom, doc );
22172217
delete geom;
22182218
}
2219-
else if ( otherFnDef->name() == "geomFromGML" )
2219+
else if ( otherFnDef->name() == "geom_from_gml" )
22202220
{
22212221
QgsExpression::Node* firstFnArg = otherFn->args()->list()[0];
22222222
if ( firstFnArg->nodeType() != QgsExpression::ntLiteral )
22232223
{
2224-
errorMessage = "geomFromGML: argument must be string literal";
2224+
errorMessage = "geom_from_gml: argument must be string literal";
22252225
return QDomElement();
22262226
}
22272227

22282228
QDomDocument geomDoc;
22292229
QString gml = static_cast<const QgsExpression::NodeLiteral*>( firstFnArg )->value().toString();
22302230
if ( !geomDoc.setContent( gml, true ) )
22312231
{
2232-
errorMessage = "geomFromGML: unable to parse XML";
2232+
errorMessage = "geom_from_gml: unable to parse XML";
22332233
return QDomElement();
22342234
}
22352235

‎tests/src/core/testqgsexpression.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,43 @@ class TestQgsExpression: public QObject
145145
QCOMPARE( v.toDouble(), 5.79 );
146146
}
147147

148+
void alias_data()
149+
{
150+
//test function aliases
151+
QTest::addColumn<QString>( "string" );
152+
QTest::addColumn<bool>( "evalError" );
153+
QTest::addColumn<QString>( "dump" );
154+
QTest::addColumn<QVariant>( "result" );
155+
156+
QTest::newRow( "toint alias" ) << "toint(3.2)" << false << "to_int(3.2)" << QVariant( 3 );
157+
QTest::newRow( "int to double" ) << "toreal(3)" << false << "to_real(3)" << QVariant( 3. );
158+
QTest::newRow( "int to text" ) << "tostring(6)" << false << "to_string(6)" << QVariant( "6" );
159+
}
160+
161+
void alias()
162+
{
163+
QFETCH( QString, string );
164+
QFETCH( bool, evalError );
165+
QFETCH( QString, dump );
166+
QFETCH( QVariant, result );
167+
168+
QgsExpression exp( string );
169+
QCOMPARE( exp.hasParserError(), false );
170+
if ( exp.hasParserError() )
171+
qDebug() << exp.parserErrorString();
172+
173+
QVariant res = exp.evaluate();
174+
if ( exp.hasEvalError() )
175+
qDebug() << exp.evalErrorString();
176+
if ( res.type() != result.type() )
177+
{
178+
qDebug() << "got " << res.typeName() << " instead of " << result.typeName();
179+
}
180+
QCOMPARE( exp.hasEvalError(), evalError );
181+
QCOMPARE( res, result );
182+
QCOMPARE( exp.dump(), dump );
183+
}
184+
148185
void evaluation_data()
149186
{
150187
QTest::addColumn<QString>( "string" );
@@ -794,7 +831,7 @@ class TestQgsExpression: public QObject
794831
QgsPolygon polygon;
795832
polygon << polygon_ring;
796833

797-
QTest::newRow( "geomFromWKT Point" ) << "geomFromWKT('" + QgsGeometry::fromPoint( point )->exportToWkt() + "')" << ( void* ) QgsGeometry::fromPoint( point ) << false;
834+
QTest::newRow( "geomFromWKT Point" ) << "geom_from_wkt('" + QgsGeometry::fromPoint( point )->exportToWkt() + "')" << ( void* ) QgsGeometry::fromPoint( point ) << false;
798835
QTest::newRow( "geomFromWKT Line" ) << "geomFromWKT('" + QgsGeometry::fromPolyline( line )->exportToWkt() + "')" << ( void* ) QgsGeometry::fromPolyline( line ) << false;
799836
QTest::newRow( "geomFromWKT Polyline" ) << "geomFromWKT('" + QgsGeometry::fromPolyline( polyline )->exportToWkt() + "')" << ( void* ) QgsGeometry::fromPolyline( polyline ) << false;
800837
QTest::newRow( "geomFromWKT Polygon" ) << "geomFromWKT('" + QgsGeometry::fromPolygon( polygon )->exportToWkt() + "')" << ( void* ) QgsGeometry::fromPolygon( polygon ) << false;

‎tests/src/core/testqgsogcutils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void TestQgsOgcUtils::testExpressionFromOgcFilter_data()
174174
"<BBOX><PropertyName>Name>NAME</PropertyName><gml:Box srsName='foo'>"
175175
"<gml:coordinates>135.2239,34.4879 135.8578,34.8471</gml:coordinates></gml:Box></BBOX>"
176176
"</Filter>" )
177-
<< QString( "bbox($geometry, geomFromGML('<Box srsName=\"foo\"><coordinates>135.2239,34.4879 135.8578,34.8471</coordinates></Box>'))" );
177+
<< QString( "intersects_bbox($geometry, geom_from_gml('<Box srsName=\"foo\"><coordinates>135.2239,34.4879 135.8578,34.8471</coordinates></Box>'))" );
178178

179179
QTest::newRow( "Intersects" ) << QString(
180180
"<Filter>"
@@ -185,7 +185,7 @@ void TestQgsOgcUtils::testExpressionFromOgcFilter_data()
185185
"</gml:Point>"
186186
"</Intersects>"
187187
"</Filter>" )
188-
<< QString( "intersects($geometry, geomFromGML('<Point><coordinates>123,456</coordinates></Point>'))" );
188+
<< QString( "intersects($geometry, geom_from_gml('<Point><coordinates>123,456</coordinates></Point>'))" );
189189
}
190190

191191
void TestQgsOgcUtils::testExpressionFromOgcFilter()
@@ -326,7 +326,7 @@ void TestQgsOgcUtils::testExpressionToOgcFilter_data()
326326

327327
/*
328328
QTest::newRow( "bbox with GML3 Envelope" )
329-
<< QString( "bbox($geometry, geomFromGML('<gml:Envelope><gml:lowerCorner>13.0983 31.5899</gml:lowerCorner><gml:upperCorner>35.5472 42.8143</gml:upperCorner></gml:Envelope>'))" )
329+
<< QString( "intersects_bbox($geometry, geomFromGML('<gml:Envelope><gml:lowerCorner>13.0983 31.5899</gml:lowerCorner><gml:upperCorner>35.5472 42.8143</gml:upperCorner></gml:Envelope>'))" )
330330
<< QString(
331331
"<ogc:Filter>"
332332
"<ogc:BBOX>"

0 commit comments

Comments
 (0)
Please sign in to comment.