1

I have a shapefile that is a 10-by-10 grid of squares. The shapefile has 50 attribute columns (named p_1 through p_50) where each column has 100 entries (corresponding to the 100 grid squares) and each entry is one of 1, 2, 3, 4, or 5.

In order to visualize p_1, I navigate to Layer Properties, click on the Symbology tab, and click on 'Unique values' which is listed under Categories. I then select 'p_1' from the Value Field, click the 'Add All Values' button, and click the 'Apply' button. This results in a grid where each square has a color based on the number corresponding to the grid square in the p_1 column of the shapefile attribute table. I then use the Export Map feature to save the colored grid as a png.

However, because there 50 columns corresponding to 50 different plans, I would like to automate this process.

How do I automate this process?


With help from @Fezter the following code accomplishes most of what I want:

import arcpy

mxd = arcpy.mapping.MapDocument("current")

# Find the plan layer
kdp_lyr = None
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.name == 'kd_plans':
        kdp_lyr = lyr
        break

num_plans = 50      
for i in range(0, num_plans):
    if kdp_lyr.symbologyType == "UNIQUE_VALUES":
        kdp_lyr.symbology.valueField = "all_the_plans$.plan_" + str(i)
        kdp_lyr.symbology.addAllValues()

        ## Remove the Null value
        sourceList = kdp_lyr.symbology.classValues
        sourceList.remove("<Null>")
        kdp_lyr.symbology.classValues = sourceList
        kdp_lyr.symbology.showOtherValues = False

    arcpy.RefreshActiveView()
    arcpy.mapping.ExportToPNG(mxd, 'plan_' + str(i) + '.png')
del mxd

The one thing it does not do is use the same color for each value (e.g., blue for the number 1 value across all plans i).

@Michael Miles-Stimson suggested using Apply Symbology from Layer however I don't believe this will work in my case because according to the documentation:

The field in the Input Layer that will be displayed must have the same name as that of the corresponding Symbology Layer field. If this field is missing, the output data is drawn with default symbology.

and each field in my Input Layer has a different name.

3

Provided you're comfortable with python, you can do this using the UniqueValuesSymbology class in the arcpy.mapping module.

You can iterate through your fields and mxd's and change the value field.

The two examples in the help file should get you going.

  • 2
    Another possibility, but only if the ranges are the same, is to do one (as a template) and then use Apply Symbology from Layer resources.arcgis.com/en/help/main/10.1/index.html#//…, this can also be batched by right clicking, select batch, put the 'to' layers in, select the from layer on the top row then right click on the populated one and 'fill'... no python necessary. – Michael Stimson Mar 2 '15 at 2:12
  • @Fezter I can now iterate through the 50 different plans, but I don't see an easy way to tell it to always use blue for grid squares with a number 1, red for number 2, etc. Do you know if that is possible? – derNincompoop Mar 2 '15 at 2:31
  • @Fezter I meant to say I don't see ANY way to make sure that the same color is used for a given number across each plan. – derNincompoop Mar 2 '15 at 2:57
  • see Michael Miles-Stimson's comment above. Create a lyr file based on the symbology you want and use Apply Symbology from layer. – Fezter Mar 2 '15 at 3:19
  • @Fezter I am not sure how to using the arcpy.ApplySymbologyFromLayer_Management() function. In particular, how do I tell it to apply the symbology to the data corresponding to plan_1, then plan_2, etc.? – derNincompoop Mar 2 '15 at 18:51
1

I finally overcame the issue where each field in my input layer had a different name (and the Apply Symbology From Layer function highlighted by @Michael Miles-Stimson requires the input layer field to have the same name as the Symbology layer field.

I first created layer named plan_template which had a field named "drawplan". In the Symbology tab of the plan_template layer I selected the colors and values I wanted to use.

I then created a field named "drawplan" in the district_plans layer.

Finally I added my plan data from a .dbf file (see this question for issues trying this approach when data is stored in an Excel file), and joined the plan information to the district_plans layer.

I then ran the following code

import arcpy

mxd = arcpy.mapping.MapDocument("current")

num_plans = 50
for i in range(0, num_plans):
    ## Copy data from plan_to_draw field to drawplan field 
    ## in "district_plans" layer 
    plan_to_draw = "!plan_" + str(i) + "!"
    arcpy.CalculateField_management("district_plans", "drawplan", plan_to_draw, "PYTHON")

    ## Apply symbology from "plan_template" layer
    arcpy.ApplySymbologyFromLayer_management("district_plans", "plan_template")

    ## Refresh and save map
    arcpy.RefreshActiveView()
    arcpy.mapping.ExportToPNG(mxd, 'dplan_' + str(i) + '.png')
del mxd

which produced 25 different plans where each district has the same color.

The code iterates through the different fields in the district_plans layer that contain the plan information, copies the information over to the drawplan field using the Calculate Field function, and then applies the symbology defined in the plan_template layer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.