根据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 创建完成,包含所有属性信息。")