我知道這是這裏的帖子的常見話題,但是,我似乎無法找到一個真正幫助我解決問題的例子。我試圖運行一個腳本,它會將兩個新字段(x和y座標)添加到shapefile,然後使用代碼塊填充X和Y座標的字段。這是AcrGIS 10.2.2,但我不認爲這個問題是ArcGIS問題。以下腳本:AttributeError:'int'對象沒有屬性'x'
# Add new fields for "New_X" and "New_Y" for new points to be added
# Calculate values for those new fields based on distance along line
import arcpy, arcpy.mapping
from arcpy import env
env.workspace = r"G:\Geocomputation_Project\Section_C\Model_Shapes"
env.overwriteOutput = True
# Set local Variables
in_table = 'Points.shp'
field_x = 'New_X'
field_y = 'New_Y'
expression = "getXY(!Shape!, !ITEMID!, !CHAINAGE!)"
code_block_x = """def getXY (point, id, d2add):
mxd = arcpy.mapping.MapDocument("G:\Geocomputation_Project\Section_C\Model_Shapes\Geocomputation_Project.mxd")
lyr=arcpy.mapping.ListLayers(mxd,"LINES")[0]
q='"ITEMID"=%s%s%s' %(r"'",id,"'")
pNew = 0
with arcpy.da.SearchCursor(lyr,"[email protected]",q)as cursor:
for row in cursor:
line=row[0];break
pointPos=line.measureOnLine(point)+d2add
pNew+=line.positionAlongLine(pointPos).firstPoint
pNew.X"""
code_block_y = """def getXY (point, id, d2add):
mxd = arcpy.mapping.MapDocument("G:\Geocomputation_Project\Section_C\Model_Shapes\Geocomputation_Project.mxd")
lyr=arcpy.mapping.ListLayers(mxd,"LINES")[0]
q='"ITEMID"=%s%s%s' %(r"'",id,"'")
pNew = 0
with arcpy.da.SearchCursor(lyr,"[email protected]",q)as cursor:
for row in cursor:
line=row[0];break
pointPos=line.measureOnLine(point)+d2add
pNew+=line.positionAlongLine(pointPos).firstPoint
pNew.Y"""
# Execute AddField for each new X and Y coord
arcpy.AddField_management(in_table, field_x, "Double")
arcpy.AddField_management(in_table, field_y, "Double")
# Execute CalculateField to each new X and Y field
arcpy.CalculateField_management(in_table, field_x, expression, "PYTHON_9.3", code_block_x)
arcpy.CalculateField_management(in_table, field_y, expression, "PYTHON_9.3", code_block_y)
我不斷收到AttributeError:'int'對象沒有屬性'X'。
要下定決心 - 是在錯誤消息'int'或'list'? – Eric
對不起現在修好了 –
爲什麼你有'line = row [0]; break'? – Eric