arcpy将xml文件转换为矢量shp格式文件

37 篇文章 3 订阅
28 篇文章 1 订阅
该代码示例展示了如何利用Python的arcpy模块将XML文件解析并转换为矢量SHP格式的地理空间数据。首先设置工作空间,然后创建SHP文件,定义其坐标系统。接着添加字段到SHP文件中,最后读取XML文件中的数据,提取点的经纬度信息并插入到SHP文件中。
摘要由CSDN通过智能技术生成

arcpy将xml文件转换为矢量shp格式文件

import os
import sys
import arcpy
from arcpy import env

from xml.dom import minidom
from compiler.ast import Node
from platform import node
from ast import NodeVisitor

#Set workspace
sourceDir = os.path.abspath(os.path.dirname(sys.argv[0]))    #source dir
env.workspace = sourceDir

def createShapefile(path,filename):
    #Set local variables
    outPath = path + "/output"
    fileName = filename
    geometryType = "POINT"
    # Creating a spatial reference object
    spatialReference = arcpy.SpatialReference(path+"/projected/WGS1984.prj")

    # Execute CreateFeatureclass
    arcpy.CreateFeatureclass_management(outPath, fileName, geometryType, "#", "DISABLED", "DISABLED", spatialReference)
    print "Create Shapefile success!"

    return

def addFieldToShapefile(filename,fieldname,fieldtype):
    # Execute AddField for new fields
    arcpy.AddField_management(sourceDir+"\\output\\"+filename,fieldname,fieldtype)
    print fieldname

    return

def readXML(xmlfilename,shpfilename):
    xmlDoc = minidom.parse(sourceDir+"\\input\\"+xmlfilename)
    items = xmlDoc.getElementsByTagName("wfs:member")
    #=print rootNode.toxml()
    # Create insert cursor for table
    #
    rows = arcpy.InsertCursor(sourceDir+"\\output\\"+shpfilename)
    print rows,type(rows)
    fc = sourceDir+"\\output\\"+shpfilename
    cursor = arcpy.da.InsertCursor(fc, ["SHAPE@XY"])
    for item in items:
        row = rows.newRow()
        print row
        scenicName = item.getElementsByTagName("gml:name")[0]
        row.NAME = scenicName.firstChild.data
        # print "scenicname:" + scenicName.firstChild.data
        scenicLngLon = item.getElementsByTagName("gml:pos")[0]
        LNGLAT = scenicLngLon.firstChild.data
        # print LNGLAT
        strscenicLngLon=str(LNGLAT)
        # print strscenicLngLon
        strLngLon=strscenicLngLon.split(' ')
        # print strLngLon[0]
        row.LNG = float(strLngLon[0])
        # print "lng:" +scenicLng.firstChild.data
        row.LAT = float(strLngLon[1])
        # print "lat:" + scenicLat.firstChild.data
        scenicFrom = item.getElementsByTagName("pku:type")[0]
        row.FROM_ = scenicFrom.firstChild.data
        # print "from:" +scenicFrom.firstChild.data
        rows.insertRow(row)
        xy = (float(strLngLon[0]), float(strLngLon[1]))
        # print xy
        cursor.insertRow([xy])
        # print cursor
    return
createShapefile(sourceDir,"features.shp")

addFieldToShapefile("features.shp","NAME","TEXT")
addFieldToShapefile("features.shp","LNG","DOUBLE")
addFieldToShapefile("features.shp","LAT","DOUBLE")
addFieldToShapefile("features.shp","FROM","TEXT")

readXML("features.xml","features.shp")
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木易GIS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值