Skip to content

Commit

Permalink
[FEATURE] Add linear referencing functions to expression engine
Browse files Browse the repository at this point in the history
Adds new functions
- line_interpolate_point: interpolates a point by a given distance
along a linestring geometry
- line_locate_point: returns the distance along a linestring to
the closest position on the linestring to a given point
  • Loading branch information
nyalldawson committed Aug 21, 2016
1 parent 409dfdf commit e110ba7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
8 changes: 8 additions & 0 deletions resources/function_help/json/line_interpolate_point
@@ -0,0 +1,8 @@
{
"name": "line_interpolate_point",
"type": "function",
"description": "Returns the point interpolated by a specified distance along a linestring geometry.",
"arguments": [ {"arg":"geometry","description":"a linestring geometry"},
{"arg":"distance","description":"distance along line to interpolate"}],
"examples": [ { "expression":"geom_to_wkt(line_interpolate_point(geometry:=geom_from_wkt('LineString(0 0, 10 0)'),distance:=5))", "returns":"'Point (5 0)'"}]
}
8 changes: 8 additions & 0 deletions resources/function_help/json/line_locate_point
@@ -0,0 +1,8 @@
{
"name": "line_locate_point",
"type": "function",
"description": "Returns the distance along a linestring corresponding to the closest position the linestring comes to a specified point geometry.",
"arguments": [ {"arg":"geometry","description":"a linestring geometry"},
{"arg":"point","description":"point geometry to locate closest position on linestring to"}],
"examples": [ { "expression":"line_locate_point(geometry:=geom_from_wkt('LineString(0 0, 10 0)'),point:=geom_from_wkt('Point(5 0)'))", "returns":"5.0"}]
}
26 changes: 26 additions & 0 deletions src/core/qgsexpression.cpp
Expand Up @@ -2543,6 +2543,27 @@ static QVariant fcnShortestLine( const QVariantList& values, const QgsExpression
return result;
}

static QVariant fcnLineInterpolatePoint( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
{
QgsGeometry lineGeom = getGeometry( values.at( 0 ), parent );
double distance = getDoubleValue( values.at( 1 ), parent );

QgsGeometry geom = lineGeom.interpolate( distance );

QVariant result = !geom.isEmpty() ? QVariant::fromValue( geom ) : QVariant();
return result;
}

static QVariant fcnLineLocatePoint( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
{
QgsGeometry lineGeom = getGeometry( values.at( 0 ), parent );
QgsGeometry pointGeom = getGeometry( values.at( 1 ), parent );

double distance = lineGeom.lineLocatePoint( pointGeom );

return distance >= 0 ? distance : QVariant();
}

static QVariant fcnRound( const QVariantList& values, const QgsExpressionContext *, QgsExpression* parent )
{
if ( values.length() == 2 && values.at( 1 ).toInt() != 0 )
Expand Down Expand Up @@ -3121,6 +3142,7 @@ const QStringList& QgsExpression::BuiltinFunctions()
<< "bounds_width" << "bounds_height" << "is_closed" << "convex_hull" << "difference"
<< "distance" << "intersection" << "sym_difference" << "combine"
<< "extrude" << "azimuth" << "project" << "closest_point" << "shortest_line"
<< "line_locate_point" << "line_interpolate_point"
<< "union" << "geom_to_wkt" << "geomToWKT" << "geometry"
<< "transform" << "get_feature" << "getFeature"
<< "levenshtein" << "longest_common_substring" << "hamming_distance"
Expand Down Expand Up @@ -3345,6 +3367,10 @@ const QList<QgsExpression::Function*>& QgsExpression::Functions()
<< new StaticFunction( "order_parts", 3, fcnOrderParts, "GeometryGroup", QString() )
<< new StaticFunction( "closest_point", 2, fcnClosestPoint, "GeometryGroup" )
<< new StaticFunction( "shortest_line", 2, fcnShortestLine, "GeometryGroup" )
<< new StaticFunction( "line_interpolate_point", ParameterList() << Parameter( "geometry" )
<< Parameter( "distance" ), fcnLineInterpolatePoint, "GeometryGroup" )
<< new StaticFunction( "line_locate_point", ParameterList() << Parameter( "geometry" )
<< Parameter( "point" ), fcnLineLocatePoint, "GeometryGroup" )
<< new StaticFunction( "$id", 0, fcnFeatureId, "Record" )
<< new StaticFunction( "$currentfeature", 0, fcnFeature, "Record" )
<< new StaticFunction( "uuid", 0, fcnUuid, "Record", QString(), false, QStringList(), false, QStringList() << "$uuid" )
Expand Down
8 changes: 8 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -772,6 +772,14 @@ class TestQgsExpression: public QObject
QTest::newRow( "shortest_line geom" ) << "geom_to_wkt(shortest_line( geom_from_wkt('LineString( 1 1, 5 1, 5 5 )'),geom_from_wkt('Point( 6 3 )')))" << false << QVariant( "LineString (5 3, 6 3)" );
QTest::newRow( "shortest_line not geom" ) << "shortest_line('g','a')" << true << QVariant();
QTest::newRow( "shortest_line null" ) << "shortest_line(NULL,NULL)" << false << QVariant();
QTest::newRow( "line_interpolate_point not geom" ) << "line_interpolate_point('g', 5)" << true << QVariant();
QTest::newRow( "line_interpolate_point null" ) << "line_interpolate_point(NULL, 5)" << false << QVariant();
QTest::newRow( "line_interpolate_point point" ) << "line_interpolate_point(geom_from_wkt('POINT(1 2)'),5)" << false << QVariant();
QTest::newRow( "line_interpolate_point line" ) << "geom_to_wkt(line_interpolate_point(geometry:=geom_from_wkt('LineString(0 0, 10 0)'),distance:=5))" << false << QVariant( "Point (5 0)" );
QTest::newRow( "line_locate_point not geom" ) << "line_locate_point('g', geom_from_wkt('Point 5 0'))" << false << QVariant();
QTest::newRow( "line_locate_point null" ) << "line_locate_point(NULL, geom_from_wkt('Point 5 0'))" << false << QVariant();
QTest::newRow( "line_locate_point point" ) << "line_locate_point(geom_from_wkt('POINT(1 2)'),geom_from_wkt('Point 5 0'))" << false << QVariant();
QTest::newRow( "line_locate_point line" ) << "line_locate_point(geometry:=geom_from_wkt('LineString(0 0, 10 0)'),point:=geom_from_wkt('Point(5 0)'))" << false << QVariant( 5.0 );

// string functions
QTest::newRow( "lower" ) << "lower('HeLLo')" << false << QVariant( "hello" );
Expand Down

0 comments on commit e110ba7

Please sign in to comment.