Johannes,
Thanks again for helping me with this. Code is below.
Please keep in mind that this was not written by me (I'm not trying to pass blame or anything, just being honest). I imagine you may find one (or many) flaw(s) but that doesn't mean I don't want to learn or improve my python skills, just want to let you know where I'm coming from.
And just to be clear, this current script works fine as intended. That is to say, when done I have an excel file with the output from the GDB that was read by the script. It just can't read from the Oracle enterprise database, hence the reason we are here now.
In sum, the overall goal here is to add the additional code to this script so it can connect then iterate from the Oracle enterprise database and generate a table with feature counts from the feature classes from within the datasets.
Looking forward to your reply.
Phil
import arcpy
import time
import datetime
import os
import shutil
import Tkinter, tkFileDialog
initdir = "c:\\geobase\\"
print "Please select which file you want run the report on:"
#wait = os.system("pause")
root = Tkinter.Tk()
root.withdraw()
file_path = tkFileDialog.askdirectory(initialdir=initdir)
t=1
while t < 5 and file_path.split("/")[-1][-4:] != ".gdb":
if t != 4:
print "No file selected or GDB not selected. Try again."
file_path = tkFileDialog.askdirectory(initialdir=initdir)
t += 1
else:
print "No file selected or GDB not selected. Quiting Script"
wait = os.system("pause")
quit()
print "Generating Report from file: " + file_path
file_name = file_path.split("/")[-1]
def f_time_hr (seconds):
if seconds < 60:
output = str(round(seconds)) + " seconds."
return output
if seconds > 60 and seconds < 3600:
minutes = seconds/60
output = str(round(minutes)) + " minutes."
return output
if seconds > 3600:
hours= seconds/3600
minutes=int(round(hours-int(hours),2)*60)
hours=int(hours)
output = str(hours) + "hrs " + str(minutes) + " minutes"
return output
arcpy.env.workspace = file_path
# the following table has to be created first before script can run
# see the attribute fields listed at the bottom of the script,
RT_Path = "in_memory"
RT_Name = "GIS_PR_Attributes"
Results_Table = "in_memory\\GIS_PR_Attributes"
Results_Table_Fields = ["Dataset", "Entity", "Combine", "FeatureCount","ISSOLDPROPERTY", "PRUID","FacilityNumber","FacilityID","BuildingNumber","StructureNumber","CategoryCode","True_PR_Asset","GEO_Comment"]
if arcpy.Exists(Results_Table):
arcpy.Delete_management(Results_Table)
Results_Table_Fields = [["Dataset", "TEXT" ,40], ["Entity", "TEXT", 40], ["Combine", "TEXT", 1], ["FeatureCount","DOUBLE"],["ISSOLDPROPERTY", "TEXT", 1], ["PRUID", "TEXT", 1],["FacilityNumber", "TEXT", 1],["FacilityID", "TEXT", 1],["BuildingNumber", "TEXT", 1],["StructureNumber", "TEXT", 1],["CategoryCode", "TEXT", 1],["True_PR_Asset", "TEXT", 1],["GEO_Comment", "TEXT", 255]]
RT_Table = arcpy.CreateTable_management(RT_Path, RT_Name)
for fld in Results_Table_Fields:
if fld[1] == "TEXT":
arcpy.AddField_management(RT_Table, fld[0], "TEXT", field_length=fld[2])
else:
arcpy.AddField_management(RT_Table, fld[0], fld[1])
datasets = arcpy.ListDatasets("*", "Feature")
datasets = [''] + datasets if datasets is not None else []
if datasets == [''] or datasets == []:
print "The file you selected does not seem to contain any data. Please try again."
wait = os.system("pause")
quit()
print "Counting Feature Classes in GDB"
n=0
for dataset in datasets:
for entity in arcpy.ListFeatureClasses(feature_dataset=dataset):
n=n+1
output = "\rCounting: " + str(n)
sys.stdout.write(output)
sys.stdout.flush()
print "\nThere are " + str(n) + " feature classes"
print "Starting to look through Feature Classes"
avg_time_taken = 10
# the following are the attribute fields in the GIS database table
# the first two need to be text/string 50 char and the next should be text/string 1 char
r=n
x=1
fieldsdict = {}
start_time = time.time()
for dataset in datasets:
fieldsdict[dataset] = {}
fcs = arcpy.ListFeatureClasses(feature_dataset=dataset)
for entity in fcs:
remaining_time = f_time_hr((avg_time_taken * r))
output = "\rWorking on " + str(x) + " out of " + str(n) + " Current: " + dataset + "\\" + entity + "\t\tETC: " + remaining_time + "\t\t\t\t"
sys.stdout.write(output)
sys.stdout.flush()
fcDesc = arcpy.Describe(entity)
fields = [f.name for f in arcpy.ListFields(entity)]
PRUID = "Y" if "PRUID" in fields else "N"
FACILITYNUMBER = "Y" if "FACILITYNUMBER" in fields else "N"
FACILITYID = "Y" if "FACILITYID" in fields else "N"
BUILDINGNUMBER = "Y" if "BUILDINGNUMBER" in fields else "N"
STRUCTURENUMBER = "Y" if "STRUCTURENUMBER" in fields else "N"
CATEGORYCODE = "Y" if "CATEGORYCODE" in fields else "N"
ISSOLDPROPERTY = "Y" if "ISSOLDPROPERTY" in fields else "N"
fccount = 0
with arcpy.da.SearchCursor(entity, "OBJECTID") as FeatureClass:
for fcRow in FeatureClass:
fccount = fccount + 1
FeatureCount = fccount
fieldsdict[dataset][entity] = [FeatureCount, PRUID, FACILITYNUMBER, FACILITYID, BUILDINGNUMBER, STRUCTURENUMBER, CATEGORYCODE, ISSOLDPROPERTY]
time_taken = (time.time() - start_time)
avg_time_taken = (time_taken/x)
r=r-1
x=x+1
cursor = arcpy.da.InsertCursor(Results_Table, ["Dataset", "Entity", "FeatureCount", "PRUID", "FACILITYNUMBER","FACILITYID", "BUILDINGNUMBER","STRUCTURENUMBER","CATEGORYCODE","ISSOLDPROPERTY"])
for dataset in fieldsdict:
for entity in fieldsdict[dataset]:
cursor.insertRow([dataset, entity] + fieldsdict[dataset][entity])
del cursor
date = datetime.datetime.today()
datet = date.strftime('%Y-%m-%d-%H%M')
fType = ".xls"
excel_file = tkFileDialog.asksaveasfilename(defaultextension=fType,initialdir=initdir, filetypes = [('Excel', '*' + fType)])
x=1
while os.path.exists(excel_file):
print "File already exist, please select another name"
excel_file = tkFileDialog.asksaveasfilename(defaultextension=fType,initialdir=initdir, filetypes = [('Excel', '*' + fType)])
arcpy.TableToExcel_conversion(Results_Table,excel_file)
print "\nDone"
wait = os.system("pause")