Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE] Add wedge_buffer expression function
Returns a wedge shaped buffer originating from a point geometry,
with arguments for azimuth, buffer width (in degrees), outer radius
and inner radius.
  • Loading branch information
nyalldawson committed Apr 23, 2018
1 parent 2850b30 commit d5647bc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
12 changes: 12 additions & 0 deletions resources/function_help/json/wedge_buffer
@@ -0,0 +1,12 @@
{
"name": "wedge_buffer",
"type": "function",
"description": "Returns a wedge shaped buffer originating from a point geometry.",
"arguments": [ {"arg":"center","description":"center point (origin) of buffer. Must be a point geometry."},
{"arg":"azimuth","description":"angle (in degrees) for the middle of the wedge to point."},
{"arg":"width","description":"buffer width (in degrees). Note that the wedge will extend to half of the angular width either side of the azimuth direction."},
{"arg":"outer_radius","description":"outer radius for buffers"},
{"arg":"inner_radius","optional":true,"default":"0.0","description":"optional inner radius for buffers"}],
"examples": [ { "expression":"wedge_buffer(center:=geom_from_wkt('POINT(1 2)'),azimuth:=90,width:=180,outer_radius:=1)", "returns":"A wedge shaped buffer centered on the point (1,2), facing to the East, with a width of 180 degrees and outer radius of 1."}]
}

25 changes: 25 additions & 0 deletions src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -2492,6 +2492,26 @@ static QVariant fcnBuffer( const QVariantList &values, const QgsExpressionContex
return result;
}

static QVariant fcnWedgeBuffer( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
const QgsPoint *pt = qgsgeometry_cast<const QgsPoint *>( fGeom.constGet() );
if ( !pt )
{
parent->setEvalErrorString( QObject::tr( "Function `wedge_buffer` requires a point value for the center." ) );
return QVariant();
}

double azimuth = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
double width = QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent );
double outerRadius = QgsExpressionUtils::getDoubleValue( values.at( 3 ), parent );
double innerRadius = QgsExpressionUtils::getDoubleValue( values.at( 4 ), parent );

QgsGeometry geom = QgsGeometry::createWedgeBuffer( *pt, azimuth, width, outerRadius, innerRadius );
QVariant result = !geom.isNull() ? QVariant::fromValue( geom ) : QVariant();
return result;
}

static QVariant fcnOffsetCurve( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
Expand Down Expand Up @@ -4272,6 +4292,11 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
<< new QgsStaticExpressionFunction( QStringLiteral( "within" ), 2, fcnWithin, QStringLiteral( "GeometryGroup" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "translate" ), 3, fcnTranslate, QStringLiteral( "GeometryGroup" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "buffer" ), -1, fcnBuffer, QStringLiteral( "GeometryGroup" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "wedge_buffer" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "center" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "azimuth" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "width" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "outer_radius" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "inner_radius" ), true, 0.0 ), fcnWedgeBuffer, QStringLiteral( "GeometryGroup" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "offset_curve" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geometry" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "distance" ) )
<< QgsExpressionFunction::Parameter( QStringLiteral( "segments" ), true, 8.0 )
Expand Down
6 changes: 5 additions & 1 deletion tests/src/core/testqgsexpression.cpp
Expand Up @@ -814,6 +814,10 @@ class TestQgsExpression: public QObject
QTest::newRow( "offset_curve line" ) << "geom_to_wkt(offset_curve(geom_from_wkt('LineString(0 0, 10 0)'),1,segments:=4))" << false << QVariant( "LineString (0 1, 10 1)" );
QTest::newRow( "offset_curve line miter" ) << "geom_to_wkt(offset_curve(geometry:=geom_from_wkt('LineString(0 0, 10 0)'),distance:=-1,join:=2,miter_limit:=1))" << false << QVariant( "LineString (10 -1, 0 -1)" );
QTest::newRow( "offset_curve line bevel" ) << "geom_to_wkt(offset_curve(geometry:=geom_from_wkt('LineString(0 0, 10 0, 10 10)'),distance:=1,join:=3))" << false << QVariant( "LineString (0 1, 9 1, 9 10)" );
QTest::newRow( "wedge_buffer not geom" ) << "wedge_buffer('g', 0, 45, 1)" << true << QVariant();
QTest::newRow( "wedge_buffer null" ) << "wedge_buffer(NULL, 0, 45, 1)" << false << QVariant();
QTest::newRow( "wedge_buffer point" ) << "geom_to_wkt(wedge_buffer(center:=geom_from_wkt('POINT(1 2)'),azimuth:=90,width:=180,outer_radius:=1))" << false << QVariant( QStringLiteral( "CurvePolygon (CompoundCurve (CircularString (1 3, 2 2, 1 1),(1 1, 1 2),(1 2, 1 3)))" ) );
QTest::newRow( "wedge_buffer point inner" ) << "geom_to_wkt(wedge_buffer(center:=geom_from_wkt('POINT(1 2)'),azimuth:=90,width:=180,outer_radius:=2,inner_radius:=1))" << false << QVariant( QStringLiteral( "CurvePolygon (CompoundCurve (CircularString (1 4, 3 2, 1 0),(1 0, 1 1),CircularString (1 1, 0 2, 1 3),(1 3, 1 4)))" ) );
QTest::newRow( "single_sided_buffer not geom" ) << "single_sided_buffer('g', 5)" << true << QVariant();
QTest::newRow( "single_sided_buffer null" ) << "single_sided_buffer(NULL, 5)" << false << QVariant();
QTest::newRow( "single_sided_buffer point" ) << "single_sided_buffer(geom_from_wkt('POINT(1 2)'),5)" << false << QVariant();
Expand Down Expand Up @@ -1261,9 +1265,9 @@ class TestQgsExpression: public QObject

void run_evaluation_test( QgsExpression &exp, bool evalError, QVariant &expected )
{
QCOMPARE( exp.hasParserError(), false );
if ( exp.hasParserError() )
qDebug() << exp.parserErrorString();
QCOMPARE( exp.hasParserError(), false );

QVariant result = exp.evaluate();
if ( exp.hasEvalError() )
Expand Down

0 comments on commit d5647bc

Please sign in to comment.