Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
1  
Have you tried data driven pages? It says getValue is returning a bad type on line 34 - hard to tell which one that is without line numbers. Perhaps instead of all those getValue statements you put int cName = row.getValue(field) and then replace the rest with cName. A few arcpy.AddMessage statements would help too, even if it's only "still alive 1", "still alive 2"... the field 'County_Name' exists doesn't it? –  Michael Miles-Stimson Mar 4 at 3:28
1  
Try running your code from an IDE like IDLE to find out which line in your script is throwing that error. It looks like a possible use case for Data Driven Pages to me too. –  PolyGeo Mar 4 at 3:29
    
Does data driven pages allow for all other counties except selected county to be masked? I need the other counties to be excluded. Otherwise, I would need to create a new layer with just that county. –  Jacqui Chan Mar 4 at 19:11
    
The data driven pages is a great solution! Thank you so much. I found a way to mask the other layers using the clip to extent of data driven pages (under Data Frame properties). –  Jacqui Chan Mar 5 at 0:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.