需求:
如何遍历mxd(工程文档)中图层(layer)的字段列表以及获取字段的名称、类型和字段长度等信息
脚本如下:
# -*- coding:utf-8 -*-
import arcpy
import arcpy.mapping as mapping
mxd = mapping.MapDocument(r"D:\data\listfiled\test.mxd")
layers = mapping.ListLayers(mxd)
for lyr in layers:
print(lyr.name)
descFC=arcpy.Describe(lyr)
flds = descFC.fields
for fld in flds:
print "Field: " + fld.name
print "Type: " + fld.type
print "Length: " + str(fld.length)
结果:
D:\\app\\desktop108\\Python27\\ArcGIS10.8\\python.exe D:/java/pycharmExample/ArcpyExample/ArcMapPython32/Listfields.py
AAAAA风景名胜区
Field: OBJECTID
Type: OID
Length: 4
Field: Shape
Type: Geometry
Length: 0
Field: Name
Type: String
Length: 320
Field: 游客人数
Type: Integer
Length: 4
Field: 旅游收益
Type: Integer
Length: 4
北京
Field: OBJECTID
Type: OID
Length: 4
Field: SHAPE
Type: Geometry
Length: 0
Field: PAC
Type: Integer
Length: 4
Field: NAME
Type: String
Length: 60
Field: 地区生产总值_2017
Type: Double
Length: 8
Field: 人口_2017
Type: Single
Length: 4
Field: 人口_2010
Type: Single
Length: 4
Field: SHAPE_Length
Type: Double
Length: 8
Field: SHAPE_Area
Type: Double
Length: 8
Field: height
Type: SmallInteger
Length: 2
Field: group1
Type: SmallInteger
Length: 2
Field: pop
Type: Double
Length: 8
Field: test
Type: Double
Length: 8
水系面
Field: OBJECTID
Type: OID
Length: 4
Field: Shape
Type: Geometry
Length: 0
Field: GB
Type: Integer
Length: 4
Field: HYDC
Type: String
Length: 8
Field: NAME
Type: String
Length: 60
Field: PERIOD
Type: String
Length: 20
Field: VOL
Type: String
Length: 2
Field: Shape_Length
Type: Double
Length: 8
Field: Shape_Area
Type: Double
Length: 8
Process finished with exit code 0
参考资料:
https://www.jianshu.com/p/77e114fbdaaa