python - 用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!!!'
我希望它应该通过用1和0填充内部字段来正常运行,但它会在第 19 行停止并出现以下错误:
Runtime error
Traceback (most recent call last):
File "<string>", line 18, in <module>
SystemError: error return without exception set
我在这里做错了什么?
解决方案
Arcpy 游标返回 a iterator
,而不是 a list
。这polygon[0]
就是您获得SystemError
.
polygon
您的光标中似乎只有一个多边形。使用该方法.next
转到第一个(也是唯一一个)多边形。
之后:
polygon = arcpy.da.SearchCursor(polygons,['SHAPE@'],'polygonid = '+str(LINEvalue))
添加:
polygon.next()
然后,使用polygon[0]
将返回光标中第一个多边形的形状。
推荐阅读
- c - 队列卡在循环中
- ios - 在后台一段时间后将应用程序带到前台时,iOS黑屏
- sockets - Python 中的 Google 语音 API
- sql - 大型 MS SQL Server 表非常慢
- python - 为什么 get_success_url 一直将我引导到同一页面?(姜戈)
- angular - 从ionic3中的模态取回数据
- javascript - ReactiveSearch 中的自定义组件
- python - 按条件从列表中排除值的循环失败
- xamarin - 如何在 VS 2019 RC 中安装 Xamarin.Forms 4.0 模板?
- excel - VBA:如果然后在之后有2个语句