AnsweredAssumed Answered

GIS Tutorial for Python Scripting 2-7 cursor problem

Question asked by Evanrees on Oct 18, 2016
Latest reply on Oct 18, 2016 by blake.terhune

This is the example code of tutorial 2-7 of GIS Tutorial for Python Scripting by David Allen. I followed the example code from the book, but the cursor in line 83 wasn't there so I added it myself and it's not working properly. I´m not even sure I used the right cursor. Any help would be very much appreciated on getting it to work.

 

I'm using ArcMap 10.2.2 and I noticed env.overwriteOutput = True isn´t working properly, which could also be a problem here.

 

try:
    # import the modules
    import arcpy
    from arcpy import env

    # set up the environment
    env.workspace = "C:/EsriPress/GISTPython/MyExercises"
    env.overwriteOutput = True

    # prompt the user for the input table
    inTable = arcpy.GetParameterAsText(0)
    # when this is set up as a cursor tool, set the input to tables only

    # Get the fields from the input
    fields = arcpy.ListFields(inTable)

    # Create a fieldinfo object
    fieldinfo = arcpy.FieldInfo()

    # Define a fieldinfo object to bring only certain fields into the view
    # inci_no, alm_date, arv_date, inci_type
    # descript, station, shift, city
    # number, st_prefix, street, st_type, st_suffix
    # (you can´t add new fields to a table view, so reuse a discarded one)
    # Change the name of addr_2 to GeoAddress in the output table
    # Code was copied and modified from Help screen

    # interate through the fields and set them to fieldinfo
    for field in fields:
        if field.name == "inci_no":
            fieldinfo.addField(field.name, field.name , "VISIBLE", "")
        elif field.name == "alm_date":
            fieldinfo.addField(field.name, field.name , "VISIBLE", "")
        elif field.name == "alm_time":
            fieldinfo.addField(field.name, field.name , "VISIBLE", "")
        elif field.name == "arv_date":
            fieldinfo.addField(field.name, field.name , "VISIBLE", "")
        elif field.name == "arv.time":
            fieldinfo.addField(field.name, field.name , "VISIBLE", "")
        elif field.name == "inci_type":
            fieldinfo.addField(field.name, field.name , "VISIBLE", "")
        elif field.name == "descript":
            fieldinfo.addField(field.name, field.name , "VISIBLE", "")
        elif field.name == "station":
            fieldinfo.addField(field.name, field.name , "VISIBLE", "")
        elif field.name == "shift":
            fieldinfo.addField(field.name, field.name , "VISIBLE", "")
        elif field.name == "city":
            fieldinfo.addField(field.name, field.name , "VISIBLE", "")
        elif field.name == "number":
            fieldinfo.addField(field.name, field.name , "VISIBLE", "")
        elif field.name == "st_prefix":
            fieldinfo.addField(field.name, field.name , "VISIBLE", "")
        elif field.name == "street":
            fieldinfo.addField(field.name, field.name , "VISIBLE", "")
        elif field.name == "st_type":
            fieldinfo.addField(field.name, field.name , "VISIBLE", "")
        elif field.name == "st_suffix":
            fieldinfo.addField(field.name, field.name , "VISIBLE", "")
        elif field.name == "addr_2":
            fieldinfo.addField(field.name, "GeoAddress" , "VISIBLE", "")
        else:
            fieldinfo.addField(field.name, field.name , "HIDDEN", "")

    # Create a table view of the input table
    # The created fire_view table will have fields as set in fieldinfo object
    arcpy.MakeTableView_management(inTable, "fire_view", "", "", fieldinfo)

    # Do the address formatting into GeoAddress for the whole table
    # Concatenate number + st_prefix + street + st_type + st_suffix and remove spaces
    arcpy.CalculateField_management("fire_view","GeoAddress","str(!number!) + ' ' + !st_prefix!.strip() + ' ' + !street!.strip() + ' ' + !st_type!.strip() + ' ' + !st_suffix!.srip()", "PYTHON")

    # Create new geodatabase to store results for year
    # ("Fire Files for " + last 4 digits of file name)
    gdbName = "Fire_Files_For_" + inTable[-8:]
    arcpy.CreateFileGDB_management("C:\\EsriPress\\GISTPython\\MyExercises",gdbName)

    # Use cursor to find each unique city name and add it to a list.append
    # City names included may differ from file to file.
    # Set up a list to hold unique ity names.   

    # Start cursor iteration
    with arcpy.da.SearchCursor(inTable, ["city"]) as fireCursor:       
        for row in fireCursor:
            cityList = []
            cityName = row[23]
            if cityName not in cityList:
                cityList.append(cityName)
    # Result is a list object with all the unique values of the CITY field
        del row
        arcpy.AddWarning("Made the list of city names")

    # Use the names in the list object to select records
    for name in cityList:
        cityQuery = '"city" = \'' + name + '\''

        arcpy.SelectLayerByAttribute_management("fire_view", "NEW_SELECTION",cityQuery)

        newTable = "C:\\EsriPress\\GISTPython\\MyExercises\\" + gdbName + ".gdb\\" + name.replace(" ","_")

        itemCount = int(arcpy.GetCount_management("fire_view").getOutput(0))

        arcpy.AddWarning("A tale called " + newTable + " was created with " + str(itemCount) + " rows.")

except arcpy.ExecuteError:
    print arcpy.GetMessages(2)
except:
    print "Process did not complete."
       

Outcomes