我需要根据点的位置填充字段

问题描述

in_fc = "C:/Users/Olivia/Desktop/Desktop Developement/Module3_Lab_Data.gdb/Customers"

nearest_dict = dict()

with arcpy.da.SearchCursor(in_fc,["OID@","NEAR_FID"]) as rows:
    for row in rows:
        nearest_id = row[0] #get OID value
        input_id = row[1] #get Near_ID value
       
        if input_id in nearest_dict:
            #if a dict key already exists,append it
            nearest_dict[input_id].append(nearest_id)
            
        else:
            #if not,create new list with near_ID and add to dictionary
            nearest_dict[input_id]= [nearest_id]
            
print(nearest_dict)

我有5个总部,需要将最接近他们的客户分配给最接近的总部。这将打印出一个字典,其中包含与每个总部最接近的所有点或客户的OBJECTID。关键是总部编号,值是客户ID。如何用最接近的销售团队填充客户层中的字段?我得到了每个点应该到达的位置的结果,但是我不知道如何填充客户层中的字段。

解决方法

您可以创建一个附加的Update游标,以根据字典中的项目和值循环并更新行。

From the help - Use UpdateCursor to update a field value by evaluating the values of other fields.

with arcpy.da.UpdateCursor(fc,fields) as cursor:
    # For each row,evaluate the WELL_YIELD value and update WELL_CLASS
    for row in cursor:
        if (row[0] >= 0 and row[0] <= 10):
            row[1] = 1