环境:Win 7 Enterprise,SP1 | ArcGIS Desktop 10.0 (Python 2.6.5)
我正在尝试将 Arcpy Polyline 对象作为值存储在 Python 字典中,以便可以在数据比较工具中快速引用它们。
问题:一旦我的脚本退出用于将折线对象保存到字典的 FOR 循环,Python 字典中的所有折线对象都具有相同的属性(它们应该都不同)。下面的每个函数都会在内存中创建五个不同的 Polyline 对象,但是,ReadData1 对象的属性都不同,而 ReadData2 对象的属性都相同。
问题:为什么折线对象属性 (.firstPoint .centroid .lastPoint) 在将它们分配为字典值后立即正确,但在 FOR 循环完成后不正确?具体来说,为什么 ReadData1 输出中最终 Polyline 对象的属性与 ReadData2 输出中的所有对象相关联?
代码:
import arcpy
def ReadData1(inShapefile):
print arcpy.Describe(inShapefile).dataType, arcpy.Describe(inShapefile).shapeType
sRows = arcpy.SearchCursor(inShapefile)
dataDict = {}
for row in sRows:
dataDict[row.FID] = row.Shape
print '%s || %s || %s' % (dataDict[row.FID].firstPoint, dataDict[row.FID].centroid, dataDict[row.FID].lastPoint)
def ReadData2(inShapefile):
print arcpy.Describe(inShapefile).dataType, arcpy.Describe(inShapefile).shapeType
sRows = arcpy.SearchCursor(inShapefile)
dataDict = {}
for row in sRows:
dataDict[row.FID] = row.Shape
for key in dataDict:
print '%s || %s || %s' %(dataDict[key].firstPoint, dataDict[key].centroid, dataDict[key].lastPoint)
输出:
>>> ReadData1(r'U:\...\TestShapefile.shp')
ShapeFile Polyline
7590284.40892474 668332.064064309 NaN NaN || 7590324.42908373 666711.247624955 NaN NaN || 7592576.19984799 665145.042311657 NaN NaN
7592021.15673377 667418.928618325 NaN NaN || 7592961.14910463 667427.880926619 NaN NaN || 7593901.1414755 667436.833234913 NaN NaN
7594098.09225797 668421.587147249 NaN NaN || 7595183.14682072 667013.998170435 NaN NaN || 7596891.21244568 665682.180809295 NaN NaN
7593793.71377597 665897.03620835 NaN NaN || 7593516.19221886 665261.422319479 NaN NaN || 7593238.67066174 664625.808430607 NaN NaN
7590302.31354132 663336.676036276 NaN NaN || 7593453.20916493 663528.873380711 NaN NaN || 7596533.12011392 663873.814533914 NaN NaN
>>> ReadData2(r'U:\...\TestShapefile.shp')
ShapeFile Polyline
7590302.31354132 663336.676036276 NaN NaN || 7593453.20916493 663528.873380711 NaN NaN || 7596533.12011392 663873.814533914 NaN NaN
7590302.31354132 663336.676036276 NaN NaN || 7593453.20916493 663528.873380711 NaN NaN || 7596533.12011392 663873.814533914 NaN NaN
7590302.31354132 663336.676036276 NaN NaN || 7593453.20916493 663528.873380711 NaN NaN || 7596533.12011392 663873.814533914 NaN NaN
7590302.31354132 663336.676036276 NaN NaN || 7593453.20916493 663528.873380711 NaN NaN || 7596533.12011392 663873.814533914 NaN NaN
7590302.31354132 663336.676036276 NaN NaN || 7593453.20916493 663528.873380711 NaN NaN || 7596533.12011392 663873.814533914 NaN NaN
测试形状文件:http: //dl.dropbox.com/u/67329940/TestShapefile.zip
ESRI 折线对象文档: http ://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v000000n2000000
谢谢!