迭代属性表(ArcGIS 10.1,Python)

时间:2014-01-08 18:02:04

标签: python r

我希望在ArcGIS / arcpy框架中使用python复制我在R中循环表的技术。具体来说,是否有一种实用的方法可以使用python循环遍历属性表的行,并根据以前表值的值复制该数据? 例如,使用R我会使用类似于以下的代码来复制一个表中具有特定变量唯一值的数据行:

## table name:  data
## variable of interest:  variable
## new table:  new.data

for (i in 1:nrow(data))
{
    if (data$variable[i] != data$variable[i-1]) 
    {
        rbind(new.data,data[i,])
    }
}

如果我已经正确地编写了上面的代码,那么for循环只是检查表中的当前值是否与前一个值不同,并将该行的所有列值添加到新表中它实际上是一个新的价值。对这个思考过程的任何帮助都会很棒。 谢谢!

2 个答案:

答案 0 :(得分:1)

要在arcpy中的字段中获取表中的唯一值:

import arcpy
table = "mytable"
field = "my_field"

# ArcGIS 10.0
unique_values = set(row.getValue(field) for row in iter(arcpy.SearchCursor(table).next, None))
# ArcGIS 10.1+
unique_values = {row[0] for row in arcpy.da.SearchCursor(table, field)}

答案 1 :(得分:0)

是,使用要使用游标的arcpy循环遍历表中的值。自从我使用arcpy之后已经有一段时间了,但是如果我没记错的话你想要的是一个搜索光标。最简单的形式就是这样:

import arcpy
curObj = arcpy.SearchCursor(r"C:/shape.shp")
row = curObj.next()
while row:
    columnValue = row.getValue("columnName")
    row = curObj.next()

从版本10开始(我认为)他们引入了数据访问光标,其速度提高了几个数量级。数据访问或DA游标要求您在创建游标时声明要返回的列。例如:

import arcpy
columns = ['column1', 'something', 'someothercolumn']
curObj = arcpy.da.SearchCursor(r"C:/somefile.shp", columns)
for row in curObj:
    print 'column1 is', row[0]
    print 'someothercolumn is', row[2]
相关问题