# FrequencyWithCase.py # do it properly ESRI # 19 May 2010 # add a case item to the source table # like Workstation import arcgisscripting,sys,os try : table = sys.argv[1] strFields = sys.argv[2] except : table = "e:/data/nzpost/paf2010/geopaf.gdb/pafpost" strFields = "postcode;postcode_1" gp = arcgisscripting.create(9.3) casefld = "case" ws = os.path.dirname(table) tabname = os.path.basename(table) gp.Workspace = ws if len(gp.Listfields(table,casefld)) == 0 : result = gp.AddField(table,casefld,"LONG") print result.GetOutput(0) else : # name = gp.AddField(table,"case1","LONG") print casefld,"Exists" if gp.ListFields(table,casefld) == None : raise Exception,"no case field" seqfld = 'sequence' if len(gp.Listfields(table,seqfld)) == 0 : result = gp.AddField(table,seqfld,"LONG") print result.GetOutput(0) else : # name = gp.AddField(table,"case1","LONG") print seqfld,"Exists" if gp.ListFields(table,seqfld) == None : raise Exception,"no sequence field" # make a dictionary with a combo key # populate the source table as we go # export the dictionary at the end # make a tuple of all fields # check if its in dictionary # if it is increment counter # otherwise add new entry # retrieve number # put number into index or back on table # dFreq = {} lstFields = strFields.split(";") print lstFields cur = gp.UpdateCursor(table) row = cur.next() n = 0 ccase = 0 while row: combo = [] for f in lstFields : combo.append( row.GetValue(f) ) combo = tuple(combo) if dFreq.has_key(combo) : thiscase,freq = dFreq[combo] dFreq[combo] = (thiscase,freq+1) row.case = thiscase row.sequence = freq+1 else : ccase+=1 dFreq[combo] = (ccase,1) row.case = ccase row.sequence = 1 cur.UpdateRow(row) ## if n > 1000 : ## break n+=1 row = cur.next() del cur,row print print "case freq" keytab = dFreq.values() keytab.sort() ##for x in keytab: ## print "%4d %4d" % x print len(dFreq),"cases" # write out frequency table with case item fTab = tabname+"Freq" if gp.Exists(fTab) : gp.Delete(fTab) gp.CreateTable(ws,fTab) gp.AddField(fTab,casefld,"LONG") gp.AddField(fTab,"Frequency","LONG") dscTable = gp.Describe(table) dType = {"String":"TEXT","Integer":"LONG"} for f in lstFields: fType = dType[gp.ListFields(table,f)[0].type] gp.AddField(fTab,f,fType) # may not be a "long" in general case print f,"added to",fTab cur = gp.InsertCursor(fTab) for k in dFreq.keys(): ## print dFreq[0],dFreq[1],dFreq row = cur.NewRow() row.case = dFreq[0] row.Frequency = dFreq[1] n = 0 for i in k: ##print i,n,lstFields fld = lstFields row.SetValue(fld, i) n+=1 cur.InsertRow(row) del cur,row print "Done"
Use the SummaryStatistics tool: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00080000001z000000.htmIt does everything that Frequecy does and more...
Does anyone know why the case_item field was eliminated from the frequency tool in ArcGIS Desktop? In ArcInfo Workstation the option to add a case_item made it very easy to join the frequency table to the feature table. This question was initially asked in 2007 (http://forums.esri.com/thread.asp?c=93&f=1729&t=233587), but did not get a response.
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.