diff --git a/python/core/qgsexpression.sip b/python/core/qgsexpression.sip index b92ab8b41fc..cc2abeeddd3 100644 --- a/python/core/qgsexpression.sip +++ b/python/core/qgsexpression.sip @@ -202,6 +202,13 @@ class QgsExpression /** Does this function use a geometry object. */ bool usesgeometry(); + /** Returns a list of possible aliases for the function. These include + * other permissible names for the function, eg deprecated names. + * @return list of known aliases + * @note added in QGIS 2.9 + */ + virtual QStringList aliases() const; + /** True if this function should use lazy evaluation. Lazy evaluation functions take QgsExpression::Node objects * rather than the node results when called. You can use node->eval(parent, feature) to evaluate the node and return the result * Functions are non lazy default and will be given the node return value when called **/ @@ -231,7 +238,7 @@ class QgsExpression static bool isFunctionName( QString name ); // return index of the function in Functions array - static int functionIndex( QString name ); + static int functionIndex( const QString& name ); /** Returns the number of functions defined in the parser * @return The number of function defined in the parser. diff --git a/resources/function_help/bbox b/resources/function_help/bbox deleted file mode 100644 index d65bff44d28..00000000000 --- a/resources/function_help/bbox +++ /dev/null @@ -1,13 +0,0 @@ -

bbox function

-Returns 1 if the geometries spatially intersect the bounding box defined and 0 if they don't. - -

Syntax

-
bbox( a, b )
- -

Arguments

-a → geometry -b → geometry - -

Example

-
bbox( geomFromWKT( 'POINT(4 5)' ) , geomFromWKT( 'LINESTRING(3 3 , 4 4 , 5 5)' )) → returns 1
-
bbox( geomFromWKT( 'POINT(6 5)' ) , geomFromWKT( 'POLYGON((3 3 , 4 4 , 5 5, 3 3))' )) → returns 0
diff --git a/resources/function_help/convexHull b/resources/function_help/convex_hull similarity index 52% rename from resources/function_help/convexHull rename to resources/function_help/convex_hull index 54ecd6927c0..123a9286ce9 100644 --- a/resources/function_help/convexHull +++ b/resources/function_help/convex_hull @@ -1,12 +1,12 @@ -

convexHull function

+

convex_hull function

Returns the convex hull of a geometry. It represents the minimum convex geometry that encloses all geometries within the set.

Syntax

-
convexHull( a, b )
+
convex_hull( a, b )

Arguments

a → geometry a → geometry

Example

-
  geomToWKT( convexHull( geomFromWKT( 'LINESTRING(3 3 , 4 4 , 4 10)' )) )   → returns POLYGON((3 3,4 10,4 4,3 3)) 
+
  geom_to_wkt( convex_hull( geom_from_wkt( 'LINESTRING(3 3 , 4 4 , 4 10)' )) )   → returns POLYGON((3 3,4 10,4 4,3 3)) 
diff --git a/resources/function_help/geomFromGML b/resources/function_help/geomFromGML deleted file mode 100644 index 929d7114724..00000000000 --- a/resources/function_help/geomFromGML +++ /dev/null @@ -1,12 +0,0 @@ -

geomFromGML function

-Returns a geometry from a GML representation of geometry - -

Syntax

-
geomFromGML(text)
- -

Arguments

-text → GML representation of a geometry - -

Example

-
 geomFromGML( '4,4 5,5 6,6') → returns a geometry
- diff --git a/resources/function_help/geom_from_gml b/resources/function_help/geom_from_gml new file mode 100644 index 00000000000..1d55476f0b5 --- /dev/null +++ b/resources/function_help/geom_from_gml @@ -0,0 +1,12 @@ +

geom_from_gml function

+Returns a geometry from a GML representation of geometry + +

Syntax

+
geom_from_gml(text)
+ +

Arguments

+text → GML representation of a geometry + +

Example

+
 geom_from_gml( '4,4 5,5 6,6') → returns a geometry
+ diff --git a/resources/function_help/geomFromWKT b/resources/function_help/geom_from_wkt similarity index 59% rename from resources/function_help/geomFromWKT rename to resources/function_help/geom_from_wkt index d028d053a7d..7125b7cdeb1 100644 --- a/resources/function_help/geomFromWKT +++ b/resources/function_help/geom_from_wkt @@ -1,12 +1,12 @@ -

geomFromWKT function

+

geom_from_wkt function

Returns a geometry created from a Well-Known Text (WKT) representation.

Syntax

-
geomFromWKT(text)
+
geom_from_wkt(text)

Arguments

text → Well-Known Text (WKT) representation of a geometry

Example

-
 geomFromWKT( 'POINT(4 5)' ) → returns a geometry
+
 geom_from_wkt( 'POINT(4 5)' ) → returns a geometry
diff --git a/resources/function_help/geomToWKT b/resources/function_help/geom_to_wkt similarity index 58% rename from resources/function_help/geomToWKT rename to resources/function_help/geom_to_wkt index b45684b86db..cb5914178a6 100644 --- a/resources/function_help/geomToWKT +++ b/resources/function_help/geom_to_wkt @@ -1,11 +1,11 @@ -

geomToWKT function

+

geom_to_wkt function

Returns the Well-Known Text (WKT) representation of the geometry without SRID metadata.

Syntax

-
geomToWKT(a)
+
geom_to_wkt(a)

Arguments

a → geometry

Example

-
 geomToWKT( $geometry ) → POINT(6 50)
+
 geom_to_wkt( $geometry ) → POINT(6 50)
diff --git a/resources/function_help/getFeature b/resources/function_help/get_feature similarity index 50% rename from resources/function_help/getFeature rename to resources/function_help/get_feature index 9b703354f22..7ca2f5d6288 100644 --- a/resources/function_help/getFeature +++ b/resources/function_help/get_feature @@ -1,6 +1,6 @@ -

getFeature function

+

get_feature function

Returns the first feature of a layer matching a given attribute value

Syntax

-
getFeature( layer, attributeField, value )
+
get_feature( layer, attributeField, value )
diff --git a/resources/function_help/intersects_bbox b/resources/function_help/intersects_bbox new file mode 100644 index 00000000000..6fec554e131 --- /dev/null +++ b/resources/function_help/intersects_bbox @@ -0,0 +1,13 @@ +

intersects_bbox function

+Returns 1 if the geometries spatially intersect the bounding box defined and 0 if they don't. + +

Syntax

+
intersects_bbox( a, b )
+ +

Arguments

+a → geometry +b → geometry + +

Example

+
intersects_bbox( geomFromWKT( 'POINT(4 5)' ) , geom_from_wkt( 'LINESTRING(3 3 , 4 4 , 5 5)' )) → returns 1
+
intersects_bbox( geomFromWKT( 'POINT(6 5)' ) , geom_from_wkt( 'POLYGON((3 3 , 4 4 , 5 5, 3 3))' )) → returns 0
diff --git a/resources/function_help/symDifference b/resources/function_help/symDifference deleted file mode 100644 index 1cdaefaafe9..00000000000 --- a/resources/function_help/symDifference +++ /dev/null @@ -1,12 +0,0 @@ -

symDifference function

-Returns a geometry that represents the portions of a and b that do not intersect. - -

Syntax

-
symDifference( geometry a, geometry b)
- -

Arguments

-geometry → geometry -geometry → geometry - -

Example

-
 geomToWKT( symDifference(  geomFromWKT( 'LINESTRING(3 3 , 4 4 , 5 5)' ) , geomFromWKT( 'LINESTRING(3 3 , 8 8)' ))   )   → returns LINESTRING(5 5, 8 8)
diff --git a/resources/function_help/sym_difference b/resources/function_help/sym_difference new file mode 100644 index 00000000000..1101ff59165 --- /dev/null +++ b/resources/function_help/sym_difference @@ -0,0 +1,12 @@ +

sym_difference function

+Returns a geometry that represents the portions of a and b that do not intersect. + +

Syntax

+
sym_difference( geometry a, geometry b)
+ +

Arguments

+geometry → geometry +geometry → geometry + +

Example

+
 geom_to_wkt( sym_difference(  geom_from_wkt( 'LINESTRING(3 3 , 4 4 , 5 5)' ) , geom_from_wkt( 'LINESTRING(3 3 , 8 8)' ))   )   → returns LINESTRING(5 5, 8 8)
diff --git a/resources/function_help/todate b/resources/function_help/to_date similarity index 60% rename from resources/function_help/todate rename to resources/function_help/to_date index 64ec39ebc11..8e02c548634 100644 --- a/resources/function_help/todate +++ b/resources/function_help/to_date @@ -1,8 +1,8 @@ -

todate() function

+

to_date() function

Convert a string into Qt data type.

Syntax

-todate('string')
+to_date('string')

Arguments

string - is string in Qt date format. @@ -10,4 +10,4 @@ Convert a string into Qt data type.

Example

-todate('2012-05-04') → 2012-05-04
\ No newline at end of file +to_date('2012-05-04') → 2012-05-04
diff --git a/resources/function_help/todatetime b/resources/function_help/to_datetime similarity index 56% rename from resources/function_help/todatetime rename to resources/function_help/to_datetime index 5eeecbe1e14..f9ab89d192d 100644 --- a/resources/function_help/todatetime +++ b/resources/function_help/to_datetime @@ -1,8 +1,8 @@ -

todatetime() function

+

to_datetime() function

Convert a string into Qt data time type.

Syntax

-todatetime('string')
+to_datetime('string')

Arguments

string - is string in Qt date time format. @@ -10,4 +10,4 @@ Convert a string into Qt data time type.

Example

-todatetime('2012-05-04 12:50:00') → 2012-05-04T12:50:00
\ No newline at end of file +to_datetime('2012-05-04 12:50:00') → 2012-05-04T12:50:00
diff --git a/resources/function_help/toint b/resources/function_help/to_int similarity index 78% rename from resources/function_help/toint rename to resources/function_help/to_int index 709946955ed..6f1f8491efe 100644 --- a/resources/function_help/toint +++ b/resources/function_help/to_int @@ -1,8 +1,8 @@ -

toint() function

+

to_int() function

Converts a string to integer number. Nothing changed if a value cannot be converted to integer (e.g '123asd' is invalid).

Syntax

- toint(string)

+ to_int(string)

Arguments

@@ -10,4 +10,4 @@ Converts a string to integer number. Nothing changed if a value cannot be conver

Example

- toint('123') → 123

+ to_int('123') → 123

diff --git a/resources/function_help/tointerval b/resources/function_help/to_interval similarity index 59% rename from resources/function_help/tointerval rename to resources/function_help/to_interval index 3973c13f331..432d9013be6 100644 --- a/resources/function_help/tointerval +++ b/resources/function_help/to_interval @@ -1,8 +1,8 @@ -

tointerval() function

+

to_interval() function

Converts a string to a interval type. Can be used to take days, hours, month, etc off a date.

Syntax

-tointerval('string')
+to_interval('string')

Arguments

string - is string. Format {n} days {n} hours {n} months @@ -10,4 +10,4 @@ Converts a string to a interval type. Can be used to take days, hours, month, e

Example

-todatetime('2012-05-05 12:00:00') - tointerval('1 day 2 hours') → 2012-05-04T10:00:00
\ No newline at end of file +todatetime('2012-05-05 12:00:00') - to_interval('1 day 2 hours') → 2012-05-04T10:00:00
diff --git a/resources/function_help/toreal b/resources/function_help/to_real similarity index 81% rename from resources/function_help/toreal rename to resources/function_help/to_real index da733d75f66..a698bd996cb 100644 --- a/resources/function_help/toreal +++ b/resources/function_help/to_real @@ -1,8 +1,8 @@ -

toreal() function

+

to_real() function

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.

Syntax

- toreal(string)

+ to_real(string)

Arguments

@@ -10,4 +10,4 @@ Converts a string to real number. Nothing changed if a value cannot be converted

Example

- toreal('123.45') → 123.45

+ to_real('123.45') → 123.45

diff --git a/resources/function_help/tostring b/resources/function_help/to_string similarity index 70% rename from resources/function_help/tostring rename to resources/function_help/to_string index 8929ec3a411..fddd1b5194c 100644 --- a/resources/function_help/tostring +++ b/resources/function_help/to_string @@ -1,8 +1,8 @@ -

tostring() function

+

to_string() function

Converts a number to string.

Syntax

- tostring(number)

+ to_string(number)

Arguments

@@ -10,4 +10,4 @@ Converts a number to string.

Example

- tostring(123) → '123'

+ to_string(123) → '123'

diff --git a/resources/function_help/totime b/resources/function_help/to_time similarity index 60% rename from resources/function_help/totime rename to resources/function_help/to_time index 869d099cb5b..7235d6168f4 100644 --- a/resources/function_help/totime +++ b/resources/function_help/to_time @@ -1,8 +1,8 @@ -

totime() function

+

to_time() function

Convert a string into Qt time type.

Syntax

-totime('string')
+to_time('string')

Arguments

string - is string in Qt time format. @@ -10,4 +10,4 @@ Convert a string into Qt time type.

Example

-totime('12:30:01') → 12:30:01
\ No newline at end of file +to_time('12:30:01') → 12:30:01
diff --git a/resources/function_help/xat b/resources/function_help/x_at similarity index 74% rename from resources/function_help/xat rename to resources/function_help/x_at index 95d1b2e79bd..c77183b89d0 100644 --- a/resources/function_help/xat +++ b/resources/function_help/x_at @@ -1,11 +1,11 @@ -

xat() function

+

x_at() function

Retrieves a x coordinate of the current feature

Syntax

-xat(i) +x_at(i)

Arguments

i - is int. index of point of a line (indices start at 0; negative values apply to the last index).

Example

-
xat(1) → 5
+
x_at(1) → 5
diff --git a/resources/function_help/xmax b/resources/function_help/x_max similarity index 60% rename from resources/function_help/xmax rename to resources/function_help/x_max index 717a530a8e7..b4e3a0570cc 100644 --- a/resources/function_help/xmax +++ b/resources/function_help/x_max @@ -1,12 +1,12 @@ -

xmax function

+

x_max function

Returns the maximum x coordinate of a geometry. Calculations are in the Spatial Reference System of this Geometry.

Syntax

-
xmax(geom)
+
x_max(geom)

Arguments

geom → a geometry

Example

-
 xmax($geometry) → returns maximum x coordinate of $geometry
+
 x_max($geometry) → returns maximum x coordinate of $geometry
diff --git a/resources/function_help/xmin b/resources/function_help/x_min similarity index 60% rename from resources/function_help/xmin rename to resources/function_help/x_min index e04c9076d18..6d8362df0c7 100644 --- a/resources/function_help/xmin +++ b/resources/function_help/x_min @@ -1,12 +1,12 @@ -

xmin function

+

x_min function

Returns the minimum x coordinate of a geometry. Calculations are in the Spatial Reference System of this Geometry.

Syntax

-
xmin(geom)
+
x_min(geom)

Arguments

geom → a geometry

Example

-
 xmin($geometry) → returns minimum x coordinate of $geometry
+
 x_min($geometry) → returns minimum x coordinate of $geometry
diff --git a/resources/function_help/yat b/resources/function_help/y_at similarity index 74% rename from resources/function_help/yat rename to resources/function_help/y_at index 42e0d046d7f..4cacf84d6f5 100644 --- a/resources/function_help/yat +++ b/resources/function_help/y_at @@ -1,11 +1,11 @@ -

yat() function

+

y_at() function

Retrieves a y coordinate of the current feature

Syntax

-yat(i) +y_at(i)

Arguments

i - is int. index of point of a line (indices start at 0; negative values apply to the last index).

Example

-
yat(1) → 5
+
y_at(1) → 5
diff --git a/resources/function_help/ymax b/resources/function_help/y_max similarity index 60% rename from resources/function_help/ymax rename to resources/function_help/y_max index 68d9a99bcd6..c125745b02b 100644 --- a/resources/function_help/ymax +++ b/resources/function_help/y_max @@ -1,12 +1,12 @@ -

ymax function

+

y_max function

Returns the maximum y coordinate of a geometry. Calculations are in the Spatial Reference System of this Geometry.

Syntax

-
ymax(geom)
+
y_max(geom)

Arguments

geom → a geometry

Example

-
 ymax($geometry) → returns maximum y coordinate of $geometry
+
 y_max($geometry) → returns maximum y coordinate of $geometry
diff --git a/resources/function_help/ymin b/resources/function_help/y_min similarity index 60% rename from resources/function_help/ymin rename to resources/function_help/y_min index e860a053136..6c5bb34fae6 100644 --- a/resources/function_help/ymin +++ b/resources/function_help/y_min @@ -1,12 +1,12 @@ -

ymin function

+

y_min function

Returns the minimum y coordinate of a geometry. Calculations are in the Spatial Reference System of this Geometry.

Syntax

-
ymin(geom)
+
y_min(geom)

Arguments

geom → a geometry

Example

-
 ymin($geometry) → returns minimum y coordinate of $geometry
+
 y_min($geometry) → returns minimum y coordinate of $geometry
diff --git a/src/core/qgsexpression.cpp b/src/core/qgsexpression.cpp index 9ccd4e67246..ddbc09d3d2a 100644 --- a/src/core/qgsexpression.cpp +++ b/src/core/qgsexpression.cpp @@ -1666,22 +1666,33 @@ const QStringList &QgsExpression::BuiltinFunctions() << "asin" << "acos" << "atan" << "atan2" << "exp" << "ln" << "log10" << "log" << "round" << "rand" << "randf" << "max" << "min" << "clamp" - << "scale_linear" << "scale_exp" << "floor" << "ceil" - << "toint" << "toreal" << "tostring" - << "todatetime" << "todate" << "totime" << "tointerval" - << "coalesce" << "regexp_match" << "$now" << "age" << "year" + << "scale_linear" << "scale_exp" << "floor" << "ceil" << "$pi" + << "toint" << "to_int" << "toreal" << "to_real" << "tostring" << "to_string" + << "todatetime" << "to_datetime" << "todate" << "to_date" + << "totime" << "to_time" << "tointerval" << "to_interval" + << "coalesce" << "if" << "regexp_match" << "age" << "year" << "month" << "week" << "day" << "hour" << "minute" << "second" << "lower" << "upper" << "title" << "length" << "replace" << "trim" << "wordwrap" << "regexp_replace" << "regexp_substr" << "substr" << "concat" << "strpos" << "left" - << "right" << "rpad" << "lpad" + << "right" << "rpad" << "lpad" << "format" << "format_number" << "format_date" << "color_rgb" << "color_rgba" << "ramp_color" << "color_hsl" << "color_hsla" << "color_hsv" << "color_hsva" << "color_cymk" << "color_cymka" << "xat" << "yat" << "$area" << "$length" << "$perimeter" << "$x" << "$y" + << "x_at" << "xat" << "y_at" << "yat" << "x_min" << "xmin" << "x_max" << "xmax" + << "y_min" << "ymin" << "y_max" << "ymax" << "geom_from_wkt" << "geomFromWKT" + << "geom_from_gml" << "geomFromGML" << "intersects_bbox" << "bbox" + << "disjoint" << "intersects" << "touches" << "crosses" << "contains" + << "overlaps" << "within" << "buffer" << "centroid" << "bounds" + << "bounds_width" << "bounds_height" << "convex_hull" << "difference" + << "distance" << "intersection" << "sym_difference" << "combine" + << "union" << "geom_to_wkt" << "geomToWKT" << "geometry" + << "transform" << "get_feature" << "getFeature" + << "attribute" << "$rownum" << "$id" << "$scale" << "_specialcol_"; } return gmBuiltinFunctions; @@ -1718,13 +1729,13 @@ const QList &QgsExpression::Functions() << new StaticFunction( "floor", 1, fcnFloor, "Math" ) << new StaticFunction( "ceil", 1, fcnCeil, "Math" ) << new StaticFunction( "$pi", 0, fcnPi, "Math" ) - << new StaticFunction( "toint", 1, fcnToInt, "Conversions" ) - << new StaticFunction( "toreal", 1, fcnToReal, "Conversions" ) - << new StaticFunction( "tostring", 1, fcnToString, "Conversions" ) - << new StaticFunction( "todatetime", 1, fcnToDateTime, "Conversions" ) - << new StaticFunction( "todate", 1, fcnToDate, "Conversions" ) - << new StaticFunction( "totime", 1, fcnToTime, "Conversions" ) - << new StaticFunction( "tointerval", 1, fcnToInterval, "Conversions" ) + << new StaticFunction( "to_int", 1, fcnToInt, "Conversions", QString(), false, QStringList(), false, QStringList() << "toint" ) + << new StaticFunction( "to_real", 1, fcnToReal, "Conversions", QString(), false, QStringList(), false, QStringList() << "toreal" ) + << new StaticFunction( "to_string", 1, fcnToString, "Conversions", QString(), false, QStringList(), false, QStringList() << "tostring" ) + << new StaticFunction( "to_datetime", 1, fcnToDateTime, "Conversions", QString(), false, QStringList(), false, QStringList() << "todatetime" ) + << new StaticFunction( "to_date", 1, fcnToDate, "Conversions", QString(), false, QStringList(), false, QStringList() << "todate" ) + << new StaticFunction( "to_time", 1, fcnToTime, "Conversions", QString(), false, QStringList(), false, QStringList() << "totime" ) + << new StaticFunction( "to_interval", 1, fcnToInterval, "Conversions", QString(), false, QStringList(), false, QStringList() << "tointerval" ) << new StaticFunction( "coalesce", -1, fcnCoalesce, "Conditionals" ) << new StaticFunction( "if", 3, fcnIf, "Conditionals", "", False, QStringList(), true ) << new StaticFunction( "regexp_match", 2, fcnRegexpMatch, "Conditionals" ) @@ -1771,15 +1782,15 @@ const QList &QgsExpression::Functions() << new StaticFunction( "$perimeter", 0, fcnGeomPerimeter, "GeometryGroup", "", true ) << new StaticFunction( "$x", 0, fcnX, "GeometryGroup", "", true ) << new StaticFunction( "$y", 0, fcnY, "GeometryGroup", "", true ) - << new StaticFunction( "xat", 1, fcnXat, "GeometryGroup", "", true ) - << new StaticFunction( "yat", 1, fcnYat, "GeometryGroup", "", true ) - << new StaticFunction( "xmin", 1, fcnXMin, "GeometryGroup", "", true ) - << new StaticFunction( "xmax", 1, fcnXMax, "GeometryGroup", "", true ) - << new StaticFunction( "ymin", 1, fcnYMin, "GeometryGroup", "", true ) - << new StaticFunction( "ymax", 1, fcnYMax, "GeometryGroup", "", true ) - << new StaticFunction( "geomFromWKT", 1, fcnGeomFromWKT, "GeometryGroup" ) - << new StaticFunction( "geomFromGML", 1, fcnGeomFromGML, "GeometryGroup" ) - << new StaticFunction( "bbox", 2, fcnBbox, "GeometryGroup" ) + << new StaticFunction( "x_at", 1, fcnXat, "GeometryGroup", "", true, QStringList(), false, QStringList() << "xat" ) + << new StaticFunction( "y_at", 1, fcnYat, "GeometryGroup", "", true, QStringList(), false, QStringList() << "yat" ) + << new StaticFunction( "x_min", 1, fcnXMin, "GeometryGroup", "", true, QStringList(), false, QStringList() << "xmin" ) + << new StaticFunction( "x_max", 1, fcnXMax, "GeometryGroup", "", true, QStringList(), false, QStringList() << "xmax" ) + << new StaticFunction( "y_min", 1, fcnYMin, "GeometryGroup", "", true, QStringList(), false, QStringList() << "ymin" ) + << new StaticFunction( "y_max", 1, fcnYMax, "GeometryGroup", "", true, QStringList(), false, QStringList() << "ymax" ) + << new StaticFunction( "geom_from_wkt", 1, fcnGeomFromWKT, "GeometryGroup", QString(), false, QStringList(), false, QStringList() << "geomFromWKT" ) + << new StaticFunction( "geom_from_gml", 1, fcnGeomFromGML, "GeometryGroup", QString(), false, QStringList(), false, QStringList() << "geomFromGML" ) + << new StaticFunction( "intersects_bbox", 2, fcnBbox, "GeometryGroup", QString(), false, QStringList(), false, QStringList() << "bbox" ) << new StaticFunction( "disjoint", 2, fcnDisjoint, "GeometryGroup" ) << new StaticFunction( "intersects", 2, fcnIntersects, "GeometryGroup" ) << new StaticFunction( "touches", 2, fcnTouches, "GeometryGroup" ) @@ -1792,14 +1803,14 @@ const QList &QgsExpression::Functions() << new StaticFunction( "bounds", 1, fcnBounds, "GeometryGroup", "", true ) << new StaticFunction( "bounds_width", 1, fcnBoundsWidth, "GeometryGroup", "", true ) << new StaticFunction( "bounds_height", 1, fcnBoundsHeight, "GeometryGroup", "", true ) - << new StaticFunction( "convexHull", 1, fcnConvexHull, "GeometryGroup" ) + << new StaticFunction( "convex_hull", 1, fcnConvexHull, "GeometryGroup", QString(), false, QStringList(), false, QStringList() << "convexHull" ) << new StaticFunction( "difference", 2, fcnDifference, "GeometryGroup" ) << new StaticFunction( "distance", 2, fcnDistance, "GeometryGroup" ) << new StaticFunction( "intersection", 2, fcnIntersection, "GeometryGroup" ) - << new StaticFunction( "symDifference", 2, fcnSymDifference, "GeometryGroup" ) + << new StaticFunction( "sym_difference", 2, fcnSymDifference, "GeometryGroup", QString(), false, QStringList(), false, QStringList() << "symDifference" ) << new StaticFunction( "combine", 2, fcnCombine, "GeometryGroup" ) << new StaticFunction( "union", 2, fcnCombine, "GeometryGroup" ) - << new StaticFunction( "geomToWKT", -1, fcnGeomToWKT, "GeometryGroup" ) + << new StaticFunction( "geom_to_wkt", -1, fcnGeomToWKT, "GeometryGroup", QString(), false, QStringList(), false, QStringList() << "geomToWKT" ) << new StaticFunction( "geometry", 1, fcnGetGeometry, "GeometryGroup" ) << new StaticFunction( "transform", 3, fcnTransformGeometry, "GeometryGroup" ) << new StaticFunction( "$rownum", 0, fcnRowNumber, "Record" ) @@ -1807,7 +1818,7 @@ const QList &QgsExpression::Functions() << new StaticFunction( "$currentfeature", 0, fcnFeature, "Record" ) << new StaticFunction( "$scale", 0, fcnScale, "Record" ) << new StaticFunction( "$uuid", 0, fcnUuid, "Record" ) - << new StaticFunction( "getFeature", 3, fcnGetFeature, "Record" ) + << new StaticFunction( "get_feature", 3, fcnGetFeature, "Record", QString(), false, QStringList(), false, QStringList() << "getFeature" ) //return all attributes string for referencedColumns - this is caught by // QgsFeatureRequest::setSubsetOfAttributes and causes all attributes to be fetched by the @@ -1933,13 +1944,18 @@ bool QgsExpression::isFunctionName( QString name ) return functionIndex( name ) != -1; } -int QgsExpression::functionIndex( QString name ) +int QgsExpression::functionIndex( const QString &name ) { int count = functionCount(); for ( int i = 0; i < count; i++ ) { if ( QString::compare( name, Functions()[i]->name(), Qt::CaseInsensitive ) == 0 ) return i; + foreach ( QString alias, Functions()[i]->aliases() ) + { + if ( QString::compare( name, alias, Qt::CaseInsensitive ) == 0 ) + return i; + } } return -1; } diff --git a/src/core/qgsexpression.h b/src/core/qgsexpression.h index a0469780e6a..2bd7c712699 100644 --- a/src/core/qgsexpression.h +++ b/src/core/qgsexpression.h @@ -295,6 +295,13 @@ class CORE_EXPORT QgsExpression /** Does this function use a geometry object. */ bool usesgeometry() { return mUsesGeometry; } + /** Returns a list of possible aliases for the function. These include + * other permissible names for the function, eg deprecated names. + * @return list of known aliases + * @note added in QGIS 2.9 + */ + virtual QStringList aliases() const { return QStringList(); } + /** True if this function should use lazy evaluation. Lazy evaluation functions take QgsExpression::Node objects * rather than the node results when called. You can use node->eval(parent, feature) to evaluate the node and return the result * Functions are non lazy default and will be given the node return value when called **/ @@ -330,16 +337,19 @@ class CORE_EXPORT QgsExpression class StaticFunction : public Function { public: - StaticFunction( QString fnname, int params, FcnEval fcn, QString group, QString helpText = QString(), bool usesGeometry = false, QStringList referencedColumns = QStringList(), bool lazyEval = false ) - : Function( fnname, params, group, helpText, usesGeometry, referencedColumns, lazyEval ), mFnc( fcn ) {} + 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() ) + : Function( fnname, params, group, helpText, usesGeometry, referencedColumns, lazyEval ), mFnc( fcn ), mAliases( aliases ) {} virtual QVariant func( const QVariantList& values, const QgsFeature* f, QgsExpression* parent ) override { return mFnc( values, f, parent ); } + virtual QStringList aliases() const override { return mAliases; } + private: FcnEval mFnc; + QStringList mAliases; }; static const QList &Functions(); @@ -355,7 +365,7 @@ class CORE_EXPORT QgsExpression static bool isFunctionName( QString name ); // return index of the function in Functions array - static int functionIndex( QString name ); + static int functionIndex( const QString& name ); /** Returns the number of functions defined in the parser * @return The number of function defined in the parser. diff --git a/src/core/qgsogcutils.cpp b/src/core/qgsogcutils.cpp index 811a5edf047..46287ef709d 100644 --- a/src/core/qgsogcutils.cpp +++ b/src/core/qgsogcutils.cpp @@ -2128,7 +2128,7 @@ static QgsGeometry* geometryFromConstExpr( const QgsExpression::Node* node ) { const QgsExpression::NodeFunction* fnNode = static_cast( node ); QgsExpression::Function* fnDef = QgsExpression::Functions()[fnNode->fnIndex()]; - if ( fnDef->name() == "geomFromWKT" ) + if ( fnDef->name() == "geom_from_wkt" ) { const QList& args = fnNode->args()->list(); if ( args[0]->nodeType() == QgsExpression::ntLiteral ) @@ -2146,7 +2146,7 @@ QDomElement QgsOgcUtils::expressionFunctionToOgcFilter( const QgsExpression::Nod { QgsExpression::Function* fd = QgsExpression::Functions()[node->fnIndex()]; - if ( fd->name() == "bbox" ) + if ( fd->name() == "intersects_bbox" ) { QList argNodes = node->args()->list(); Q_ASSERT( argNodes.count() == 2 ); // binary spatial ops must have two args @@ -2203,12 +2203,12 @@ QDomElement QgsOgcUtils::expressionFunctionToOgcFilter( const QgsExpression::Nod const QgsExpression::NodeFunction* otherFn = static_cast( otherNode ); QgsExpression::Function* otherFnDef = QgsExpression::Functions()[otherFn->fnIndex()]; - if ( otherFnDef->name() == "geomFromWKT" ) + if ( otherFnDef->name() == "geom_from_wkt" ) { QgsExpression::Node* firstFnArg = otherFn->args()->list()[0]; if ( firstFnArg->nodeType() != QgsExpression::ntLiteral ) { - errorMessage = "geomFromWKT: argument must be string literal"; + errorMessage = "geom_from_wkt: argument must be string literal"; return QDomElement(); } QString wkt = static_cast( firstFnArg )->value().toString(); @@ -2216,12 +2216,12 @@ QDomElement QgsOgcUtils::expressionFunctionToOgcFilter( const QgsExpression::Nod otherGeomElem = QgsOgcUtils::geometryToGML( geom, doc ); delete geom; } - else if ( otherFnDef->name() == "geomFromGML" ) + else if ( otherFnDef->name() == "geom_from_gml" ) { QgsExpression::Node* firstFnArg = otherFn->args()->list()[0]; if ( firstFnArg->nodeType() != QgsExpression::ntLiteral ) { - errorMessage = "geomFromGML: argument must be string literal"; + errorMessage = "geom_from_gml: argument must be string literal"; return QDomElement(); } @@ -2229,7 +2229,7 @@ QDomElement QgsOgcUtils::expressionFunctionToOgcFilter( const QgsExpression::Nod QString gml = static_cast( firstFnArg )->value().toString(); if ( !geomDoc.setContent( gml, true ) ) { - errorMessage = "geomFromGML: unable to parse XML"; + errorMessage = "geom_from_gml: unable to parse XML"; return QDomElement(); } diff --git a/tests/src/core/testqgsexpression.cpp b/tests/src/core/testqgsexpression.cpp index 007f95425e8..32da69a418c 100644 --- a/tests/src/core/testqgsexpression.cpp +++ b/tests/src/core/testqgsexpression.cpp @@ -145,6 +145,43 @@ class TestQgsExpression: public QObject QCOMPARE( v.toDouble(), 5.79 ); } + void alias_data() + { + //test function aliases + QTest::addColumn( "string" ); + QTest::addColumn( "evalError" ); + QTest::addColumn( "dump" ); + QTest::addColumn( "result" ); + + QTest::newRow( "toint alias" ) << "toint(3.2)" << false << "to_int(3.2)" << QVariant( 3 ); + QTest::newRow( "int to double" ) << "toreal(3)" << false << "to_real(3)" << QVariant( 3. ); + QTest::newRow( "int to text" ) << "tostring(6)" << false << "to_string(6)" << QVariant( "6" ); + } + + void alias() + { + QFETCH( QString, string ); + QFETCH( bool, evalError ); + QFETCH( QString, dump ); + QFETCH( QVariant, result ); + + QgsExpression exp( string ); + QCOMPARE( exp.hasParserError(), false ); + if ( exp.hasParserError() ) + qDebug() << exp.parserErrorString(); + + QVariant res = exp.evaluate(); + if ( exp.hasEvalError() ) + qDebug() << exp.evalErrorString(); + if ( res.type() != result.type() ) + { + qDebug() << "got " << res.typeName() << " instead of " << result.typeName(); + } + QCOMPARE( exp.hasEvalError(), evalError ); + QCOMPARE( res, result ); + QCOMPARE( exp.dump(), dump ); + } + void evaluation_data() { QTest::addColumn( "string" ); @@ -794,7 +831,7 @@ class TestQgsExpression: public QObject QgsPolygon polygon; polygon << polygon_ring; - QTest::newRow( "geomFromWKT Point" ) << "geomFromWKT('" + QgsGeometry::fromPoint( point )->exportToWkt() + "')" << ( void* ) QgsGeometry::fromPoint( point ) << false; + QTest::newRow( "geomFromWKT Point" ) << "geom_from_wkt('" + QgsGeometry::fromPoint( point )->exportToWkt() + "')" << ( void* ) QgsGeometry::fromPoint( point ) << false; QTest::newRow( "geomFromWKT Line" ) << "geomFromWKT('" + QgsGeometry::fromPolyline( line )->exportToWkt() + "')" << ( void* ) QgsGeometry::fromPolyline( line ) << false; QTest::newRow( "geomFromWKT Polyline" ) << "geomFromWKT('" + QgsGeometry::fromPolyline( polyline )->exportToWkt() + "')" << ( void* ) QgsGeometry::fromPolyline( polyline ) << false; QTest::newRow( "geomFromWKT Polygon" ) << "geomFromWKT('" + QgsGeometry::fromPolygon( polygon )->exportToWkt() + "')" << ( void* ) QgsGeometry::fromPolygon( polygon ) << false; diff --git a/tests/src/core/testqgsogcutils.cpp b/tests/src/core/testqgsogcutils.cpp index 0ce2416d285..ab6f65fcb0e 100644 --- a/tests/src/core/testqgsogcutils.cpp +++ b/tests/src/core/testqgsogcutils.cpp @@ -174,7 +174,7 @@ void TestQgsOgcUtils::testExpressionFromOgcFilter_data() "Name>NAME" "135.2239,34.4879 135.8578,34.8471" "" ) - << QString( "bbox($geometry, geomFromGML('135.2239,34.4879 135.8578,34.8471'))" ); + << QString( "intersects_bbox($geometry, geom_from_gml('135.2239,34.4879 135.8578,34.8471'))" ); QTest::newRow( "Intersects" ) << QString( "" @@ -185,7 +185,7 @@ void TestQgsOgcUtils::testExpressionFromOgcFilter_data() "" "" "" ) - << QString( "intersects($geometry, geomFromGML('123,456'))" ); + << QString( "intersects($geometry, geom_from_gml('123,456'))" ); } void TestQgsOgcUtils::testExpressionFromOgcFilter() @@ -326,7 +326,7 @@ void TestQgsOgcUtils::testExpressionToOgcFilter_data() /* QTest::newRow( "bbox with GML3 Envelope" ) - << QString( "bbox($geometry, geomFromGML('13.0983 31.589935.5472 42.8143'))" ) + << QString( "intersects_bbox($geometry, geomFromGML('13.0983 31.589935.5472 42.8143'))" ) << QString( "" ""