2015-11-22 202 views
-1

我知道這是這裏的帖子的常見話題,但是,我似乎無法找到一個真正幫助我解決問題的例子。我試圖運行一個腳本,它會將兩個新字段(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'。

+0

要下定決心 - 是在錯誤消息'int'或'list'? – Eric

+0

對不起現在修好了 –

+0

爲什麼你有'line = row [0]; break'? – Eric

回答

0

假設錯誤的標題是正確的,而不是一個在後的身體,這裏是你的問題:

pNew = 0 
# ... 
pNew.Y 

這應該是很明顯,不是去上班

+0

以及我添加'pnew = 0'的全部原因是爲了避免在賦值之前引用的「UnboundLocalError:局部變量'pNew_Y' –

+1

在代碼中出現兩次,一次是X,一次是Y. –

0

我得到了它現在,似乎更多的是語法/縮進錯誤。還需要「返回」pNew變量。請參見下面的工作如下腳本:

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,"'") 
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 
return 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,"'") 
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 
return pNew.Y"""