I have a very simple program to toggle on and off layers and export PNGs. The issue is trying to pass a string to search for in the ListLayers result.
If I pass the parameter with the tool dialog, I get no return, even though I AddMessage the string and it shows as expected (see the 4th line under Messages).
If I hardcode the string on line 27:
for lyr in arcpy.mapping.ListLayers(mxd, "*Events", df):
, no troubles.
I expect I am missing something basic in the interpolation of the passed string, but I have tried enclosing in additional quotes and using escapes before the quotes, to no avail.
Best Regards,
Jim
import arcpy
Folder = arcpy.GetParameterAsText(0)
Folder = Folder.replace("\\","/")
SearchStr = arcpy.GetParameterAsText(1)
arcpy.AddMessage( SearchStr)
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd,"Layers") [0]
def ExportPNG(sspaText):
# Update the layout text
#for el in arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","SSPA_Title"):
# el.text = sspaText
arcpy.AddMessage("Processing " + sspaText)
arcpy.RefreshActiveView()
# Export the Map
arcpy.mapping.ExportToPNG(mxd, Folder + "/" + sspaText + ".png", resolution=600)
def SetVisibility(visNam):
for lyr in arcpy.mapping.ListLayers(mxd,visNam,df):
lyr.visible = 'true'
def SetInvisibility(visNam):
for lyr in arcpy.mapping.ListLayers(mxd, visNam,df):
lyr.visible = 'false'
for lyr in arcpy.mapping.ListLayers(mxd, SearchStr, df):
baseName = lyr.name
arcpy.AddMessage(baseName)
# Call the SetVisibility function to turn on the layer
SetVisibility(baseName)
arcpy.RefreshActiveView()
sspaText = lyr.name[:-7]
ExportPNG(sspaText)
# Call the SetInvisibility function to turn off the layer
SetInvisibility(baseName)
Solved! Go to Solution.
Have you tried passing just *Events, no quotes. Since the parameter is defined as String, whatever you are passing is already being interpreted as a string. I would have to double check, but I believe quotes in string parameters are escaped to be treated as literal single or double quotes, which isn't what you want because you don't have literal quotes in your layer names.
A guess (without testing), remove lines 20 and 24 above (and then adjust the indents after, of curse).
Since you are already passing the basename on 30 and 34,
>>> search_string = "*Events"
>>> search_string
'*Events'
>>> print(search_string)
*Events
>>> s = '"{}"'.format(search_string)
>>> s
'"*Events"'
>>> print(s)
"*Events"
It is almost like it has no double or single quotes.. Examine the above. especially line 6 to generate quotes around strings. I don't think this is an AddFieldDelimiters issue that is commonly suggested, but have a look at it as well
I don't quite follow. The code runs without issues if I hardcode the SearchStr variable.
Lines 20 and 24 turn on the layer prior to export, and then turn it back off before the next event theme is turned on and exported.
Line 30 is simply an add message statement so I can see what the code is working with, and line 34 is just an active view refresh.
Perhaps the line numbers are skewed?
Thanks for your input just the same!
It appeared to me that the quotes might be being dropped... if "*Events" with the quotes worked, then *Events wouldn't
That was my original thought as well. I have just tried:
SearchStr = arcpy.GetParameterAsText(1)
SearchStr = '"{}"'.format(SearchStr)
No luck
Tried:
SearchStr = '"'+str(SearchStr)+'"'
No luck
The parameter is set to a string type in the dialog.
Actually at a bit of a loss for what else to try.
print it using print(SearchStr) and if running from a dialog, arcpy.AddMessage(SearchStr) and see what if anything it is returning. In any event, if hardcoding works and the filter doesn't the answer lies in what it is returning.
Have you tried passing just *Events, no quotes. Since the parameter is defined as String, whatever you are passing is already being interpreted as a string. I would have to double check, but I believe quotes in string parameters are escaped to be treated as literal single or double quotes, which isn't what you want because you don't have literal quotes in your layer names.
That is a good thought, Joshua. I had tried that as well as using single quotes, double quotes, escaped quotes (/', \', " ' ", ' " ') No joy yet. I have submitted a support ticket to ESRI, and will let you all know what shakes out.
Regards,
Jim
I have restarted my machine and it runs successfully now. I have no idea why it was failing previously, as I can look at the Geoprocessing Results tab, and see the same arguments entered with an unsuccessful result as what I have used for a successful result today. The entry for the string does not need any quotes, as Joshua surmised. ESRI support ran my code as provided with no troubles, I tried it on a different machine with no troubles, and then tried it again on mine with success. No idea what was preventing it before. Thanks to all for your help and input.
Best Regards,
Jim