0

In my MXD with 14 different Raster-Sets (12 Months(January, February...), 1 GRID, 1 Border-Raster) I am looking to create a list (Layer Objects) with the function

arcpy.mapping.ListLayers(mxd,"FILTER",df)[0]. 

I don't know how to specify a filter (Wildcard) to create a list containing only the 12 Months. So far I tried to filter the list monlist with a tmplist containing all the months as Strings. In the following example, the line

monlist = arcpy.mapping.ListLayers(mxd, tmplist ,df)

does not work. In result I get an empty list monlist=[]

Valid XHTML

import arcpy  
raster = arcpy.GetParameterAsText(0)  
outPDF = arcpy.GetParameterAsText(1)  
hill_bool = arcpy.GetParameterAsText(2)

mxd = arcpy.mapping.MapDocument("CURRENT")  
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]   
hillshade = arcpy.mapping.ListLayers(mxd, "hillshade", df)[0]     
hillshade.visible = hill_bool

tmplist = ["Januar","Februar","Maerz","April","April",
"Mai","Juni","Juli","August","September","Oktober","November","Dezember"]
monlist = arcpy.mapping.ListLayers(mxd, tmplist ,df) #Select only the months
addLayer = arcpy.mapping.Layer(r"C:Python\Legende_2.lyr")
arcpy.mapping.MoveLayer(df, monlist[:-1],hillshade, "AFTER")

for element in monlist:
    element.visible = "TRUE"
    arcpy.ApplySymbologyFromLayer_management("","addLayer")
    if hillshade.visible = "TRUE":     #<<< ???
        element.transparency = 35
arcpy.RefreshActiveView()
...

#...align Legend and other objects -> ExportToPDF
arcpy.mapping.ExportToPDF(mxd, outPDF)
  • Can you add a screenshot of what the workspace looks like in ArcCatalog? – Bjorn Jan 23 '17 at 19:44
  • 1
    Please edit your question to include an actual snippet of your code so we can see what you've tried. Currently your question is a bit unclear. – Midavalo Jan 23 '17 at 19:47
  • @RichardMorgan I think you should post that, with the actual code snippet, as an answer. – PolyGeo Jan 23 '17 at 21:48
2

I don't believe you can pass in a list to the wildcard filter.

If you return a full list of the layers, use Python to walk through looking to see if there is a month in the name.

Here is an untested example:

import calendar

months = [calendar.month_name[month_idx] for month_idx in range(1,13)]

# layers = ['Test', 'January_2017', 'Bob', 'FebruaryIsCold']
layers = [item.name for item in arcpy.mapping.ListLayers(mxd, '' ,df)]

layers_of_interest = [lay for lay in layers if any(mon in lay for mon in months)]
print (layers_of_interest)

print ('Done!')

The layers variable just holds the names of the layers; you may not want that, but it made finding name matches easier.

The commented out layers is to show that a partial match of the layer name and the month name will be found in layers_of_interest.

Finally, you can use your tmplist for the month names; there's no problem with that. The months example was to show how to get the month names without hard-coding them in. Seeing your month names, you might need to change the locale first:

import locale
locale.setlocale(locale.LC_ALL, 'de_DE')

Here is a repl example showing the list comprehension working.

| improve this answer | |
1

You could use some loops to clear the list. This is far from elegant, but it should be easy to follow the flow.

# You already have you list of expected good names
tmplist
>>> ['Januar', 'Februar', 'Maerz', 'April', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember']
# Get the list of layers from the map and look at them
lyrs = arcpy.mapping.ListLayers(mxd)
for l in lyrs:print(l.name)
>>> aaaa_add
>>> Roads
>>> Januar
>>> Februar

# Loop through the layers in the map and if the name matches, remove that layer from the lyrs list
for idx, l in enumerate(lyrs):
     if l.name in tmplist:
         print("this layer is ok: {}".format(l.name))
     else:
         lyrs.pop(idx)
         print("removing {}".format(l.name))

>>>removing aaaa_add
>>>this layer is ok: Januar
>>>this layer is ok: Februar
>>>removing Roads
| improve this answer | |

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.