AnsweredAssumed Answered

Python script keep producing error 000732

Question asked by rbasaria_DTS on Sep 26, 2017
Latest reply on Sep 28, 2017 by steven_haymes

Hello.

 

I've been searching around trying to figure out why this is happening, but can't seem to find much. I tried looking into ESRI's workarounds for this error, but no luck with those.

 

I am writing a Python script to be used as an Arc tool. I am down to the last 3 lines of code in the script, and I keep getting this 000732 error.

 

Traceback (most recent call last):
File "C:\Projects\CreateSamples\CreateSamples.py", line 147, in <module>
arcpy.CalculateField_management(sampevents, "SAMPLE_ID", "[FID] + 1", "VB", "")
File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\management.py", line 3360, in CalculateField
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Input Table: Dataset C:\Projects\CreateSamples\SampleFeatureClasses\Sample_Events does not exist or is not supported
Failed to execute (CalculateField).


Failed to execute (CreateSamplesTest).

However, the Sample_Events shapefile in question does in fact exist. It is an output of the previous step in the script. In addition, all paths do work. They are set as workspaces by the user when running the tool. Other data within these folders seems to work fine, but for some reason this one layer is holding me up from finishing this thing.

 

I can add the shapefile to ArcMap and do the field calc, table export, and event layer creation manually, but ideally it should all work within the script/tool.

 

Copied the entire script below. Not sure if I am copying it correctly...

 

Thanks for any help!

 

import arcpy, sys, string, os, subprocess

centerline = arcpy.GetParameterAsText(0)
stname = arcpy.GetParameterAsText(1)
dtsid = arcpy.GetParameterAsText(2)
width = arcpy.GetParameterAsText(3)
gisdata = arcpy.GetParameterAsText(4)
sampfcdata = arcpy.GetParameterAsText(5)

#Convert centerline into an LRS for creating event layer later.
arcpy.CreateRoutes_lr(centerline, dtsid, gisdata+"\\"+centerline+"_LRS", "LENGTH", "", "", "LOWER_LEFT", "1", "0", "IGNORE", "INDEX")

#TEMP DELETE FIELDS
addfields = 0
with arcpy.da.SearchCursor(centerline, "SAMPINS") as cursor:
    for row in cursor:
        addfields = max(int(row[0]), addfields)    
fieldtag = str(addfields)
while (addfields > 1):
    arcpy.DeleteField_management(centerline, ["F_"+fieldtag])
    addfields = addfields - 1
    fieldtag = str(addfields)
arcpy.DeleteField_management(centerline, ["ADJWIDTH", "ESTAREA", "RAWSAMP", "SAMPLES", "SAMPAREA", "SA_W", "SAMPLF", "ADJAREA", "ADJUSTED", "SAMPINS", "SAMP_1", "SAMPDIST", "SAMPLEN", "SAMPSTRT", "LENGTH", "WIDTH", "SAMPLE_ID", "SEGMENT_ID", "BEGIN_MP", "END_MP"])
#TEMP DELETE FIELDS

#Add and calculate necessary fields to create sample event measures.                  
arcpy.AddGeometryAttributes_management(centerline, "LENGTH", "FEET_US", "","")
length = "LENGTH"

arcpy.AddField_management(centerline, "ADJWIDTH", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.CalculateField_management(centerline, "ADJWIDTH", "["+width+"]", "VB", "")
arcpy.SelectLayerByAttribute_management(centerline, "NEW_SELECTION", "\"ADJWIDTH\" > 26")
arcpy.CalculateField_management(centerline, "ADJWIDTH", "26", "VB", "")
arcpy.SelectLayerByAttribute_management(centerline, "CLEAR_SELECTION", "")

arcpy.AddField_management(centerline, "ESTAREA", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(centerline, "RAWSAMP", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(centerline, "SAMPLES", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(centerline, "SAMPAREA", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(centerline, "SA_W", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(centerline, "SAMPLF", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(centerline, "ADJAREA", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(centerline, "ADJUSTED", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(centerline, "SAMPINS", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(centerline, "SAMP_1", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(centerline, "SAMPDIST", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(centerline, "SAMPLEN", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(centerline, "SAMPSTRT", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

arcpy.CalculateField_management(centerline, "ESTAREA", "["+length+"] * [ADJWIDTH]", "VB", "")
arcpy.CalculateField_management(centerline, "RAWSAMP", "[ESTAREA]/2500", "VB", "")
arcpy.CalculateField_management(centerline, "SAMPLES", "Round(([RAWSAMP] + 0.5),0)", "VB", "")
arcpy.CalculateField_management(centerline, "SAMPAREA", "[ESTAREA] / [SAMPLES]", "VB", "")
arcpy.CalculateField_management(centerline, "SA_W", "[SAMPAREA] / [ADJWIDTH]", "VB", "")
arcpy.SelectLayerByAttribute_management(centerline, "NEW_SELECTION", "\"SA_W\" <= 50")
arcpy.CalculateField_management(centerline, "SAMPLF", "50", "VB", "")
arcpy.SelectLayerByAttribute_management(centerline, "NEW_SELECTION", "\"SA_W\" > 50 AND \"SA_W\" <= 75")
arcpy.CalculateField_management(centerline, "SAMPLF", "75", "VB", "")
arcpy.SelectLayerByAttribute_management(centerline, "NEW_SELECTION", "\"SA_W\" > 75 AND \"SA_W\" <= 125")
arcpy.CalculateField_management(centerline, "SAMPLF", "100", "VB", "")                                       
arcpy.SelectLayerByAttribute_management(centerline, "NEW_SELECTION", "\"SA_W\" > 125 AND \"SA_W\" <= 175")
arcpy.CalculateField_management(centerline, "SAMPLF", "150", "VB", "")                                        
arcpy.SelectLayerByAttribute_management(centerline, "NEW_SELECTION", "\"SA_W\" > 175")                                                                
arcpy.CalculateField_management(centerline, "SAMPLF", "200", "VB", "")
arcpy.SelectLayerByAttribute_management(centerline, "CLEAR_SELECTION", "")
arcpy.CalculateField_management(centerline, "ADJAREA", "[ADJWIDTH] * [SAMPLF]", "VB", "")
arcpy.SelectLayerByAttribute_management(centerline, "NEW_SELECTION", "\"ADJAREA\" < 1500")
arcpy.CalculateField_management(centerline, "ADJAREA", "1500", "VB", "")
arcpy.SelectLayerByAttribute_management(centerline, "NEW_SELECTION", "\"ADJAREA\" > 3500")
arcpy.CalculateField_management(centerline, "ADJAREA", "3500", "VB", "")
arcpy.SelectLayerByAttribute_management(centerline, "CLEAR_SELECTION", "")                                        
arcpy.CalculateField_management(centerline, "ADJUSTED", "Round((([ESTAREA] / [ADJAREA]) + 0.5),0)", "VB", "")
arcpy.SelectLayerByAttribute_management(centerline, "NEW_SELECTION", "\"ADJUSTED\" < 6")                                        
arcpy.CalculateField_management(centerline, "SAMPINS", "1", "VB", "")
arcpy.SelectLayerByAttribute_management(centerline, "NEW_SELECTION", "\"ADJUSTED\" >= 6 AND \"ADJUSTED\" < 11")
arcpy.CalculateField_management(centerline, "SAMPINS", "2", "VB", "")
arcpy.SelectLayerByAttribute_management(centerline, "NEW_SELECTION", "\"ADJUSTED\" >= 11 AND \"ADJUSTED\" < 16")
arcpy.CalculateField_management(centerline, "SAMPINS", "3", "VB", "")
arcpy.SelectLayerByAttribute_management(centerline, "NEW_SELECTION", "\"ADJUSTED\" >= 16 AND \"ADJUSTED\" < 41")
arcpy.CalculateField_management(centerline, "SAMPINS", "4", "VB", "")
arcpy.SelectLayerByAttribute_management(centerline, "NEW_SELECTION", "\"ADJUSTED\" > 40")
arcpy.CalculateField_management(centerline, "SAMPINS", "Round(([ADJUSTED] * 0.17),0)", "VB", "")
arcpy.SelectLayerByAttribute_management(centerline, "CLEAR_SELECTION", "")
arcpy.CalculateField_management(centerline, "SAMP_1", "(["+length+"] - ([SAMPLF]*[SAMPINS])) / ([SAMPINS] + 1)", "VB", "")
arcpy.CalculateField_management(centerline, "SAMPDIST", "[SAMP_1]", "VB", "")
arcpy.CalculateField_management(centerline, "SAMPLEN", "["+length+"]", "VB", "")
arcpy.CalculateField_management(centerline, "SAMPSTRT", "0", "VB", "")
arcpy.SelectLayerByAttribute_management(centerline, "NEW_SELECTION", "\"SAMPLEN\" >= 200")
arcpy.CalculateField_management(centerline, "SAMPLEN", "[SAMPLF]", "VB", "")
arcpy.CalculateField_management(centerline, "SAMPSTRT", "[SAMP_1]", "VB", "")
arcpy.SelectLayerByAttribute_management(centerline, "CLEAR_SELECTION", "")

#Add fields based on the maximum number of samples, and calculate.
addfields = 0
with arcpy.da.SearchCursor(centerline, "SAMPINS") as cursor:
    for row in cursor:
        addfields = max(int(row[0]), addfields)    
fieldtag = str(addfields)
while (addfields > 1):
    arcpy.AddField_management(centerline, "F_"+fieldtag, "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
    arcpy.SelectLayerByAttribute_management(centerline, "NEW_SELECTION", "\"SAMPINS\" > "+fieldtag+" - 1")
    arcpy.CalculateField_management(centerline, "F_"+fieldtag, "[SAMPSTRT] + (([SAMPDIST] + [SAMPLF])*("+fieldtag+"-1))", "VB", "")
    arcpy.SelectLayerByAttribute_management(centerline, "SWITCH_SELECTION", "")
    arcpy.CalculateField_management(centerline, "F_"+fieldtag, "0", "VB", "")
    arcpy.SelectLayerByAttribute_management(centerline, "CLEAR_SELECTION", "")
    addfields = addfields - 1
    fieldtag = str(addfields)

#Add fields to improve formatting.
arcpy.AddField_management(centerline, "SAMPLE_ID", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(centerline, "SEGMENT_ID", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(centerline, "NAME", "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(centerline, "WIDTH", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(centerline, "BEGIN_MP", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(centerline, "END_MP", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.CalculateField_management(centerline, "SEGMENT_ID", "["+dtsid+"]", "VB", "")
arcpy.CalculateField_management(centerline, "NAME", "["+stname+"]", "VB", "")
arcpy.CalculateField_management(centerline, "WIDTH", "[ADJWIDTH]", "VB", "")
arcpy.CalculateField_management(centerline, "BEGIN_MP", "[SAMPSTRT]", "VB", "")
arcpy.CalculateField_management(centerline, "END_MP", "[SAMPSTRT] + [SAMPLEN]", "VB", "")

#Export first sample measures. Only export specific fields.
arcpy.FeatureClassToFeatureClass_conversion(centerline, sampfcdata, "Samples_1", "", "[SAMPLE_ID], [SEGMENT_ID], [NAME], [WIDTH], [LENGTH], [BEGIN_MP], [END_MP]")

#Select based on data that exists in F_n fields, and calculate BMP and EMP fields based on selection. Then, export that selection as a new feature class.
addfields = 0
with arcpy.da.SearchCursor(centerline, "SAMPINS") as cursor:
    for row in cursor:
        addfields = max(int(row[0]), addfields)    
fieldtag = str(addfields)
while (addfields > 1):
    arcpy.SelectLayerByAttribute_management(centerline, "NEW_SELECTION", "\"F_"+fieldtag+"\" <> 0")
    arcpy.CalculateField_management(centerline, "BEGIN_MP", "[F_"+fieldtag+"]", "VB", "")
    arcpy.CalculateField_management(centerline, "END_MP", "[F_"+fieldtag+"]+[SAMPLEN]", "VB", "")
    arcpy.FeatureClassToFeatureClass_conversion(centerline, sampfcdata, "Samples_"+fieldtag, "", "[SAMPLE_ID], [SEGMENT_ID], [NAME], [WIDTH], [LENGTH], [BEGIN_MP], [END_MP]")
    arcpy.SelectLayerByAttribute_management(centerline, "CLEAR_SELECTION", "")
    addfields = addfields - 1
    fieldtag = str(addfields)

#Merge all exported feature classes.
arcpy.env.workspace = sampfcdata
fclist = arcpy.ListFeatureClasses()
sampevents = "C:\\Projects\\CreateSamples\\SampleFeatureClasses\\Sample_Events"
arcpy.Merge_management(fclist, sampevents)

#Calculate SAMPLE_ID field.
arcpy.CalculateField_management(sampevents, "SAMPLE_ID", "[FID] + 1", "VB", "")
   
#Export table.
arcpy.TableToTable_conversion(sampevents, gisdata, "Sample_Events_Table")
   
#Make route event layer\samples feature class.
arcpy.MakeRouteEventLayer_lr(gisdata+"\\"+centerline+"_LRS", dtsid, gisdata+"Sample_Events_Table", ["LINE", "[BEGIN_MP]", "[END_MP]"], gisdata+"\\"+centerline+"_Samples")

Outcomes