本文整理汇总了Python中qgis.core.QgsRelation.referencingFields方法的典型用法代码示例。如果您正苦于以下问题:Python QgsRelation.referencingFields方法的具体用法?Python QgsRelation.referencingFields怎么用?Python QgsRelation.referencingFields使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qgis.core.QgsRelation
的用法示例。
在下文中一共展示了QgsRelation.referencingFields方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: import_in_qgis
# 需要导入模块: from qgis.core import QgsRelation [as 别名]
# 或者: from qgis.core.QgsRelation import referencingFields [as 别名]
#.........这里部分代码省略.........
relations_1_1.append(rel)
# add 1:N relations
relations_1_n = []
sql = """
select
layer_name, r.parent_pkid, field_related_layer as child_layer, r.child_pkid
from
{0}_ogr_fields_metadata f
join {0}_ogr_layer_relationships r
on r.parent_layer = f.layer_name
and r.child_layer = f.field_related_layer
where
field_category in ('PATH_TO_CHILD_ELEMENT_WITH_LINK', 'PATH_TO_CHILD_ELEMENT_NO_LINK')
and field_max_occurs>1
-- junctions - 1st way
union all
select
layer_name, r.parent_pkid, field_junction_layer as child_layer, 'parent_pkid' as child_pkid
from
{0}_ogr_fields_metadata f
join {0}_ogr_layer_relationships r
on r.parent_layer = f.layer_name
and r.child_layer = f.field_related_layer
where
field_category = 'PATH_TO_CHILD_ELEMENT_WITH_JUNCTION_TABLE'
-- junctions - 2nd way
union all
select
field_related_layer as layer_name, r.child_pkid, field_junction_layer as child_layer, 'child_pkid' as child_pkid
from
{0}_ogr_fields_metadata f
join {0}_ogr_layer_relationships r
on r.parent_layer = f.layer_name
and r.child_layer = f.field_related_layer
where
field_category = 'PATH_TO_CHILD_ELEMENT_WITH_JUNCTION_TABLE'
""".format(schema_s)
l = ds.ExecuteSQL(sql)
if l is not None:
for f in l:
parent_layer = f.GetField("layer_name")
child_layer = f.GetField("child_layer")
if parent_layer not in layers or child_layer not in layers:
continue
rel = QgsRelation()
rel.setId('1_n_' + f.GetField('layer_name') + '_' + f.GetField('child_layer') + '_' + f.GetField('parent_pkid') + '_' + f.GetField('child_pkid'))
rel.setName(f.GetField('child_layer'))
# parent layer
rel.setReferencedLayer(layers[parent_layer]['layer_id'])
# child layer
rel.setReferencingLayer(layers[child_layer]['layer_id'])
# parent, child
rel.addFieldPair(f.GetField('child_pkid'), f.GetField('parent_pkid'))
#rel.addFieldPair(f.GetField('child_pkid'), 'ogc_fid')
if rel.isValid():
relations_1_n.append(rel)
# add relation to layer
layers[f.GetField('layer_name')]['1_n'].append(rel)
for rel in relations_1_1 + relations_1_n:
QgsProject.instance().relationManager().addRelation(rel)
# add "show form" option to 1:1 relations
for rel in relations_1_1:
l = rel.referencingLayer()
idx = rel.referencingFields()[0]
s = QgsEditorWidgetSetup("RelationReference", {'AllowNULL': False,
'ReadOnly': True,
'Relation': rel.id(),
'OrderByValue': False,
'MapIdentification': False,
'AllowAddFeatures': False,
'ShowForm': True})
l.setEditorWidgetSetup(idx, s)
# setup form for layers
for layer, lyr in layers.items():
l = lyr['layer']
fc = l.editFormConfig()
fc.clearTabs()
fc.setLayout(QgsEditFormConfig.TabLayout)
# Add fields
c = QgsAttributeEditorContainer("Main", fc.invisibleRootContainer())
c.setIsGroupBox(False) # a tab
for idx, f in enumerate(l.fields()):
c.addChildElement(QgsAttributeEditorField(f.name(), idx, c))
fc.addTab(c)
# Add 1:N relations
c_1_n = QgsAttributeEditorContainer("1:N links", fc.invisibleRootContainer())
c_1_n.setIsGroupBox(False) # a tab
fc.addTab(c_1_n)
for rel in lyr['1_n']:
c_1_n.addChildElement(QgsAttributeEditorRelation(rel.name(), rel, c_1_n))
l.setEditFormConfig(fc)
install_viewer_on_feature_form(l)