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

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. May 20 at 11:09
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

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.