使用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()
相关推荐
程序员小远2 小时前
Python+requests+unittest+excel 实现接口自动化测试框架
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·excel
好家伙VCC2 小时前
# 发散创新:用Selenium实现自动化测试的智能断言与异常处理策略在现代Web应用开发中,*
java·前端·python·selenium
小陈工2 小时前
Python测试实战:单元测试、集成测试与性能测试全解析
大数据·网络·数据库·人工智能·python·单元测试·集成测试
城数派2 小时前
1901-2024年我国省市县三级逐月潜在蒸散发数据(Shp/Excel格式)
arcgis·信息可视化·数据分析·excel
文人sec2 小时前
抛弃 Postman!用 Pytest+Requests+Allure+Playwright+Minium 搭建高逼格接口+UI自动化测试平台
自动化测试·python·测试工具·ui·pytest·playwright
2501_908329852 小时前
使用Python分析你的Spotify听歌数据
jvm·数据库·python
qq_416018722 小时前
使用Scikit-learn进行机器学习模型评估
jvm·数据库·python
m0_587958952 小时前
Python字典与集合:高效数据管理的艺术
jvm·数据库·python
2301_776508723 小时前
用Python制作一个文字冒险游戏
jvm·数据库·python