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
-
@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.soumik chakraborty– soumik chakraborty2021年05月20日 11:09:32 +00:00Commented May 20, 2021 at 11:09
1 Answer 1
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)
-
Thanks will surely try this out.soumik chakraborty– soumik chakraborty2021年05月20日 12:48:33 +00:00Commented May 20, 2021 at 12:48
Explore related questions
See similar questions with these tags.