python-2.7 - 使用 SearchCursor 和 InsertCursor 从折线要素类填充字段
问题描述
我在将 [OID] 和 [FEA_KEY] 字段输入到我通过从线要素类中提取lastPoint 属性创建的点要素类中时遇到了一些问题。
错误是:
for item in sCurs:
RuntimeError: A column was specified that does not exist.
...因为我可以看到我在 outFc 中创建了必填字段。我不确定我在这里做错了什么。
## turn ss line feature vertices into points - try using only endpoints. Include the
## [OID] and [FEA_KEY] for each vertice in the outFc
def VerticesToPoints(inFc, outFc):
arcpy.env.overwriteOutput = True
try:
# let esri do it (advanced license)
arcpy.FeatureVerticesToPoints_management(inFc, outFc, "ALL")
return outFc
except:
pass
# no advanced license
sr = arcpy.Describe(inFc).spatialReference
outPath, outName = os.path.split(outFc)
start = time.clock()
arcpy.CreateFeatureclass_management(outPath,
outName,
"POINT",
spatial_reference=sr)
arcpy.AddField_management(outFc, "OID", "LONG")
arcpy.AddField_management(outFc, "FEA_KEY", "LONG")
end = time.clock()
sds.writeLog(logFile, "Create Feature Class {}".format(outFc))
## USE SHAPE@ which would give you a polyline geometry object. There is a lastPoint property on that object.
i = 0
with arcpy.da.SearchCursor(inFc, ["OID@", "SHAPE@", "OID", "FEA_KEY"]) as sCurs:
with arcpy.da.InsertCursor(outFc,["OID@", "SHAPE@", "OID", "FEA_KEY"]) as iCurs:
for item in sCurs:
iCurs.insertRow([item[0], (item[1].lastPoint.X, item[1].lastPoint.Y), item[2], item[3]])
# for testing purposes, just run through 12 of these
i += 1
if i >= 12:
break
return outFc
解决方案
找到了!通过使用 arcpy.ListFields() 打印要素类中的所有字段,发现 FEA_KEY 是别名 - 不是全名。在这里注意这一点,以防其他人被难住。
推荐阅读
- r - 如何在 R 中读取不同的数据类型作为用户输入?
- javascript - NodeJS & React w/ DevExpress - 导入变量抛出语法错误
- mongodb - 如果参数值不是数组,如何计算大小
- python - 在情节中从x轴中删除重复的年份
- r - 在 R 中不使用密钥访问 S3 存储桶
- vue.js - 观看从父母到孩子的道具变化
- javascript - SVG 动画(ICO 到 SVG 文件)
- django-models - 如何在 Django 中设计有向图模型?
- react-native - 字体大小在反应原生的不同设备中缩小(不响应)
- elasticsearch - 如何使用 elasticsearch 获取嵌套字段的所有值?