使用arcpy Polyline对象作为字典值时,形状信息丢失

时间:2013-01-09 23:56:25

标签: python dictionary arcgis shapefile arcpy

环境:Win 7 Enterprise,SP1 | ArcGIS Desktop 10.0(Python 2.6.5)

我试图将Arcpy Polyline对象存储为Python字典中的值,以便可以在数据比较工具中快速引用它们。

问题: 一旦我的脚本退出用于将Polyline对象保存到字典的FOR循环,Python字典中的所有Polyline对象都具有相同的属性(它们应该都是不同的)。下面的每个函数在内存中创建五个不同的Polyline对象,但是,ReadData1对象的属性都是不同的,而ReadData2对象的属性都是相同的。

问题: 为什么Polyline对象属性(.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

测试Shapefile: http://dl.dropbox.com/u/67329940/TestShapefile.zip

ESRI Polyline对象文档: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v000000n2000000

谢谢!

2 个答案:

答案 0 :(得分: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

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

答案 1 :(得分:1)

您必须设置arcpy.Geometry()来存储您的行。否则您将丢失几何体。这里是上面代码中的一个例子。

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