Skip to content

Commit

Permalink
length3D function: handle multipart geometries
Browse files Browse the repository at this point in the history
  • Loading branch information
agiudiceandrea authored and nyalldawson committed Feb 24, 2021
1 parent d7167e4 commit 3ebf915
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -1364,7 +1364,23 @@ static QVariant fcnLength3D( const QVariantList &values, const QgsExpressionCont
if ( geom.type() != QgsWkbTypes::LineGeometry )
return QVariant();

return QVariant( qgsgeometry_cast< const QgsLineString * >( geom.constGet() )->length3D() );
if ( !geom.isMultipart() )
return QVariant( qgsgeometry_cast< const QgsLineString * >( geom.constGet() )->length3D() );

if ( const QgsGeometryCollection *collection = qgsgeometry_cast< const QgsGeometryCollection * >( geom.constGet() ) )
{
if ( collection->numGeometries() > 0 )
{
double sumLength3DParts = 0;
for ( int i = 0; i < collection->numGeometries(); ++i )
{
sumLength3DParts += qgsgeometry_cast< const QgsLineString * >( collection->geometryN( i ) )->length3D();
}
return QVariant( sumLength3DParts );
}
}

return QVariant();
}

static QVariant fcnReplace( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
Expand Down

0 comments on commit 3ebf915

Please sign in to comment.