使用arcpy,批量读取多个文件夹的*.shp中的图层,统计提取图层的个数和要素总个数

使用arcpy,批量读取多个文件夹的*.shp中的图层,统计提取图层的个数和要素总个数

python 复制代码
import arcpy
import os
import json
import csv
from datetime import datetime

def analyze_shapefile_folders(root_folder):
    """
    批量分析多个文件夹中的Shapefile文件
    统计每个Shapefile中图层数和要素总个数
    """
    # 存储统计结果
    results = []
    
    # 遍历所有文件夹
    for root, dirs, files in os.walk(root_folder):
        # 查找所有.shp文件
        for file in files:
            if file.endswith('.shp') and not file.startswith('.'):
                shp_path = os.path.join(root, file)
                
                try:
                    # 分析Shapefile
                    feature_count = analyze_single_shapefile(shp_path)
                    
                    # 记录结果
                    results.append({
                        'shp_path': shp_path,
                        'feature_count': feature_count,
                        'analysis_time': datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                    })
                    
                    print(f"已分析: {shp_path} - 要素数: {feature_count}")
                    
                except Exception as e:
                    print(f"分析 {shp_path} 时出错: {str(e)}")
    
    return results

def analyze_single_shapefile(shp_path):
    """
    分析单个Shapefile文件
    返回要素总数
    """
    try:
        # 获取要素数量
        count = int(arcpy.GetCount_management(shp_path).getOutput(0))
        return count
    except Exception as e:
        print(f"统计 {shp_path} 要素数时出错: {str(e)}")
        return 0

def save_results_to_json(results, output_file):
    """
    将结果保存为JSON文件
    """
    with open(output_file, 'w', encoding='utf-8') as f:
        json.dump(results, f, ensure_ascii=False, indent=2)
    print(f"结果已保存至: {output_file}")

def save_results_to_csv(results, output_file):
    """
    将结果保存为CSV文件
    """
    with open(output_file, 'w', newline='', encoding='utf-8') as f:
        fieldnames = ['shp_path', 'feature_count', 'analysis_time']
        writer = csv.DictWriter(f, fieldnames=fieldnames)
        
        writer.writeheader()
        for result in results:
            writer.writerow(result)
    
    print(f"CSV结果已保存至: {output_file}")

def generate_summary_report(results):
    """
    生成汇总报告
    """
    print("\n=== Shapefile分析汇总报告 ===")
    
    if not results:
        print("未找到任何Shapefile文件")
        return
    
    total_shapefiles = len(results)
    total_features = sum(r['feature_count'] for r in results)
    
    # 找出最大和最小的Shapefile
    max_features_shp = max(results, key=lambda x: x['feature_count'])
    min_features_shp = min(results, key=lambda x: x['feature_count'])
    
    print(f"总Shapefile文件数: {total_shapefiles}")
    print(f"总要素数: {total_features}")
    print(f"平均每个Shapefile要素数: {total_features/total_shapefiles:.2f}")
    print(f"要素最多的Shapefile: {max_features_shp['shp_path']} ({max_features_shp['feature_count']} 要素)")
    print(f"要素最少的Shapefile: {min_features_shp['shp_path']} ({min_features_shp['feature_count']} 要素)")

def main():
    """
    主函数
    """
    # 获取用户输入的路径
    root_folder = input("请输入包含Shapefile文件的根目录路径: ").strip()
    
    # 检查路径是否存在
    if not os.path.exists(root_folder):
        print(f"错误: 路径 {root_folder} 不存在")
        return
    
    print("开始批量分析Shapefile文件...")
    print(f"根目录: {root_folder}")
    
    # 分析Shapefile文件
    results = analyze_shapefile_folders(root_folder)
    
    if not results:
        print("未找到任何Shapefile文件")
        return
    
    # 生成时间戳
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    
    # 保存结果
    json_output = f"shapefile_analysis_results_{timestamp}.json"
    csv_output = f"shapefile_analysis_results_{timestamp}.csv"
    
    save_results_to_json(results, json_output)
    save_results_to_csv(results, csv_output)
    
    # 生成汇总报告
    generate_summary_report(results)
    
    print(f"\n分析完成!结果已保存至:")
    print(f"  - {json_output}")
    print(f"  - {csv_output}")

if __name__ == "__main__":
    main()
相关推荐
yaoxin5211237 小时前
462. Java 反射 - 获取声明类与封闭类
java·开发语言·python
中微极客8 小时前
解锁LLM开发全栈能力:Python + LangChain + RAG 工程实战指南
人工智能·python·langchain
hhzz8 小时前
Barbershop:基于GAN和分割Mask的图像合成技术——从理论到实战全解析
图像处理·人工智能·python·深度学习·计算机视觉
Ulyanov9 小时前
雷达导引头Python仿真框架:GPU加速、6-DOF模型与半实物仿真接口
开发语言·python·雷达信号处理·雷达导引头
列逍10 小时前
博客系统测试
自动化测试·python·性能测试
星云开发11 小时前
拒绝无效加班!用Python打造自动化办公流,附Word/PDF互转硬核代码
python
dream_home840711 小时前
图像算法模型NPU适配与算法服务实战指南
人工智能·python·算法·npu 图像服务
AIGS00111 小时前
跨越语义鸿沟:企业本体语义平台的构建与落地
java·人工智能·python·机器学习·人工智能ai大模型应用
李可以量化11 小时前
PTrade 量化策略实战:二八轮动策略深度解析(下)
python
Mx_coder11 小时前
8年Java开发者AI转型第二周:RAG检索优化-从60%到90%准确率的3个关键技巧 (Day 13-14)
人工智能·python