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, "TEXT&#