Skip to content
Permalink
Browse files

fix deleteVertex for POLYGON and MULTILINESTRING

  • Loading branch information
jef-n committed Jan 24, 2014
1 parent a30a9dd commit 6f15e59ff9e94e12b752e2af8cc45a3de246ee7e
Showing with 46 additions and 18 deletions.
  1. +3 −14 src/core/qgsgeometry.cpp
  2. +43 −4 tests/src/python/test_qgsgeometry.py
@@ -1383,6 +1383,9 @@ bool QgsGeometry::deleteVertex( QgsConstWkbPtr &srcPtr, QgsWkbPtr &dstPtr, int a
return false;
}

if ( isRing && atVertex == pointIndex + nPoints - 1 )
atVertex = pointIndex;

dstPtr << nPoints - 1;

int len = ( atVertex - pointIndex ) * ps;
@@ -1424,8 +1427,6 @@ bool QgsGeometry::deleteVertex( QgsConstWkbPtr &srcPtr, QgsWkbPtr &dstPtr, int a

bool QgsGeometry::deleteVertex( int atVertex )
{
QgsDebugMsg( QString( "0x%1 %2 %3" ).arg(( qint64 ) mGeometry, 0, 16 ).arg( mGeometrySize ).arg( atVertex ) );

if ( atVertex < 0 )
return false;

@@ -1449,8 +1450,6 @@ bool QgsGeometry::deleteVertex( int atVertex )
if ( QGis::flatType( wkbType ) == QGis::WKBMultiPoint )
ps += 1 + sizeof( int );

QgsDebugMsg( QString( "wktType:%1[%2] mGeometrySize:%3 ps:%4" ).arg( wkbType ).arg( QGis::featureType( wkbType ) ).arg( mGeometrySize ).arg( ps ) );

unsigned char *dstBuffer = new unsigned char[mGeometrySize - ps];
QgsWkbPtr dstPtr( dstBuffer );
dstPtr << endianess << wkbType;
@@ -1477,7 +1476,6 @@ bool QgsGeometry::deleteVertex( int atVertex )
srcPtr >> nRings;
dstPtr << nRings;

bool deleted = false;
for ( int ringnr = 0, pointIndex = 0; ringnr < nRings; ++ringnr )
deleted |= deleteVertex( srcPtr, dstPtr, atVertex, hasZValue, pointIndex, true, ringnr == nRings - 1 );

@@ -1490,34 +1488,28 @@ bool QgsGeometry::deleteVertex( int atVertex )
int nPoints;
srcPtr >> nPoints;

QgsDebugMsg( QString( "nPoint:%1" ).arg( nPoints ) );

if ( atVertex < nPoints )
{
dstPtr << nPoints - 1;

int len = ps * atVertex;
if ( len > 0 )
{
QgsDebugMsg( QString( "first part:%1" ).arg( len ) );
memcpy( dstPtr, srcPtr, len );
srcPtr += len;
dstPtr += len;
}

QgsDebugMsg( QString( "skip:%1" ).arg( ps ) );
srcPtr += ps;

len = ps * ( nPoints - atVertex - 1 );
if ( len > 0 )
{
QgsDebugMsg( QString( "second part:%1" ).arg( ps ) );
memcpy( dstPtr, srcPtr, len );
srcPtr += len;
dstPtr += len;
}

QgsDebugMsg( QString( "deleted!: size:%1" ).arg( dstPtr - dstBuffer ) );
deleted = true;
}

@@ -1531,7 +1523,6 @@ bool QgsGeometry::deleteVertex( int atVertex )
srcPtr >> nLines;
dstPtr << nLines;

bool deleted = false;
for ( int linenr = 0, pointIndex = 0; linenr < nLines; ++linenr )
{
srcPtr >> endianess >> wkbType;
@@ -1568,7 +1559,6 @@ bool QgsGeometry::deleteVertex( int atVertex )

if ( deleted )
{
QgsDebugMsg( QString( "deleted true replace:0x%1 with 0x%2" ).arg(( qint64 ) mGeometry, 0, 16 ).arg(( qint64 ) dstBuffer, 0, 16 ) );
delete [] mGeometry;
mGeometry = dstBuffer;
mGeometrySize -= ps;
@@ -1577,7 +1567,6 @@ bool QgsGeometry::deleteVertex( int atVertex )
}
else
{
QgsDebugMsg( QString( "deleted false: free 0x%1" ).arg(( qint64 ) dstBuffer, 0, 16 ) );
delete [] dstBuffer;
return false;
}
@@ -766,7 +766,6 @@ def testMultipoint(self):
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)"
wkt = multipoint.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
@@ -775,19 +774,16 @@ def testMultipoint(self):
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)"
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)"
wkt = multipoint.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )

assert multipoint.deleteVertex(0), "MULTIPOINT delete at 2 failed"

expwkt = "MULTIPOINT(6 6)"
wkt = multipoint.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
@@ -852,6 +848,49 @@ def testDeleteVertex(self):
assert not polyline.deleteVertex( -5 ), "Delete vertex -5 unexpectedly succeeded"
assert not polyline.deleteVertex( 100 ), "Delete vertex 100 unexpectedly succeeded"

# 2-3 6-+-7
# | | | |
# 0-1 4 5 8-9
polyline = QgsGeometry.fromWkt("MULTILINESTRING((0 0, 1 0, 1 1, 2 1,2 0),(3 0, 3 1, 5 1, 5 0, 6 0))")
assert polyline.deleteVertex(5), "Delete vertex 5 failed"
expwkt = "MULTILINESTRING((0 0, 1 0, 1 1, 2 1, 2 0), (3 1, 5 1, 5 0, 6 0))"
wkt = polyline.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )

assert not polyline.deleteVertex(-100), "Delete vertex -100 unexpectedly succeeded"
assert not polyline.deleteVertex(100), "Delete vertex 100 unexpectedly succeeded"

assert polyline.deleteVertex(0), "Delete vertex 0 failed"
expwkt = "MULTILINESTRING((1 0, 1 1, 2 1, 2 0), (3 1, 5 1, 5 0, 6 0))"
wkt = polyline.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )

# 5---4
# | |
# | 2-3
# | |
# 0-1
polygon = QgsGeometry.fromWkt("POLYGON((0 0, 1 0, 1 1, 2 1, 2 2, 0 2, 0 0))")

assert polygon.deleteVertex(2), "Delete vertex 2 failed"
print "FIXME: exportToWkt doesn't put a blanks behind the comma"
expwkt = "POLYGON((0 0,1 0,2 1,2 2,0 2,0 0))"
wkt = polygon.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )

assert polygon.deleteVertex(0), "Delete vertex 0 failed"
expwkt = "POLYGON((1 0,2 1,2 2,0 2,1 0))"
wkt = polygon.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )

assert polygon.deleteVertex(4), "Delete vertex 4 failed"
expwkt = "POLYGON((2 1,2 2,0 2,2 1))"
wkt = polygon.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )

assert not polygon.deleteVertex(-100), "Delete vertex -100 unexpectedly succeeded"
assert not polygon.deleteVertex(100), "Delete vertex 100 unexpectedly succeeded"

# 5-+-4 0-+-9
# | | | |
# 6 2-3 1-2 +

0 comments on commit 6f15e59

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