Skip to content

Commit

Permalink
Fix QgsGeometry::insertVertex and deleteVertex compatibility with
Browse files Browse the repository at this point in the history
<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 806da2d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
6 changes: 6 additions & 0 deletions python/core/geometry/qgsgeometrycollectionv2.sip
Expand Up @@ -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 );
Expand Down
21 changes: 20 additions & 1 deletion src/core/geometry/qgsgeometry.cpp
Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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 ) );
}

Expand Down
13 changes: 12 additions & 1 deletion src/core/geometry/qgsgeometrycollectionv2.cpp
Expand Up @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions src/core/geometry/qgsgeometrycollectionv2.h
Expand Up @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions tests/src/python/test_qgsgeometry.py
Expand Up @@ -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)

Expand Down

0 comments on commit 806da2d

Please sign in to comment.