AddToolbox

摘要

将指定的工具箱导入到 ArcPy 中,以便访问工具箱中的相关工具。

注:

此函数相当于 ImportToolbox 函数。

说明

默认情况下,可在脚本中访问任何核心 ArcGIS 工具箱,但对于自定义工具箱或第三方工具箱,必须使用 ImportToolbox 来添加它们以便在脚本中使用。

可在多个文件夹或地理数据库中找到其他工具箱,这些工具箱的来源可能各不相同;它们可能是您个人创建的工具箱,或者是您所在组织内部创建的工具箱,也可能是从 ArcGIS Online 或其他站点下载的工具箱。 在任何情况下,将这些工具箱用作 Python 中的工具之前,需要将其一步导入到 ArcPy 中。

还可使用分号分隔符添加服务器工具箱。

服务器语法

Internet ArcGIS Server

URL;服务名称;{用户名};{密码}

AddToolbox 还支持通过使用 ArcGIS Server 连接 (.ags) 文件来使用受保护的 ArcGIS Online 服务。 使用 .ags 文件可将凭据存储并隐藏在文件中。

要允许任何 arcgis.com 用户访问您的工具,请使用 UseSSOIdentityIfPortalOwned 关键字。 如未登录,执行 AddToolbox 时,系统将提示登录对话框;如已登录,可成功执行此操作。

import arcpy
tbx = "http://logistics.arcgis.com/arcgis/services;World/ServiceAreas;UseSSOIdentityIfPortalOwned"
arcpy.AddToolbox(tbx)
arcpy.GenerateServiceAreas_ServiceAreas()
注:

将使用登录对话框运行独立 Python 文件。

如果先前已获取令牌,则该令牌也可与 AddToolbox 组合使用。

import arcpy

token = 'sadsa213d2j32jsdw02dm2'
referrer = 'http://www.arcgis.com/'
tbx = 'http://logistics.arcgis.com/arcgis/services;' + \
      'World/ServiceAreas;token={};{}'.format(token, referrer)
arcpy.ImportToolbox(tbx)
result = arcpy.GenerateServiceAreas_ServiceAreas()

语法

AddToolbox (input_file, {module_name})
参数说明数据类型
input_file

The geoprocessing toolbox to be accessed from ArcPy.

String
module_name

If the toolbox does not have an alias, the module_name is required.

When a tool is accessed through the ArcPy site package, the toolbox alias where the tool is contained is a required suffix (arcpy.<toolname>_<alias> or arcpy.<alias>.<toolname>). Since ArcPy depends on toolbox aliases to access and execute the correct tool, aliases are extremely important when importing custom toolboxes. A good practice is to always define a custom toolbox's alias; however, if the toolbox alias is not defined, a temporary alias can be set as the second parameter.

String
返回值
数据类型说明
Module

返回导入的模块。

如果需要,可从模块的 __all__ 属性访问工具名称。

代码示例

AddToolbox 示例

添加指定工具箱。

import arcpy

# Import custom toolbox
arcpy.AddToolbox("c:/tools/My_Analysis_Tools.tbx")

try:
    # Run tool in the custom toolbox.  The tool is identified by the tool name
    # and the toolbox alias.
    arcpy.GetPoints_myanalysis("c:/data/forest.shp")
except arcpy.ExecuteError:
    print(arcpy.GetMessages(2))

相关主题