如果涉及到了Arcpy,就是基本断定是自带的功能(模块)和自动化工具(模型)已经不能满足平常的工作了。比如,可能需要具体操作某SHAPE的点,线,具体的字段,更加繁琐的事务。
只想简单的写一下过程吧。毕竟太深奥的东西真的有些触碰不到,就比如调试着代码就涉及到了看不见的底层算法,无功而返。
总体来说,基于Arcpy的程序的调试还是有一定的限制的,出错后给出的信息同样继承了Python自有的特点:让人莫名其妙,找不出问题的地方。
PyCharm是的非常棒的开发软件,配置好对应Python编译器后,会自动找寻到对应的lib文件,这样会自动识别arcpy模块。加上特别优秀的代码提示功能,很好也很快的编写或者独立于arcgis的程序,或者基于arcgis工具箱的工具。
独立的程序,这一点和python普通脚本程序没什么区别,无非将arcpy当做一个库来执行,会发挥python自身的优点,比如:多线程。最近的一次数,使用multiprocessing模块完全利用电脑的4个CPU,很快的解决有算法缺陷的数据处理。
基于工具箱的优点就是能够结合arcgis桌面环境,更好的进行数据可视化。
arcgis桌面版中的工具箱,可以使用的是两种:纯python的工具箱和脚本。
纯python工具箱就是整个界面都是由Python定义,所有内容都可以动态的生成,所需参数,设计参数类型等等。
class Tool(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = u"设置图层要素名称"
self.description = u"本插件中使用默认的图层要素名称<b>有可能</b>与实际图层要素有出路,可现在本配置中更改对应要素名称。<br/>"
self.canRunInBackground = False
self.items = {}
def getParameterInfo(self):
"""Define parameter definitions"""
params = []
count=1
pickle_file=os.path.join(sys.path[0],"item_config")
if os.path.exists(pickle_file):
with open(pickle_file,'r') as f:
self.items=pickle.load(f)
else:
with open(os.path.join(sys.path[0],"item_config.txt"),'r') as f:
for line in f.readlines():
line = line.strip()
if not line.startswith("#"):
line=line.split("=")
self.items[line[0].decode("utf8")]=line[1].decode("utf8")
for k,v in self.items.items():
params.append(arcpy.Parameter(k,u"{0}#{1}".format(count,k), datatype="GPString", parameterType="Optional"))
params[-1].value=v.strip()
count+=1
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, param):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
for param in parameters:
self.items[param.name]=param.value
pickle_file=os.path.join(sys.path[0],"item_config")
with open(pickle_file,'w') as f:
pickle.dump(self.items,f)
return
而脚本就简单的多。更像是MVC性质,独立的界面设计,功能操作实现,优势就是设计简单,需要将精力更多的放在数据内容处理。
通过自己编写适当的函数,能够更好的操作,只是心中记得界面中配置好的参数位置还是很费劲的。如果有一天需要增加某个字段,有可能导致所有的参数位置需要全部更新,这将是个惨剧。
import arcpy
from lib import find_lane_id
from xUtil import _,__
if __name__=="__main__":
"""
0,use global
1,lane
2,seg_id_fd_name
3,seg line
4,seg_id_fd
5,lane_id_fd_name
6,
"""
if _(6):
find_lane_id(__(1), __(2), __(3), __(4), __(5), __(9),__(7),__(8))
else:
find_lane_id(__(1), __(2), __(3), __(4), __(5))