[ArcPy] Excel转shp文件 Python解析Excel ArcPy创建要素类并保存
涉及Python读取excel,ArcPy创建要素类,并保存(要素仅有空间坐标,不保存其他属性)
若需要写入要素的其他属性,可以参考shp文件创建与写入数据:blog.csdn/summer_dew/article/details/78072870xlrd解析Excel打开excel文件打开表获取整行和整列值数组获取行数和列数循环行列表数据指定单元格行列索引简单写入ArcPy创建多边行并保存成shp文件完整代码创建工具箱xlrd解析Excelxlrd并非python原生库,需要安装打开excel文件data = xlrd.open_workbook('excelFile.xlsx') 打开表table = data.sheets()[0] #通过索引table = data.sheet_by_name(u'Sheet1') #通过名称获取获取整行和整列值(数组)w_values(i) #获取第i行l_values(i) #获取第i列获取行数和列数nrows = wsncols = ls循环行列表数据for i in range(nrows): w_values(i)指定单元格cell_A1 = ll(0,0).valuecell_C4 = ll(2,3).value行列索引cell_A1 = w(0)[0].valuecell_A2 = l(1)[0].value简单写入table.put_cell(row,col,ctype,value,xf)# row行号# col列号# ctype类型 # 0 empty,1 string,2 nu
mber,3 date,4 boolean,5 error# value单元格的值# xf扩展的格式化ll(0,0) #单元格的值ll(0,0).value #单元格的值ArcPy创建多边行并保存成shp文件其他类型一样操作
先创建ArcPy要素类的变量:①面:arcpy.Polygon②线:Polyline③点:Point类型说明查文档:ArcPy–>几何保存数据到文件:arcpy.CopyFeatures_management(变量,路径)# 存放多边形的 点集polygonGeometryList = []# 遍历行(第一行->最后一行)for i in range(1,nrows): # 取出第i行,第5列的值 str = ll(i,5).value # ;分隔 points = str.split(u';') # 字符串转成坐标 for j in points: xy = j.split(',') # 创点类型 point.X = float(xy[0]);point.Y = float(xy[1]) array.add(point) # 创面类型 polygon = arcpy.Polygon(array) polygonGeometryList.append(polygon) veAll()# 保存数据 arcpy.CopyFeatures_management(polygonGeometryList, 'd:\\polygon.shp') 完整代码excel表:代码:import xlrd # must init xlrd import arcpy# v.workspace = r'F:\note\python\ArcPy' #workspaceexcelPath = r'test.xlsx' # excel file pathexcelTableIndex = 0 # excel's table indexoutName = r'point.shp' # out fileexcel = xlrd.open_workbook('test.xlsx') # get exceltable = excel.sheets()[0] # get table by sheets indexnrows = ws # number of table's row# get datapointGeometryList = [] # a list
python怎么读入excelto hold the PointGeometry objectspoint = arcpy.Point() #create an empty Point objectspRef = arcpy.SpatialReference('WGS84 ARC System Zone 18')for i in range(1,nrows): # get row once x = ll(i,0).value y = ll(i,1).value point.X = float(x) point.Y = float(y) pointGeometry = arcpy.PointGeometry(point,spRef) pointGeometryList.append(pointGeometry)arcpy.CopyFeatures_management(pointGeometryList,outName) # save the shape file结果:
创建工具箱为脚本创建工具箱,点我
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论