custom geoprocessing tool error

253
1
05-20-2013 07:26 PM
DamonOsbourne
New Contributor II
Trying to complete a python tutorial that shows how to make custom tools.  I run the code below and it still produces an error message stating that "cur" is not defined.  I've experimented with the indentation, but still doesn't seem to run.  It appears that the problem is after "finally", the 'del cur' statement keeps the script from executing right.

import arcpy, os
try:
    outputFC = arcpy.GetParametersAsText(0)
    fClassTemaplate = arcpy.GetParametersAsText(1)
    f = open(arcpy.GetParameterAsText(2),'r')
    arcpy.CreateFeatureclass_management(os.path.split(outputFC)[0], os.path.split(outputFC[1]), "point", fClassTemplate)
    lstFires = f.readlines()
    cur = arcpy.InsertCursor(outputFC)
    cntr = 1
    for fire in lstFires:
        if 'Latitude' in fire:
            continue
        vals = fire.split(",")
        latitude = float(vals[0])
        longitude = float(vals[1])
        confid = int(vals[2])
        pnt = arcpy.Point(longitude, latitude)
        feat = cur.newRow()
        feat.shape = pnt
        feat.setValue("CONFIDENCEVALUE", confid)
        cur.insertRow(feat)
        arcpy.AddMessage("Record number" + str(cntr) + "written to feature class")
        cntr = cntr + 1
except:
    print arcpy.GetMessages()
finally:
    del cur
    f.close()
Tags (2)
0 Kudos
1 Reply
curtvprice
MVP Esteemed Contributor
It looks to me like you're getting an error somewhere in the code so "cur" never gets set.  Your except block is setup to report geoprocessing errors but not python errors, so you aren't seeing them.

try this:

import arcpy
import os
try:
    outputFC = arcpy.GetParametersAsText(0)
    fClassTemaplate = arcpy.GetParametersAsText(1)
    f = open(arcpy.GetParameterAsText(2),'r')
    arcpy.CreateFeatureclass_management(os.path.split(outputFC)[0], os.path.split(outputFC[1]), "point", fClassTemplate)
    lstFires = f.readlines()
    cur = arcpy.InsertCursor(outputFC)
    cntr = 1
    for fire in lstFires:
        if 'Latitude' in fire:
            continue
        vals = fire.split(",")
        latitude = float(vals[0])
        longitude = float(vals[1])
        confid = int(vals[2])
        pnt = arcpy.Point(longitude, latitude)
        feat = cur.newRow()
        feat.shape = pnt
        feat.setValue("CONFIDENCEVALUE", confid)
        cur.insertRow(feat)
        arcpy.AddMessage("Record number" + str(cntr) + "written to feature class")
        cntr = cntr + 1
except arcpy.ExecuteError:
    print arcpy.GetMessages()
except Exception, msg
    print msg
finally:
    try:
        del cur
        f.close()
    except:
        pass
0 Kudos