列出数据
批处理脚本中的首要任务之一是为可用数据编写目录,以便在处理过程中可以遍历数据。ArcPy 具有多个专为创建此类列表而构建的函数。
|
ListFields(dataset, wild_card, field_type) |
返回在输入值中找到的字段的列表 |
|
ListIndexes(dataset, wild_card) |
返回在输入值中找到的属性索引的列表 |
|
ListDatasets(wild_card, feature_type) |
返回当前工作空间中的数据集 |
|
ListFeatureClasses(wild_card, feature_type) |
返回当前工作空间中的要素类 |
|
ListFiles(wild_card) |
返回当前工作空间中的文件 |
|
ListRasters(wild_card, raster_type) |
返回在当前工作空间中找到的栅格数据的列表 |
|
ListTables(wild_card, table_type) |
返回在当前工作空间中找到的表的列表 |
|
ListWorkspaces(wild_card, workspace_type) |
返回在当前工作空间中找到的工作空间的列表 |
|
ListVersions(sde_workspace) |
返回已连接用户有权使用的版本的列表 |
这些函数中每个函数的结果都是一个 Python 列表,该列表为值列表。脚本中的列表可以包含任何类型的数据,如字符串(例如,可以是数据集的路径、字段或表中的行)。创建完具有所需值的列表后,可在脚本中循环该列表以处理各个值。
列表函数参数
这些函数的参数是相似的。一些函数(例如 ListFields)需要输入数据集值,因为函数列出的项目驻留在特定的对象或数据集中。其他函数则不需要输入数据集值,因为它们在当前工作空间中列出的数据类型已在环境设置中定义。所有函数都具有一个通配符参数,用于限制按名称列出的对象或数据集。一个通配符定义一个名称过滤器,新创建的列表中的所有内容都必须通过该过滤器。例如,您可能想要列出工作空间中所有以字母 G 开头的要素类。下例说明了如何进行此操作:
import arcpy
from arcpy import env
# Set the workspace. List all of the feature classes that start with 'G'
#
env.workspace = "D:/St_Johns/data.gdb"
fcs = arcpy.ListFeatureClasses("G*")
还可将列表限制为匹配特定的数据属性,例如仅匹配多边形要素类、整型字段或 coverage 数据集。这就是所有函数中“Type”参数所起的作用。在下一示例中,使用一个通配符和一个数据类型对工作空间中的要素类进行过滤,这样只有以字母 G 开头的多边形要素类会出现在结果列表中:
# Set the workspace. List all of the polygon feature classes that
# start with 'G'
#
env.workspace = "D:/St_Johns/data.gdb"
fcs = arcpy.ListFeatureClasses("G*", "polygon")
使用列表
ArcPy 使用 Python 列表类型作为其全部列表函数结果的返回类型,因为列表支持简单数据访问所需的灵活性和多种数据类型。For 循环非常适用于处理 Python 列表,因为使用它可以一次一个项目的方式浏览列表。For 循环可遍历列表中的每个项目。下面的示例为使用 For 循环遍历前一个示例中生成的列表:
# For each feature class in the list of feature classes
#
for fc in fcs:
# Copy the features from the workspace to a folder
#
arcpy.CopyFeatures_management(fc, "D:/St_Johns/Shapefiles/” + fc)
下面是另一个如何使用“列表”函数的示例。该脚本用于为文件夹内形式为标记图像文件格式 (TIFF) 图像的所有栅格创建栅格金字塔。
# Set the workspace. List all of the TIFF files
#
env.workspace= "D:/St_Johns/images"
# For each raster in the list of rasters
#
for tiff in arcpy.ListRasters("*", "TIF"):
# Create pyramids
#
arcpy.BuildPyramids_management(tiff)
通过 Python 列表可以多种方式使用和管理列表函数的结果。列表是一种用途广泛的 Python 类型,提供了大量可用于处理和提取信息的方法(append、count、extend、index、insert、pop、remove、reverse、sort)。
例如,如果想知道某个工作空间中有多少要素类,可以使用 Python 内置的 len 函数来获取该数值。
import arcpy from arcpy import env env.workspace = "c:/St_Johns/Shapefiles" fcs = arcpy.ListFeatureClasses() # Use Python's built-in function len to reveal the number of feature classes # in the workspace # fcCount = len(fcs) print fcCount
提示:Python 列表可以轻易地显示其内容。可使用多种方法来操作 Python 列表,包括 sort、append 和 reverse。
>>> import arcpy >>> from arcpy import env >>> env.workspace = "c:/data/water.gdb" >>> fcs = arcpy.ListFeatureClasses() >>> print fcs [u'water_pipes', u'water_services', u'water_stations'] >>> fcs.sort(reverse=True) >>> print fcs [u'water_stations', u'water_services', u'water_pipes']
由于列表是有序集合,因此还允许进行索引和切片。
>>> print fcs[0] water_stations >>> print fcs[1:] [u'water_services', u'water_pipes']
列表函数类型关键字
所有列表函数的默认行为是列出所有支持的类型。可使用关键字将返回列表限制为特定的类型。下表列出了各个函数的类型关键字。
|
功能 |
类型关键字 |
|---|---|
|
ListDatasets |
All、Feature、Coverage、RasterCatalog、CAD、VPF、TIN、Topology |
|
ListFeatureClasses |
All、Point、Label、Node、Line、Arc、Route、Polygon、Region |
|
ListFields |
All、SmallInteger、Integer、Single、Double、String、Date、OID、Geometry、BLOB |
|
ListTables |
All、dBASE、INFO |
|
ListRasters |
All、ADRG、BIL、BIP、BSQ、BMP、CADRG、CIB、ERS、GIF、GIS、GRID、STACK、IMG、JPEG、LAN、SID、SDE、TIFF、RAW、PNG、NITF |
|
ListWorkspaces |
All、Coverage、Access、SDE、Folder |
示例:使用列表函数从个人地理数据库迁移到文件地理数据库
一个常见的转换任务是从一种格式到另一种格式的大规模数据转换。下面的脚本利用 ListWorkspaces、ListFeatureClasses、ListTables 和 ListDatasets 来标识文件夹中的所有个人地理数据库,并将每个数据库的要素数据集、要素类和表转换为文件地理数据库。
import arcpy
from arcpy import env
import os
# Allow for the overwriting of file geodatabases, if they already exist
#
env.overwriteOutput = True
# Set workspace to folder containing personal geodatabases
#
env.workspace = arcpy.GetParameterAsText(0)
# Identify personal geodatabases
#
for pgdb in arcpy.ListWorkspaces("", "Access"):
# Set workspace to current personal geodatabase
#
env.workspace = pgdb
# Create file geodatabase based on personal geodatabase
#
fgdb = pgdb[:-4] + ".gdb"
arcpy.CreateFileGDB_management(os.path.dirname(fgdb), os.path.basename(fgdb))
# Identify feature classes and copy to file gdb
#
for fc in arcpy.ListFeatureClasses():
print "Copying feature class " + fc + " to " + fgdb
arcpy.Copy_management(fc, fgdb + os.sep + fc)
# Identify tables and copy to file gdb
#
for table in arcpy.ListTables():
print "Copying table " + table + " to " + fgdb
arcpy.Copy_management(table, fgdb + os.sep + table)
# Identify datasets and copy to file gdb
# Copy will include contents of datasets
#
for dataset in arcpy.ListDatasets():
print "Copying dataset " + dataset + " to " + fgdb
arcpy.Copy_management(dataset, fgdb + os.sep + dataset)