##This script calculates the n-th percentile of a list of numbers that can be read from ## a shape file or table #P is the percentile P = 0.3 import math import arcgisscripting gp = arcgisscripting.create() #The location of the file to be read gp.Workspace = "path to the folder or gdb" #name of the file cur = gp.SearchCursor("the name of the shape file or table") List = [] row = cur.Next() while row <> None: List.append(row.VerticalVelN) row = cur.next() List.sort #Sort the list #Excel method to determine the percentile n = P * (len(List)-1)+1 #rank if n == 1: N = List[n-1] elif n == len(List): N = List[len(N)-1] else: rank = math.modf(n) d = rank[0] k = int(rank[1]) N = List[k-1] + d * (List - List[k-1]) print N
List = [15, 20, 35, 40, 50,20.1,15.2,55,60]
##This script calculates the n-th percentile of x number of point objects that fall within y number of polygon objects## #workspace is the name of the root folder or gdb where the shape file or table can be found workspace = "...." #bestand is the name of the shape or table to be read bestand = "...." #the name of the field that contains the values for which you want to know the percentile name = "...." #A unique ID field that identifies the polygon object id = "...." P = 0.3 #P is the percentile import math import arcgisscripting gp = arcgisscripting.create() gp.Workspace = workspace cur = gp.SearchCursor(bestand) List = [] List2= [] row = cur.Next() while row <> None: List.append(row.name) List2.append(row.id) row = cur.next() List3 = [None] * len(List) #This list is used to store the calculated percentiles later on M = [List, List2, List3] #The list is sorted by ID indices = range(len(List)) indices.sort(key = M[1].__getitem__) for i, sublist in enumerate(M): M = [sublist for j in indices] #Keeps track of the row numbers for wich the list jumps to the next unique ID grens = [] grens.append(0) p = 0 count = 0 while p<=48 and count<=(len(List)-2): #48 is a hardcoded number of unique polygon objects #This block counts the number of instances of all unique polygon ID's. temp = M[1][count] while M[1][count]==temp: if count == len(List)-1: break else: count += 1 if count == len(List)-1: grens.append(count) else: grens.append(count-1) #Here a subpart of the list is sorted on ascending order of point object values if count == len(List)-1: M[0][grens +1:count+1]=sorted(M[0][grens +1:count+1]) elif p == 0: M[0][grens :count]=sorted(M[0][grens :count]) else: M[0][grens +1:count]=sorted(M[0][grens +1:count]) #Excel method for calculating the percentile #rank if p == 0 or count == len(List)-1: n = P * (count-grens -1)+1 else: n = P * (count-grens -2)+1 if n == 1: n = int(n) N = M[0][n-1] elif n == count-grens : N = M[0][count-grens -1] else: rank = math.modf(n) d = rank[0] if p == 0: k = int(rank[1]) + grens else: k = int(rank[1]) + grens + 1 N = M[0][k-1] + d * (M[0] - M[0][k-1]) #The calculated percentile is saved in the list if p==0: k=grens else: k=grens +1 while k<=count: M[2]=N k += 1 p += 1 #The calculated percentiles should now be written to the file i = 0 while i < len(List2): where = "id=" + str(M[1]) cur = gp.UpdateCursor(bestand, where) row = cur.Next() while row: row.SetValue("percentiel03", M[2]) #percentiel03 is the name of the new field cur.UpdateRow(row) row = cur.Next() i += 1
j = 0 i = 0 while j <=len(grens) while i < len(List2) where = "id=" + str(M[1][grens+1]) cur = gp.UpdateCursor(bestand, where) row = cur.Next() while row: row.SetValue("percentiel03", M[2]) #percentiel03 is the name of the new field cur.UpdateRow(row) row = cur.Next() i += 1 j += 1
## {{{ http://code.activestate.com/recipes/511478/ (r1) import math import functools def percentile(N, percent, key=lambda x:x): """ Find the percentile of a list of values. @parameter N - is a list of values. Note N MUST BE already sorted. @parameter percent - a float value from 0.0 to 1.0. @parameter key - optional key function to compute value from each element of N. @return - the percentile of the values """ if not N: return None k = (len(N)-1) * percent f = math.floor(k) c = math.ceil(k) if f == c: return key(N[int(k)]) d0 = key(N[int(f)]) * (c-k) d1 = key(N[int(c)]) * (k-f) return d0+d1 # median is 50th percentile. median = functools.partial(percentile, percent=0.5) ## end of http://code.activestate.com/recipes/511478/ }}}
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.