首页 > 解决方案 > 用python检查的各个多边形内的线

问题描述

我在 GDB 中有两个要素类:
- 具有 65.000 个要素的多边形要素类和
- 具有 3.000.000 个要素的线要素类。
这些要素类中的每一个都有一个多边形字段,可以将线与每个相应的多边形链接起来。
我想检查具有相同多边形ID的线是否在相应的多边形内。
如果这是真的,那么线要素类中调用的字段填充1,在任何其他情况下填充0

为此,我编写了以下代码:

print u'Define line FC....'
LINES = ur'D:\MyGDB\Lines'
print u'    Completed'
print u'Define polygon FC....'
polygons = ur'D:\MyGDB\polygons'
print u'    Completed'
print u'Create the ID list of polygons....'
LINEList = sorted(set([row[0] for row in arcpy.da.SearchCursor(LINES,'polygonid')]))
print u'    '+str(len(LINEList))+u' polygons. Completed'
for LINEvalue in LINEList:
    print u'Select polygon with id: "'+str(LINEvalue)+u'"'
    polygon = arcpy.da.SearchCursor(polygons,['SHAPE@'],'polygonid = '+str(LINEvalue))
    print u'    Completed'
    cnt = 0
    with arcpy.da.UpdateCursor(LINES,['SHAPE@','within'],'polygonid = '+str(LINEvalue)) as cur:
        for row in cur:
            cnt += 1
            row[1] = 1 if row[0].within(arcpy.Geometry(polygon[0]),'BOUNDARY') else 0
            cur.updateRow(row)
    print u'The '+str(cnt)+' lines of the polygon with id "'+str(LINEvalue)+u'" were checked!'
del POLYList
del polygons
del LINES
print u'!!!FINISH!!!'

我希望它应该通过用10填充内部字段来正常运行,但它会在第 19 行停止并出现以下错误:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 18, in <module>
SystemError: error return without exception set

我在这里做错了什么?

标签: pythonarcgisarcpy

解决方案


Arcpy 游标返回 a iterator,而不是 a list。这polygon[0]就是您获得SystemError.

polygon您的光标中似乎只有一个多边形。使用该方法.next转到第一个(也是唯一一个)多边形。

之后:
polygon = arcpy.da.SearchCursor(polygons,['SHAPE@'],'polygonid = '+str(LINEvalue))
添加:
polygon.next()

然后,使用polygon[0]将返回光标中第一个多边形的形状。


推荐阅读