1. Tips
ArcGIS中以文件中的经纬度字段转化为矢量点的操作为:
(1)文件-添加数据-添加XY数据
(2)右键导出数据成矢量
在这里我们用代码的方式实现批量转换。
2. 实例
import arcpy
import os
# 设置环境
arcpy.env.workspace = r"G:\2001_2020\PlantYearCSV"
out_workspace = r"G:\2001_2020\PlantYearSHP"
# 列出所有的csv文件
csv_files = arcpy.ListFiles("*.csv")
# 遍历所有csv文件
for csv_file in csv_files:
# 清洁文件名,替换破折号为下划线
clean_name = os.path.splitext(csv_file)[0].replace("-", "_")#这里是根据我的文件命名方式来获取文件名,请自行更改
# 构建输出shapefile的名称
out_shp = os.path.join(out_workspace, clean_name)
in_x_field = "longitude"
in_y_field = "latitude"
# 设置输入坐标系为WGS 1984
spRef = arcpy.SpatialReference(4326) # 4326 是 WGS 1984 的 EPSG 代码
# 创建临时的XY事件图层
tempLayer = "tempLayer"
arcpy.MakeXYEventLayer_management(csv_file, in_x_field, in_y_field, tempLayer, spRef)
# 将事件图层保存为shapefile
arcpy.CopyFeatures_management(tempLayer, out_shp)
# 删除临时图层
arcpy.Delete_management(tempLayer)
print("Converted {} to {}".format(csv_file,out_shp))
print("All CSV files have been converted.")