ArcGIS工具 - 统计要素数量

查询和统计是GIS中的重要功能之一。在ArcGIS中可以按属性信息、按空间位置关系进行查询和统计。今天为源GIS给大家分享使用ArcPy编程实现批量统计地理数据库要素类记录数量。

软件应用

统计单个图层的记录数非常简单,直接打开属性表,在表格下方查看总数即可;如果有多个图层,以此类推。
img

如果您有更好的解决方案,请搜索和关注为源GIS

实现效果

img

统计要素数量工具,主要包括有以下特点:

  • 能够批量统计数据库里所有图层的要素或记录数量;
  • 支持ArcGIS 所有类型的工作空间、要素类和表;
  • 统计结果输出为CSV格式;
  • 兼容ArcGIS 10.0~10.8、ArcGIS Pro多个版本。

统计结果包括路径文件、要素数据集、要素名称、类型、数量等字段信息:

img

操作示例

在ArcGIS Pro加载为源工具箱,运行此功能,选择输入数据库、输出统计表格,单击“运行”即可得到结果。

img

在ArcMap中使用方法类似,如下图所示:

img

程序源码

主要用到GetCount_management统计数量,完整代码如下:

# -- coding:cp936 --
# ---------------------------------------------------------------------------
# 名称:PrintCount
# 功能:统计图层数量
# 作者:为源GIS
# 日期:2022.7.11
# 说明:
# ---------------------------------------------------------------------------

import codecs
import csv
from imp import reload
import os
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
import  arcpy
import  string

try:
    #参数1:输入工作空间列表
    in_dbs=arcpy.GetParameterAsText(0)
    dbs=in_dbs.split(";")

    #参数2:输出CSV文件
    csv_file=arcpy.GetParameterAsText(1)
    f = codecs.open(csv_file, 'w','utf_8_sig')
    csv_writer=csv.writer(f)
    
    count=0

    # 写入标题行
    title=['路径','文件','要素数据集','要素名称','类型','数量']
    csv_writer.writerow(title)
    arcpy.AddMessage(title)

    for db in dbs:
        db=db.strip("'")
        arcpy.env.workspace = db
        db_desc=arcpy.Describe(db)
        datasets = arcpy.ListDatasets()
        datasets = [''] + datasets if datasets is not None else []
        for ds in datasets:
            for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
                arcpy.SetProgressorLabel("正在统计 "+ fc)
                arcpy.AddMessage(fc)
                shapeType = arcpy.Describe(fc).shapeType
                total = int(arcpy.GetCount_management(fc).getOutput(0))
                result=[db_desc.path,db_desc.baseName,ds,fc,shapeType,str(total)]
                csv_writer.writerow(result)
                count=count+1
        for table in arcpy.ListTables():
                arcpy.SetProgressorLabel("正在统计 "+ table)
                shapeType = 'Table'
                total = int(arcpy.GetCount_management(table).getOutput(0))
                result=[db_desc.path,db_desc.baseName,'',fc,shapeType,str(total)]
                csv_writer.writerow(result)
                count=count+1
    arcpy.AddMessage('共输出 '+str(count)+'条统计结果。')
except arcpy.ExecuteError:
    arcpy.GetMessages()

技术支持

如果有任何疑问或问题,可联系微信:wygis2022,公众号:为源GIS, 企鹅:1811335143

posted @ 2022-07-11 22:23  为源地理  阅读(457)  评论(1编辑  收藏  举报