I am trying to create county level maps by looping through a master file for each county. However, I get an error message when I try to run the code. I'm not sure what is wrong with my code and I would like to know if the loops are coded correctly. Could someone help me find out what's wrong?
## Import system modules
import arcpy
## Set workspace where output files will be saved
# NOTE: background processing was disabled in geoprocessing options in order to access current map
# NOTE: geoprocessing output overwrite enabled in geoprocessing options to allow replacement of previous runs
from arcpy import env
origSetting = env.overwriteOutput # define variable to be used at end of code to change setting back to default
env.overwriteOutput = True # change environment setting to overwrite output
arcpy.env.workspace = r"T:\HCI\Data\Insurance_187\Maps" # define workspace where output files would be saved
## Identify mapping document to be referenced in code
mxd = arcpy.mapping.MapDocument("CURRENT") # define map document currently opened in ArcGIS
#mxd = arcpy.mapping.MapDocument(r"T:\HCI\Shapefiles\Inset_Maps\CountyInsetMaps.mxd") # define map document based on file location
df = mxd.activeDataFrame # define current data frame
mxd.author = "Jacqueline Chan" # Set map author
## create local variables
#counties = arcpy.mapping.Layer("T:\HCI\Shapefiles\Counties2010.lyr") # create variable for shapefile directly referencing layer in file directory
masterfile = "census_tracts2010" # create variable for shapefile referencing layer in current map
field = "County_Name" # create variable for fields
cursor = arcpy.SearchCursor(masterfile) # create variable for loop
names = [] # empty list
basemap = ["Cities","tl_rd13_06_prisecroads_I","tl_rd13_06_prisecroads_M","tl_rd13_06_prisecroads_C","tl_rd13_06_prisecroads_U","tl_rd13_06_prisecroads","tl_rd13_06_prisecroads_S","Census tracts","Counties2010"] # create list of layers in base map
dummy = arcpy.mapping.Layer(r"T:\HCI\Data\Insurance_187\Maps\dummy.lyr")
## Loop through a list of feature classes in the workspace to create new layer from selection
for row in cursor: # iterate for each row in dataset
names.append(row.getValue(field)) # store name of field in row within list
if arcpy.Exists(row.getValue(field)): # delete feature class if it already exists before using copy feature to recreate it
arcpy.Delete_management(row.getValue(field))
whereClause = '"County_Name" = \'' + row.getValue(field) + "'" # Create a clause to select only the current record
arcpy.Select_analysis(masterfile,row.getValue(field),whereClause)
arcpy.CopyFeatures_management(masterfile, row.getValue(field)) # Write the selected features to a new featureclass (this copies the layer selection)
input = arcpy.mapping.ListLayers(mxd,"row.getValue(field)",df)[0]
arcpy.mapping.UpdateLayer(df,input,dummy,True)
arcpy.SelectLayerByAttribute_management(masterfile,"NEW_SELECTION",whereClause) # Select layer by county name
df.zoomToSelectedFeatures()
arcpy.SelectLayerByAttribute_management(masterfile, "CLEAR_SELECTION") # clear selection to prepare for next loop
#Turn of all layers in map except for county layer
for lyr in arcpy.mapping.ListLayers(mxd, '', df): # iterate for each layer in the map layer list
for layer in names: # iterate for each map layer equal to value in "names" list
if lyr.name == layer: # if layer name is equal to value in "names" list, then turn off layer
lyr.visible = False
if lyr.name in basemap: # if layer name is equal to value in "basemap" list, then turn on layer
lyr.visible = True
arcpy.RefreshActiveView() # refresh ArcMap view to reflect changes made from python code
# Loop through each layer, turn it on and export map as JPEG
for lyr in arcpy.mapping.ListLayers(mxd, '', df): # iterate for each layer in the map layer list
for layer in names: # iterate for each map layer equal to value in "names" list
if lyr.name == layer: # if layer name is equal to value in "names" list, then iterate steps below
lyr.visible = True # turn on map layer
arcpy.RefreshActiveView() # May want to test without this -- ArcMap might export correctly without need for refresh active view
arcpy.mapping.ExportToJPEG(mxd,"T:\\HCI\\Data\\Insurance_187\\Maps" + lyr.name + "_InsetMap.jpeg","PAGE_LAYOUT")
lyr.visible = False # turn off map layer
del row, cursor, masterfile, mxd, input, dummy, basemap, lyr, layer, names # delete local variables from session
env.overwriteOutput = origSetting # reset data overwriting back to default setting
Here's the error message I get:
Runtime error Traceback (most recent call last): File "", line 34, in File "c:\program files\arcgis\desktop10.2\arcpy\arcpy\arcobjects\arcobjects.py", line 1048, in getValue return convertArcObjectToPythonObject(self._arc_object.GetValue(*gp_fixargs(args))) RuntimeError: ERROR 999999: Error executing function.