2015-11-22 205 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"""