本文整理汇总了Python中qgis.core.QgsProject.auxiliaryStorage方法的典型用法代码示例。如果您正苦于以下问题:Python QgsProject.auxiliaryStorage方法的具体用法?Python QgsProject.auxiliaryStorage怎么用?Python QgsProject.auxiliaryStorage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qgis.core.QgsProject
的用法示例。
在下文中一共展示了QgsProject.auxiliaryStorage方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testQgdCreation
# 需要导入模块: from qgis.core import QgsProject [as 别名]
# 或者: from qgis.core.QgsProject import auxiliaryStorage [as 别名]
def testQgdCreation(self):
# New project
p = QgsProject()
self.assertTrue(p.auxiliaryStorage().isValid())
# Save the project
path = tmpPath()
qgs = path + '.qgs'
self.assertTrue(p.write(qgs))
self.assertTrue(os.path.exists(qgs))
# Auxiliary storage is empty so .qgd file should not be saved
qgd = path + '.qgd'
self.assertFalse(os.path.exists(qgd))
# Add a vector layer and an auxiliary layer in the project
vl = createLayer()
self.assertTrue(vl.isValid())
p.addMapLayers([vl])
pkf = vl.fields().field(vl.fields().indexOf('pk'))
al = p.auxiliaryStorage().createAuxiliaryLayer(pkf, vl)
self.assertTrue(al.isValid())
vl.setAuxiliaryLayer(al)
# Add an auxiliary field to have a non empty auxiliary storage
pdef = QgsPropertyDefinition('propname', QgsPropertyDefinition.DataTypeNumeric, '', '', 'ut')
self.assertTrue(al.addAuxiliaryField(pdef))
# Save the project
newpath = tmpPath()
qgs = newpath + '.qgs'
self.assertTrue(p.write(qgs))
self.assertTrue(os.path.exists(qgs))
# Auxiliary storage is NOT empty so .qgd file should be saved now
qgd = newpath + '.qgd'
self.assertTrue(os.path.exists(qgd))
示例2: testProjectStorage
# 需要导入模块: from qgis.core import QgsProject [as 别名]
# 或者: from qgis.core.QgsProject import auxiliaryStorage [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'")
#.........这里部分代码省略.........