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)

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

相关推荐
CodeCraft Studio6 小时前
Excel处理控件Aspose.Cells教程:使用 C# 在 Excel 中创建组合图表
c#·excel·aspose·图表
小镇学者6 小时前
【PHP】导入excel 报错Trying to access array offset on value of type int
android·php·excel
CodeCraft Studio6 小时前
Excel处理控件Aspose.Cells教程:使用 C# 从 Excel 进行邮件合并
开发语言·c#·excel
小艳加油6 小时前
GIS应用攻略:全面涵盖GIS基础、ArcGIS操作、数据制备、空间分析、地图制作、三维建模与多领域实战应用,助力科研与行业实践!
arcgis·gis·空间分析·数据制备
新中地GIS开发老师7 小时前
三维GIS开发cesium智慧地铁教程(4)城市白模加载与样式控制
学习·arcgis·智慧城市·webgl·gis开发·webgis·地理信息科学
新中地GIS开发老师11 小时前
三维GIS开发cesium智慧地铁教程(5)Cesium相机控制
javascript·arcgis·智慧城市·大学生·gis开发·webgis·地理信息科学
CodeCraft Studio12 小时前
Excel处理控件Aspose.Cells教程:使用 C# 在 Excel 中应用数据验证
c#·excel·aspose·文档开发·文档处理
天青色等烟雨..15 小时前
ArcGIS土地利用数据制备、分析及基于FLUS模型土地利用预测技术应用
经验分享·arcgis
雾林小妖16 小时前
POI设置Excel单元格背景色
excel·poi设置背景色
只有干货16 小时前
java若依 excel 导出高度自适应
java·excel