当前位置:   article > 正文

【Python】Arcpy将excel点生成shp文件

【Python】Arcpy将excel点生成shp文件

根据excel点经纬度数据,生成shp,参考博主的代码,进行了修改,在属性表中保留excel中的数据。
参考资料:http://t.csdnimg.cn/OleyT

注意修改以下两句中的数字。

latitude = float(row[1])
longitude = float(row[2])
  • 1
  • 2
import xlrd
import arcpy

# 设置参数
arcpy.env.workspace = r"E:\data\shp"  # 工作空间
excelPath = ur"E:\data\shp\采样点.xlsx"  # Excel 文件路径
excelTableIndex = 0  # Excel 表索引
outName = r"采样点.shp"  # 输出文件名

# 读取 Excel 文件
excel = xlrd.open_workbook(excelPath)
table = excel.sheet_by_index(excelTableIndex)
nrows = table.nrows  # 表的行数
ncols = table.ncols  # 表的列数

# 定义空间参考
spRef = arcpy.SpatialReference(4326) # WGS-1984

# 创建空的 shapefile
arcpy.CreateFeatureclass_management(arcpy.env.workspace, outName, "POINT", spatial_reference=spRef)

# 获取字段名称列表
field_names = [table.cell(0, i).value for i in range(ncols)]

# 为 shapefile 添加字段
for field_name in field_names:
    if field_name:  # 确保字段名不为空
        arcpy.AddField_management(outName, field_name, "TEXT")

# 获取游标以便插入数据
with arcpy.da.InsertCursor(outName, ["SHAPE@XY"] + field_names) as cursor:
    for i in range(1, nrows):
        row = table.row_values(i, 0, ncols)  # 读取整行数据
        # 假设原数据第二列是纬度,第三列是经度
        latitude = float(row[1])
        longitude = float(row[2])
        point = arcpy.Point(longitude, latitude)  # 创建点对象
        cursor.insertRow([(point.X, point.Y)] + row)

print("Shapefile 创建完成,包含所有属性信息。")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/835445
推荐阅读
相关标签
  

闽ICP备14008679号