列出MXD fi中图层的字段名

2024-06-23 02:39:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个ArcMap文件(.MXD),我想搜索它的层,然后选择一个层,让Python显示该层的属性表的字段名。在

到目前为止,Python(ArcPy)列出了mxd的层名称,但我无法找到如何获得字段名。在

在ArcMap中,我可以很容易地这样做:

fields = arcpy.ListFields(Layer)
for field in fields:
    print field.name

但是如何在ArcMap之外通过MXD文件来实现呢?我找了很多东西,什么也没有找到,所以我期待着你的帮助!谢谢!在


Tags: 文件in名称layerfieldfieldsfor属性
2条回答

好吧,我找到了一个好办法。我首先从MXD文件中获取所有层,然后将每个层的名称和源代码保存到字典中。然后,我将从GUI中选择我想要的层,并将其与字典中的层名称进行核对,然后我可以通过字典访问字段名:

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\MyMap.mxd") # loads my map
df = arcpy.mapping.ListDataFrames(mxd) # checks out the dataframes

layersources = {} # creates an empty dictionary

for d in df:
    layers = arcpy.mapping.ListLayers(mxd, "", d) # lists all available layers

    for lyr in layers:
        layersources[lyr.name] = lyr.dataSource # fills keys and values of the layers (names and sources) into the dictionary

selecteditem = "the wanted layer"  # this I choose from a GUI then, just defined it here as a variable for testing purposes

fields = arcpy.ListFields(layersources[selecteditem]) # creates a list with all the fields from that layer

for field in fields: # iterates through the list of fields
    print field.name # and prints them one by one :-)

通过arcpy.mapping.MapDocument方法访问mxd。然后获取名称并打开属性表

mxd = arcpy.mapping.MapDocument(r"path/Project.mxd")
for df in arcpy.mapping.ListLayers(mxd):
    print df.name

您可以使用arcpy并通过使用ListFileds方法运行python脚本来显示表文件

^{pr2}$

相关问题 更多 >