How we get Feature count of 2 GDB(Include dataset and feature class) in CSV format.

1258
8
Jump to solution
04-21-2022 04:23 AM
Rakesh_Kumar_GIS
New Contributor III

How we get Feature count of 2 GDB(Include dataset and feature class) in CSV format in Model builder or script tool.

In CSV format GDB can differentiate with sheets and every sheet have count of respective feature classes.

 

 

Tags (2)
0 Kudos
2 Solutions

Accepted Solutions
curtvprice
MVP Esteemed Contributor

This can't be done in ModelBuilder tools, but you can use the Calculate Value tool like this in ModelBuilder.

Expression: proc(r"C:\users\me\my.gdb", r"C:\users\me\my.csv")

Code Block:

def get_gdb_feature_count(gdb_path):
    # set workspace to gdb
    prev_ws = arcpy.env.workspace
    arcpy.env.workspace = gdb_path
    # list all feature classes and tables in workspace
    fcs_and_tbls = arcpy.ListFeatureClasses() + arcpy.ListTables()
    # GetCount() on all of these
    counts = [int(arcpy.management.GetCount(ft)[0]) for ft in fcs_and_tbls]
    # revert workspace
    arcpy.env.workspace = prev_ws
    # return [ ["TableName", count] ]
    return zip(fcs_and_tbls, counts)

def proc(gdb, csv_path):
    result = get_gdb_feature_count(gdb)
    with open(csv_path, "w") as f:
        f.write("Name,Count\n")
        for r in result:
            f.write(f"{r[0]},{r[1]}\n")

 

View solution in original post

0 Kudos
curtvprice
MVP Esteemed Contributor
Use ModelBuilder variables for example r"%gdb% and r"%csv%" - the path is substituted in at runtime.

View solution in original post

8 Replies
JohannesLindner
MVP Frequent Contributor

Basic structure:

def get_gdb_feature_count(gdb_path):
    # set workspace to gdb
    prev_ws = arcpy.env.workspace
    arcpy.env.workspace = gdb_path
    # list all feature classes and tables in workspace
    fcs_and_tbls = arcpy.ListFeatureClasses() + arcpy.ListTables()
    # GetCount() on all of these
    counts = [int(arcpy.management.GetCount(ft)[0]) for ft in fcs_and_tbls]
    # revert workspace
    arcpy.env.workspace = prev_ws
    # return [ ["TableName", count] ]
    return zip(fcs_and_tbls, counts)


csv_path = "path:/to/your/csv_file.csv"
result = get_gdb_feature_count("path:/to/your/gdb")
with open(csv_path, "w") as f:
    f.write("Name,Count\n")
    for r in result:
        f.write(f"{r[0]},{r[1]}\n")

Have a great day!
Johannes
Rakesh_Kumar_GIS
New Contributor III

Hi Johannes ,

Thanks your effort.

Please give me this script as a model builder.

Rakesh_Kumar_GIS_0-1650955485753.png

 

thanks for advance,

Rakesh

0 Kudos
JohannesLindner
MVP Frequent Contributor

Sorry, I don't know how to do this in ModelBuilder.

Try running the script in the Python Window first, that makes it easier to troubleshoot.


Have a great day!
Johannes
curtvprice
MVP Esteemed Contributor

This can't be done in ModelBuilder tools, but you can use the Calculate Value tool like this in ModelBuilder.

Expression: proc(r"C:\users\me\my.gdb", r"C:\users\me\my.csv")

Code Block:

def get_gdb_feature_count(gdb_path):
    # set workspace to gdb
    prev_ws = arcpy.env.workspace
    arcpy.env.workspace = gdb_path
    # list all feature classes and tables in workspace
    fcs_and_tbls = arcpy.ListFeatureClasses() + arcpy.ListTables()
    # GetCount() on all of these
    counts = [int(arcpy.management.GetCount(ft)[0]) for ft in fcs_and_tbls]
    # revert workspace
    arcpy.env.workspace = prev_ws
    # return [ ["TableName", count] ]
    return zip(fcs_and_tbls, counts)

def proc(gdb, csv_path):
    result = get_gdb_feature_count(gdb)
    with open(csv_path, "w") as f:
        f.write("Name,Count\n")
        for r in result:
            f.write(f"{r[0]},{r[1]}\n")

 

0 Kudos
Rakesh_Kumar_GIS
New Contributor III

Hi curtvprice ,

Thanks for the reply.

can be set gdb path and csv  path as a parameter. if possible then please let me know.

thanks,

Rakesh

0 Kudos
curtvprice
MVP Esteemed Contributor
Use ModelBuilder variables for example r"%gdb% and r"%csv%" - the path is substituted in at runtime.
Rakesh_Kumar_GIS
New Contributor III

Thank you so much !!!!!!!  curtvprice 

But I observe its working on both are same folder path.

But any how... Its really usefull for me

 

thanks for your time in future I will defiantly reach you  🙂 

0 Kudos
Rakesh_Kumar_GIS
New Contributor III

And I think its not write dataset name and inside the dataset feature class.

0 Kudos