diff --git a/python/core/geometry/qgsgeometrycollectionv2.sip b/python/core/geometry/qgsgeometrycollectionv2.sip index 40e7c2b4e7a..7265f969a2e 100644 --- a/python/core/geometry/qgsgeometrycollectionv2.sip +++ b/python/core/geometry/qgsgeometrycollectionv2.sip @@ -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 ); diff --git a/src/core/geometry/qgsgeometry.cpp b/src/core/geometry/qgsgeometry.cpp index 2f047bda43a..fe9ba06cf97 100644 --- a/src/core/geometry/qgsgeometry.cpp +++ b/src/core/geometry/qgsgeometry.cpp @@ -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 ) ); } diff --git a/src/core/geometry/qgsgeometrycollectionv2.cpp b/src/core/geometry/qgsgeometrycollectionv2.cpp index 7b0bce1a5ec..a84ebb34aa8 100644 --- a/src/core/geometry/qgsgeometrycollectionv2.cpp +++ b/src/core/geometry/qgsgeometrycollectionv2.cpp @@ -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; } diff --git a/src/core/geometry/qgsgeometrycollectionv2.h b/src/core/geometry/qgsgeometrycollectionv2.h index 59ed1be217f..7a0ee55ecd6 100644 --- a/src/core/geometry/qgsgeometrycollectionv2.h +++ b/src/core/geometry/qgsgeometrycollectionv2.h @@ -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. diff --git a/tests/src/python/test_qgsgeometry.py b/tests/src/python/test_qgsgeometry.py index 3fd6bdbe253..0f5a07df46a 100644 --- a/tests/src/python/test_qgsgeometry.py +++ b/tests/src/python/test_qgsgeometry.py @@ -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)