使用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()
相关推荐
铁皮哥8 分钟前
【后端/Agent 开发】给你的项目配置一套 .claude/ 工作流:别再裸用 Claude Code 了!
java·windows·python·spring·github·maven·生活
m0_6315298226 分钟前
CSS如何利用CSS变量进行渐变色管理_提升渐变配置的灵活性
jvm·数据库·python
2301_8180084439 分钟前
数据库模型设计实战:如何正向工程从模型建表_规范化项目开发流程
jvm·数据库·python
科研前沿43 分钟前
多视角相机驱动的室内人员空间定位技术白皮书
大数据·人工智能·python·科技·数码相机·音视频
覆东流1 小时前
第10天:python元组
开发语言·后端·python
万事大吉CC1 小时前
【5】Django 的模板语言:页面架构设计
后端·python·django
码界奇点2 小时前
基于Python的微信公众号爬虫系统设计与实现
开发语言·爬虫·python·毕业设计·web·源代码管理
2401_846339562 小时前
Vue 3 中集成 Three.js 场景的完整实现指南
jvm·数据库·python
落雪寒窗-2 小时前
Python开发个人日常记录
开发语言·python
2301_775639892 小时前
Golang怎么写TODO待办应用_Golang TODO应用教程【深入】
jvm·数据库·python