本文整理匯總了Python中qgis.core.QgsAuxiliaryLayer.propertyDefinitionFromField方法的典型用法代碼示例。如果您正苦於以下問題:Python QgsAuxiliaryLayer.propertyDefinitionFromField方法的具體用法?Python QgsAuxiliaryLayer.propertyDefinitionFromField怎麽用?Python QgsAuxiliaryLayer.propertyDefinitionFromField使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類qgis.core.QgsAuxiliaryLayer
的用法示例。
在下文中一共展示了QgsAuxiliaryLayer.propertyDefinitionFromField方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: testProjectStorage
# 需要導入模塊: from qgis.core import QgsAuxiliaryLayer [as 別名]
# 或者: from qgis.core.QgsAuxiliaryLayer import propertyDefinitionFromField [as 別名]
def testProjectStorage(self):
# New project without fileName
p0 = QgsProject()
self.assertTrue(p0.auxiliaryStorage().isValid())
# Create new layers with key otherwise auxiliary layers are not
# automacially created when added in project
vl0 = createLayer()
vl0Shp = writeShape(vl0, 'vl0.shp')
vl1 = createLayer()
vl1Shp = writeShape(vl1, 'vl1.shp')
vl0 = QgsVectorLayer(vl0Shp, 'points', 'ogr')
self.assertTrue(vl0.isValid())
vl1 = QgsVectorLayer(vl1Shp, 'points', 'ogr')
self.assertTrue(vl1.isValid())
# Add layers to project and check underlying auxiliary layers
p0.addMapLayers([vl0, vl1])
self.assertTrue(vl0.loadAuxiliaryLayer(p0.auxiliaryStorage(), 'pk'))
self.assertTrue(vl1.loadAuxiliaryLayer(p0.auxiliaryStorage(), 'num_char'))
al0 = vl0.auxiliaryLayer()
al1 = vl1.auxiliaryLayer()
self.assertEqual(al0.joinInfo().targetFieldName(), 'pk')
self.assertEqual(al1.joinInfo().targetFieldName(), 'num_char')
# Add a field in auxiliary layers
pdef0 = QgsPropertyDefinition('propname', QgsPropertyDefinition.DataTypeNumeric, '', '', 'ut')
self.assertTrue(al0.addAuxiliaryField(pdef0))
pdef1 = QgsPropertyDefinition('propname1', QgsPropertyDefinition.DataTypeString, '', '', 'ut')
self.assertTrue(al1.addAuxiliaryField(pdef1))
# Check auxiliary fields names
af0Name = QgsAuxiliaryLayer.nameFromProperty(pdef0, False)
self.assertEqual(af0Name, 'ut_propname')
af1Name = QgsAuxiliaryLayer.nameFromProperty(pdef1, False)
self.assertEqual(af1Name, 'ut_propname1')
# Set value for auxiliary fields
req = QgsFeatureRequest().setFilterExpression("name = 'Honey'")
f = QgsFeature()
vl0.getFeatures(req).nextFeature(f)
self.assertTrue(f.isValid())
af0Name = QgsAuxiliaryLayer.nameFromProperty(pdef0, True)
index0 = vl0.fields().indexOf(af0Name)
vl0.changeAttributeValue(f.id(), index0, 333)
req = QgsFeatureRequest().setFilterExpression("name = 'Apple'")
f = QgsFeature()
vl1.getFeatures(req).nextFeature(f)
self.assertTrue(f.isValid())
af1Name = QgsAuxiliaryLayer.nameFromProperty(pdef1, True)
index1 = vl1.fields().indexOf(af1Name)
vl1.changeAttributeValue(f.id(), index0, 'myvalue')
req = QgsFeatureRequest().setFilterExpression("name = 'Orange'")
f = QgsFeature()
vl1.getFeatures(req).nextFeature(f)
self.assertTrue(f.isValid())
vl1.changeAttributeValue(f.id(), index0, 'myvalue1')
# Save the project in a zip file
f = tmpPath() + '.qgz'
p0.write(f)
# Open the zip file with embedded auxiliary storage
p1 = QgsProject()
p1.read(f)
# Check that auxiliary fields are well loaded in layers
self.assertEqual(len(p1.mapLayers().values()), 2)
for vl in p1.mapLayers().values():
al = vl.auxiliaryLayer()
self.assertEqual(len(al.auxiliaryFields()), 1)
af = al.auxiliaryFields()[0]
afPropDef = QgsAuxiliaryLayer.propertyDefinitionFromField(af)
self.assertEqual(afPropDef.origin(), 'ut')
if vl.auxiliaryLayer().joinInfo().targetFieldName() == 'pk':
self.assertEqual(afPropDef.name(), 'propname')
self.assertEqual(al.featureCount(), 1)
req = QgsFeatureRequest().setFilterExpression("name = 'Honey'")
f = QgsFeature()
vl.getFeatures(req).nextFeature(f)
self.assertTrue(f.isValid())
self.assertEqual(f.attributes()[index0], 333.0)
else: # num_char
self.assertEqual(al.featureCount(), 2)
self.assertEqual(afPropDef.name(), 'propname1')
req = QgsFeatureRequest().setFilterExpression("name = 'Apple'")
#.........這裏部分代碼省略.........