本文整理汇总了Python中qgis.core.QgsVectorLayerUtils.duplicateFeature方法的典型用法代码示例。如果您正苦于以下问题:Python QgsVectorLayerUtils.duplicateFeature方法的具体用法?Python QgsVectorLayerUtils.duplicateFeature怎么用?Python QgsVectorLayerUtils.duplicateFeature使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qgis.core.QgsVectorLayerUtils
的用法示例。
在下文中一共展示了QgsVectorLayerUtils.duplicateFeature方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testDuplicateFeature
# 需要导入模块: from qgis.core import QgsVectorLayerUtils [as 别名]
# 或者: from qgis.core.QgsVectorLayerUtils import duplicateFeature [as 别名]
#.........这里部分代码省略.........
# - create the relation
rel = QgsRelation()
rel.setId('rel1')
rel.setName('childrel')
rel.setReferencingLayer(layer2.id())
rel.setReferencedLayer(layer1.id())
rel.addFieldPair('foreign_key', 'pkid')
rel.setStrength(QgsRelation.Composition)
# > check relation
self.assertTrue(rel.isValid())
# - add relation
relMgr.addRelation(rel)
# > check if referencedLayer is layer1
self.assertEqual(rel.referencedLayer(), layer1)
# > check if referencingLayer is layer2
self.assertEqual(rel.referencingLayer(), layer2)
# > check if the layers are correct in relation when loading from relationManager
relations = project.relationManager().relations()
relation = relations[list(relations.keys())[0]]
# > check if referencedLayer is layer1
self.assertEqual(relation.referencedLayer(), layer1)
# > check if referencingLayer is layer2
self.assertEqual(relation.referencingLayer(), layer2)
# > check the relatedfeatures
'''
# testoutput 1
print( "\nAll Features and relations")
featit=layer1.getFeatures()
f=QgsFeature()
while featit.nextFeature(f):
print( f.attributes())
childFeature = QgsFeature()
relfeatit=rel.getRelatedFeatures(f)
while relfeatit.nextFeature(childFeature):
print( childFeature.attributes() )
print( "\n--------------------------")
print( "\nFeatures on layer1")
for f in layer1.getFeatures():
print( f.attributes() )
print( "\nFeatures on layer2")
for f in layer2.getFeatures():
print( f.attributes() )
'''
# DUPLICATION
# - duplicate feature l1f1orig with children
layer1.startEditing()
results = QgsVectorLayerUtils.duplicateFeature(layer1, l1f1orig, project, 0)
# > check if name is name of duplicated (pk is different)
result_feature = results[0]
self.assertEqual(result_feature.attribute('fldtxt'), l1f1orig.attribute('fldtxt'))
# > check duplicated child layer
result_layer = results[1].layers()[0]
self.assertEqual(result_layer, layer2)
# > check duplicated child features
self.assertTrue(results[1].duplicatedFeatures(result_layer))
'''
# testoutput 2
print( "\nFeatures on layer1 (after duplication)")
for f in layer1.getFeatures():
print( f.attributes() )
print( "\nFeatures on layer2 (after duplication)")
for f in layer2.getFeatures():
print( f.attributes() )
print( "\nAll Features and relations")
featit=layer1.getFeatures()
f=QgsFeature()
while featit.nextFeature(f):
print( f.attributes())
childFeature = QgsFeature()
relfeatit=rel.getRelatedFeatures(f)
while relfeatit.nextFeature(childFeature):
print( childFeature.attributes() )
'''
# > compare text of parent feature
self.assertEqual(result_feature.attribute('fldtxt'), l1f1orig.attribute('fldtxt'))
# - create copyValueList
childFeature = QgsFeature()
relfeatit = rel.getRelatedFeatures(result_feature)
copyValueList = []
while relfeatit.nextFeature(childFeature):
copyValueList.append(childFeature.attribute('fldtxt'))
# - create origValueList
childFeature = QgsFeature()
relfeatit = rel.getRelatedFeatures(l1f1orig)
origValueList = []
while relfeatit.nextFeature(childFeature):
origValueList.append(childFeature.attribute('fldtxt'))
# - check if the ids are still the same
self.assertEqual(copyValueList, origValueList)