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

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

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

latitude = float(row[1])
longitude = float(row[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 创建完成,包含所有属性信息。")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值