Select to view content in your preferred language

Loop to make layers and create maps

1508
10
03-05-2012 08:14 AM
EvaJenkins
Deactivated User
Hi,

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:
    valueList.append(aVal)

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")
  arcpy.RefreshTOC()
 
 
  #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)
Tags (2)
0 Kudos
10 Replies
MathewCoyle
Honored Contributor
I meant the field type as integer. But that is really outside the scope of what you are doing here. You're where clause is still not formatted properly.

This won't work unless your month is called "value"
arcpy.MakeFeatureLayer_management(inFC, lyrname, "[survey_month] = value")


This should work for you.
where =  "[survey_month] = " + str(value)
arcpy.MakeFeatureLayer_management(inFC, lyrname, where)
0 Kudos