检查数据是否存在

要检查脚本中数据是否存在,请使用 Exists 函数。

Exists(数据集)

测试在执行期间当前工作空间中是否存在要素类、表、数据集、shapefile、工作空间、图层和其他文件。函数返回指示元素是否存在的布尔值。

Exists 函数

由于 Exists 函数能够识别目录路径,所以在检查地理数据是否存在时,必须使用该函数。目录路径是仅 ArcGIS 可识别的路径名称。例如:D:\Data\Final\Infrastructure.gdb\EastValley\powerlines 是指在文件地理数据库 InfrastructureEastValley 要素数据集中找到的 powerlines 要素类。就 Windows 操作系统而言,这不是有效的系统路径,因为 Infrastructure.gdb(文件夹)并不包含名为 Infrastructure 的文件。简言之,Windows 不了解要素数据集或要素类,因此用户不能使用像 os.path.exists 这样的 Python 存在函数。当然,在 ArcGIS 中,系统知道如何处理目录路径。也可以使用 UNC(通用命名约定)路径。

import arcpy
from arcpy import env

env.workspace = "d:/St_Johns/data.gdb"
fc = "roads"

# Clip a roads feature class if it exists
#
if arcpy.Exists(fc):
   arcpy.Clip_analysis(fc,"urban_area","urban_roads")
提示提示:

Exists 函数支持地理处理工作空间环境,允许您仅指定基本名称。

如果数据位于企业地理数据库中,则必须对名称进行完全限定。

import arcpy
from arcpy import env

env.workspace = "Database Connections/Bluestar.sde"
fc = "ORASPATIAL.Rivers"

# Confirm that the feature class exists
#
if arcpy.Exists(fc): 
    print "Verified %s exists" % fc

在脚本中,所有工具的默认行为是不覆盖任何已存在的输出。此行为可通过将 overwriteOutput 属性设置为 True (arcpy.env.overwriteOutput = True) 进行更改。在 overwriteOutput 为“假”时试图进行覆盖将会导致工具无法使用。

相关主题

5/10/2014