本文整理汇总了Python中utilities.compareWkt函数的典型用法代码示例。如果您正苦于以下问题:Python compareWkt函数的具体用法?Python compareWkt怎么用?Python compareWkt使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了compareWkt函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: testInsertVertex
def testInsertVertex(self):
linestring = QgsGeometry.fromWkt( "LINESTRING(1 0,2 0)" )
if TestQgsGeometry.wkbPtr:
# CHANGE old implementation fails to insert vertex
assert linestring.insertVertex( 0, 0, 0 ), "Insert vertex 0 0 at 0 failed"
expwkt = "LINESTRING(0 0, 1 0, 2 0)"
wkt = linestring.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
assert linestring.insertVertex( 1.5, 0, 2 if TestQgsGeometry.wkbPtr else 1 ), "Insert vertex 1.5 0 at 2 failed"
expwkt = "LINESTRING(0 0, 1 0, 1.5 0, 2 0)" if TestQgsGeometry.wkbPtr else "LINESTRING(1 0, 1.5 0, 2 0)"
wkt = linestring.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
assert not linestring.insertVertex( 3, 0, 5 ), "Insert vertex 3 0 at 5 should have failed"
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)))" )
assert polygon.insertVertex( 0, 0, 8 ), "Insert vertex 0 0 at 8 failed"
expwkt = "MULTIPOLYGON(((0 0,1 0,1 1,2 1,2 2,0 2,0 0)),((4 0,0 0,5 0,5 2,3 2,3 1,4 1,4 0)))"
wkt = polygon.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
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)))" )
assert polygon.insertVertex( 0, 0, 7 ), "Insert vertex 0 0 at 7 failed"
expwkt = "MULTIPOLYGON(((0 0,1 0,1 1,2 1,2 2,0 2,0 0)),((0 0,4 0,5 0,5 2,3 2,3 1,4 1,0 0)))"
wkt = polygon.exportToWkt()
if TestQgsGeometry.wkbPtr:
# CHANGE old implementation produces: MULTIPOLYGON(((0 0,1 0,1 1,2 1,2 2,0 2,0 0)),())
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
开发者ID:idesign3000,项目名称:QGIS,代码行数:31,代码来源:test_qgsgeometry.py
示例2: testAsWktCoordinates
def testAsWktCoordinates(self):
"""Test that we can get a proper wkt representation fo the rect"""
rect1 = QgsRectangle(0.0, 0.0, 5.0, 5.0)
myExpectedWkt = "0 0, " "5 5"
myWkt = rect1.asWktCoordinates()
myMessage = "Expected: %s\nGot: %s\n" % (myExpectedWkt, myWkt)
assert compareWkt(myWkt, myExpectedWkt), myMessage
开发者ID:dwadler,项目名称:QGIS,代码行数:7,代码来源:test_qgsrectangle.py
示例3: testWriteShapefileWithMultiConversion
def testWriteShapefileWithMultiConversion(self):
"""Check writing geometries to an ESRI shapefile with conversion to multi."""
ml = QgsVectorLayer(("Point?crs=epsg:4326&field=id:int"), "test", "memory")
self.assertIsNotNone(ml, "Provider not initialized")
self.assertTrue(ml.isValid(), "Source layer not valid")
provider = ml.dataProvider()
self.assertIsNotNone(provider)
ft = QgsFeature()
ft.setGeometry(QgsGeometry.fromWkt("Point (1 2)"))
ft.setAttributes([1])
res, features = provider.addFeatures([ft])
self.assertTrue(res)
self.assertTrue(features)
dest_file_name = os.path.join(str(QDir.tempPath()), "to_multi.shp")
crs = QgsCoordinateReferenceSystem()
crs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId)
write_result = QgsVectorFileWriter.writeAsVectorFormat(
ml, dest_file_name, "utf-8", crs, "ESRI Shapefile", forceMulti=True
)
self.assertEqual(write_result, QgsVectorFileWriter.NoError)
# Open result and check
created_layer = QgsVectorLayer("{}|layerid=0".format(dest_file_name), "test", "ogr")
f = next(created_layer.getFeatures(QgsFeatureRequest()))
g = f.geometry()
wkt = g.exportToWkt()
expWkt = "MultiPoint ((1 2))"
self.assertTrue(
compareWkt(expWkt, wkt),
"saving geometry with multi conversion failed: mismatch Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt),
)
开发者ID:liminlu0314,项目名称:QGIS,代码行数:34,代码来源:test_qgsvectorfilewriter.py
示例4: testAsWktPolygon
def testAsWktPolygon(self):
"""Test that we can get a proper rect wkt polygon representation for rect"""
rect1 = QgsRectangle(0.0, 0.0, 5.0, 5.0)
myExpectedWkt = "POLYGON((0 0, " "5 0, " "5 5, " "0 5, " "0 0))"
myWkt = rect1.asWktPolygon()
myMessage = "Expected: %s\nGot: %s\n" % (myExpectedWkt, myWkt)
assert compareWkt(myWkt, myExpectedWkt), myMessage
开发者ID:dwadler,项目名称:QGIS,代码行数:7,代码来源:test_qgsrectangle.py
示例5: testGeometry
def testGeometry(self):
""" Test calculation of aggregates on geometry expressions """
layer = QgsVectorLayer("Point?", "layer", "memory")
pr = layer.dataProvider()
# must be same length:
geometry_values = [
QgsGeometry.fromWkt("Point ( 0 0 )"),
QgsGeometry.fromWkt("Point ( 1 1 )"),
QgsGeometry.fromWkt("Point ( 2 2 )"),
]
features = []
for i in range(len(geometry_values)):
f = QgsFeature()
f.setGeometry(geometry_values[i])
features.append(f)
self.assertTrue(pr.addFeatures(features))
agg = QgsAggregateCalculator(layer)
val, ok = agg.calculate(QgsAggregateCalculator.GeometryCollect, "$geometry")
self.assertTrue(ok)
expwkt = "MultiPoint ((0 0), (1 1), (2 2))"
wkt = val.exportToWkt()
self.assertTrue(compareWkt(expwkt, wkt), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt))
开发者ID:medspx,项目名称:QGIS,代码行数:27,代码来源:test_qgsaggregatecalculator.py
示例6: testMoveVertex
def testMoveVertex(self):
multipoint = QgsGeometry.fromWkt( "MULTIPOINT(5 0,0 0,0 4,5 4,5 1,1 1,1 3,4 3,4 2,2 2)" )
for i in range(0,10):
assert multipoint.moveVertex( i+1, -1-i, i ), "move vertex %d failed" % i
expwkt = "MULTIPOINT(1 -1, 2 -2, 3 -3, 4 -4, 5 -5, 6 -6, 7 -7, 8 -8, 9 -9, 10 -10)"
wkt = multipoint.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
# 2-+-+-+-+-3
# | |
# + 6-+-+-7 +
# | | | |
# + + 9-+-8 +
# | | |
# ! 5-+-+-+-4 !
# |
# 1-+-+-+-+-0 !
polyline = QgsGeometry.fromWkt( "LINESTRING(5 0,0 0,0 4,5 4,5 1,1 1,1 3,4 3,4 2,2 2)" )
assert polyline.moveVertex( 5.5, 4.5, 3 ), "move vertex failed"
expwkt = "LINESTRING(5 0, 0 0, 0 4, 5.5 4.5, 5 1, 1 1, 1 3, 4 3, 4 2, 2 2)"
wkt = polyline.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
# 5-+-4 0-+-9
# | | | |
# 6 2-3 1-2!+
# | | | |
# 0-1 7-8
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)))" )
assert polygon.moveVertex( 6, 2, 9 ), "move vertex failed"
expwkt = "MULTIPOLYGON(((0 0,1 0,1 1,2 1,2 2,0 2,0 0)),((4 0,5 0,6 2,3 2,3 1,4 1,4 0)))"
wkt = polygon.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
assert polygon.moveVertex( 1, 2, 0 ), "move vertex failed"
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)))"
wkt = polygon.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
assert polygon.moveVertex( 2, 1, 7 ), "move vertex failed"
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)))"
wkt = polygon.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
开发者ID:idesign3000,项目名称:QGIS,代码行数:43,代码来源:test_qgsgeometry.py
示例7: testAddPart
def testAddPart(self):
# 2-3 6-+-7
# | | | |
# 0-1 4 5 8-9
points = [
[ QgsPoint(0,0), QgsPoint(1,0), QgsPoint(1,1), QgsPoint(2,1), QgsPoint(2,0), ],
[ QgsPoint(3,0), QgsPoint(3,1), QgsPoint(5,1), QgsPoint(5,0), QgsPoint(6,0), ]
]
polyline = QgsGeometry.fromPolyline( points[0] )
assert polyline.addPart( points[1][0:1] ) == 2, "addPart with one point line unexpectedly succeeded."
assert polyline.addPart( points[1][0:2] ) == 0, "addPart with two point line failed."
expwkt = "MULTILINESTRING((0 0, 1 0, 1 1, 2 1, 2 0), (3 0, 3 1))"
wkt = polyline.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
polyline = QgsGeometry.fromPolyline( points[0] )
assert polyline.addPart( points[1] ) == 0, "addPart with %d point line failed." % len(points[1])
expwkt = "MULTILINESTRING((0 0, 1 0, 1 1, 2 1, 2 0), (3 0, 3 1, 5 1, 5 0, 6 0))"
# 5-+-4 0-+-9
# | | | |
# | 2-3 1-2 |
# | | | |
# 0-1 7-8
points = [
[ [ QgsPoint(0,0), QgsPoint(1,0), QgsPoint(1,1), QgsPoint(2,1), QgsPoint(2,2), QgsPoint(0,2), QgsPoint(0,0), ] ],
[ [ QgsPoint(4,0), QgsPoint(5,0), QgsPoint(5,2), QgsPoint(3,2), QgsPoint(3,1), QgsPoint(4,1), QgsPoint(4,0), ] ]
]
polygon = QgsGeometry.fromPolygon( points[0] )
assert polygon.addPart( points[1][0][0:1] ) == 2, "addPart with one point ring unexpectedly succeeded."
assert polygon.addPart( points[1][0][0:2] ) == 2, "addPart with two point ring unexpectedly succeeded."
assert polygon.addPart( points[1][0][0:3] ) == 2, "addPart with unclosed three point ring unexpectedly succeeded."
assert polygon.addPart( [ QgsPoint(4,0), QgsPoint(5,0), QgsPoint(4,0) ] ) == 2, "addPart with 'closed' three point ring unexpectedly succeeded."
assert polygon.addPart( points[1][0] ) == 0, "addPart failed"
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)))"
wkt = polygon.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
开发者ID:idesign3000,项目名称:QGIS,代码行数:41,代码来源:test_qgsgeometry.py
示例8: testAsWktPolygon
def testAsWktPolygon(self):
"""Test that we can get a proper rect wkt polygon representation for rect"""
rect1 = QgsRectangle(0.0, 0.0, 5.0, 5.0)
myExpectedWkt = ('POLYGON((0 0, '
'5 0, '
'5 5, '
'0 5, '
'0 0))')
myWkt = rect1.asWktPolygon()
myMessage = ('Expected: %s\nGot: %s\n' %
(myExpectedWkt, myWkt))
assert compareWkt(myWkt, myExpectedWkt), myMessage
开发者ID:phborba,项目名称:QGIS,代码行数:12,代码来源:test_qgsrectangle.py
示例9: testAddFeatures
def testAddFeatures(self):
layer = QgsVectorLayer("Point", "test", "pythonprovider")
provider = layer.dataProvider()
res = provider.addAttributes([QgsField("name", QVariant.String),
QgsField("age", QVariant.Int),
QgsField("size", QVariant.Double)])
assert res, "Failed to add attributes"
myMessage = ('Expected: %s\nGot: %s\n' %
(3, len(provider.fields())))
assert len(provider.fields()) == 3, myMessage
ft = QgsFeature()
ft.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(10, 10)))
ft.setAttributes(["Johny",
20,
0.3])
res, t = provider.addFeatures([ft])
assert res, "Failed to add feature"
myMessage = ('Expected: %s\nGot: %s\n' %
(1, provider.featureCount()))
assert provider.featureCount() == 1, myMessage
for f in provider.getFeatures(QgsFeatureRequest()):
myMessage = ('Expected: %s\nGot: %s\n' %
("Johny", f[0]))
assert f[0] == "Johny", myMessage
myMessage = ('Expected: %s\nGot: %s\n' %
(20, f[1]))
assert f[1] == 20, myMessage
myMessage = ('Expected: %s\nGot: %s\n' %
(0.3, f[2]))
assert (f[2] - 0.3) < 0.0000001, myMessage
geom = f.geometry()
myMessage = ('Expected: %s\nGot: %s\n' %
("Point (10 10)", str(geom.asWkt())))
assert compareWkt(str(geom.asWkt()), "Point (10 10)"), myMessage
开发者ID:yoichigmf,项目名称:QGIS,代码行数:49,代码来源:test_provider_python.py
示例10: testGetFeatures
def testGetFeatures(self):
""" Test that expected results are returned when fetching all features """
# IMPORTANT - we do not use `for f in provider.getFeatures()` as we are also
# testing that existing attributes & geometry in f are overwritten correctly
# (for f in ... uses a new QgsFeature for every iteration)
it = self.provider.getFeatures()
f = QgsFeature()
attributes = {}
geometries = {}
while it.nextFeature(f):
# expect feature to be valid
self.assertTrue(f.isValid())
# split off the first 5 attributes only - some provider test datasets will include
# additional attributes which we ignore
attrs = f.attributes()[0:5]
# force the num_char attribute to be text - some providers (eg delimited text) will
# automatically detect that this attribute contains numbers and set it as a numeric
# field
attrs[4] = str(attrs[4])
attributes[f["pk"]] = attrs
geometries[f["pk"]] = f.hasGeometry() and f.geometry().exportToWkt()
expected_attributes = {
5: [5, -200, NULL, "NuLl", "5"],
3: [3, 300, "Pear", "PEaR", "3"],
1: [1, 100, "Orange", "oranGe", "1"],
2: [2, 200, "Apple", "Apple", "2"],
4: [4, 400, "Honey", "Honey", "4"],
}
self.assertEqual(attributes, expected_attributes, "Expected {}, got {}".format(expected_attributes, attributes))
expected_geometries = {
1: "Point (-70.332 66.33)",
2: "Point (-68.2 70.8)",
3: None,
4: "Point(-65.32 78.3)",
5: "Point(-71.123 78.23)",
}
for pk, geom in list(expected_geometries.items()):
if geom:
assert compareWkt(geom, geometries[pk]), "Geometry {} mismatch Expected:\n{}\nGot:\n{}\n".format(
pk, geom, geometries[pk].exportToWkt()
)
else:
self.assertFalse(geometries[pk], "Expected null geometry for {}".format(pk))
开发者ID:mbernasocchi,项目名称:QGIS,代码行数:47,代码来源:providertestbase.py
示例11: recordDifference
def recordDifference(self, record1, record2):
# Compare a record defined as a dictionary
for k in list(record1.keys()):
if k not in record2:
return "Field {0} is missing".format(k)
r1k = record1[k]
r2k = record2[k]
if k == geomkey:
if not compareWkt(r1k, r2k):
return "Geometry differs: {0:.50} versus {1:.50}".format(r1k, r2k)
else:
if record1[k] != record2[k]:
return "Field {0} differs: {1:.50} versus {2:.50}".format(k, repr(r1k), repr(r2k))
for k in list(record2.keys()):
if k not in record1:
return "Output contains extra field {0}".format(k)
return ''
开发者ID:alexbruy,项目名称:QGIS,代码行数:17,代码来源:test_qgsdelimitedtextprovider.py
示例12: testMultipoint
def testMultipoint(self):
# CHANGE previous implementation didn't support multipoint too much
if not TestQgsGeometry.wkbPtr:
return
# #9423
points = [ QgsPoint(10, 30), QgsPoint(40, 20), QgsPoint(30,10), QgsPoint(20,10) ]
wkt = "MULTIPOINT (10 30, 40 20, 30 10, 20 10)"
multipoint = QgsGeometry.fromWkt(wkt)
assert multipoint.isMultipart(), "Expected MULTIPOINT to be multipart"
assert multipoint.wkbType() == QGis.WKBMultiPoint, "Expected wkbType to be WKBMultipoint"
i = 0
for p in multipoint.asMultiPoint():
assert p == points[i], "Expected %s at %d, got %s" % (points[i].toString(), i, p.toString())
i+=1
multipoint = QgsGeometry.fromWkt( "MULTIPOINT(5 5)" )
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)"
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)"
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)"
wkt = multipoint.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
assert not multipoint.deleteVertex(4), "MULTIPOINT delete at 4 unexpectedly succeeded"
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 )
开发者ID:idesign3000,项目名称:QGIS,代码行数:51,代码来源:test_qgsgeometry.py
示例13: testTranslate
def testTranslate(self):
point = QgsGeometry.fromWkt( "POINT(1 1)" )
assert point.translate( 1, 1 )==0, "Translate failed"
expwkt = "POINT(2 2)"
wkt = point.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
point = QgsGeometry.fromWkt( "MULTIPOINT(1 1,2 2,3 3)" )
assert point.translate( 1, 1 )==0, "Translate failed"
expwkt = "MULTIPOINT(2 2, 3 3, 4 4)"
wkt = point.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
linestring = QgsGeometry.fromWkt( "LINESTRING(1 0,2 0)" )
assert linestring.translate( 1, 1 )==0, "Translate failed"
expwkt = "LINESTRING(2 1, 3 1)"
wkt = linestring.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
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)))" )
assert polygon.translate( 1, 1 )==0, "Translate failed"
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)))"
wkt = polygon.exportToWkt()
ct = QgsCoordinateTransform()
point = QgsGeometry.fromWkt( "POINT(1 1)" )
assert point.transform( ct )==0, "Translate failed"
expwkt = "POINT(1 1)"
wkt = point.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
point = QgsGeometry.fromWkt( "MULTIPOINT(1 1,2 2,3 3)" )
assert point.transform( ct )==0, "Translate failed"
expwkt = "MULTIPOINT(1 1, 2 2, 3 3)"
wkt = point.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
linestring = QgsGeometry.fromWkt( "LINESTRING(1 0,2 0)" )
assert linestring.transform( ct )==0, "Translate failed"
expwkt = "LINESTRING(1 0, 2 0)"
wkt = linestring.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
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)))" )
assert polygon.transform( ct )==0, "Translate failed"
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)))"
wkt = polygon.exportToWkt()
开发者ID:SinSiXX,项目名称:QGIS,代码行数:49,代码来源:test_qgsgeometry.py
示例14: testWriteShapefileWithMultiConversion
def testWriteShapefileWithMultiConversion(self):
"""Check writing geometries to an ESRI shapefile with conversion to multi."""
ml = QgsVectorLayer(
('Point?crs=epsg:4326&field=id:int'),
'test',
'memory')
self.assertIsNotNone(ml, 'Provider not initialized')
self.assertTrue(ml.isValid(), 'Source layer not valid')
provider = ml.dataProvider()
self.assertIsNotNone(provider)
ft = QgsFeature()
ft.setGeometry(QgsGeometry.fromWkt('Point (1 2)'))
ft.setAttributes([1])
res, features = provider.addFeatures([ft])
self.assertTrue(res)
self.assertTrue(features)
dest_file_name = os.path.join(str(QDir.tempPath()), 'to_multi.shp')
crs = QgsCoordinateReferenceSystem()
crs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId)
write_result, error_message = QgsVectorFileWriter.writeAsVectorFormat(
ml,
dest_file_name,
'utf-8',
crs,
'ESRI Shapefile',
forceMulti=True)
self.assertEqual(write_result, QgsVectorFileWriter.NoError, error_message)
# Open result and check
created_layer = QgsVectorLayer('{}|layerid=0'.format(dest_file_name), 'test', 'ogr')
f = next(created_layer.getFeatures(QgsFeatureRequest()))
g = f.geometry()
wkt = g.asWkt()
expWkt = 'MultiPoint ((1 2))'
self.assertTrue(compareWkt(expWkt, wkt),
"saving geometry with multi conversion failed: mismatch Expected:\n%s\nGot:\n%s\n" % (
expWkt, wkt))
开发者ID:boundlessgeo,项目名称:QGIS,代码行数:40,代码来源:test_qgsvectorfilewriter.py
示例15: testGetFeatures
def testGetFeatures(self):
""" Test that expected results are returned when fetching all features """
# IMPORTANT - we do not use `for f in provider.getFeatures()` as we are also
# testing that existing attributes & geometry in f are overwritten correctly
# (for f in ... uses a new QgsFeature for every iteration)
it = self.provider.getFeatures()
f = QgsFeature()
attributes = {}
geometries = {}
while it.nextFeature(f):
# split off the first 5 attributes only - some provider test datasets will include
# additional attributes which we ignore
attrs = f.attributes()[0:5]
# force the num_char attribute to be text - some providers (eg delimited text) will
# automatically detect that this attribute contains numbers and set it as a numeric
# field
attrs[4] = str(attrs[4])
attributes[f['pk']] = attrs
geometries[f['pk']] = f.constGeometry() and f.constGeometry().exportToWkt()
expected_attributes = {5: [5, -200, NULL, 'NuLl', '5'],
3: [3, 300, 'Pear', 'PEaR', '3'],
1: [1, 100, 'Orange', 'oranGe', '1'],
2: [2, 200, 'Apple', 'Apple', '2'],
4: [4, 400, 'Honey', 'Honey', '4']}
self.assertEqual(attributes, expected_attributes, 'Expected {}, got {}'.format(expected_attributes, attributes))
expected_geometries = {1: 'Point (-70.332 66.33)',
2: 'Point (-68.2 70.8)',
3: None,
4: 'Point(-65.32 78.3)',
5: 'Point(-71.123 78.23)'}
for pk, geom in expected_geometries.iteritems():
if geom:
assert compareWkt(geom, geometries[pk]), "Geometry {} mismatch Expected:\n{}\nGot:\n{}\n".format(pk, geom, geometries[pk].exportToWkt())
else:
self.assertFalse(geometries[pk], 'Expected null geometry for {}'.format(pk))
开发者ID:MrBenjaminLeb,项目名称:QGIS,代码行数:39,代码来源:providertestbase.py
示例16: testWriteShapefileWithZ
def testWriteShapefileWithZ(self):
"""Check writing geometries with Z dimension to an ESRI shapefile."""
# start by saving a memory layer and forcing z
ml = QgsVectorLayer(("Point?crs=epsg:4326&field=id:int"), "test", "memory")
self.assertIsNotNone(ml, "Provider not initialized")
self.assertTrue(ml.isValid(), "Source layer not valid")
provider = ml.dataProvider()
self.assertIsNotNone(provider)
ft = QgsFeature()
ft.setGeometry(QgsGeometry.fromWkt("PointZ (1 2 3)"))
ft.setAttributes([1])
res, features = provider.addFeatures([ft])
self.assertTrue(res)
self.assertTrue(features)
# check with both a standard PointZ and 25d style Point25D type
for t in [QgsWkbTypes.PointZ, QgsWkbTypes.Point25D]:
dest_file_name = os.path.join(str(QDir.tempPath()), "point_{}.shp".format(QgsWkbTypes.displayString(t)))
crs = QgsCoordinateReferenceSystem()
crs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId)
write_result = QgsVectorFileWriter.writeAsVectorFormat(
ml, dest_file_name, "utf-8", crs, "ESRI Shapefile", overrideGeometryType=t
)
self.assertEqual(write_result, QgsVectorFileWriter.NoError)
# Open result and check
created_layer = QgsVectorLayer("{}|layerid=0".format(dest_file_name), "test", "ogr")
f = next(created_layer.getFeatures(QgsFeatureRequest()))
g = f.geometry()
wkt = g.exportToWkt()
expWkt = "PointZ (1 2 3)"
self.assertTrue(
compareWkt(expWkt, wkt),
"saving geometry with Z failed: mismatch Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt),
)
# also try saving out the shapefile version again, as an extra test
# this tests that saving a layer with z WITHOUT explicitly telling the writer to keep z values,
# will stay retain the z values
dest_file_name = os.path.join(
str(QDir.tempPath()), "point_{}_copy.shp".format(QgsWkbTypes.displayString(t))
)
crs = QgsCoordinateReferenceSystem()
crs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId)
write_result = QgsVectorFileWriter.writeAsVectorFormat(
created_layer, dest_file_name, "utf-8", crs, "ESRI Shapefile"
)
self.assertEqual(write_result, QgsVectorFileWriter.NoError)
# Open result and check
created_layer_from_shp = QgsVectorLayer("{}|layerid=0".format(dest_file_name), "test", "ogr")
f = next(created_layer_from_shp.getFeatures(QgsFeatureRequest()))
g = f.geometry()
wkt = g.exportToWkt()
self.assertTrue(
compareWkt(expWkt, wkt),
"saving geometry with Z failed: mismatch Expected:\n%s\nGot:\n%s\n" % (expWkt, wkt),
)
开发者ID:liminlu0314,项目名称:QGIS,代码行数:61,代码来源:test_qgsvectorfilewriter.py
示例17: testClipping
def testClipping(self):
"""Test that we can clip geometries using other geometries."""
myMemoryLayer = QgsVectorLayer(
('LineString?crs=epsg:4326&field=name:string(20)&index=yes'),
'clip-in',
'memory')
assert myMemoryLayer is not None, 'Provider not initialised'
myProvider = myMemoryLayer.dataProvider()
assert myProvider is not None
myFeature1 = QgsFeature()
myFeature1.setGeometry(QgsGeometry.fromPolyline([
QgsPoint(10,10),
QgsPoint(20,10),
QgsPoint(30,10),
QgsPoint(40,10),
]
))
myFeature1.setAttributes(['Johny'])
myFeature2 = QgsFeature()
myFeature2.setGeometry(QgsGeometry.fromPolyline([
QgsPoint(10,10),
QgsPoint(20,20),
QgsPoint(30,30),
QgsPoint(40,40),
]
))
myFeature2.setAttributes(['Be'])
myFeature3 = QgsFeature()
myFeature3.setGeometry(QgsGeometry.fromPolyline([
QgsPoint(10,10),
QgsPoint(10,20),
QgsPoint(10,30),
QgsPoint(10,40),
]
))
myFeature3.setAttributes(['Good'])
myResult, myFeatures = myProvider.addFeatures(
[myFeature1, myFeature2, myFeature3])
assert myResult == True
assert len(myFeatures) == 3
myClipPolygon = QgsGeometry.fromPolygon([[
QgsPoint(20,20),
QgsPoint(20,30),
QgsPoint(30,30),
QgsPoint(30,20),
QgsPoint(20,20),
]]
)
print 'Clip: %s' % myClipPolygon.exportToWkt()
writeShape(myMemoryLayer, 'clipGeometryBefore.shp')
fit = myProvider.getFeatures()
myFeatures = []
myFeature = QgsFeature()
while fit.nextFeature(myFeature):
myGeometry = myFeature.geometry()
if myGeometry.intersects(myClipPolygon):
# Adds nodes where the clip and the line intersec
myCombinedGeometry = myGeometry.combine(myClipPolygon)
# Gives you the areas inside the clip
mySymmetricalGeometry = myGeometry.symDifference(
myCombinedGeometry)
# Gives you areas outside the clip area
# myDifferenceGeometry = myCombinedGeometry.difference(
# myClipPolygon)
#print 'Original: %s' % myGeometry.exportToWkt()
#print 'Combined: %s' % myCombinedGeometry.exportToWkt()
#print 'Difference: %s' % myDifferenceGeometry.exportToWkt()
print 'Symmetrical: %s' % mySymmetricalGeometry.exportToWkt()
myExpectedWkt = 'LINESTRING(20 20, 30 30)'
# There should only be one feature that intersects this clip
# poly so this assertion should work.
assert compareWkt( myExpectedWkt,
mySymmetricalGeometry.exportToWkt() )
myNewFeature = QgsFeature()
myNewFeature.setAttributes(myFeature.attributes())
myNewFeature.setGeometry(mySymmetricalGeometry)
myFeatures.append(myNewFeature)
myNewMemoryLayer = QgsVectorLayer(
('LineString?crs=epsg:4326&field=name:string(20)&index=yes'),
'clip-out',
'memory')
myNewProvider = myNewMemoryLayer.dataProvider()
myResult, myFeatures = myNewProvider.addFeatures(myFeatures)
self.assertTrue(myResult)
self.assertEqual(len(myFeatures), 1)
writeShape(myNewMemoryLayer, 'clipGeometryAfter.shp')
开发者ID:ChowZenki,项目名称:QGIS,代码行数:97,代码来源:test_qgsgeometry.py
示例18: testDeleteVertex
def testDeleteVertex(self):
# 2-+-+-+-+-3
# | |
# + 6-+-+-7 +
# | | | |
# + + 9-+-8 +
# | | |
# ! 5-+-+-+-4
# |
# 1-+-+-+-+-0
polyline = QgsGeometry.fromWkt( "LINESTRING(5 0,0 0,0 4,5 4,5 1,1 1,1 3,4 3,4 2,2 2)" )
assert polyline.deleteVertex( 3 ), "Delete vertex 5 4 failed"
expwkt = "LINESTRING(5 0, 0 0, 0 4, 5 1, 1 1, 1 3, 4 3, 4 2, 2 2)"
wkt = polyline.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
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-1 7-8
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)))" )
assert polygon.deleteVertex( 9 ), "Delete vertex 5 2 failed"
expwkt = "MULTIPOLYGON(((0 0,1 0,1 1,2 1,2 2,0 2,0 0)),((4 0,5 0,3 2,3 1,4 1,4 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 = "MULTIPOLYGON(((1 0,1 1,2 1,2 2,0 2,1 0)),((4 0,5 0,3 2,3 1,4 1,4 0)))"
wkt = polygon.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
assert polygon.deleteVertex( 6 ), "Delete vertex 6 failed"
expwkt = "MULTIPOLYGON(((1 0,1 1,2 1,2 2,0 2,1 0)),((5 0,3 2,3 1,4 1,5 0)))"
wkt = polygon.exportToWkt()
assert compareWkt( expwkt, wkt ), "Expected:\n%s\nGot:\n%s\n" % (expwkt, wkt )
开发者ID:idesign3000,项目名称:QGIS,代码行数:82,代码来源:test_qgsgeometry.py
示例19: testWriteShapefileWithZ
def testWriteShapefileWithZ(self):
"""Check writing geometries with Z dimension to an ESRI shapefile."""
#start by saving a memory layer and forcing z
ml = QgsVectorLayer(
('Point?crs=epsg:4326&field=id:int'),
'test',
'memory')
assert ml is not None, 'Provider not initialized'
assert ml.isValid(), 'Source layer not valid'
provider = ml.dataProvider()
assert provider is not None
ft = QgsFeature()
ft.setGeometry(QgsGeometry.fromWkt('PointZ (1 2 3)'))
ft.setAttributes([1])
res, features = provider.addFeatures([ft])
assert res
assert len(features) > 0
# check with both a standard PointZ and 25d style Point25D type
for t in [QgsWKBTypes.PointZ, QgsWKBTypes.Point25D]:
dest_file_name = os.path.join(str(QDir.tempPath()), 'point_{}.shp'.format(QgsWKBTypes.displayString(t)))
print(dest_file_name)
crs = QgsCoordinateReferenceSystem()
crs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId)
write_result = QgsVectorFileWriter.writeAsVectorFormat(
ml,
dest_fi
|
请发表评论