AnsweredAssumed Answered

da.UpdateCursor - Runtime Error: Object invalid or no longer set

Question asked by GivenToMap on Jun 16, 2015
Latest reply on Jun 17, 2015 by GivenToMap

Anybody have any insight on why this is failing? (Line 62 below).

 

I am trying to update a text field using a DA Update Cursor.  Please help!

 

Traceback (most recent call last):

  File "\\bctsdata.bcgov\tsg_root\General_User_Workspace\Recce_Data\scripts\UploadPDFReports\UploadPDFReports.py", line 135, in <module>

    UploadPDFReports(fcBlockPolygons, dirReceivedReports)

  File "\\bctsdata.bcgov\tsg_root\General_User_Workspace\Recce_Data\scripts\UploadPDFReports\UploadPDFReports.py", line 105, in UploadPDFReports

    cursor.updateRow(row)

RuntimeError: Object invalid or no longer set.

 

 

def GetOperArea_SpatialIntersect(fcOperArea, flds, shp):
    fcOperArea = gp.MakeFeatureLayer_management(fcOperArea)
    gp.SelectLayerByLocation_management(fcOperArea, "INTERSECT", shp )
    with arcpy.da.SearchCursor(fcOperArea, flds) as cursor:
        for row in cursor:
            #gp.AddMessage("District:{0} OpArea:{1}".format(row[0], row[1]))
            sDist = row[0]
            sOp = row[1]
            sBlkRepDir = os.path.join(sReportDir, sDist, sOp)
            #gp.AddMessage(sBlkRepDir)
            if os.path.exists(sBlkRepDir):
                return sBlkRepDir
            else:
                os.makedirs(sBlkRepDir)
                gp.AddMessage("**Need to create output directory:{}".format(sBlkRepDir))
                return sBlkRepDir


def UploadPDFReports(fcRecceBlks, dirReports):
  lstDist = []
  startDir = sReportDir
  #--Hardcoded field values that must exist
  #--Block Polygon attributes
  fldBlkName = "BLOCK_NAME"
  fldLink = "FILE_LINK"
  #--Operating Area attributes
  fldDist = "MOF_DISTRI"
  fldOpCode = "OP_AREA"
  fldOpArea = "AREA_NAME"
  #------------------------------------------------------------------------------------------------------------
  #Loop through PDF Reports
  #------------------------------------------------------------------------------------------------------------
  #print dirReports
  #gp.AddMessage(dirReports)
  for dirpath, dirnames, filenames in os.walk(dirReports, True):
      for file in filenames:
          if "_RecceReport.pdf" in file:
              sBlock = file.split("_")[0]
              gp.AddMessage("Recce Report: " + sBlock)
              #------------------------------------------------------------------------------------------------------------
              #Loop through associated block spatial
              #------------------------------------------------------------------------------------------------------------
              iCnt = 0
              exp = fldBlkName + " = '{0}'".format(sBlock)
              gp.AddMessage(exp)
              with arcpy.da.UpdateCursor(fcRecceBlks, ("SHAPE@XY", fldLink), where_clause=exp) as cursor:
                  for row in cursor:
                      #--Get the centroid of the polygon.  Use the polygon centroid to determine which Op Area it's in
                      pnt = row[0]
                      shp = arcpy.Point()
                      shp.X = pnt[0]
                      shp.Y = pnt[1]
                      gp.AddMessage(row[1])
                      sCurrLink = NzStr(row[1])
                      pntGeom = arcpy.PointGeometry(shp)
                      #--Get the directory to store the report based on the spatial operating area
                      #lyrOpArea = gp.MakeFeatureLayer_management(fcOpArea)
                      sBlkRepDir = GetOperArea_SpatialIntersect(fcOpArea, (fldDist, fldOpArea), pntGeom)
                      sLink = os.path.join(sReportDir, sBlkRepDir, file)
                      gp.AddMessage("*{0}".format(sLink))
                      row[1] = sLink
                      cursor.updateRow(row)

Outcomes