Skip to content
Permalink
Browse files

Fix QgsGeometry::insertVertex and deleteVertex compatibility with

<2.10 API and multipoint geometries

Also fix a crash in QgsGeometryCollection::removeGeometry
  • Loading branch information
nyalldawson committed Sep 9, 2015
1 parent fe5085e commit 806da2d38535cfd62e444afe5656182d45008008
@@ -21,6 +21,12 @@ class QgsGeometryCollectionV2: public QgsAbstractGeometryV2

/** Adds a geometry and takes ownership. Returns true in case of success*/
virtual bool addGeometry( QgsAbstractGeometryV2* g /Transfer/ );

/** Inserts a geometry before a specified index and takes ownership. Returns true in case of success.
* @param index position to insert geometry before
*/
virtual bool insertGeometry( QgsAbstractGeometryV2* g /Transfer/, int index );

virtual bool removeGeometry( int nr );

virtual void transform( const QgsCoordinateTransform& ct, QgsCoordinateTransform::TransformDirection d = QgsCoordinateTransform::ForwardTransform );
@@ -438,6 +438,15 @@ bool QgsGeometry::deleteVertex( int atVertex )
return false;
}

//maintain compatibility with < 2.10 API
if ( d->geometry->geometryType() == "MultiPoint" )
{
detach( true );
removeWkbGeos();
//delete geometry instead of point
return static_cast< QgsGeometryCollectionV2* >( d->geometry )->removeGeometry( atVertex );
}

//if it is a point, set the geometry to NULL
if ( QgsWKBTypes::flatType( d->geometry->wkbType() ) == QgsWKBTypes::Point )
{
@@ -467,15 +476,25 @@ bool QgsGeometry::insertVertex( double x, double y, int beforeVertex )
return false;
}

detach( true );
//maintain compatibility with < 2.10 API
if ( d->geometry->geometryType() == "MultiPoint" )
{
detach( true );
removeWkbGeos();
//insert geometry instead of point
return static_cast< QgsGeometryCollectionV2* >( d->geometry )->insertGeometry( new QgsPointV2( x, y ), beforeVertex );
}

QgsVertexId id;
if ( !vertexIdFromVertexNr( beforeVertex, id ) )
{
return false;
}

detach( true );

removeWkbGeos();

return d->geometry->insertVertex( id, QgsPointV2( x, y ) );
}

@@ -105,9 +105,20 @@ bool QgsGeometryCollectionV2::addGeometry( QgsAbstractGeometryV2* g )
return true;
}

bool QgsGeometryCollectionV2::insertGeometry( QgsAbstractGeometryV2 *g, int index )
{
if ( !g )
{
return false;
}

mGeometries.insert( index, g );
return true;
}

bool QgsGeometryCollectionV2::removeGeometry( int nr )
{
if ( nr >= mGeometries.size() )
if ( nr >= mGeometries.size() || nr < 0 )
{
return false;
}
@@ -57,6 +57,11 @@ class CORE_EXPORT QgsGeometryCollectionV2: public QgsAbstractGeometryV2
/** Adds a geometry and takes ownership. Returns true in case of success.*/
virtual bool addGeometry( QgsAbstractGeometryV2* g );

/** Inserts a geometry before a specified index and takes ownership. Returns true in case of success.
* @param index position to insert geometry before
*/
virtual bool insertGeometry( QgsAbstractGeometryV2* g, int index );

/** Removes a geometry from the collection.
* @param nr index of geometry to remove
* @returns true if removal was successful.
@@ -751,30 +751,30 @@ def testMultipoint(self):
assert multipoint.vertexAt(0) == QgsPoint(5, 5), "MULTIPOINT fromWkt failed"

assert multipoint.insertVertex(4, 4, 0), "MULTIPOINT insert 4,4 at 0 failed"
expwkt = "MultiPoint ((4 4, 5 5))"
expwkt = "MultiPoint ((4 4),(5 5))"
wkt = multipoint.exportToWkt()
assert compareWkt(expwkt, wkt), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt)

assert multipoint.insertVertex(7, 7, 2), "MULTIPOINT append 7,7 at 2 failed"
expwkt = "MultiPoint ((4 4, 5 5, 7 7))"
expwkt = "MultiPoint ((4 4),(5 5),(7 7))"
wkt = multipoint.exportToWkt()
assert compareWkt(expwkt, wkt), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt)

assert multipoint.insertVertex(6, 6, 2), "MULTIPOINT append 6,6 at 2 failed"
expwkt = "MultiPoint ((4 4, 5 5, 6 6, 7 7))"
expwkt = "MultiPoint ((4 4),(5 5),(6 6),(7 7))"
wkt = multipoint.exportToWkt()
assert compareWkt(expwkt, wkt), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt)

assert not multipoint.deleteVertex(4), "MULTIPOINT delete at 4 unexpectedly succeeded"
assert not multipoint.deleteVertex(-1), "MULTIPOINT delete at -1 unexpectedly succeeded"

assert multipoint.deleteVertex(1), "MULTIPOINT delete at 1 failed"
expwkt = "MultiPoint ((4 4, 6 6, 7 7))"
expwkt = "MultiPoint ((4 4),(6 6),(7 7))"
wkt = multipoint.exportToWkt()
assert compareWkt(expwkt, wkt), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt)

assert multipoint.deleteVertex(2), "MULTIPOINT delete at 2 failed"
expwkt = "MultiPoint ((4 4, 6 6))"
expwkt = "MultiPoint ((4 4),(6 6))"
wkt = multipoint.exportToWkt()
assert compareWkt(expwkt, wkt), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt)

0 comments on commit 806da2d

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