Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add reversed method to QgsCurveV2
Also clean up and add missing docs for QgsLineStringV2
  • Loading branch information
nyalldawson committed Nov 18, 2015
1 parent 8783fe2 commit 86c1ffa
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 44 deletions.
2 changes: 2 additions & 0 deletions python/core/geometry/qgscircularstringv2.sip
Expand Up @@ -60,6 +60,8 @@ class QgsCircularStringV2: public QgsCurveV2
@return rotation in radians, clockwise from north*/
double vertexAngle( const QgsVertexId& vertex ) const;

virtual QgsCircularStringV2* reversed() const /Factory/;

virtual bool addZValue( double zValue = 0 );
virtual bool addMValue( double mValue = 0 );

Expand Down
2 changes: 2 additions & 0 deletions python/core/geometry/qgscompoundcurvev2.sip
Expand Up @@ -66,6 +66,8 @@ class QgsCompoundCurveV2: public QgsCurveV2
@return rotation in radians, clockwise from north*/
double vertexAngle( const QgsVertexId& vertex ) const;

virtual QgsCompoundCurveV2* reversed() const /Factory/;

virtual bool addZValue( double zValue = 0 );
virtual bool addMValue( double mValue = 0 );
};
5 changes: 5 additions & 0 deletions python/core/geometry/qgscurvev2.sip
Expand Up @@ -23,6 +23,11 @@ class QgsCurveV2: public QgsAbstractGeometryV2
virtual bool nextVertex( QgsVertexId& id, QgsPointV2& vertex ) const;
virtual bool pointAt( int i, QgsPointV2& vertex, QgsVertexId::VertexType& type ) const = 0;

/** Returns a reversed copy of the curve, where the direction of the curve has been flipped.
* @note added in QGIS 2.14
*/
virtual QgsCurveV2* reversed() const = 0 /Factory/;

/** Returns a geometry without curves. Caller takes ownership*/
QgsAbstractGeometryV2* segmentize() const /Factory/;

Expand Down
63 changes: 48 additions & 15 deletions python/core/geometry/qgslinestringv2.sip
@@ -1,3 +1,9 @@
/** \ingroup core
* \class QgsLineStringV2
* \brief Line string geometry type, with support for z-dimension and m-values.
* \note added in QGIS 2.10
*/

class QgsLineStringV2: public QgsCurveV2
{
%TypeHeaderCode
Expand All @@ -8,13 +14,49 @@ class QgsLineStringV2: public QgsCurveV2
QgsLineStringV2();
~QgsLineStringV2();

/** Resets the line string to match the line string in a WKB geometry.
* @param type WKB type
* @param wkb WKB representation of line geometry
* @note not available in Python bindings
*/
//void fromWkbPoints( QgsWKBTypes::Type type, const QgsConstWkbPtr& wkb );

/** Returns the specified point from inside the line string.
* @param i index of point, starting at 0 for the first point
*/
QgsPointV2 pointN( int i ) const;

/** Resets the line string to match the specified list of points.
* @param points new points for line string. If empty, line string will be cleared.
*/
void setPoints( const QList<QgsPointV2>& points );

/** Appends the contents of another line string to the end of this line string.
* @param line line to append. Ownership is not transferred.
*/
void append( const QgsLineStringV2* line );

/** Adds a new vertex to the end of the line string.
* @param pt vertex to add
*/
void addVertex( const QgsPointV2& pt );

/** Closes the line string by appending the first point to the end of the line, if it is not already closed.*/
void close();

virtual QgsLineStringV2* reversed() const /Factory/;

/** Returns a QPolygonF representing the line string.
*/
QPolygonF asQPolygonF() const;

//reimplemented methods

virtual QString geometryType() const;
virtual int dimension() const;
virtual QgsLineStringV2* clone() const;
virtual void clear();
virtual QgsLineStringV2* clone() const /Factory/;

virtual bool fromWkb( const unsigned char* wkb );
//void fromWkbPoints( QgsWKBTypes::Type type, const QgsConstWkbPtr& wkb );
virtual bool fromWkt( const QString& wkt );

int wkbSize() const;
Expand All @@ -28,39 +70,30 @@ class QgsLineStringV2: public QgsCurveV2
virtual double length() const;
virtual QgsPointV2 startPoint() const;
virtual QgsPointV2 endPoint() const;
virtual QgsLineStringV2* curveToLine() const;
virtual QgsLineStringV2* curveToLine() const /Factory/;

int numPoints() const;
QgsPointV2 pointN( int i ) const;
void points( QList<QgsPointV2>& pt ) const;

void setPoints( const QList<QgsPointV2>& points );
void append( const QgsLineStringV2* line );

void draw( QPainter& p ) const;

void transform( const QgsCoordinateTransform& ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform );
void transform( const QTransform& t );

void addToPainterPath( QPainterPath& path ) const;
void drawAsPolygon( QPainter& p ) const;

const QPolygonF& qPolygonF() const;

virtual bool insertVertex( const QgsVertexId& position, const QgsPointV2& vertex );
virtual bool moveVertex( const QgsVertexId& position, const QgsPointV2& newPos );
virtual bool deleteVertex( const QgsVertexId& position );
void addVertex( const QgsPointV2& pt );

double closestSegment( const QgsPointV2& pt, QgsPointV2& segmentPt, QgsVertexId& vertexAfter, bool* leftOf, double epsilon ) const;
bool pointAt( int i, QgsPointV2& vertex, QgsVertexId::VertexType& type ) const;

void sumUpArea( double& sum ) const;

/** Returns approximate rotation angle for a vertex. Usually average angle between adjacent segments.
@param vertex the vertex id
@return rotation in radians, clockwise from north*/
double vertexAngle( const QgsVertexId& vertex ) const;

virtual bool addZValue( double zValue = 0 );
virtual bool addMValue( double mValue = 0 );

};
5 changes: 5 additions & 0 deletions python/core/geometry/qgsmulticurvev2.sip
Expand Up @@ -22,4 +22,9 @@ class QgsMultiCurveV2: public QgsGeometryCollectionV2
virtual bool addGeometry( QgsAbstractGeometryV2* g );
/** Returns a geometry without curves. Caller takes ownership*/
QgsAbstractGeometryV2* segmentize() const /Factory/;

/** Returns a copy of the multi curve, where each component curve has had its line direction reversed.
* @note added in QGIS 2.14
*/
QgsMultiCurveV2* reversed() const /Factory/;
};
2 changes: 1 addition & 1 deletion python/core/geometry/qgspointv2.sip
Expand Up @@ -117,7 +117,7 @@ class QgsPointV2: public QgsAbstractGeometryV2
//implementation of inherited methods
virtual QString geometryType() const;
virtual int dimension() const;
virtual QgsPointV2* clone() const;
virtual QgsPointV2* clone() const /Factory/;
void clear();
virtual bool fromWkb( const unsigned char* wkb );
virtual bool fromWkt( const QString& wkt );
Expand Down
16 changes: 16 additions & 0 deletions src/core/geometry/qgscircularstringv2.cpp
Expand Up @@ -1009,6 +1009,22 @@ double QgsCircularStringV2::vertexAngle( const QgsVertexId& vId ) const
return 0.0;
}

QgsCircularStringV2* QgsCircularStringV2::reversed() const
{
QgsCircularStringV2* copy = clone();
std::reverse( copy->mX.begin(), copy->mX.end() );
std::reverse( copy->mY.begin(), copy->mY.end() );
if ( is3D() )
{
std::reverse( copy->mZ.begin(), copy->mZ.end() );
}
if ( isMeasure() )
{
std::reverse( copy->mM.begin(), copy->mM.end() );
}
return copy;
}

bool QgsCircularStringV2::addZValue( double zValue )
{
if ( QgsWKBTypes::hasZ( mWkbType ) )
Expand Down
2 changes: 2 additions & 0 deletions src/core/geometry/qgscircularstringv2.h
Expand Up @@ -125,6 +125,8 @@ class CORE_EXPORT QgsCircularStringV2: public QgsCurveV2
@return rotation in radians, clockwise from north*/
double vertexAngle( const QgsVertexId& vertex ) const override;

virtual QgsCircularStringV2* reversed() const override;

virtual bool addZValue( double zValue = 0 ) override;
virtual bool addMValue( double mValue = 0 ) override;

Expand Down
11 changes: 11 additions & 0 deletions src/core/geometry/qgscompoundcurvev2.cpp
Expand Up @@ -625,6 +625,17 @@ double QgsCompoundCurveV2::vertexAngle( const QgsVertexId& vertex ) const
}
}

QgsCompoundCurveV2* QgsCompoundCurveV2::reversed() const
{
QgsCompoundCurveV2* clone = new QgsCompoundCurveV2();
Q_FOREACH ( QgsCurveV2* curve, mCurves )
{
QgsCurveV2* reversedCurve = curve->reversed();
clone->addCurve( reversedCurve );
}
return clone;
}

bool QgsCompoundCurveV2::addZValue( double zValue )
{
if ( QgsWKBTypes::hasZ( mWkbType ) )
Expand Down
2 changes: 2 additions & 0 deletions src/core/geometry/qgscompoundcurvev2.h
Expand Up @@ -109,6 +109,8 @@ class CORE_EXPORT QgsCompoundCurveV2: public QgsCurveV2
@return rotation in radians, clockwise from north*/
double vertexAngle( const QgsVertexId& vertex ) const override;

virtual QgsCompoundCurveV2* reversed() const override;

virtual bool addZValue( double zValue = 0 ) override;
virtual bool addMValue( double mValue = 0 ) override;

Expand Down
5 changes: 5 additions & 0 deletions src/core/geometry/qgscurvev2.h
Expand Up @@ -87,6 +87,11 @@ class CORE_EXPORT QgsCurveV2: public QgsAbstractGeometryV2
*/
virtual bool pointAt( int i, QgsPointV2& vertex, QgsVertexId::VertexType& type ) const = 0;

/** Returns a reversed copy of the curve, where the direction of the curve has been flipped.
* @note added in QGIS 2.14
*/
virtual QgsCurveV2* reversed() const = 0;

QgsAbstractGeometryV2* segmentize() const override;

virtual int vertexCount( int /*part*/ = 0, int /*ring*/ = 0 ) const override { return numPoints(); }
Expand Down
31 changes: 26 additions & 5 deletions src/core/geometry/qgslinestringv2.cpp
Expand Up @@ -191,7 +191,7 @@ int QgsLineStringV2::numPoints() const

QgsPointV2 QgsLineStringV2::pointN( int i ) const
{
if ( mX.size() <= i )
if ( i < 0 || i >= mX.size() )
{
return QgsPointV2();
}
Expand Down Expand Up @@ -240,7 +240,9 @@ void QgsLineStringV2::points( QList<QgsPointV2>& pts ) const

void QgsLineStringV2::setPoints( const QList<QgsPointV2>& points )
{
if ( points.size() < 1 )
mBoundingBox = QgsRectangle(); //set bounding box invalid

if ( points.isEmpty() )
{
mWkbType = QgsWKBTypes::Unknown;
mX.clear();
Expand Down Expand Up @@ -307,11 +309,29 @@ void QgsLineStringV2::append( const QgsLineStringV2* line )
mY += line->mY;
mZ += line->mZ;
mM += line->mM;

mBoundingBox = QgsRectangle(); //set bounding box invalid
}

QgsLineStringV2* QgsLineStringV2::reversed() const
{
QgsLineStringV2* copy = clone();
std::reverse( copy->mX.begin(), copy->mX.end() );
std::reverse( copy->mY.begin(), copy->mY.end() );
if ( copy->is3D() )
{
std::reverse( copy->mZ.begin(), copy->mZ.end() );
}
if ( copy->isMeasure() )
{
std::reverse( copy->mM.begin(), copy->mM.end() );
}
return copy;
}

void QgsLineStringV2::draw( QPainter& p ) const
{
p.drawPolyline( qPolygonF() );
p.drawPolyline( asQPolygonF() );
}

void QgsLineStringV2::addToPainterPath( QPainterPath& path ) const
Expand All @@ -335,10 +355,10 @@ void QgsLineStringV2::addToPainterPath( QPainterPath& path ) const

void QgsLineStringV2::drawAsPolygon( QPainter& p ) const
{
p.drawPolygon( qPolygonF() );
p.drawPolygon( asQPolygonF() );
}

QPolygonF QgsLineStringV2::qPolygonF() const
QPolygonF QgsLineStringV2::asQPolygonF() const
{
QPolygonF points;
for ( int i = 0; i < mX.count(); ++i )
Expand Down Expand Up @@ -534,6 +554,7 @@ void QgsLineStringV2::importVerticesFromWkb( const QgsConstWkbPtr& wkb )
wkb >> mM[i];
}
}
mBoundingBox = QgsRectangle(); //set bounding box invalid
}

void QgsLineStringV2::close()
Expand Down

0 comments on commit 86c1ffa

Please sign in to comment.