首页 > 解决方案 > 使用 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

标签: python-2.7arcgisarcpy

解决方案


找到了!通过使用 arcpy.ListFields() 打印要素类中的所有字段,发现 FEA_KEY 是别名 - 不是全名。在这里注意这一点,以防其他人被难住。


推荐阅读