现在有一批含有表头的excel文件,表头是经纬度等,但是在arcgis中无法直接将excel转shp,需要先转table再转shp。只有单个文件可以直接使用工具:XY Table To Point (Data Management)—ArcGIS Pro | Documentation直接手动处理,但是现在有大批数据,不能一个个执行,故使用arcpy处理。
更新:221209 之前脑子糊涂了,其实不用代码,直接构建个模型应该就行吧,还是当时试了不行所以才用arcpy的,忘记了
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
arcpy是基于arcgis的python里面的模块,如果没有ARCGIS是没办法使用的。
主要的参考来源有以下几个函数:
ListFiles()函数
https://pro.arcgis.com/zh-cn/pro-app/latest/arcpy/functions/listfiles.htm
这个函数在https://blog.csdn.net/weixin_43416590/article/details/124813336?spm=1001.2014.3001.5502 第6.4节有所提及。
EXCEL转表函数:
https://pro.arcgis.com/zh-cn/pro-app/2.6/tool-reference/conversion/excel-to-table.htm
XY表转点函数:
https://pro.arcgis.com/zh-cn/pro-app/latest/tool-reference/data-management/xy-table-to-point.htm
具体代码如下所示:
# -*- coding: utf-8 -*-
import os
import arcpy
import xlrd
arcpy.env.overwriteOutput = True # 允许覆盖之前产生的数据集,便于测试
# 设置工作空间
arcpy.env.workspace = r"D:\DELL\E\zong\yuejiangshuiliang2"
out_table = r"D:\DELL\E\zong\yuejiangshuiliang3"
out_shp = r"D:\DELL\E\zong\yuejiangshuiliang4"
coordinate = r"D:\DELL\E\zong\yuejiangshuiliang2\WGS 1984.prj"
# 读取excel文件列表
for xlsx_file in arcpy.ListFiles("*.xlsx"):
name = xlsx_file[:-5]
print(name)
arcpy.ExcelToTable_conversion(xlsx_file, out_table+"/"+name, "Sheet1")
arcpy.management.XYTableToPoint(out_table+"/"+name+".dbf", out_shp+"/"+name,
"lon", "lat", "pre",
coordinate)
其中,工作空间为存储了excel文件的文件夹,out_table存储了生成的table文件,out_shp存储了生成的shp文件,coordinate是投影文件。print(name)可以保留,用于查看进度。我这里有1500来个excel,所以我使用了print(name)来查看处理到哪里了。name = xlsx_file[:-5]
用于删掉文件拓展名.xlsx