max value of a field as variable

731
3
06-02-2020 02:50 AM
AlfonsZitterbacke
New Contributor II

Hello, 

With the following script I was able to determine the maximum values of a column [POINT_Z] for certain groups from [ID_1].

    #creating a list ->"mast_value" with unique values of field [ID_1]
    with arcpy.da.SearchCursor(inLAS[:-4] + '_POLE_POINTS.SHP','ID_1') as max_cursor:
        for row in max_cursor:
            mast_value = sorted({row[0] for row in max_cursor})

    #iterate through the list to find highest value in field [pointz], but only in features which have the value of the element of the list "mast_value"
    field = "ID_1"
    for mast in mast_value:
        abfrage = field + " = " + str(mast)
        with arcpy.da.SearchCursor(inLAS[:-4] + '_POLE_POINTS.SHP',('POINT_Z', 'FID'), where_clause=abfrage) as mast_cursor:
            mylist = []
            for row1 in mast_cursor:
                #print('ID ' + str(mast) + u': max altitude ' + str(max(mast_cursor)))
                print(str(max(mast_cursor)))‍‍‍‍‍‍‍‍‍‍‍‍‍‍

With the output I can display the results. A manual check showed that the result is correct.

ID 1: max altitude (477.56, 5997) 
ID 7: max altitude (523.27, 3401) 
ID 16: max altitude (495.69, 3203) 
ID 38: max altitude (495.69, 5574) 
ID 39: max altitude (470.16, 5986) 
ID 44: max altitude (503.93, 4713) 
ID 63: max altitude (503.97, 4185) 
ID 73: max altitude (463.67, 4205) 
ID 75: max altitude (518.59, 2880) 
ID 77: max altitude (463.63, 5877) 
ID 79: max altitude (485.91, 5594) 
ID 87: max altitude (482.06, 853) 
ID 88: max altitude (475.2, 1836) 
ID 91: max altitude (492.0, 1074) 
ID 97: max altitude (468.77, 1323) 
ID 98: max altitude (462.52, 1546) 
ID 99: max altitude (463.81, 1294)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

To continue working (selection), I need the corrosponding value [FID] (second value in the brackets) of the maximum value of [POINT_Z] as a variable. My attempt to write the result into a variable

myMastList = str(max(mast_cursor))

fails with an error message.

File "E:\Documents\LAS\Las_Wire.py", line 264, in masten myMastList = str(max(mast_cursor))ValueError: max() arg is an empty sequence ‍

How can I write the value of [FID] (second value in the brackets in the output) from the highest value of [POINT_Z] (or both [FID] and [POINT_Z]) into a variable?

Tags (1)
0 Kudos
3 Replies
DavidPike
MVP Frequent Contributor

I t's very hard to follow your code in that format.

can you populate a list in 

mylist = []

for row1 in mast_cursor:               

print('ID ' + str(mast) + u': max altitude ' + str(max(mast_cursor)))  

mylist.append…..

0 Kudos
AlfonsZitterbacke
New Contributor II

Hi David, 

sorry for the missing syntax highlighting. I corrected my initial post, so it should be less confusing.

Furthermore I tried your sugestion, but unfortuntaly I got the same error message:

ValueError: max() arg is an empty sequence

#creating a list ->"mast_value" with unique values of field [ID_1]
 with arcpy.da.SearchCursor(inLAS[:-4] + '_POLE_POINTS.SHP','ID_1') as max_cursor:
     for row in max_cursor:
         mast_value = sorted({row[0] for row in max_cursor})

#iterate through the list to find highest value in field [pointz], but only in features which have the value of the element of the list "mast_value"
 field = "ID_1"
 for mast in mast_value:
     abfrage = field + " = " + str(mast)
     with arcpy.da.SearchCursor(inLAS[:-4] + '_POLE_POINTS.SHP',('POINT_Z', 'FID'), where_clause=abfrage) as mast_cursor:
         mylist = []
         for row1 in mast_cursor:
         #print('ID ' + str(mast) + u': max altitude ' + str(max(mast_cursor)))
             mylist.append(max(mast_cursor))
             print(str(max(mast_cursor)))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
DavidPike
MVP Frequent Contributor

I'm not entirely sure of what's going on, why the dictionary comprehension in the first cursor?

Also I don't see the need for the SQL filter (where clause), can you not evaluate it for each row - if ID=x... row.append

Also think you can instantiate the cursor once, i.e. put it above for mast in mast_value: then call max_cursor.reset()

0 Kudos