描述数据
地理处理工具可处理所有类型的数据,如地理数据库要素类、shapefile、栅格、表、拓扑和网络。每种数据都具有可访问的特定属性,以进一步用于控制脚本流或用作工具参数。例如,相交工具的输出要素类型取决于要相交的数据的形状类型 - 点、线或面。在脚本中对输入数据集列表运行相交工具时,必须能够确定输入数据集的形状类型,以便设置正确的输出形状类型。可以使用 Describe 函数确定所有输入数据集的形状类型。
使用 Describe 函数可确定数据集的属性,为下一步操作做好准备。例如,在以下示例中,脚本使用 Describe 来估计输入数据的形状类型(折线、面、点等等)并确定合适的地理处理工具。
import arcpy
inFC = arcpy.GetParameterAsText(0)
outFC = arcpy.GetParameterAsText(1)
# Describe a feature class
#
desc = arcpy.Describe(inFC)
# Get the shape type (Polygon, Polyline) of the feature class
#
type = desc.shapeType
# If the shapeType is Polygon convert the data to polylines
# using the FeatureToLine tool, otherwise just copy the
# data using the CopyFeatures tool.
#
if type == "Polygon":
arcpy.FeatureToLine_management(inFC, outFC)
else:
arcpy.CopyFeatures_management(inFC, outFC)
Describe 函数返回的 Describe 对象包含多个属性,如数据类型、字段、索引以及许多其他属性。该对象的属性是动态的,这意味着根据所描述的数据类型,会有不同的描述属性可供使用。
Describe 属性被组织成一系列属性组。任何特定数据集都将至少获取其中一个组的属性。例如,如果要描述一个地理数据库要素类,您可访问 GDB 要素类、要素类、表和数据集属性组中的属性。所有数据,不管是哪种数据类型,总会获取通用的 Describe 对象属性。
处理属性集
有些属性是一个属性集的成员。例如,coverage 的容差或工作空间的连接属性将以属性集的形式返回。属性集中包含已命名的属性,您可从属性集中调用这些属性。在下例中,coverage 的容差(Fuzzy、Dangle、TicMatch、Edit、NodeSnap、Weed、Grain 和 Snap)被打印为标准输出:
import arcpy
# Create a describe object from a coverage feature class
#
desc = arcpy.Describe("D:/St_Johns/covs/freshwater")
# Create a property set of coverage tolerances
#
covTols = desc.tolerances
# Print each coverage tolerance
#
print covTols.fuzzy
print covTols.dangle
print covTols.ticMatch
print covTols.edit
print covTols.nodeSnap
print covTols.weed
print covTols.grain
print covTols.snap
通常会在被描述的对象属性发生变化时使用属性集。企业级地理数据库工作空间的连接属性(服务器、实例、数据库、用户和版本)会根据所使用的 ArcSDE 数据库类型的不同而有所不同,因此比较适用于不包含预定义值集的属性集。