1

This code I've created works for 1 (one) active layer and for 1 (one) field. I need to implement this code for many layers and fields in a open .mxd file.

I have 11 fields (years 2003 to 2015). I want to select some o of them (multivalue field) and after choose, generate the layers view to each field (year). I already did for 1 (one) field. But get stuck to many layers.

The shapefiles are from the same region/place so the best way to view is the layout view (create more than 1 data view)

The process is:

i. choose the table ii. choose the years (multivalue field) iii. generate N data views from the selected fields

import arcpy

mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, "")[0]
lyr = arcpy.mapping.ListLayers(mxd, "", df)[0]
lyrFile = arcpy.mapping.Layer(r"F:\PercACH.lyr")
arcpy.mapping.UpdateLayer(df, lyr, lyrFile, True)
Tabela = arcpy.GetParameterAsText(0)   
Campo = arcpy.GetParameterAsText(1)

if lyr.symbologyType == "GRADUATED_COLORS":
    lyr.symbology.valueField = Campo

arcpy.mapping.ExportToPDF(mxd, r"F:\TesteAgro.pdf")
arcpy.RefreshActiveView()
arcpy.RefreshTOC()

del mxd, lyrFile

EDIT:

What`s the best way to create many layers and data view for every year selected?

enter image description here

  • Can you please elaborate on the question and state more clearly what exactly is your problem? – Alex Tereshenkov Jul 17 '17 at 17:55
  • Alex, I put more information after the code – Vinicius Cruvinel Jul 17 '17 at 18:07
  • Maybe it's just me, but I am still having a hard time figuring out what you are trying to achieve.. – Alex Tereshenkov Jul 17 '17 at 23:56
  • Alex, my apologies for not being so clear! I put and answer for Jason and edit my post with a print to explain better. – Vinicius Cruvinel Jul 19 '17 at 1:32
1

Vinicius,

I would agree with Alex that it's a little hard to understand exactly what you are doing, but maybe this will help solve your problem. Essentially you make a list of all the years (this is hard coded, not a parameter), and then cycle through each year on the list...something along these lines...

import arcpy

mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, "")[0]
lyr = arcpy.mapping.ListLayers(mxd, "", df)[0]
lyrFile = arcpy.mapping.Layer(r"F:\PercACH.lyr")
arcpy.mapping.UpdateLayer(df, lyr, lyrFile, True)

years = ['2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015']

# Don't need parameters
# Tabela = arcpy.GetParameterAsText(0)   
# Campo = arcpy.GetParameterAsText(1)

for year in years:
    if lyr.symbologyType == "GRADUATED_COLORS":
        lyr.symbology.valueField = year

    arcpy.mapping.ExportToPDF(mxd, r"F:\TesteAgro{0}.pdf".format(year))
    arcpy.RefreshActiveView()
    arcpy.RefreshTOC()

del mxd, lyrFile
  • Jason, sorry for not being so clear with my question. My apologies for Alex too. Your almost attend my needs. I want to do this steps with all 'years' fields and also create one new layer to the active mxd for each year. Is more clear? – Vinicius Cruvinel Jul 19 '17 at 1:28

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.