使用Arcpy移动功能

时间:2017-02-05 15:08:30

标签: python arcpy

我希望使用ArcPy移动一些几何特征。但是,每次运行脚本时,我都会收到以下错误消息。有什么问题?

import arcpy
def shift_features (in_features):  
...  with arcpy.da.UpdateCursor(in_features, ['SHAPE@XY','XShift',YShift']) as cursor:  
...       for row in cursor:  
...           cursor.updateRow([[row[0][0] + (row[1] or 0), 
...                              row[0][1] + (row[2] or 0)]])  
...  return
...     

然后我把:

shape=r'E:\Yael\All Sorts\Testing\MovingPolygon.shp'
shift_features(shape)

(其中shape包含字段名称XShift,YShift)

我一直在:

  

解析错误SyntaxError:扫描字符串文字时的EOL

1 个答案:

答案 0 :(得分:0)

(我假设您的代码基于this ArcPy Café recipe。)

当你致电cursor.updateRow时,你需要向它传递一个参数:一个与它将使用的row列表长度相同的值列表。所以,例如......

with arcpy.da.UpdateCursor(feature, ['FIELD', 'FOO', BAR']) as cursor:
    for row in cursor:
        print row                # prints a list of 3 values -- ['a', 'b', 'c']
        row[0] = 'd'             # changes element 0 of list
        print row                # ['d', 'b', 'c']
        cursor.updateRow(row)    # passes ['d', 'b', 'c']

我只更改 FIELD的值,但还必须发回FOOBAR的值。我也可以缩短它:

with arcpy.da.UpdateCursor(feature, ['FIELD', 'FOO', BAR']) as cursor:
    for row in cursor:
        cursor.updateRow(['d', 'b', 'c'])    # will work

但是在列表中传递较少的值不会起作用:

with arcpy.da.UpdateCursor(feature, ['FIELD', 'FOO', BAR']) as cursor:
    for row in cursor:
        cursor.updateRow(['d'])    # will fail

(如果我传递太多值,它同样会中断 - 列表中的元素数量需要与UpdateCursor调用的字段数相匹配。)

因此,根据您的具体情况,您需要传回SHAPE@XYXShiftYShift的值。现在,它只获得SHAPE@XY(这是原始的片段配方正在使用)。

尝试:

with arcpy.da.UpdateCursor(in_features, ['SHAPE@XY']) as cursor:
    for row in cursor:
        cursor.updateRow([[row[0][0] + (row[1] or 0),
                           row[0][1] + (row[2] or 0)]], row[1], row[2])