arcpy实现批量分区统计

arcpy实现批量分区统计,并将最后结果输出到一张表中。代码如下。



# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *
import os
from pathlib import Path

current_folder = Path(os.getcwd())
data_path = current_folder.parent / "data/sj_analysis/data_class"

# 土地利用的数据表格
# Set environment settings
arcpy.env.workspace = os.path.normcase(data_path)
# 获取工作空间中的所有栅格文件




# 遍历所有栅格文件
for sj_raster in arcpy.ListRasters():

    zoneField = "WYZ"
    inZoneData = current_folder.parent / "map/data/行政界限_merge.shp"
    
    print(inZoneData)

    # 允许覆盖
    arcpy.gp.overwriteOutput = True
    #
    outTable = "sj_area_{}.dbf" .format(sj_raster[-8:-4])
    table_path = data_path/"table"

    # 确保路径一定存在
    if not os.path.exists(table_path):
        os.makedirs(table_path)

    # 输出表格的路径
    table_out_path = os.path.join(table_path,outTable)

    print(table_out_path)

    # Check out the ArcGIS Spatial Analyst extension license
    # arcpy.CheckOutExtension("Spatial")

    # Execute ZonalStatisticsAsTable
    outZSaT = ZonalStatisticsAsTable(os.path.normcase(inZoneData), zoneField, sj_raster, 
                                    table_out_path, "DATA", "ALL")


###### 新建两个字段,并计算它们的字段值.###### 新建两个字段,并计算它们的字段值.
 
# Set environment settings
arcpy.env.workspace = os.path.normpath(table_path)


table_list_t = arcpy.ListTables()

for table_singe_1 in table_list_t:

    # Set local variables
    inFeatures = table_singe_1
    fieldName1 = "AREA_WK"
    fieldName2 = "MEAN_V"
    fieldName3 = "sumValue"

    expression1 = "!SUM!*8100/1000000/10000" #万公顷
    expression2 = "!MEAN!"
    expression3 = "!AREA_WK!*!MEAN_V!/100"    
    
    # Execute AddField
    arcpy.gp.overwriteOutput = True
    arcpy.AddField_management(inFeatures, fieldName1, "DOUBLE")
    arcpy.AddField_management(inFeatures, fieldName2, "DOUBLE")
    arcpy.AddField_management(inFeatures, fieldName3, "DOUBLE")

    # Execute CalculateField 
    arcpy.CalculateField_management(inFeatures, fieldName1, expression1, "PYTHON3")#arcgis pro比较新,因而用的是PYTHON3
    arcpy.CalculateField_management(inFeatures, fieldName2, expression2, "PYTHON3")
    arcpy.CalculateField_management(inFeatures, fieldName3, expression3, "PYTHON3")

####  将表格中dbf全部导出为excel

import arcpy

# Set environment settings
arcpy.env.workspace = os.path.normpath(table_path)


table_lists = arcpy.ListTables()
print(table_lists)

for table_singe in table_lists:
    in_table = table_singe

    excel_path = data_path/"table_xls"

    # 确保文件夹存在
    if not os.path.exists(excel_path):
        os.makedirs(excel_path)

    print("finish{}".format(in_table))

    out_xls = os.path.join(excel_path, "{}.xls".format(in_table[:-4]))
    arcpy.TableToExcel_conversion(in_table, out_xls)

###
### 把一个文件夹里所有的excel的第5和第6行提取出来

import os
import pandas as pd

def process_excel_files(input_folder, output_file):
    # 创建一个空的列表来存储所有文件的数据
    all_data = []

    # 遍历文件夹中的所有xls文件
    for file_name in os.listdir(input_folder):
        if file_name.endswith('.xls'):
            # 生成文件的完整路径
            file_path = os.path.join(input_folder, file_name)
            
            # 读取Excel文件
            df = pd.read_excel(file_path)
            
            # 检查文件是否有足够的列数
            if df.shape[1] >= 22:
                # 取出第五和第六列
                selected_columns = df.iloc[:, [1, 19]]
                
                # 添加文件名到每一列的顶部
                selected_columns.columns = [f"{file_name} - {col}" for col in selected_columns.columns]
                
                # 将数据添加到列表中
                all_data.append(selected_columns)
            else:
                print(f"{file_name} 文件没有足够的列数,跳过此文件。")

    # 将所有的数据合并到一个DataFrame中
    if all_data:
        combined_df = pd.concat(all_data, axis=1)
        
        # 写入到一个新的Excel文件中
        combined_df.to_excel(output_file, index=False)
    else:
        print("没有找到任何合适的文件来处理。")

# 设置输入文件夹路径和输出文件名
input_folder = excel_path  # 替换为你的文件夹路径
output_file = data_path/"result.xlsx"  # 输出文件名

# 调用函数处理Excel文件
process_excel_files(input_folder, output_file)

总结
1.环境设置中需要用到os.path.normcase
2.分区统计时,路径设置需要用到

v2版本(未来会改变的版本)
1.需要优化路径,特别是最后结果输出的路径

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Q一件事

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值