^ 关注我,带你一起学GIS ^
前言
❝
Python 脚本使自动化 ArcGIS Pro 中的工作流成为可能。Python 脚本可用于自动化 ArcGIS Pro 中的任务。可以将自己创建的脚本作为 Python .py 文件与其他人共享。 但是,使用其他人编写的脚本需要掌握一些 Python 知识,即使只是更改要处理的数据集的名称。为了以更易于使用的形式共享 Python 脚本,可以创建 Python 脚本工具。
本教程来源于ESRI官方示例如何创建 Python 脚本工具。
文中以ArcGIS Pro3.5为例,默认你已经具备了Python的基础知识。本篇教程在之前一系列文章的基础上讲解。
如果你还没有看过,建议从以上内容开始。
1. 开发环境
本文使用如下开发环境,以供参考。
时间:2026年
系统:Windows 11
ArcGIS Pro:3.5
Python:3.11.11
2. 数据准备
俗话说巧妇难为无米之炊,数据就是软件开发的基石,没有数据,再美好的设想都是空中楼阁。因此,第一步需要下载GIS数据。
别急,GIS之路公众号都给你准备好了
在公众号后台回复关键字:vector,获取数据下载链接。
而本文示例数据来源于ESRI官方教程,在此感谢ESRI相关工作人员的辛勤付出与免费共享。
数据下载地址:https://learn.arcgis.com/zh-cn/projects/create-a-python-script-tool下载数据解压完成,将工程添加到地图,可在ArcGIS Pro目录窗口查看图层数据,存储在Transportation.gdb数据库中。
数据集是 GIS 工程的典型内容,包含不同格式的要素类和表格数据,以及组织这些数据的其他元素。 您将使用 Python 代码基于其类型和其他属性识别这些数据集。 请注意,这些分组意味着工程数据具有多级别嵌套结构。
对于ArcGIS Pro底图失效的同学,可查看以下文章进行解决。
3. 编辑执行代码
Python 脚本工具的执行代码是,当用户在地理处理窗格中,单击运行时运行的 Python 代码。 您将使用之前查看的独立脚本,但需要对其进行一些更改,以便脚本能够接收用户在工具窗格中输入的参数。
右键单击 Clip and ZIP 工具,然后单击属性。
再单击执行选项卡。该选项卡显示脚本工具的 Python 代码模板。
接下来,您需要将函数 script_tool() 的名称修改为更有意义的名称。 首先,您将直接在工具属性对话框中编辑嵌入式代码。
在执行选项卡的代码框中,将 def script_tool(param0, param1): 一行编辑为 def ClipZip(param0, param1):在此处对其进行更改:
在代码框中,将 script_tool(param0, param1) 一行编辑为 ClipZip(param0, param1)
除了在代码编辑框中修改代码之外,也可以打开外部代码编辑器。
显示效果如下
在 IDLE 中,在 import arcpy 下方添加新代码行,并添加以下代码:
import os
import zipfile
编辑器中显示如下。
在 IDLE 中,将 if name == "main": 行之后,以 param0 和 param1 开头的两个参数行替换为以下代码:
inputs = arcpy.GetParameter(0)
clip_fc = arcpy.GetParameterAsText(1)
gdb = arcpy.GetParameterAsText(2)
out_zip = arcpy.GetParameterAsText(3)
编辑 ClipZip(param0, param1) 行,将 param0, param1 替换为包含参数值的变量:
ClipZip(inputs, clip_fc, gdb, out_zip)
编辑 def ClipZip(param0, param1): 行,将 param0, param1 替换为包含参数值的变量:
def ClipZip(inputs, clip_fc, gdb, out_zip):
粘贴原脚本代码,调整格式。
# Workspace and datasets
arcpy.env.workspace = "C:/Tutorials/PythonTool/DC.gdb" # Geodatabase where feature classes to be clipped reside
clip_fc = "C:/Tutorials/PythonTool/neighborhood.shp" # Full path of clip polygons
gdb = "C:/Tutorials/PythonTool/Clip.gdb" # Full path of output geodatabase
gdb_path, new_gdb = os.path.split(gdb) # Split full path for output gdb into folder and gdb name
out_zip = "C:/Tutorials/PythonTool/Clip.zip" # Full path of the output ZIP archive
arcpy.env.overwriteOutput = True
# Create a new geodatabase to store the result
arcpy.CreateFileGDB_management(gdb_path, new_gdb)
print(f"Output geodatabase {gdb} created")
# Clip each input feature class
inputs = arcpy.ListFeatureClasses()
print(inputs)
for fc in inputs:
fc_name = arcpy.da.Describe(fc)["baseName"] # Remove folder and any file extensions from full path
new_fc = os.path.join(gdb, fc_name) # Create full path for output feature class
arcpy.analysis.Clip(fc, clip_fc, new_fc)
print(f"Output feature class {new_fc} created")
# Create a ZIP file for the new geodatabase
with zipfile.ZipFile(out_zip, "w") as myzip:
for f in os.listdir(gdb): # Iterate over all the files in the geodatabase folder
if not f.endswith(".lock"): # Skips any files with .lock extension
file_name = os.path.join(gdb, f) # Original file name with full path
arc_name = os.path.join(new_gdb, f) # Archive name with gdb name only
myzip.write(file_name, arc_name) # Write original file to new geodatabase
代码显示效果如下。
删除用于指定硬编码路径的每一行。这些需要删除的行以 arcpy.env.workspace = 、clip_fc = 、gdb = 和 out_zip = 开头。
保留通过分割 gdb 变量值,为变量 gdb_path 和 new_gdb 分配值的代码行。
编辑 print(f"Output geodatabase {gdb} created") 行,将 print 更改为 arcpy.AddMessage。
arcpy.AddMessage(f"Output geodatabase {gdb} created")
在 # Clip each input feature class 部分,选择 inputs = arcpy.ListFeatureClasses() 行和 print(inputs) 行,然后将其删除。
编辑 print(f"Output feature class {new_fc} created") 行,将 print 更改为 arcpy.AddMessage。
arcpy.AddMessage(f"Output feature class {new_fc} created")
4. 测试工具
添加源数据到当前活动地图,并设这裁剪工具参数,图形显示如下。
裁剪完成后显示结果如下。
显示效果
参考资料
- 创建 Python 脚本工具 :
https://learn.arcgis.com/zh-cn/projects/create-a-python-script-tool - Describe函数参考地址:
https://pro.arcgis.com/zh-cn/pro-app/latest/arcpy/data-access/what-is-the-data-access-module-.htm
❝
GIS之路-开发示例数据下载,请在公众号后台回复:vector
全国信息化工程师-GIS 应用水平考试资料,请在公众号后台回复:GIS考试
❝
GIS之路 公众号已经接入了智能 助手,可以在对话框进行提问,也可以直接搜索历史文章进行查看。
都看到这了,不要忘记点赞、收藏 + 关注 哦 !
本号不定时更新有关 GIS开发 相关内容,欢迎关注