使用arcpy,如何检测文件GDB中的关系类(或者不能)?


10

我希望能够检测文件GDB中的关系类。因为我的脚本用户可能只有ArcView级别的许可证,所以他们将无法操纵具有关系类的工作空间中的要素类(特别是添加字段)的架构。 如何检测关系类的存在,以便可以记录它们,以编程方式避免它们并允许脚本继续?

Answers:


6

relationshipClassNames属性应该可以执行此操作,但它似乎对我不起作用(在文件地理数据库中进行了测试,在两个要素类之间创建了关系类,检查了该属性,两个返回的列表均为空)。也许它将为您工作。


谢谢。我一开始很想念关系课程,但您却给了我线索。我将检查RelationshipClassNames属性,并让您知道它是如何工作的(或不起作用)。
celticflute 2011年

3

按照@ blah238的建议,此python代码列出了地理数据库中的所有关系类,并将它们放在唯一列表中(relClasses):

inGDB = r"D:\mygeodatabase.gdb"
env.workspace = inGDB
#################Getting all Tables and Feature Classes###########
fcs = []
#root of workspace
for item in arcpy.ListFeatureClasses("*"):    fcs.append(item)
for item in arcpy.ListTables("*"):    fcs.append(item)

fds = arcpy.ListDatasets("*","Feature")
for fd in fds:
    env.workspace = inGDB +'\\'+fd
    for fc in arcpy.ListFeatureClasses("*"):
        fcs.append(fd+'/'+fc)
    for tb in arcpy.ListTables("*"):
        fcs.append(fd+'/'+tb)

env.workspace = inGDB
relClasses = set()
for i,fc in enumerate(fcs): 
    desc = arcpy.Describe(fc)
    for j,rel in enumerate(desc.relationshipClassNames):
        relDesc = arcpy.Describe(rel)
        if relDesc.isAttachmentRelationship:
            continue
        relClasses.add(rel)

print relClasses

1

我为此苦苦挣扎,直到意识到arcpy可以通过与之关联的表看到关系类。这是一段代码,用于检查长度大于30的关系类名称:

arcpy.env.workspace = 'C:/workspace'

# Local variables
tables = arcpy.ListTables()

# Iterate through tables in file geodatabase (workspace)
for t in tables:
    # Get relationship class(es) associated with table
    desc = arcpy.Describe(t)
    rcs = desc.relationshipClassNames
# Iterate through any relationship classes associated with current table in loop
    for r in rcs:
        if len(r) > 30:
            print 'Relationship class ' + r + ' has ' + str(len(r)) + ' characters.'

0

我现在是10.5.1,看起来RelationshipClassNames给了我应该像的关系类名称列表

layer = "C:\\Geodatabases\\somegeodatabase.gdb\\my_layer"
desc = arcpy.Describe(layer)
print desc.relationshipClassNames
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.