Skip to content

Commit bc757ff

Browse files
committedJan 29, 2014
* fix QgsGeometry::moveVertex for multipolygons()
* add test for QgsGeometry::addPart() * remove expectedFailure decoration from PyQgsGeometry.testSimplifyIssue4189 * modify move and translate tests by 1,1 to 1,2
1 parent d63104a commit bc757ff

File tree

3 files changed

+81
-34
lines changed

3 files changed

+81
-34
lines changed
 

‎python/core/qgsgeometry.sip

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class QgsGeometry
200200
* Searches for the closest vertex in this geometry to the given point.
201201
* @param point Specifiest the point for search
202202
* @param atVertex Receives index of the closest vertex
203-
* @return The squared cartesian distance, negative number on error
203+
* @return The squared cartesian distance is also returned in sqrDist, negative number on error
204204
*/
205205
double closestVertexWithContext( const QgsPoint& point, int& atVertex /Out/ );
206206

@@ -415,6 +415,7 @@ class QgsGeometry
415415
class Error
416416
{
417417
public:
418+
Error();
418419
Error( QString m );
419420
Error( QString m, QgsPoint p );
420421

‎src/core/qgsgeometry.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,13 +1198,16 @@ bool QgsGeometry::moveVertex( QgsWkbPtr &wkbPtr, const double &x, const double &
11981198
const int ps = ( hasZValue ? 3 : 2 ) * sizeof( double );
11991199

12001200
// Not this linestring/ring?
1201-
if ( atVertex > pointIndex + nPoints )
1201+
if ( atVertex >= pointIndex + nPoints )
12021202
{
12031203
wkbPtr += ps * nPoints;
12041204
pointIndex += nPoints;
12051205
return false;
12061206
}
12071207

1208+
if ( isRing && atVertex == pointIndex + nPoints - 1 )
1209+
atVertex = pointIndex;
1210+
12081211
// Goto point in this linestring/ring
12091212
wkbPtr += ps * ( atVertex - pointIndex );
12101213
wkbPtr << x << y;
@@ -2449,7 +2452,7 @@ int QgsGeometry::addPart( const QList<QgsPoint> &points, QGis::GeometryType geom
24492452
break;
24502453

24512454
case QGis::Line:
2452-
// Line needs to have at least two points and must be closed
2455+
// line needs to have at least two points
24532456
if ( points.size() < 2 )
24542457
{
24552458
QgsDebugMsg( "line must at least have two points: " + QString::number( points.size() ) );
@@ -2458,14 +2461,14 @@ int QgsGeometry::addPart( const QList<QgsPoint> &points, QGis::GeometryType geom
24582461
break;
24592462

24602463
case QGis::Polygon:
2461-
// Polygon needs to have at least three points and must be closed
2462-
if ( points.size() < 3 )
2464+
// polygon needs to have at least three distinct points and must be closed
2465+
if ( points.size() < 4 )
24632466
{
2464-
QgsDebugMsg( "polygon must at least have three points: " + QString::number( points.size() ) );
2467+
QgsDebugMsg( "polygon must at least have three distinct points and must be closed: " + QString::number( points.size() ) );
24652468
return 2;
24662469
}
24672470

2468-
// polygon must be closed
2471+
// Polygon must be closed
24692472
if ( points.first() != points.last() )
24702473
{
24712474
QgsDebugMsg( "polygon not closed" );

‎tests/src/python/test_qgsgeometry.py

Lines changed: 70 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ def testCrosses(self):
216216
("True", crossesGeom))
217217
assert crossesGeom == True, myMessage
218218

219-
@expectedFailure
220219
def testSimplifyIssue4189(self):
221220
"""Test we can simplify a complex geometry.
222221
@@ -752,16 +751,16 @@ def testMultipoint(self):
752751
if not TestQgsGeometry.wkbPtr:
753752
return
754753

755-
# #9423
756-
points = [ QgsPoint(10, 30), QgsPoint(40, 20), QgsPoint(30,10), QgsPoint(20,10) ]
757-
wkt = "MULTIPOINT (10 30, 40 20, 30 10, 20 10)"
758-
multipoint = QgsGeometry.fromWkt(wkt)
759-
assert multipoint.isMultipart(), "Expected MULTIPOINT to be multipart"
760-
assert multipoint.wkbType() == QGis.WKBMultiPoint, "Expected wkbType to be WKBMultipoint"
761-
i = 0
762-
for p in multipoint.asMultiPoint():
763-
assert p == points[i], "Expected %s at %d, got %s" % (points[i].toString(), i, p.toString())
764-
i+=1
754+
# #9423
755+
points = [ QgsPoint(10, 30), QgsPoint(40, 20), QgsPoint(30,10), QgsPoint(20,10) ]
756+
wkt = "MULTIPOINT (10 30, 40 20, 30 10, 20 10)"
757+
multipoint = QgsGeometry.fromWkt(wkt)
758+
assert multipoint.isMultipart(), "Expected MULTIPOINT to be multipart"
759+
assert multipoint.wkbType() == QGis.WKBMultiPoint, "Expected wkbType to be WKBMultipoint"
760+
i = 0
761+
for p in multipoint.asMultiPoint():
762+
assert p == points[i], "Expected %s at %d, got %s" % (points[i].toString(), i, p.toString())
763+
i+=1
765764

766765
multipoint = QgsGeometry.fromWkt( "MULTIPOINT(5 5)" )
767766
assert multipoint.vertexAt( 0 ) == QgsPoint(5,5), "MULTIPOINT fromWkt failed"
@@ -802,8 +801,8 @@ def testMultipoint(self):
802801
def testMoveVertex(self):
803802
multipoint = QgsGeometry.fromWkt( "MULTIPOINT(5 0,0 0,0 4,5 4,5 1,1 1,1 3,4 3,4 2,2 2)" )
804803
for i in range(0,10):
805-
assert multipoint.moveVertex( i+1, i+1, i ), "move vertex %d failed" % i
806-
expwkt = "MULTIPOINT(1 1, 2 2, 3 3, 4 4, 5 5, 6 6, 7 7, 8 8, 9 9, 10 10)"
804+
assert multipoint.moveVertex( i+1, -1-i, i ), "move vertex %d failed" % i
805+
expwkt = "MULTIPOINT(1 -1, 2 -2, 3 -3, 4 -4, 5 -5, 6 -6, 7 -7, 8 -8, 9 -9, 10 -10)"
807806
wkt = multipoint.exportToWkt()
808807
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
809808

@@ -833,12 +832,15 @@ def testMoveVertex(self):
833832
wkt = polygon.exportToWkt()
834833
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
835834

836-
assert polygon.moveVertex( 1, 1, 0 ), "move vertex failed"
837-
expwkt = "MULTIPOLYGON(((1 1,1 0,1 1,2 1,2 2,0 2,1 1)),((4 0,5 0,6 2,3 2,3 1,4 1,4 0)))"
838-
839-
assert polygon.moveVertex( 1, 1, 8 ), "move vertex failed"
840-
expwkt = "MULTIPOLYGON(((1 1,1 0,1 1,2 1,2 2,0 2,1 1)),((1 1,5 0,6 2,3 2,3 1,4 1,1 1)))"
835+
assert polygon.moveVertex( 1, 2, 0 ), "move vertex failed"
836+
expwkt = "MULTIPOLYGON(((1 2,1 0,1 1,2 1,2 2,0 2,1 2)),((4 0,5 0,6 2,3 2,3 1,4 1,4 0)))"
837+
wkt = polygon.exportToWkt()
838+
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
841839

840+
assert polygon.moveVertex( 2, 1, 7 ), "move vertex failed"
841+
expwkt = "MULTIPOLYGON(((1 2,1 0,1 1,2 1,2 2,0 2,1 2)),((2 1,5 0,6 2,3 2,3 1,4 1,2 1)))"
842+
wkt = polygon.exportToWkt()
843+
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
842844

843845
def testDeleteVertex(self):
844846
# 2-+-+-+-+-3
@@ -957,27 +959,26 @@ def testInsertVertex(self):
957959

958960
def testTranslate(self):
959961
point = QgsGeometry.fromWkt( "POINT(1 1)" )
960-
assert point.translate( 1, 1 )==0, "Translate failed"
961-
expwkt = "POINT(2 2)"
962+
assert point.translate( 1, 2 )==0, "Translate failed"
963+
expwkt = "POINT(2 3)"
962964
wkt = point.exportToWkt()
963965
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
964966

965967
point = QgsGeometry.fromWkt( "MULTIPOINT(1 1,2 2,3 3)" )
966-
assert point.translate( 1, 1 )==0, "Translate failed"
967-
expwkt = "MULTIPOINT(2 2, 3 3, 4 4)"
968+
assert point.translate( 1, 2 )==0, "Translate failed"
969+
expwkt = "MULTIPOINT(2 3, 3 4, 4 5)"
968970
wkt = point.exportToWkt()
969971
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
970972

971973
linestring = QgsGeometry.fromWkt( "LINESTRING(1 0,2 0)" )
972-
973-
assert linestring.translate( 1, 1 )==0, "Translate failed"
974-
expwkt = "LINESTRING(2 1, 3 1)"
974+
assert linestring.translate( 1, 2 )==0, "Translate failed"
975+
expwkt = "LINESTRING(2 2, 3 2)"
975976
wkt = linestring.exportToWkt()
976977
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
977978

978979
polygon = QgsGeometry.fromWkt( "MULTIPOLYGON(((0 0,1 0,1 1,2 1,2 2,0 2,0 0)),((4 0,5 0,5 2,3 2,3 1,4 1,4 0)))" )
979-
assert polygon.translate( 1, 1 )==0, "Translate failed"
980-
expwkt = "MULTIPOLYGON(((1 1,2 1,2 2,3 2,3 3,1 3,1 1)),((5 1,6 1,6 1,4 3,4 2,5 2,5 1)))"
980+
assert polygon.translate( 1, 2 )==0, "Translate failed"
981+
expwkt = "MULTIPOLYGON(((1 2,2 2,2 3,3 3,3 4,1 4,1 2)),((5 2,6 2,6 2,4 4,4 3,5 3,5 2)))"
981982
wkt = polygon.exportToWkt()
982983

983984
ct = QgsCoordinateTransform()
@@ -1077,5 +1078,47 @@ def testBoundingBox(self):
10771078
bb = polygon.boundingBox()
10781079
assert expbb == bb, "Expected:\n%s\nGot:\n%s\n" % (expbb.toString(), bb.toString())
10791080

1081+
def testAddPart(self):
1082+
# 2-3 6-+-7
1083+
# | | | |
1084+
# 0-1 4 5 8-9
1085+
points = [
1086+
[ QgsPoint(0,0), QgsPoint(1,0), QgsPoint(1,1), QgsPoint(2,1), QgsPoint(2,0), ],
1087+
[ QgsPoint(3,0), QgsPoint(3,1), QgsPoint(5,1), QgsPoint(5,0), QgsPoint(6,0), ]
1088+
]
1089+
1090+
polyline = QgsGeometry.fromPolyline( points[0] )
1091+
assert polyline.addPart( points[1][0:1] ) == 2, "addPart with one point line unexpectedly succeeded."
1092+
assert polyline.addPart( points[1][0:2] ) == 0, "addPart with two point line failed."
1093+
expwkt = "MULTILINESTRING((0 0, 1 0, 1 1, 2 1, 2 0), (3 0, 3 1))"
1094+
wkt = polyline.exportToWkt()
1095+
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
1096+
1097+
polyline = QgsGeometry.fromPolyline( points[0] )
1098+
assert polyline.addPart( points[1] ) == 0, "addPart with %d point line failed." % len(points[1])
1099+
expwkt = "MULTILINESTRING((0 0, 1 0, 1 1, 2 1, 2 0), (3 0, 3 1, 5 1, 5 0, 6 0))"
1100+
1101+
# 5-+-4 0-+-9
1102+
# | | | |
1103+
# | 2-3 1-2 |
1104+
# | | | |
1105+
# 0-1 7-8
1106+
points = [
1107+
[ [ QgsPoint(0,0), QgsPoint(1,0), QgsPoint(1,1), QgsPoint(2,1), QgsPoint(2,2), QgsPoint(0,2), QgsPoint(0,0), ] ],
1108+
[ [ QgsPoint(4,0), QgsPoint(5,0), QgsPoint(5,2), QgsPoint(3,2), QgsPoint(3,1), QgsPoint(4,1), QgsPoint(4,0), ] ]
1109+
]
1110+
1111+
polygon = QgsGeometry.fromPolygon( points[0] )
1112+
1113+
assert polygon.addPart( points[1][0][0:1] ) == 2, "addPart with one point ring unexpectedly succeeded."
1114+
assert polygon.addPart( points[1][0][0:2] ) == 2, "addPart with two point ring unexpectedly succeeded."
1115+
assert polygon.addPart( points[1][0][0:3] ) == 2, "addPart with unclosed three point ring unexpectedly succeeded."
1116+
assert polygon.addPart( [ QgsPoint(4,0), QgsPoint(5,0), QgsPoint(4,0) ] ) == 2, "addPart with 'closed' three point ring unexpectedly succeeded."
1117+
1118+
assert polygon.addPart( points[1][0] ) == 0, "addPart failed"
1119+
expwkt = "MULTIPOLYGON(((0 0,1 0,1 1,2 1,2 2,0 2,0 0)),((4 0,5 0,5 2,3 2,3 1,4 1,4 0)))"
1120+
wkt = polygon.exportToWkt()
1121+
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
1122+
10801123
if __name__ == '__main__':
10811124
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.