Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE] New expression functions for angle/distance interpolation
angle_at_vertex: returns average (bisector) angle to a geometry
at a specified vertex index
distance_to_vertex: returns distance along geometry to a specified
vertex index
line_interpolate_angle: calculates the angle parallel to a geometry
at the specified distance along the geometry
line_interpolate_point: returns a point on line at distance
line_locate_point: returns distance along line to nearest line
location closest to specified point

Sponsored by Andreas Neumann
  • Loading branch information
nyalldawson committed Aug 29, 2016
1 parent 524d22f commit 1334524
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 0 deletions.
8 changes: 8 additions & 0 deletions resources/function_help/json/angle_at_vertex
@@ -0,0 +1,8 @@
{
"name": "angle_at_vertex",
"type": "function",
"description": "Returns the bisector angle (average angle) to the geometry for a specified vertex on a linestring geometry. Angles are in degrees clockwise from north.",
"arguments": [ {"arg":"geometry","description":"a linestring geometry"},
{"arg":"vertex","description":"vertex index, starting from 0"}],
"examples": [ { "expression":"angle_at_vertex(geometry:=geom_from_wkt('LineString(0 0, 10 0, 10 10)'),vertex:=1)", "returns":"45.0"}]
}
8 changes: 8 additions & 0 deletions resources/function_help/json/distance_to_vertex
@@ -0,0 +1,8 @@
{
"name": "distance_to_vertex",
"type": "function",
"description": "Returns the distance along the geometry to a specified vertex.",
"arguments": [ {"arg":"geometry","description":"a linestring geometry"},
{"arg":"vertex","description":"vertex index, starting from 0"}],
"examples": [ { "expression":"distance_to_vertex(geometry:=geom_from_wkt('LineString(0 0, 10 0, 10 10)'),vertex:=1)", "returns":"10.0"}]
}
8 changes: 8 additions & 0 deletions resources/function_help/json/line_interpolate_angle
@@ -0,0 +1,8 @@
{
"name": "line_interpolate_angle",
"type": "function",
"description": "Returns the angle parallel to the geometry at a specified distance along a linestring geometry. Angles are in degrees clockwise from north.",
"arguments": [ {"arg":"geometry","description":"a linestring geometry"},
{"arg":"distance","description":"distance along line to interpolate angle at"}],
"examples": [ { "expression":"line_interpolate_angle(geometry:=geom_from_wkt('LineString(0 0, 10 0)'),distance:=5)", "returns":"90.0"}]
}
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"}]
}
58 changes: 58 additions & 0 deletions src/core/qgsexpression.cpp
Expand Up @@ -2613,6 +2613,52 @@ 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 ? QVariant::fromValue( *geom ) : QVariant();
delete geom;
return result;
}

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

return lineGeom.interpolateAngle( distance ) * 180.0 / M_PI;
}

static QVariant fcnAngleAtVertex( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
{
QgsGeometry geom = getGeometry( values.at( 0 ), parent );
int vertex = getIntValue( values.at( 1 ), parent );

return geom.angleAtVertex( vertex ) * 180.0 / M_PI;
}

static QVariant fcnDistanceToVertex( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
{
QgsGeometry geom = getGeometry( values.at( 0 ), parent );
int vertex = getIntValue( values.at( 1 ), parent );

return geom.distanceToVertex( vertex );
}

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 )
Expand Down Expand Up @@ -3204,6 +3250,8 @@ 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"
<< "line_interpolate_angle" << "angle_at_vertex" << "distance_to_vertex"
<< "union" << "geom_to_wkt" << "geomToWKT" << "geometry"
<< "transform" << "get_feature" << "getFeature"
<< "levenshtein" << "longest_common_substring" << "hamming_distance"
Expand Down Expand Up @@ -3416,6 +3464,16 @@ 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_interpolate_angle", ParameterList() << Parameter( "geometry" )
<< Parameter( "distance" ), fcnLineInterpolateAngle, "GeometryGroup" )
<< new StaticFunction( "line_locate_point", ParameterList() << Parameter( "geometry" )
<< Parameter( "point" ), fcnLineLocatePoint, "GeometryGroup" )
<< new StaticFunction( "angle_at_vertex", ParameterList() << Parameter( "geometry" )
<< Parameter( "vertex" ), fcnAngleAtVertex, "GeometryGroup" )
<< new StaticFunction( "distance_to_vertex", ParameterList() << Parameter( "geometry" )
<< Parameter( "vertex" ), fcnDistanceToVertex, "GeometryGroup" )
<< new StaticFunction( "$rownum", 0, fcnRowNumber, "deprecated" )
<< new StaticFunction( "$id", 0, fcnFeatureId, "Record" )
<< new StaticFunction( "$currentfeature", 0, fcnFeature, "Record" )
Expand Down
20 changes: 20 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -758,6 +758,26 @@ 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 );
QTest::newRow( "line_interpolate_angle not geom" ) << "line_interpolate_angle('g', 5)" << true << QVariant();
QTest::newRow( "line_interpolate_angle null" ) << "line_interpolate_angle(NULL, 5)" << false << QVariant();
QTest::newRow( "line_interpolate_angle point" ) << "line_interpolate_angle(geom_from_wkt('POINT(1 2)'),5)" << false << QVariant( 0.0 );
QTest::newRow( "line_interpolate_angle line" ) << "line_interpolate_angle(geometry:=geom_from_wkt('LineString(0 0, 10 0)'),distance:=5)" << false << QVariant( 90.0 );
QTest::newRow( "angle_at_vertex not geom" ) << "angle_at_vertex('g', 5)" << true << QVariant();
QTest::newRow( "angle_at_vertex null" ) << "angle_at_vertex(NULL, 0)" << false << QVariant();
QTest::newRow( "angle_at_vertex point" ) << "angle_at_vertex(geom_from_wkt('POINT(1 2)'),0)" << false << QVariant( 0.0 );
QTest::newRow( "angle_at_vertex line" ) << "angle_at_vertex(geometry:=geom_from_wkt('LineString(0 0, 10 0, 10 10)'),vertex:=1)" << false << QVariant( 45.0 );
QTest::newRow( "distance_to_vertex not geom" ) << "distance_to_vertex('g', 5)" << true << QVariant();
QTest::newRow( "distance_to_vertex null" ) << "distance_to_vertex(NULL, 0)" << false << QVariant();
QTest::newRow( "distance_to_vertex point" ) << "distance_to_vertex(geom_from_wkt('POINT(1 2)'),0)" << false << QVariant( 0.0 );
QTest::newRow( "distance_to_vertex line" ) << "distance_to_vertex(geometry:=geom_from_wkt('LineString(0 0, 10 0, 10 10)'),vertex:=1)" << false << QVariant( 10.0 );

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

0 comments on commit 1334524

Please sign in to comment.