arcpy批量将excel一些列的点坐标制作成面

使用Arcpy可以批量将Excel表格中的点坐标制作成面,并将对应组点的属性写入到对应面的属性中。


import arcpy
import xlrd

# 设置输入数据路径
excel_file = r"C:\data\points.xlsx"  # 包含点坐标和属性的Excel表格
output_feature_class = r"C:\data\polygons.shp"  # 输出的面要素类路径

# 打开Excel表格
workbook = xlrd.open_workbook(excel_file)
worksheet = workbook.sheet_by_index(0)

# 获取Excel表格的列数
num_cols = worksheet.ncols

# 创建面要素类
arcpy.CreateFeatureclass_management(arcpy.env.workspace, 
                                    arcpy.Describe(output_feature_class).name,
                                    "POLYGON")

# 添加字段到面要素类
for col in range(1, num_cols):  # 假设第一列为点坐标,默认不包含在面属性中
    field_name = str(worksheet.cell_value(0, col))  # 字段名为Excel表格第一行
    arcpy.AddField_management(output_feature_class, field_name, "TEXT")

# 打开面要素类的编辑会话
with arcpy.da.Editor(arcpy.env.workspace) as edit_session:
    # 插入面要素
    with arcpy.da.InsertCursor(output_feature_class, ["SHAPE@"] + [str(worksheet.cell_value(0, col)) for col in range(1, num_cols)]) as cursor:
        # 遍历Excel表格的每一行
        for row_id in range(1, worksheet.nrows):
            # 提取点坐标
            points = []
            for col in range(0, num_cols-1):
                x = float(worksheet.cell_value(row_id, col))
                y = float(worksheet.cell_value(row_id, col + 1))
                points.append(arcpy.Point(x, y))

            # 构建面几何对象
            polygon = arcpy.Polygon(arcpy.Array(points))
            
            # 提取对应的属性
            attributes = [worksheet.cell_value(row_id, col) for col in range(1, num_cols)]
            
            # 插入新的面要素
            cursor.insertRow([polygon] + attributes)
 

在上述代码中,我们首先设置了输入数据的路径,即包含点坐标和属性,代码中的路径和文件名应根据你自己的数据进行相应修改。另外,坐标系设置为WGS 84经纬度坐标系(EPSG 4326),如果使用的是其他坐标系,请相应修改spatial_reference变量。

该代码将会根据Excel表格中的点坐标数据,创建面要素类并插入对应的面要素。同时,将Excel表格中对应组点的属性数据写入到面要素的属性中。

猜你喜欢

转载自blog.csdn.net/weixin_58851039/article/details/131280996