2

环境: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

谢谢!

4

2 回答 2

2

您遇到了 ArcGIS 10.0 的一个已知问题。它已在 10.1 中修复,这就是我现在正在使用的。但是我曾经通过将形状转换为列表,然后像这样重新创建它来解决 10.0 上的问题:

tractfc = 'c:/temp/test.shp'

rows = arcpy.SearchCursor(tractfc)
dct = {}
for row in rows:
    shp = row.Shape
    lst = list(shp)
    ary = arcpy.Array(lst)
    newshp = arcpy.Polyline(ary, shp.spatialReference)
    dct[row.FID] = newshp

for k in dct:
    print k, dct[k].firstPoint

您可以将 geojson 转换与 AsShape 一起使用并使用 AsShape 进行__geo_interface__重建,但这样会丢失空间参考。上述方法保留了空间参考。

于 2013-01-20T19:56:19.817 回答
1

您必须设置 arcpy.Geometry() 来存储您的 row.Shape ,否则您将丢失几何图形。这是上面代码中的一个示例。

tractfc = 'c:/temp/test.shp' 
rows = arcpy.SearchCursor(tractfc)
dct = {}

for row in rows:
   g = arcpy.Geometry()
   g = row.Shape
   dct[row.FID] = g

for k in dct:
   print k, dct[k].firstPoint.X, dct[k].firstPoint.Y
于 2013-06-21T14:37:54.063 回答