试图在arcpy中总结列表

时间:2016-06-05 04:11:08

标签: cursor arcpy

我有一个xy点列表,我试图将它们加在一起并识别质心,但它只使用行中的最后一个值。我正在尝试为每个州创建一个质心,这是代码:

Total_X1 = 0
Total_Y1 = 0
TotalPop1 = 0
#Cat = "cali"
cntyName1 = "cnty"
stateName1 = "statename"
for row in cursor:

    #if row[0] >= : ### for condition that is met
    #if row[0]== []:
        TheStateName1 = row[0]
        thecntyName1 = row[4]
        idpoly1 = row[5]
        idobject1 = row[6]
        stateFIPS1 = row[7]
        countyFIPS1 = row[8]
        fips1 = row[9]
        fipSnum1 = row[10]
        fipsNumer1 = row[11]
        #totarea = row[12]
        XPoint = row [13]
        YPoint = row[14]


        #print Cat
        print TheStateName1
        print thecntyName1

        print row       ### do something with that value!

        Total_X1 +=  row[2] *row[3]
        print Total_X1


        Total_Y1 +=  row[1] *row[3]
        print Total_Y1


        TotalPop1 += row[3]
        print TotalPop1

    print ""        
    print "X is: " , Total_X1
    print "POP is: " , TotalPop1
    centroid_X1 = Total_X1/TotalPop1
    print "your x centroid is: ",centroid_X1
    print ""
    #print Cat
    print thecntyName1
    print TheStateName1

任何建议,谢谢!

1 个答案:

答案 0 :(得分:1)

光标一次只能“看到”一行,你必须从该行中提取信息并将其存储在其他地方。

loc_list = [(row[0], row[1]) for row in arcpy.da.SearchCursor(dataset, ['X_coord', 'Y_coord'])

将从属性表中为您提供X,Y元组的列表。

之后,您有多个选项可以在计算平均值之前将元组列表转换为空间数据集 - 首先阅读arcpy.Point的ESRI文档以及链接的所有相关主题,然后从那里开始。如果您有10.3或更高版本,则可以在有点图层后使用Mean Center

如果你只是先取X和Y的平均值而不先预测,你可能会得到一个错误的答案,所以不要。