Skip to content
Permalink
Browse files

QgsPolygonV2 tests and fixes

- fixes for handling Polygon25D
- add QgsAbstractGeometryV2::convertTo( QgsWKBTypes::Type type ) for
easy conversion between geometry types
- fix crash when calculating perimeter with no exterior ring
- ensure that added rings respect dimensionality of polygon (avoids
issues such as polygons with z having a ring without z, or a
Polygon25D with LineStringZ rings)
- if a curved ring is added to a polygon then a segmentized version
of the ring is used (can't have a Polygon with a CircularString
ring)
- when calling setInteriorRings, make sure empty rings are skipped
and that all rings are converted to correct type for polygon
- don't crash when requesting or removing interior ring with
index < 0
  • Loading branch information
nyalldawson committed Dec 11, 2015
1 parent d2bf8d8 commit 233f67b4195c802a7b2b315d36b0467a4ade7a10
@@ -201,6 +201,12 @@ class QgsAbstractGeometryV2
*/
virtual bool dropMValue() = 0;

/** Converts the geometry to a specified type.
* @returns true if conversion was successful
* @note added in QGIS 2.14
*/
virtual bool convertTo( QgsWKBTypes::Type type );

protected:

/** Updates the geometry type based on whether sub geometries contain z or m values.
@@ -43,7 +43,7 @@ class QgsCurvePolygonV2: public QgsSurfaceV2
void setExteriorRing( QgsCurveV2* ring /Transfer/ );
/** Sets interior rings (takes ownership)*/
void setInteriorRings( const QList<QgsCurveV2*>& rings );
void addInteriorRing( QgsCurveV2* ring /Transfer/ );
virtual void addInteriorRing( QgsCurveV2* ring /Transfer/ );
/** Removes ring. Exterior ring is 0, first interior ring 1, ...*/
bool removeInteriorRing( int nr );

@@ -159,4 +159,6 @@ class QgsLineStringV2: public QgsCurveV2
virtual bool dropZValue();
virtual bool dropMValue();

virtual bool convertTo( QgsWKBTypes::Type type );

};
@@ -119,12 +119,16 @@ class QgsPointV2: public QgsAbstractGeometryV2
void setY( double y );

/** Sets the point's z-coordinate.
* @note calling this will have no effect if the point does not contain a z-dimension. Use addZValue() to
* add a z value and force the point to have a z dimension.
* @see z()
* @see rz()
*/
void setZ( double z );

/** Sets the point's m-value.
* @note calling this will have no effect if the point does not contain a m-dimension. Use addMValue() to
* add an m value and force the point to have an m dimension.
* @see m()
* @see rm()
*/
@@ -171,5 +175,6 @@ class QgsPointV2: public QgsAbstractGeometryV2
virtual bool addMValue( double mValue = 0 );
virtual bool dropZValue();
virtual bool dropMValue();
virtual bool convertTo( QgsWKBTypes::Type type );

};
@@ -5,6 +5,8 @@ class QgsPolygonV2: public QgsCurvePolygonV2
%End

public:
QgsPolygonV2();

virtual QString geometryType() const;
virtual QgsPolygonV2* clone() const;

@@ -19,4 +21,6 @@ class QgsPolygonV2: public QgsCurvePolygonV2
// inherited: QString asJSON( int precision = 17 ) const;

QgsPolygonV2* surfaceToPolygon() const;

void addInteriorRing( QgsCurveV2* ring /Transfer/ );
};
@@ -88,6 +88,12 @@ void QgsAbstractGeometryV2::setZMTypeFromSubGeometry( const QgsAbstractGeometryV
mWkbType = QgsWKBTypes::LineString25D;
return;
}
else if ( baseGeomType == QgsWKBTypes::Polygon &&
( subgeom->wkbType() == QgsWKBTypes::Point25D || subgeom->wkbType() == QgsWKBTypes::LineString25D ) )
{
mWkbType = QgsWKBTypes::Polygon25D;
return;
}

bool hasZ = subgeom->is3D();
bool hasM = subgeom->isMeasure();
@@ -237,6 +243,37 @@ QgsPointV2 QgsAbstractGeometryV2::centroid() const
}
}

bool QgsAbstractGeometryV2::convertTo( QgsWKBTypes::Type type )
{
if ( type == mWkbType )
return true;

if ( QgsWKBTypes::flatType( type ) != QgsWKBTypes::flatType( mWkbType ) )
return false;

bool needZ = QgsWKBTypes::hasZ( type );
bool needM = QgsWKBTypes::hasM( type );
if ( !needZ )
{
dropZValue();
}
else if ( !is3D() )
{
addZValue();
}

if ( !needM )
{
dropMValue();
}
else if ( !isMeasure() )
{
addMValue();
}

return true;
}

bool QgsAbstractGeometryV2::isEmpty() const
{
QgsVertexId vId; QgsPointV2 vertex;
@@ -340,6 +340,12 @@ class CORE_EXPORT QgsAbstractGeometryV2
*/
virtual bool dropMValue() = 0;

/** Converts the geometry to a specified type.
* @returns true if conversion was successful
* @note added in QGIS 2.14
*/
virtual bool convertTo( QgsWKBTypes::Type type );

protected:
QgsWKBTypes::Type mWkbType;
mutable QgsRectangle mBoundingBox;
@@ -378,6 +378,9 @@ double QgsCurvePolygonV2::area() const

double QgsCurvePolygonV2::perimeter() const
{
if ( !mExteriorRing )
return 0.0;

//sum perimeter of rings
double perimeter = mExteriorRing->length();
QList<QgsCurveV2*>::const_iterator ringIt = mInteriorRings.constBegin();
@@ -440,7 +443,7 @@ QgsCurveV2* QgsCurvePolygonV2::exteriorRing() const

QgsCurveV2* QgsCurvePolygonV2::interiorRing( int i ) const
{
if ( i >= mInteriorRings.size() )
if ( i < 0 || i >= mInteriorRings.size() )
{
return 0;
}
@@ -470,17 +473,37 @@ void QgsCurvePolygonV2::setExteriorRing( QgsCurveV2* ring )
void QgsCurvePolygonV2::setInteriorRings( const QList<QgsCurveV2*>& rings )
{
qDeleteAll( mInteriorRings );
mInteriorRings = rings;
mInteriorRings.clear();

//add rings one-by-one, so that they can each be converted to the correct type for the CurvePolygon
Q_FOREACH ( QgsCurveV2* ring, rings )
{
addInteriorRing( ring );
}
}

void QgsCurvePolygonV2::addInteriorRing( QgsCurveV2* ring )
{
if ( !ring )
return;

//ensure dimensionality of ring matches curve polygon
if ( !is3D() )
ring->dropZValue();
else if ( !ring->is3D() )
ring->addZValue();

if ( !isMeasure() )
ring->dropMValue();
else if ( !ring->isMeasure() )
ring->addMValue();

mInteriorRings.append( ring );
}

bool QgsCurvePolygonV2::removeInteriorRing( int nr )
{
if ( nr >= mInteriorRings.size() )
if ( nr < 0 || nr >= mInteriorRings.size() )
{
return false;
}
@@ -70,7 +70,7 @@ class CORE_EXPORT QgsCurvePolygonV2: public QgsSurfaceV2
/** Sets all interior rings (takes ownership)*/
void setInteriorRings( const QList<QgsCurveV2*>& rings );
/** Adds an interior ring to the geometry (takes ownership)*/
void addInteriorRing( QgsCurveV2* ring );
virtual void addInteriorRing( QgsCurveV2* ring );
/** Removes ring. Exterior ring is 0, first interior ring 1, ...*/
bool removeInteriorRing( int nr );

@@ -944,3 +944,22 @@ bool QgsLineStringV2::dropMValue()
mM.clear();
return true;
}

bool QgsLineStringV2::convertTo( QgsWKBTypes::Type type )
{
if ( type == mWkbType )
return true;

if ( type == QgsWKBTypes::LineString25D )
{
//special handling required for conversion to LineString25D
dropMValue();
addZValue();
mWkbType = QgsWKBTypes::LineString25D;
return true;
}
else
{
return QgsCurveV2::convertTo( type );
}
}
@@ -186,6 +186,8 @@ class CORE_EXPORT QgsLineStringV2: public QgsCurveV2
virtual bool dropZValue() override;
virtual bool dropMValue() override;

bool convertTo( QgsWKBTypes::Type type ) override;

private:
QVector<double> mX;
QVector<double> mY;
@@ -380,3 +380,33 @@ bool QgsPointV2::dropMValue()
mM = 0.0;
return true;
}

bool QgsPointV2::convertTo( QgsWKBTypes::Type type )
{
if ( type == mWkbType )
return true;

switch ( type )
{
case QgsWKBTypes::Point:
mZ = 0.0;
mM = 0.0;
mWkbType = type;
return true;
case QgsWKBTypes::PointZ:
case QgsWKBTypes::Point25D:
mM = 0.0;
mWkbType = type;
return true;
case QgsWKBTypes::PointM:
mZ = 0.0;
mWkbType = type;
return true;
case QgsWKBTypes::PointZM:
mWkbType = type;
return true;
default:
return false;
}
return false;
}
@@ -130,12 +130,16 @@ class CORE_EXPORT QgsPointV2: public QgsAbstractGeometryV2
void setY( double y ) { mY = y; mBoundingBox = QgsRectangle(); }

/** Sets the point's z-coordinate.
* @note calling this will have no effect if the point does not contain a z-dimension. Use addZValue() to
* add a z value and force the point to have a z dimension.
* @see z()
* @see rz()
*/
void setZ( double z ) { mZ = z; }

/** Sets the point's m-value.
* @note calling this will have no effect if the point does not contain a m-dimension. Use addMValue() to
* add a m value and force the point to have an m dimension.
* @see m()
* @see rm()
*/
@@ -182,6 +186,7 @@ class CORE_EXPORT QgsPointV2: public QgsAbstractGeometryV2
virtual bool addMValue( double mValue = 0 ) override;
virtual bool dropZValue() override;
virtual bool dropMValue() override;
bool convertTo( QgsWKBTypes::Type type ) override;

private:
double mX;
@@ -21,6 +21,12 @@
#include "qgslinestringv2.h"
#include "qgswkbptr.h"

QgsPolygonV2::QgsPolygonV2()
: QgsCurvePolygonV2()
{
mWkbType = QgsWKBTypes::Polygon;
}

QgsPolygonV2* QgsPolygonV2::clone() const
{
return new QgsPolygonV2( *this );
@@ -102,9 +108,35 @@ unsigned char* QgsPolygonV2::asWkb( int& binarySize ) const
curve->points( pts );
QgsGeometryUtils::pointsToWKB( wkb, pts, curve->is3D(), curve->isMeasure() );
}

return geomPtr;
}

void QgsPolygonV2::addInteriorRing( QgsCurveV2* ring )
{
if ( !ring )
return;

if ( ring->hasCurvedSegments() )
{
//can't add a curved ring to a QgsPolygonV2
QgsLineStringV2* segmented = ring->curveToLine();
delete ring;
ring = segmented;
}

if ( mWkbType == QgsWKBTypes::Polygon25D )
{
ring->convertTo( QgsWKBTypes::LineString25D );
mInteriorRings.append( ring );
}
else
{
QgsCurvePolygonV2::addInteriorRing( ring );
}
}


QgsPolygonV2* QgsPolygonV2::surfaceToPolygon() const
{
return clone();
@@ -29,6 +29,8 @@
class CORE_EXPORT QgsPolygonV2: public QgsCurvePolygonV2
{
public:
QgsPolygonV2();

virtual QString geometryType() const override { return "Polygon"; }
virtual QgsPolygonV2* clone() const override;

@@ -43,5 +45,7 @@ class CORE_EXPORT QgsPolygonV2: public QgsCurvePolygonV2
// inherited: QString asJSON( int precision = 17 ) const;

QgsPolygonV2* surfaceToPolygon() const override;

void addInteriorRing( QgsCurveV2* ring ) override;
};
#endif // QGSPOLYGONV2_H

0 comments on commit 233f67b

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