Arcpy基础入门-5、读取自定义格式xml

本文介绍了如何利用Python的Arcpy模块读取和处理自定义格式的XML文件,特别是针对北京54坐标系的航迹数据。通过示例展示了两种将XML数据转化为要素的方法,包括使用insertcursor创建要素并赋属性。
摘要由CSDN通过智能技术生成

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&#
  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值