2

So I have performed Spatial Join between a point and line feature class. Now for each point there are several lines. What I want is the maximum count of line features as an attribute value in the "Count" field based on "label" field same attribute values. enter image description here

I have thought of few ways to do it but isn't working exactly. I can also add a new field and write the maximum of those "Count" values there.

Here I was trying to convert it into a numpy array and then calculate the maximum.

 '''try:
 arr_label_count= arcpy.da.FeatureClassToNumPyArray("SPATIAL_JOIN_OUTPUT_Splice_Closures_Fibre_Drop_copy",
 ["label", "Count"], skip_nulls=True)
 arcpy.AddMessage(arr_label_count)
 except Exception as e:
 arcpy.AddMessage("Issue in arr_label_count...: " + str(e))
 try:
 arr_label= arcpy.da.FeatureClassToNumPyArray("SPATIAL_JOIN_OUTPUT_Splice_Closures_Fibre_Drop_copy",
 ["label"], skip_nulls=True)
 arcpy.AddMessage(arr_label)
 filtered_arr_label = self.remove_duplicates(arr_label)
 # arcpy.AddMessage(filtered_arr_label)
 except Exception as e:
 arcpy.AddMessage("Issue in arr_label_count...: " + str(e))'''

Here is an image of how the array looks - enter image description here

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked May 20, 2021 at 10:12
1
  • @BERA i want the results in that field or in a new field where it will simply match the labels and write the maximum values from "Count" for that label. I hope this makes sense. Commented May 20, 2021 at 11:09

1 Answer 1

3

To set your COUNT field to the largest value per Label you can list the values, sort by value, and store in a dictionary. Then update/calculate using da.UpdateCursor:

import arcpy
fc = r'C:\GIS\ArcMap_default_folder\Default.gdb\my_sample'
group_field = 'KKOD' #In your case Label
value_field = 'VAL' #In your case Count
vals = [i for i in arcpy.da.SearchCursor(fc, [group_field, value_field])]
vals.sort(key=lambda x: x[1]) #Sort the values by value field so largest value is last
maxval_per_group = {i[0]:i[1] for i in vals} #Create a dictionary with group as key and last=largest value as value
with arcpy.da.UpdateCursor(fc, [group_field, value_field]) as cursor:
 for row in cursor:
 if row[0] in maxval_per_group:
 row[1] = maxval_per_group[row[0]]
 cursor.updateRow(row)

enter image description here

answered May 20, 2021 at 11:22
1
  • Thanks will surely try this out. Commented May 20, 2021 at 12:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.