|
36 | 36 | QgsCoordinateTransform,
|
37 | 37 | QgsRectangle,
|
38 | 38 | QgsWkbTypes,
|
39 |
| - QgsRenderChecker |
| 39 | + QgsRenderChecker, |
| 40 | + QgsCoordinateReferenceSystem, |
| 41 | + QgsProject |
40 | 42 | )
|
41 | 43 | from qgis.PyQt.QtCore import QDir
|
42 | 44 | from qgis.PyQt.QtGui import QImage, QPainter, QPen, QColor, QBrush, QPainterPath
|
@@ -1196,33 +1198,89 @@ def testTranslate(self):
|
1196 | 1198 |
|
1197 | 1199 | 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)))")
|
1198 | 1200 | self.assertEqual(polygon.translate(1, 2), 0, "Translate failed")
|
1199 |
| - 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)))" |
| 1201 | + expwkt = "MultiPolygon (((1 2, 2 2, 2 3, 3 3, 3 4, 1 4, 1 2)),((5 2, 6 2, 6 4, 4 4, 4 3, 5 3, 5 2)))" |
1200 | 1202 | wkt = polygon.asWkt()
|
| 1203 | + assert compareWkt(expwkt, wkt), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt) |
1201 | 1204 |
|
| 1205 | + def testTransform(self): |
| 1206 | + # null transform |
1202 | 1207 | ct = QgsCoordinateTransform()
|
1203 | 1208 |
|
1204 | 1209 | point = QgsGeometry.fromWkt("Point (1 1)")
|
1205 |
| - self.assertEqual(point.transform(ct), 0, "Translate failed") |
| 1210 | + self.assertEqual(point.transform(ct), 0, "Transform failed") |
1206 | 1211 | expwkt = "Point (1 1)"
|
1207 | 1212 | wkt = point.asWkt()
|
1208 | 1213 | assert compareWkt(expwkt, wkt), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt)
|
1209 | 1214 |
|
1210 | 1215 | point = QgsGeometry.fromWkt("MultiPoint ((1 1),(2 2),(3 3))")
|
1211 |
| - self.assertEqual(point.transform(ct), 0, "Translate failed") |
| 1216 | + self.assertEqual(point.transform(ct), 0, "Transform failed") |
1212 | 1217 | expwkt = "MultiPoint ((1 1),(2 2),(3 3))"
|
1213 | 1218 | wkt = point.asWkt()
|
1214 | 1219 | assert compareWkt(expwkt, wkt), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt)
|
1215 | 1220 |
|
1216 | 1221 | linestring = QgsGeometry.fromWkt("LineString (1 0, 2 0)")
|
1217 |
| - self.assertEqual(linestring.transform(ct), 0, "Translate failed") |
| 1222 | + self.assertEqual(linestring.transform(ct), 0, "Transform failed") |
1218 | 1223 | expwkt = "LineString (1 0, 2 0)"
|
1219 | 1224 | wkt = linestring.asWkt()
|
1220 | 1225 | assert compareWkt(expwkt, wkt), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt)
|
1221 | 1226 |
|
1222 | 1227 | 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)))")
|
1223 |
| - self.assertEqual(polygon.transform(ct), 0, "Translate failed") |
| 1228 | + self.assertEqual(polygon.transform(ct), 0, "Transform failed") |
1224 | 1229 | 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)))"
|
1225 | 1230 | wkt = polygon.asWkt()
|
| 1231 | + assert compareWkt(expwkt, wkt), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt) |
| 1232 | + |
| 1233 | + # valid transform |
| 1234 | + ct = QgsCoordinateTransform(QgsCoordinateReferenceSystem(4326), QgsCoordinateReferenceSystem(3857), QgsProject.instance()) |
| 1235 | + |
| 1236 | + point = QgsGeometry.fromWkt("Point (1 1)") |
| 1237 | + self.assertEqual(point.transform(ct), 0, "Transform failed") |
| 1238 | + expwkt = "Point (111319 111325)" |
| 1239 | + wkt = point.asWkt() |
| 1240 | + assert compareWkt(expwkt, wkt, tol=100), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt) |
| 1241 | + |
| 1242 | + point = QgsGeometry.fromWkt("MultiPoint ((1 1),(2 2),(3 3))") |
| 1243 | + self.assertEqual(point.transform(ct), 0, "Transform failed") |
| 1244 | + expwkt = "MultiPoint ((111319 111325),(222638 222684),(333958 334111))" |
| 1245 | + wkt = point.asWkt() |
| 1246 | + assert compareWkt(expwkt, wkt, tol=100), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt) |
| 1247 | + |
| 1248 | + linestring = QgsGeometry.fromWkt("LineString (1 0, 2 0)") |
| 1249 | + self.assertEqual(linestring.transform(ct), 0, "Transform failed") |
| 1250 | + expwkt = "LineString (111319 0, 222638 0)" |
| 1251 | + wkt = linestring.asWkt() |
| 1252 | + assert compareWkt(expwkt, wkt, tol=100), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt) |
| 1253 | + |
| 1254 | + 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)))") |
| 1255 | + self.assertEqual(polygon.transform(ct), 0, "Transform failed") |
| 1256 | + expwkt = "MultiPolygon (((0 0, 111319 0, 111319 111325, 222638 111325, 222638 222684, 0 222684, 0 0)),((445277 0, 556597 0, 556597 222684, 333958 222684, 333958 111325, 445277 111325, 445277 0)))" |
| 1257 | + wkt = polygon.asWkt() |
| 1258 | + assert compareWkt(expwkt, wkt, tol=100), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt) |
| 1259 | + |
| 1260 | + # reverse transform |
| 1261 | + point = QgsGeometry.fromWkt("Point (111319 111325)") |
| 1262 | + self.assertEqual(point.transform(ct, QgsCoordinateTransform.ReverseTransform), 0, "Transform failed") |
| 1263 | + expwkt = "Point (1 1)" |
| 1264 | + wkt = point.asWkt() |
| 1265 | + assert compareWkt(expwkt, wkt, tol=0.01), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt) |
| 1266 | + |
| 1267 | + point = QgsGeometry.fromWkt("MultiPoint ((111319 111325),(222638 222684),(333958 334111))") |
| 1268 | + self.assertEqual(point.transform(ct, QgsCoordinateTransform.ReverseTransform), 0, "Transform failed") |
| 1269 | + expwkt = "MultiPoint ((1 1),(2 2),(3 3))" |
| 1270 | + wkt = point.asWkt() |
| 1271 | + assert compareWkt(expwkt, wkt, tol=0.01), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt) |
| 1272 | + |
| 1273 | + linestring = QgsGeometry.fromWkt("LineString (111319 0, 222638 0)") |
| 1274 | + self.assertEqual(linestring.transform(ct, QgsCoordinateTransform.ReverseTransform), 0, "Transform failed") |
| 1275 | + expwkt = "LineString (1 0, 2 0)" |
| 1276 | + wkt = linestring.asWkt() |
| 1277 | + assert compareWkt(expwkt, wkt, tol=0.01), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt) |
| 1278 | + |
| 1279 | + polygon = QgsGeometry.fromWkt("MultiPolygon (((0 0, 111319 0, 111319 111325, 222638 111325, 222638 222684, 0 222684, 0 0)),((445277 0, 556597 0, 556597 222684, 333958 222684, 333958 111325, 445277 111325, 445277 0)))") |
| 1280 | + self.assertEqual(polygon.transform(ct, QgsCoordinateTransform.ReverseTransform), 0, "Transform failed") |
| 1281 | + 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)))" |
| 1282 | + wkt = polygon.asWkt() |
| 1283 | + assert compareWkt(expwkt, wkt, tol=0.01), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt) |
1226 | 1284 |
|
1227 | 1285 | def testExtrude(self):
|
1228 | 1286 | # test with empty geometry
|
|
0 commit comments