Skip to content

Commit f538c8b

Browse files
committedFeb 28, 2014
added test for QgsGeometry::convertToType
1 parent 643e59a commit f538c8b

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed
 

‎tests/src/python/test_qgsgeometry.py

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,5 +1145,144 @@ def testAddPart(self):
11451145
wkt = polygon.exportToWkt()
11461146
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
11471147

1148+
def testConvertToType(self):
1149+
# 5-+-4 0-+-9 13-+-+-12
1150+
# | | | | | |
1151+
# | 2-3 1-2 | + 18-17 +
1152+
# | | | | | | | |
1153+
# 0-1 7-8 + 15-16 +
1154+
# | |
1155+
# 10-+-+-11
1156+
points = [
1157+
[ [ QgsPoint(0,0), QgsPoint(1,0), QgsPoint(1,1), QgsPoint(2,1), QgsPoint(2,2), QgsPoint(0,2), QgsPoint(0,0) ], ],
1158+
[ [ QgsPoint(4,0), QgsPoint(5,0), QgsPoint(5,2), QgsPoint(3,2), QgsPoint(3,1), QgsPoint(4,1), QgsPoint(4,0) ], ],
1159+
[ [ QgsPoint(10,0), QgsPoint(13,0), QgsPoint(13,3), QgsPoint(10,3), QgsPoint(10,0) ], [ QgsPoint(11,1), QgsPoint(12,1), QgsPoint(12,2), QgsPoint(11,2), QgsPoint(11,1) ] ]
1160+
]
1161+
######## TO POINT ########
1162+
# POINT TO POINT
1163+
point = QgsGeometry.fromPoint(QgsPoint(1,1))
1164+
wkt = point.convertToType(QGis.Point, False).exportToWkt()
1165+
expWkt = "POINT(1 1)"
1166+
assert compareWkt( expWkt, wkt ), "convertToType failed: from point to point. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1167+
# POINT TO MULTIPOINT
1168+
point = QgsGeometry.fromPoint(QgsPoint(1,1))
1169+
wkt = point.convertToType(QGis.Point, True).exportToWkt()
1170+
expWkt = "MULTIPOINT(1 1)"
1171+
assert compareWkt( expWkt, wkt ), "convertToType failed: from point to multipoint. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1172+
# LINE TO MULTIPOINT
1173+
line = QgsGeometry.fromPolyline(points[0][0])
1174+
wkt = line.convertToType(QGis.Point, True).exportToWkt()
1175+
expWkt = "MULTIPOINT(0 0, 1 0, 1 1, 2 1, 2 2, 0 2, 0 0)"
1176+
assert compareWkt( expWkt, wkt ), "convertToType failed: from line to multipoint. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1177+
# MULTILINE TO MULTIPOINT
1178+
multiLine = QgsGeometry.fromMultiPolyline(points[2])
1179+
wkt = multiLine.convertToType(QGis.Point, True).exportToWkt()
1180+
expWkt = "MULTIPOINT(10 0, 13 0, 13 3, 10 3, 10 0, 11 1, 12 1, 12 2, 11 2, 11 1)"
1181+
assert compareWkt( expWkt, wkt ), "convertToType failed: from multiline to multipoint. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1182+
# POLYGON TO MULTIPOINT
1183+
polygon = QgsGeometry.fromPolygon(points[0])
1184+
wkt = polygon.convertToType(QGis.Point, True).exportToWkt()
1185+
expWkt = "MULTIPOINT(0 0, 1 0, 1 1, 2 1, 2 2, 0 2, 0 0)"
1186+
assert compareWkt( expWkt, wkt ), "convertToType failed: from poylgon to multipoint. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1187+
# MULTIPOLYGON TO MULTIPOINT
1188+
multiPolygon = QgsGeometry.fromMultiPolygon(points)
1189+
wkt = multiPolygon.convertToType(QGis.Point, True).exportToWkt()
1190+
expWkt = "MULTIPOINT(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, 10 0, 13 0, 13 3, 10 3, 10 0, 11 1, 12 1, 12 2, 11 2, 11 1)"
1191+
assert compareWkt( expWkt, wkt ), "convertToType failed: from multipoylgon to multipoint. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1192+
1193+
1194+
######## TO LINE ########
1195+
# POINT TO LINE
1196+
point = QgsGeometry.fromPoint(QgsPoint(1,1))
1197+
assert point.convertToType(QGis.Line, False) !=0 , "convertToType with a point should return a null geometry"
1198+
# MULTIPOINT TO LINE
1199+
multipoint = QgsGeometry.fromMultiPoint(points[0][0])
1200+
wkt = multipoint.convertToType(QGis.Line, False).exportToWkt()
1201+
expWkt = "LINESTRING(0 0, 1 0, 1 1, 2 1, 2 2, 0 2, 0 0)"
1202+
assert compareWkt( expWkt, wkt ), "convertToType failed: from multipoint to line. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1203+
# MULTIPOINT TO MULTILINE
1204+
multipoint = QgsGeometry.fromMultiPoint(points[0][0])
1205+
wkt = multipoint.convertToType(QGis.Line, True).exportToWkt()
1206+
expWkt = "MULTILINESTRING((0 0, 1 0, 1 1, 2 1, 2 2, 0 2, 0 0))"
1207+
assert compareWkt( expWkt, wkt ), "convertToType failed: from multipoint to multiline. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1208+
# MULTILINE (which has a single part) TO LINE
1209+
multiLine = QgsGeometry.fromMultiPolyline(points[0])
1210+
wkt = multiLine.convertToType(QGis.Line, False).exportToWkt()
1211+
expWkt = "LINESTRING(0 0, 1 0, 1 1, 2 1, 2 2, 0 2, 0 0)"
1212+
assert compareWkt( expWkt, wkt ), "convertToType failed: from multiline to line. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1213+
# LINE TO MULTILINE
1214+
line = QgsGeometry.fromPolyline(points[0][0])
1215+
wkt = line.convertToType(QGis.Line, True).exportToWkt()
1216+
expWkt = "MULTILINESTRING((0 0, 1 0, 1 1, 2 1, 2 2, 0 2, 0 0))"
1217+
assert compareWkt( expWkt, wkt ), "convertToType failed: from line to multiline. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1218+
# POLYGON TO LINE
1219+
polygon = QgsGeometry.fromPolygon(points[0])
1220+
wkt = polygon.convertToType(QGis.Line, False).exportToWkt()
1221+
expWkt = "LINESTRING(0 0, 1 0, 1 1, 2 1, 2 2, 0 2, 0 0)"
1222+
assert compareWkt( expWkt, wkt ), "convertToType failed: from polygon to line. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1223+
# POLYGON TO MULTILINE
1224+
polygon = QgsGeometry.fromPolygon(points[0])
1225+
wkt = polygon.convertToType(QGis.Line, True).exportToWkt()
1226+
expWkt = "MULTILINESTRING((0 0, 1 0, 1 1, 2 1, 2 2, 0 2, 0 0))"
1227+
assert compareWkt( expWkt, wkt ), "convertToType failed: from polygon to multiline. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1228+
# POLYGON with ring TO MULTILINE
1229+
polygon = QgsGeometry.fromPolygon(points[2])
1230+
wkt = polygon.convertToType(QGis.Line, True).exportToWkt()
1231+
expWkt = "MULTILINESTRING((10 0, 13 0, 13 3, 10 3, 10 0), (11 1, 12 1, 12 2, 11 2, 11 1))"
1232+
assert compareWkt( expWkt, wkt ), "convertToType failed: from polygon with ring to multiline. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1233+
# MULTIPOLYGON (which has a single part) TO LINE
1234+
multiPolygon = QgsGeometry.fromMultiPolygon([points[0]])
1235+
wkt = multiPolygon.convertToType(QGis.Line, False).exportToWkt()
1236+
expWkt = "LINESTRING(0 0, 1 0, 1 1, 2 1, 2 2, 0 2, 0 0)"
1237+
assert compareWkt( expWkt, wkt ), "convertToType failed: from multipolygon to multiline. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1238+
# MULTIPOLYGON TO MULTILINE
1239+
multiPolygon = QgsGeometry.fromMultiPolygon(points)
1240+
wkt = multiPolygon.convertToType(QGis.Line, True).exportToWkt()
1241+
expWkt = "MULTILINESTRING((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), (10 0, 13 0, 13 3, 10 3, 10 0), (11 1, 12 1, 12 2, 11 2, 11 1))"
1242+
assert compareWkt( expWkt, wkt ), "convertToType failed: from multipolygon to multiline. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1243+
1244+
1245+
######## TO POLYGON ########
1246+
# MULTIPOINT TO POLYGON
1247+
multipoint = QgsGeometry.fromMultiPoint(points[0][0])
1248+
wkt = multipoint.convertToType(QGis.Polygon, False).exportToWkt()
1249+
expWkt = "POLYGON((0 0,1 0,1 1,2 1,2 2,0 2,0 0))"
1250+
assert compareWkt( expWkt, wkt ), "convertToType failed: from multipoint to polygon. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1251+
# MULTIPOINT TO MULTIPOLYGON
1252+
multipoint = QgsGeometry.fromMultiPoint(points[0][0])
1253+
wkt = multipoint.convertToType(QGis.Polygon, True).exportToWkt()
1254+
expWkt = "MULTIPOLYGON(((0 0,1 0,1 1,2 1,2 2,0 2,0 0)))"
1255+
assert compareWkt( expWkt, wkt ), "convertToType failed: from multipoint to multipolygon. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1256+
# LINE TO POLYGON
1257+
line = QgsGeometry.fromPolyline(points[0][0])
1258+
wkt = line.convertToType(QGis.Polygon, False).exportToWkt()
1259+
expWkt = "POLYGON((0 0,1 0,1 1,2 1,2 2,0 2,0 0))"
1260+
assert compareWkt( expWkt, wkt ), "convertToType failed: from line to polygon. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1261+
# LINE TO MULTIPOLYGON
1262+
line = QgsGeometry.fromPolyline(points[0][0])
1263+
wkt = line.convertToType(QGis.Polygon, True).exportToWkt()
1264+
expWkt = "MULTIPOLYGON(((0 0,1 0,1 1,2 1,2 2,0 2,0 0)))"
1265+
assert compareWkt( expWkt, wkt ), "convertToType failed: from line to multipolygon. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1266+
# MULTILINE (which has a single part) TO POLYGON
1267+
multiLine = QgsGeometry.fromMultiPolyline(points[0])
1268+
wkt = multiLine.convertToType(QGis.Polygon, False).exportToWkt()
1269+
expWkt = "POLYGON((0 0,1 0,1 1,2 1,2 2,0 2,0 0))"
1270+
assert compareWkt( expWkt, wkt ), "convertToType failed: from multiline to polygon. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1271+
# MULTILINE TO MULTIPOLYGON
1272+
multiLine = QgsGeometry.fromMultiPolyline([points[0][0],points[1][0]])
1273+
wkt = multiLine.convertToType(QGis.Polygon, True).exportToWkt()
1274+
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)))"
1275+
assert compareWkt( expWkt, wkt ), "convertToType failed: from multiline to multipolygon. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1276+
# POLYGON TO MULTIPOLYGON
1277+
polygon = QgsGeometry.fromPolygon(points[0])
1278+
wkt = polygon.convertToType(QGis.Polygon, True).exportToWkt()
1279+
expWkt = "MULTIPOLYGON(((0 0,1 0,1 1,2 1,2 2,0 2,0 0)))"
1280+
assert compareWkt( expWkt, wkt ), "convertToType failed: from polygon to multipolygon. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1281+
# MULTIPOLYGON (which has a single part) TO POLYGON
1282+
multiPolygon = QgsGeometry.fromMultiPolygon([points[0]])
1283+
wkt = multiPolygon.convertToType(QGis.Polygon, False).exportToWkt()
1284+
expWkt = "POLYGON((0 0,1 0,1 1,2 1,2 2,0 2,0 0))"
1285+
assert compareWkt( expWkt, wkt ), "convertToType failed: from multiline to polygon. Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt )
1286+
11481287
if __name__ == '__main__':
11491288
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.