Skip to content

Commit

Permalink
Fix filterVertices for multipoint geometries
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 29, 2018
1 parent f7618d0 commit 53d4d0b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/core/geometry/qgsmultipoint.cpp
Expand Up @@ -182,6 +182,20 @@ double QgsMultiPoint::segmentLength( QgsVertexId ) const
return 0.0;
}

void QgsMultiPoint::filterVertices( const std::function<bool ( const QgsPoint & )> &filter )
{
mGeometries.erase( std::remove_if( mGeometries.begin(), mGeometries.end(), // clazy:exclude=detaching-member
[&filter]( const QgsAbstractGeometry * part )
{
if ( const QgsPoint *point = qgsgeometry_cast< const QgsPoint * >( part ) )
{
return !filter( *point );
}
else
return true;
} ), mGeometries.end() ); // clazy:exclude=detaching-member
}

bool QgsMultiPoint::wktOmitChildType() const
{
return true;
Expand Down
1 change: 1 addition & 0 deletions src/core/geometry/qgsmultipoint.h
Expand Up @@ -47,6 +47,7 @@ class CORE_EXPORT QgsMultiPoint: public QgsGeometryCollection
double segmentLength( QgsVertexId startVertex ) const override;

#ifndef SIP_RUN
void filterVertices( const std::function< bool( const QgsPoint & ) > &filter ) override;

/**
* Cast the \a geom to a QgsLineString.
Expand Down
16 changes: 15 additions & 1 deletion tests/src/core/testqgsgeometry.cpp
Expand Up @@ -4538,7 +4538,7 @@ void TestQgsGeometry::lineString()
filterLine.filterVertices( filter ); // no crash
filterLine.setPoints( QgsPointSequence() << QgsPoint( 11, 2, 3, 4, QgsWkbTypes::PointZM ) << QgsPoint( 1, 2, 3, 4, QgsWkbTypes::PointZM ) << QgsPoint( 4, 12, 13, 14, QgsWkbTypes::PointZM ) << QgsPoint( 111, 12, 23, 24, QgsWkbTypes::PointZM ) );
filterLine.filterVertices( filter );
QCOMPARE( swapLine.asWkt( 2 ), QStringLiteral( "LineStringZM (2 11 3 4, 12 11 13 14, 12 111 23 24)" ) );
QCOMPARE( filterLine.asWkt( 2 ), QStringLiteral( "LineStringZM (1 2 3 4, 4 12 13 14)" ) );
}

void TestQgsGeometry::polygon()
Expand Down Expand Up @@ -11583,6 +11583,20 @@ void TestQgsGeometry::multiPoint()
mp.addGeometry( new QgsPoint( QgsWkbTypes::PointZM, 10, 1, 4, 8 ) );
QVERIFY( !mp.removeDuplicateNodes() );
QCOMPARE( mp.numGeometries(), 2 );

// filter vertex
QgsMultiPoint filterPoint;
auto filter = []( const QgsPoint & point )-> bool
{
return point.x() < 5;
};
filterPoint.filterVertices( filter ); // no crash
filterPoint.addGeometry( new QgsPoint( QgsWkbTypes::PointZM, 10, 0, 4, 8 ) );
filterPoint.addGeometry( new QgsPoint( QgsWkbTypes::PointZM, 3, 0, 4, 8 ) );
filterPoint.addGeometry( new QgsPoint( QgsWkbTypes::PointZM, 1, 0, 4, 8 ) );
filterPoint.addGeometry( new QgsPoint( QgsWkbTypes::PointZM, 11, 0, 4, 8 ) );
filterPoint.filterVertices( filter );
QCOMPARE( filterPoint.asWkt( 2 ), QStringLiteral( "MultiPointZM ((3 0 4 8),(1 0 4 8))" ) );
}

void TestQgsGeometry::multiLineString()
Expand Down

0 comments on commit 53d4d0b

Please sign in to comment.