Skip to content
Permalink
Browse files

Add method to transform vertices of QgsGeometry/QgsAbstractGeometry

in place using a custom lambda function
  • Loading branch information
nyalldawson committed Jul 29, 2018
1 parent df1832f commit d09faf42be1756f021be81ee575f9dbd9f720561
@@ -1595,6 +1595,7 @@ was performed on the geometry.
%End



static QgsGeometry fromQPointF( QPointF point );
%Docstring
Construct geometry from a QPointF
@@ -240,6 +240,11 @@ void QgsAbstractGeometry::filterVertices( const std::function<bool ( const QgsPo
// Ideally this would be pure virtual, but SIP has issues with that
}

void QgsAbstractGeometry::transformVertices( const std::function<QgsPoint( const QgsPoint & )> & )
{
// Ideally this would be pure virtual, but SIP has issues with that
}

QgsVertexIterator QgsAbstractGeometry::vertices() const
{
return QgsVertexIterator( this );
@@ -586,6 +586,22 @@ class CORE_EXPORT QgsAbstractGeometry
*/
virtual void filterVertices( const std::function< bool( const QgsPoint & ) > &filter );

/**
* Transforms the vertices from the geometry in place, applying the \a transform function
* to every vertex.
*
* Depending on the \a transform used, this may result in an invalid geometry.
*
* Transform functions are not permitted to alter the dimensionality of vertices. If
* a transform which adds (or removes) z/m values is desired, first call the corresponding
* addZValue() or addMValue() function to change the geometry's dimensionality and then
* transform.
*
* \note Not available in Python bindings
* \since QGIS 3.4
*/
virtual void transformVertices( const std::function< QgsPoint( const QgsPoint & ) > &transform );

/**
* \ingroup core
* The vertex_iterator class provides STL-style iterator for vertices.
@@ -592,6 +592,34 @@ void QgsCircularString::filterVertices( const std::function<bool ( const QgsPoin
clearCache();
}

void QgsCircularString::transformVertices( const std::function<QgsPoint( const QgsPoint & )> &transform )
{
bool hasZ = is3D();
bool hasM = isMeasure();
int size = mX.size();

double *srcX = mX.data();
double *srcY = mY.data();
double *srcM = hasM ? mM.data() : nullptr;
double *srcZ = hasZ ? mZ.data() : nullptr;

for ( int i = 0; i < size; ++i )
{
double x = *srcX;
double y = *srcY;
double z = hasZ ? *srcZ : std::numeric_limits<double>::quiet_NaN();
double m = hasM ? *srcM : std::numeric_limits<double>::quiet_NaN();
QgsPoint res = transform( QgsPoint( x, y, z, m ) );
*srcX++ = res.x();
*srcY++ = res.y();
if ( hasM )
*srcM++ = res.m();
if ( hasZ )
*srcZ++ = res.z();
}
clearCache();
}

void QgsCircularString::points( QgsPointSequence &pts ) const
{
pts.clear();
@@ -128,6 +128,7 @@ class CORE_EXPORT QgsCircularString: public QgsCurve

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

/**
* Cast the \a geom to a QgsCircularString.
@@ -782,6 +782,15 @@ void QgsCompoundCurve::filterVertices( const std::function<bool ( const QgsPoint
clearCache();
}

void QgsCompoundCurve::transformVertices( const std::function<QgsPoint( const QgsPoint & )> &transform )
{
for ( QgsCurve *curve : qgis::as_const( mCurves ) )
{
curve->transformVertices( transform );
}
clearCache();
}

void QgsCompoundCurve::sumUpArea( double &sum ) const
{
for ( const QgsCurve *curve : mCurves )
@@ -128,6 +128,7 @@ class CORE_EXPORT QgsCompoundCurve: public QgsCurve

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

/**
* Cast the \a geom to a QgsCompoundCurve.
@@ -1186,6 +1186,18 @@ void QgsCurvePolygon::filterVertices( const std::function<bool ( const QgsPoint
clearCache();
}

void QgsCurvePolygon::transformVertices( const std::function<QgsPoint( const QgsPoint & )> &transform )
{
if ( mExteriorRing )
mExteriorRing->transformVertices( transform );

for ( QgsCurve *curve : qgis::as_const( mInteriorRings ) )
{
curve->transformVertices( transform );
}
clearCache();
}

int QgsCurvePolygon::childCount() const
{
return 1 + mInteriorRings.count();
@@ -180,6 +180,7 @@ class CORE_EXPORT QgsCurvePolygon: public QgsSurface

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

/**
* Cast the \a geom to a QgsCurvePolygon.
@@ -2599,6 +2599,16 @@ void QgsGeometry::filterVertices( const std::function<bool ( const QgsPoint & )>
d->geometry->filterVertices( filter );
}

void QgsGeometry::transformVertices( const std::function<QgsPoint( const QgsPoint & )> &transform )
{
if ( !d->geometry )
return;

detach();

d->geometry->transformVertices( transform );
}

void QgsGeometry::convertPointList( const QVector<QgsPointXY> &input, QgsPointSequence &output )
{
output.clear();
@@ -1548,6 +1548,22 @@ class CORE_EXPORT QgsGeometry
*/
void filterVertices( const std::function< bool( const QgsPoint & ) > &filter ) SIP_SKIP;

/**
* Transforms the vertices from the geometry in place, applying the \a transform function
* to every vertex.
*
* Depending on the \a transform used, this may result in an invalid geometry.
*
* Transform functions are not permitted to alter the dimensionality of vertices. If
* a transform which adds (or removes) z/m values is desired, first call the corresponding
* addZValue() or addMValue() function to change the geometry's dimensionality and then
* transform.
*
* \note Not available in Python bindings
* \since QGIS 3.4
*/
void transformVertices( const std::function< QgsPoint( const QgsPoint & ) > &transform ) SIP_SKIP;

/**
* Construct geometry from a QPointF
* \param point source QPointF
@@ -830,6 +830,16 @@ void QgsGeometryCollection::filterVertices( const std::function<bool ( const Qgs
clearCache();
}

void QgsGeometryCollection::transformVertices( const std::function<QgsPoint( const QgsPoint & )> &transform )
{
for ( QgsAbstractGeometry *geom : qgis::as_const( mGeometries ) )
{
if ( geom )
geom->transformVertices( transform );
}
clearCache();
}

void QgsGeometryCollection::swapXy()
{
for ( QgsAbstractGeometry *geom : qgis::as_const( mGeometries ) )
@@ -150,6 +150,7 @@ class CORE_EXPORT QgsGeometryCollection: public QgsAbstractGeometry

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

/**
* Cast the \a geom to a QgsGeometryCollection.
@@ -1346,3 +1346,31 @@ void QgsLineString::filterVertices( const std::function<bool ( const QgsPoint &

clearCache();
}

void QgsLineString::transformVertices( const std::function<QgsPoint( const QgsPoint & )> &transform )
{
bool hasZ = is3D();
bool hasM = isMeasure();
int size = mX.size();

double *srcX = mX.data();
double *srcY = mY.data();
double *srcM = hasM ? mM.data() : nullptr;
double *srcZ = hasZ ? mZ.data() : nullptr;

for ( int i = 0; i < size; ++i )
{
double x = *srcX;
double y = *srcY;
double z = hasZ ? *srcZ : std::numeric_limits<double>::quiet_NaN();
double m = hasM ? *srcM : std::numeric_limits<double>::quiet_NaN();
QgsPoint res = transform( QgsPoint( x, y, z, m ) );
*srcX++ = res.x();
*srcY++ = res.y();
if ( hasM )
*srcM++ = res.m();
if ( hasZ )
*srcZ++ = res.z();
}
clearCache();
}
@@ -328,6 +328,7 @@ class CORE_EXPORT QgsLineString: public QgsCurve

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

/**
* Cast the \a geom to a QgsLineString.
@@ -573,6 +573,18 @@ void QgsPoint::filterVertices( const std::function<bool ( const QgsPoint & )> &
// no meaning for points
}

void QgsPoint::transformVertices( const std::function<QgsPoint( const QgsPoint & )> &transform )
{
QgsPoint res = transform( *this );
mX = res.x();
mY = res.y();
if ( is3D() )
mZ = res.z();
if ( isMeasure() )
mM = res.m();
clearCache();
}

double QgsPoint::distance3D( double x, double y, double z ) const
{
double zDistSquared = 0.0;
@@ -477,6 +477,7 @@ class CORE_EXPORT QgsPoint: public QgsAbstractGeometry
#ifndef SIP_RUN

void filterVertices( const std::function< bool( const QgsPoint & ) > &filter ) override;
void transformVertices( const std::function< QgsPoint( const QgsPoint & ) > &transform ) override;

/**
* Cast the \a geom to a QgsPoint.

0 comments on commit d09faf4

Please sign in to comment.
You can’t perform that action at this time.