GIS之arcgis系列08:arcpy实现批量excel转矢量点

文件夹内有很多excel表,每个excel表内有很多数据,爬取每条数据的日期、精度、纬度,根据经纬度将数据转换成矢量点,并指定坐标系。


完整版代码如下:

代码均已经过测试,可直接copy至arcgis工具箱中使用。

(copy后注意改成适用于自己数据的代码,微调相关信息)

python 复制代码
# -*- coding: utf-8 -*-
import pandas as pd
import arcpy
import os

input_folder = arcpy.GetParameterAsText(0)
output_folder = arcpy.GetParameterAsText(1)
if not os.path.exists(output_folder):
    os.makedirs(output_folder)
spatial_ref = arcpy.SpatialReference(4326)
temp_folder = output_folder

def process_excel_file(file_path, output_folder, temp_folder):
    df = pd.read_excel(file_path, header=0)

    columns_to_delete = [3, 4, 5, 6, 7, 10, 12, 13, 15, 16, 18, 21, 22]
    df.drop(df.columns[columns_to_delete], axis=1, inplace=True)

    new_column_names = {
        df.columns[2]: 'temmean',
        df.columns[3]: 'pre',
        df.columns[4]: 'temmax',
        df.columns[5]: 'windmax',
        df.columns[6]: 'temmin',
        df.columns[7]: 'rainmax'
    }
    df.rename(columns=new_column_names, inplace=True)

    date_col = 'Date'
    df[date_col] = pd.to_datetime(df[date_col])

    unique_dates = df[date_col].dt.date.unique()

    for date in unique_dates:
        date_df = df[df[date_col].dt.date == date]
        date_str = date.strftime('%Y%m%d')
        doy = date.timetuple().tm_yday
        temp_csv = os.path.join(temp_folder, "{}.csv".format(date_str))
        date_df.to_csv(temp_csv, index=False, encoding='utf-8')

        temp_layer = "temp_layer"
        if arcpy.Exists(temp_layer):
            arcpy.Delete_management(temp_layer)

        arcpy.MakeXYEventLayer_management(
            temp_csv, 'LON', 'LAT', temp_layer, spatial_ref
        )

        year_folder = file_path.split("\\")[-1].split(".")[0]
        print (year_folder)
        year_folder_path = os.path.join(output_folder, year_folder)
        if not os.path.exists(year_folder_path):
            os.makedirs(year_folder_path)

        output_shapefile = os.path.join(year_folder_path, "{}_{}.shp".format(year_folder, doy))
        arcpy.FeatureClassToFeatureClass_conversion(temp_layer, year_folder_path, os.path.basename(output_shapefile))


        os.remove(temp_csv)

for filename in os.listdir(input_folder):
    if filename.endswith(".xlsx"):
        file_path = os.path.join(input_folder, filename)
        process_excel_file(file_path, output_folder, temp_folder)

---------------完------------

相关推荐
ratel️5 小时前
小墨析 · Office Agent 日常办公不烧心
python·langchain·word·excel·fastapi·js·ppt
Logic1018 小时前
没有TextSplit函数的老版本也能拆分单元格的内容
excel·拆分数据·excel技巧·数据拆分·拆分单元格·textsplit·旧版excel
2601_967987379 小时前
你们都用Python实现了哪些自动化办公?
python·excel·自动化办公·1024程序员节·学习资料·跨平台操作
城数派9 小时前
1901-2025年中国省/市/县三级逐月平均气温数据Excel+Shp格式
excel
gtstore1 天前
从 Excel 迁移到仓库管理系统的数据准备与切换方案
excel·数据迁移·仓库管理系统·进销存·信息化实施
ms365copilot1 天前
我不再自己做 Word、Excel 和 PPT 了,我让 Copilot 直接帮我创建 (1)
word·powerpoint·excel·copilot
2501_934304402 天前
新增2026年! 2012-2026年全国逐月夜间灯光数据
arcgis·gis数据
cspttty2 天前
2027采购岗求职:大数据管理与应用专业如何把Excel、SQL变成业务能力
excel
ms365copilot2 天前
Excel Copilot 一键定位修改的内容
excel·copilot
梅如你3 天前
中国2026年路网数据
arcgis·gis·遥感