ImportToolbox

摘要

导入地理处理工具箱以用于 ArcPy,从而允许访问工具箱的关联工具。

注:

此函数相当于 AddToolbox 函数。

说明

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

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

地理处理服务也可以使用分号分隔符导入。

参数语法

input_file

"SOAP_URL;servicename;{username};{password}"

"SOAP_URL;servicename;UseSSOIdentityIfPortalOwned"

"SOAP_URL;servicename;token={token};{referer}"

"<full path to AGS connection file>;servicename"

URL 必须是 SOAP URL 才能允许 Python 访问您的服务。 在大多数情况下,URL 将为 https://machine.domain.com/webadaptor/services。 如果您的服务位于 ArcGIS Server 上的文件夹中,请使用 <folder name>/<service name> 格式,而不是下面指定的 servicename 格式。 如果地理处理服务是公共的,则不需要用户名和密码。

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

import arcpy
toolbox = "https://logistics.arcgis.com/arcgis/services;World/ServiceAreas;UseSSOIdentityIfPortalOwned"
arcpy.ImportToolbox(toolbox)
arcpy.ServiceAreas.GenerateServiceAreas()
注:

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

import arcpy
arcpy.SignInToPortal()
arcpy.ImportToolbox("https://domain.machine.com/webadaptor/services;ExtractLandUseData;UseSSOIdentityIfPortalOwned")
preprocess_result = arcpy.ExtractLandUseData.step1preprocess()
analyze_result = arcpy.ExtractLandUseData.step2analyze()

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

import arcpy
token = 'sadsa213d2j32jsdw02dm2'
referrer = 'https://www.arcgis.com/'
toolbox = 'https://logistics.arcgis.com/arcgis/services;' + \
      'World/ServiceAreas;token={};{}'.format(token, referrer)
arcpy.ImportToolbox(toolbox)
result = arcpy.ServiceAreas.GenerateServiceAreas()

ImportToolbox 还支持使用 ArcGIS Server 连接文件 (.ags) 处理安全的地理处理服务。 使用 .ags 文件可将凭据存储并隐藏在文件中。

import arcpy
arcpy.ImportToolbox('c:/logistics/logistics.ags;World/ServiceAreas')
arcpy.ServiceAreas.GenerateServiceAreas()

导入工具箱后,使用 IsSynchronous 函数确定地理处理服务是同步的还是异步。 如果是异步,请经常检查正在运行的作业的状态,直到在 ArcGIS Pro 中的 Python 窗口外访问地理处理服务时作业完成为止。 有关示例代码,请参阅 Result 类的第三个示例。

语法

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

The geoprocessing toolbox that will be accessed from ArcPy.

String
module_name

If the toolbox does not have an alias, the module_name value 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 run the correct tool, aliases are important when importing custom toolboxes. It is recommended that you 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__ 属性访问工具名称。

代码示例

ImportToolbox 示例 1

导入地理处理工具箱以在 ArcPy 中使用。

import arcpy

# Import custom toolbox
arcpy.ImportToolbox("c:/tools/My_Analysis_Tools.atbx")

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

导入公用工具箱。

import arcpy
tbx = 'https://machine.domain.com/arcgis/services/;Utilities/PrintingTools'
arcpy.ImportToolbox(tbx)
arcpy.PrintingTools.GetLayoutTemplatesInfo()

相关主题