Loop to make layers and create maps

Discussion created by ecjenkins on Mar 5, 2012
Latest reply on Mar 5, 2012 by mzcoyle

I have a python script (arcpy) that is used to create a list of unique values of a field, then I want to loop through those values and make a feature layer for each, update map text, update symbology, and export to a PDF.  So far, the issue seems to be linking the value of the field in the feature class with the value in the unique values list.  The script appears to work aside from this (i.e. exports nice maps with no data).  Can anyone help me with that link and the where clause in the MakeFeatureLayer tool?

import arcpy.mapping

arcpy.env.overwriteOutputs = True

inFC = r"F:\DATA\PROJECTS\RUBL\layers\RUBL.gdb\National_bothtimeperiods"
inField = "survey_month"

mxd = arcpy.mapping.MapDocument(r"F:\DATA\PROJECTS\RUBL\workspaces\RUBL_National_Monthly.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
outloc = r"F:\DATA\PROJECTS\RUBL\maps\Month"

sourcelayer = arcpy.mapping.ListLayers(mxd, "Survey Records", df)[0]

valueList = []
rows = arcpy.SearchCursor(inFC)
for row in rows:
  aVal = row.getValue(inField)
  if aVal not in valueList:

del row, rows
print valueList

for value in valueList:
  #make feature layer where survey_month field equals the current value in the unique list
  lyrbase = "MonthLyr"
  val = str(value)
  lyrname = lyrbase + value

  arcpy.MakeFeatureLayer_management(inFC, lyrname, "[survey_month] = value")
  #update symbology based on layer in mxd

  for lyr in arcpy.mapping.ListLayers(mxd, lyrname,df):
    arcpy.mapping.UpdateLayer(df, lyrname, sourcelayer, True)

  #change the text element to read the month of the current unique value
  for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "Month"):
    elm.text = "Month: " + value
  #export to PDF with settings
  outfile = outloc + value
  arcpy.mapping.ExportToPDF(mxd, outfile, data_frame="PAGE_LAYOUT", resolution=300, image_quality="BEST", colorspace="CMYK", image_compression="LZW", convert_markers="True", embed_fonts="True")

  #remove the current layer so that the next can be created and mapped correctly
  for df in arcpy.mapping.ListDataFrames(mxd):
    for lyr in arcpy.mapping.ListLayers(mxd, lyrname, df):
      arcpy.mapping.RemoveLayer(df, lyr)