arcpy作为一种脚本开发语言,其功能的目的是为用户提供强有力的可扩展性。
这一节通过示例,介绍比较下使用python和arcpy读取自定义格式的xml并构建要素的方法。
先简单描述下自定义的xml格式:
j
自定义了一种北京54坐标系的航迹数据,每一个航迹包括了属性和对应的几何点,由于通用的航迹格式GPS只支持经纬度坐标的读取,所以需要自己写脚本,对要素进行处理
将xml格式的数据转化为要素有两种方式:一种是较为常见的创建要素,赋属性的方式,
使用的是arcpy.insertcursor,这种方式和ArcEngine开发有点相似,代码如下:
#coding=utf-8 import arcpy,os,time import xml.dom.minidom import random from arcpy import env arcpy.env.overwriteOutput = True #inGPXFile = arcpy.GetParameterAsText(0) inpath = 'F:\\gpx\\航迹-settingDB-20150605031316.gpx' uipath = unicode(inpath,"utf8") #outputFC = arcpy.GetParameterAsText(1) outputFC = r'F:\gpx\20150605031316.shp' uoutputFC = unicode(outputFC,"utf8") # 打开xml文档 dom = xml.dom.minidom.parse(uipath) #得到文档元素对象 root = dom.documentElement def get_attrvalue(node,attrname): return node.getAttribute(attrname) if node else '' def get_nodevalue(node,index = 0): if node: if node.childNodes: return node.childNodes[index].nodeValue else: return None def get_xmlnode(node,name): return node.getElementsByTagName(name) if node else [] trk_nodes = get_xmlnode(root,'trk') #获得节点测试 print trk_nodes #创建shp path = os.path.split(outputFC)[0] name = os.path.split(outputFC)[1] print path print name arcpy.CreateFeatureclass_management(path,name,"POLYLINE") fieldName1 = "name" fieldAlias = "轨迹名称" fieldLength = 50 arcpy.AddField_management(outputFC,fieldName1,"TEXT","",fieldLength,fieldAlias,"NULLABLE") fieldName2 = "time" fieldAlias = "时间" fieldLength = 50 arcpy.AddField_management(outputFC,fieldName2,"NULLABLE") fieldName3 = "length" fieldPrecision = 9 fieldAlias = "长度" arcpy.AddField_management(outputFC,fieldName3,"DOUBLE",fieldPrecision,"NULLABLE") fieldName4 = "jiange" fieldAlias = "间隔" arcpy.AddField_management(outputFC,fieldName4,"LONG","NULLABLE") fieldName5 = "tpkname" fieldAlias = "图幅名字" fieldLength = 50 arcpy.AddField_management(outputFC,fieldName5,"NULLABLE") fieldName6 = "daino" fieldAlias = "带号" arcpy.AddField_management(outputFC,fieldName6,"NULLABLE") cur=arcpy.InsertCursor(outputFC) for trk_node in trk_nodes: trk_node_time = get_xmlnode(trk_node,'time') trk_node_length = get_xmlnode(trk_node,'length') trk_node_jiange = get_xmlnode(trk_node,'jiange') trk_node_name = get_xmlnode(trk_node,'name') trk_node_daino = get_xmlnode(trk_node,'daino') trk_node_tpkname = get_xmlnode(trk_node,'tpkname') trk_time =get_nodevalue(trk_node_time[0]).encode('utf-8','ignore') trk_length =get_nodevalue(trk_node_length[0]).encode('utf-8','ignore') trk_jiange =get_nodevalue(trk_node_jiange[0]).encode('utf-8','ignore') trk_name =get_nodevalue(trk_node_name[0]).encode('utf-8','ignore') trk_daino =get_nodevalue(trk_node_daino[0]).encode('utf-8','ignore') trk_tpkname =get_nodevalue(trk_node_tpkname[0]).encode('utf-8','ignore') array = arcpy.Array() trkseg_node = get_xmlnode(trk_node,'trkseg') print trkseg_node trkpt_nodes = get_xmlnode(trkseg_node[0],'trkpt') print trkpt_nodes for trkpt_node in trkpt_nodes: trkpt_node_x = get_xmlnode(trkpt_node,'X') trkpt_node_y = get_xmlnode(trkpt_node,'Y') trkpt_x =get_nodevalue(trkpt_node_x[0]).encode('utf-8','ignore') trkpt_y =get_nodevalue(trkpt_node_y[0]).encode('utf-8','ignore') x=float(trkpt_x) y=float(trkpt_y) pnt=arcpy.Point() pnt.X = trkpt_y pnt.Y = trkpt_x array.add(pnt) print x print y polyline = arcpy.Polyline(array) array.removeAll() feat= cur.newRow() feat.shape = polyline feat.setValue("shape",polyline) feat.setValue("time",trk_time) feat.setValue("length",float(trk_length)) feat.setValue("jiange",int(trk_jiange)) feat.setValue("name",trk_name) feat.setValue("daino",int(trk_daino)) feat.setValue("tpkname",trk_tpkname) cur.insertRow(feat) del feat del cur还有另外一种方式使用arcpy.da模块下面的插入游标,根据arcgis的帮助文档,这种方式速度会更快, 详情见这里,代码如下:
#coding=utf-8 import arcpy,time import xml.dom.minidom import random from arcpy import env arcpy.env.overwriteOutput = True inGPXFile = arcpy.GetParameterAsText(0) #inpath = 'F:\\gpx\\航迹-settingDB-20150605031316.gpx' #uipath = unicode(inpath,"utf8") outputFC = arcpy.GetParameterAsText(1) #outputFC = r'F:\gpx\20150605031316.shp' #uoutputFC = unicode(outputFC,"utf8") # 打开xml文档 dom = xml.dom.minidom.parse(inGPXFile) #得到文档元素对象 root = dom.documentElement def get_attrvalue(node,"NULLABLE") cur = arcpy.da.InsertCursor(outputFC,("time","length","jiange","name","daino","tpkname","SHAPE@")) for trk_node in trk_nodes: trk_node_time = get_xmlnode(trk_node,'ignore') array = arcpy.Array() trkseg_node = get_xmlnode(trk_node,'ignore') x=float(trkpt_x) y=float(trkpt_y) pnt=arcpy.Point() pnt.X = trkpt_y pnt.Y = trkpt_x array.add(pnt) print x print y polyline = arcpy.Polyline(array) row =[trk_time,float(trk_length),int(trk_jiange),trk_name,int(trk_daino),trk_tpkname,polyline] cur.insertRow(row)至于如何根据脚本创建模型工具,可以去看下基础入门之3和4