Select to view content in your preferred language

Get Assettype and asettgroup count from the Feature class

492
2
05-13-2023 06:52 PM
kumarprince8071
New Contributor III

In trying to get all the assettype and aasetgroup field values based on assetgroup field which are domain coded from the gdb but the script is not working for some of the gdb's and for some of the gdb's i'm not getting all the Feature Classes which are inside Feature Dataset

Any suggestions or help pleease,

 

import arcpy,openpyxl,os
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
outputfile = "output8.xlsx"
arcpy.env.overwriteOutput =True
ws.cell(row=1, column =1,value = "Feature Dataset")
ws.cell(row=1, column=2, value="Feature Class")
ws.cell(row=1, column=3, value="Feature Count")
ws.cell(row=1, column=4, value="Asset Group")
ws.cell(row=1, column=5, value="Asset Group Count")
ws.cell(row=1, column=6, value="Asset Type")
ws.cell(row=1, column=7, value=" Asset Type Count")
row_num = 2
gdb = r'path\to\gdb'
arcpy.env.workspace = gdb
fds = arcpy.ListDatasets("*", "Feature")
for fd in fds:
feature_classes = []
arcpy.env.workspace = gdb + '\\' + fd
for fc in arcpy.ListFeatureClasses("","ALL"):
feature_classes.append(fc)
#arcpy.env.workspace = gdb
for feature_class in feature_classes:
counts = arcpy.GetCount_management(feature_class).getOutput(0)
ws.cell(row=row_num, column=1, value=fd)
ws.cell(row=row_num, column=2, value=feature_class)
ws.cell(row=row_num, column=3, value=counts)
# row_num+=1
all_codedvalue = {}
all_assetgrp = {}
field_name = ["ASSETGROUP", "ASSETTYPE"]
Fields = arcpy.ListFields(feature_class)
subdict = arcpy.da.ListSubtypes(feature_class)
searchfield = False
if len(subdict) > 1:
for stcode in subdict:
if subdict[stcode]['SubtypeField'] != "":
all_assetgrp[stcode] = subdict[stcode]['Name']
for i in Fields:
if i.name == 'ASSETGROUP':
subtypename = {}
subtypename[stcode] = subdict[stcode]['Name']
codedvalue = {}
if subdict[stcode]['FieldValues']['ASSETTYPE'][1] != None:
for c in subdict[stcode]['FieldValues']['ASSETTYPE'][1].codedValues:
codedvalue[c] = subdict[stcode]['FieldValues']['ASSETTYPE'][1].codedValues[c]
FieldsNames = []
for fields in Fields:
FieldsNames.append(fields.name)
for field_n in field_name:
if field_n in FieldsNames:
searchfield = True
else:
searchfield = False
assetgroup = {}
assettype = {}
if searchfield == True:
for key in subtypename:
whee_clase = "ASSETGROUP = " + str(key)
with arcpy.da.SearchCursor(feature_class,field_name,whee_clase) as cursor:
for row in cursor:
if subtypename[row[0]] in assetgroup:
assetgroup[subtypename[row[0]]] +=1
else:
assetgroup[subtypename[row[0]]] = 1
if codedvalue[row[1]] in assettype:
assettype[codedvalue[row[1]]] += 1
else:
assettype[codedvalue[row[1]]] = 1
for group, type_counts in assetgroup.items():
ws.cell(row=row_num, column=4, value=group)
ws.cell(row=row_num, column=5, value=type_counts)
for asset, count in assettype.items():
ws.cell(row=row_num , column=6, value=asset)
ws.cell(row=row_num , column=7, value=count)
row_num+=1
else:
row_num += 1
arcpy.env.workspace = gdb
outside_dataset = []
for item in arcpy.ListFeatureClasses("","ALL"): outside_dataset.append(item)
for item in arcpy.ListTables("*"): outside_dataset.append(item)
for feature in outside_dataset:
count_ = arcpy.GetCount_management(feature).getOutput(0)
ws.cell(row=row_num+1, column=2, value=feature)
ws.cell(row=row_num+1, column=3, value=count_)
row_num+=1
wb.save(outputfile)
Tags (3)
0 Kudos
2 Replies
DavidPike
MVP Frequent Contributor

Can you explain in more detail what you're trying to achieve?  The code looks very verbose and seems to have 6 levels of iteration at one point.

0 Kudos
kumarprince8071
New Contributor III

By using this code i am trying to get the Asset type field values based on asset group from the gdb and exporting that unique values of Asset type and Asset group in the excel .But while doing that i'm not able to get all the feature class inside a feature dataset.

0 Kudos