Fix QgsGeometry::insertVertex and deleteVertex compatibility with

<2.10 API and multipoint geometries

Also fix a crash in QgsGeometryCollection::removeGeometry
This commit is contained in:
Nyall Dawson 2015-09-09 16:11:09 +10:00
parent fe5085e8e1
commit 806da2d385
5 changed files with 48 additions and 7 deletions

View File

@ -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 );

View File

@ -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,7 +476,14 @@ 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 ) )
@ -475,7 +491,10 @@ bool QgsGeometry::insertVertex( double x, double y, int beforeVertex )
return false;
}
detach( true );
removeWkbGeos();
return d->geometry->insertVertex( id, QgsPointV2( x, y ) );
}

View File

@ -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;
}

View File

@ -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.

View File

@ -751,17 +751,17 @@ class TestQgsGeometry(TestCase):
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)
@ -769,12 +769,12 @@ class TestQgsGeometry(TestCase):
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)