Hi, I am a beginner and I have been experimenting with Python module in ArcGIS and I'm trying now to create some kind of automative map. I have set of .lyr files, geodatabase with shapefiles and .csv table with list of names of shapefiles in a row. My goal is to apply symbology to layers based on list picked from .csv table. Shapefiles in geodatabase and .lyr files are named accordingly. When I run hardcoded script, it works just fine. When I use arcpy.GetParameterAsText() troubles begin - the first paremeter dosn't seem to work well at all - it just collects the first character of string that I put in it while using script tool. Do you know, where is the problem?
error log:
Traceback (most recent call last):
File "E:\Kursy_Michal\ArcGIS\Proby\Proba1\Symbologia_nocomment.py", line 30, in <module>
arcpy.MakeFeatureLayer_management(ob, fc)
File "c:\program files (x86)\arcgis\desktop10.7\arcpy\arcpy\management.py", line 6986, in MakeFeatureLayer
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Input Features: Dataset E:\Kursy_Michal\ArcGIS\Proby\Proba1\Geobaza_1.gdb\l does not exist or is not supported
Failed to execute (MakeFeatureLayer).
Failed to execute (Script3).
code:
import arcpy mxd = arcpy.mapping.MapDocument(r"E:\Kursy_Michal\ArcGIS\Proby\Proba1\Proba1.mxd") df = arcpy.mapping.ListDataFrames(mxd)[0] arcpy.env.workspace = r'E:\Kursy_Michal\ArcGIS\Proby\Proba1\Geobaza_1.mdb' import csv g = globals() i = 0 dct = {} with open('Warianty1.csv', 'r') as csvfile: reader = csv.reader(csvfile, delimiter=';') for row in reader: dct['l_%s' % i] = [] #Creating dynamic lists named l_0, l_1, etc for each row g['l_{0}'.format(i)] = row[:] i += 1 #Choosing list by putting its' name to var2 var2 = arcpy.GetParameterAsText(0) #Delete empty values in list filter(None, eval(var2)) path1 = arcpy.GetParameterAsText(1) path2 = arcpy.GetParameterAsText(2) #Giving symbology to layers created accordingly to names stored in listfor fc in eval(var2): ob = path2 + '\\' + fc arcpy.MakeFeatureLayer_management(ob, fc) arcpy.ApplySymbologyFromLayer_management(fc, path1 + '\\' + fc + '.lyr')
Solved! Go to Solution.
I have finally worked this out. There appears that in this case you do not want to have quotes in arcpy.GetParameterAsText(). Putting l_1 instead of 'l_1' made this script work.
I have been scripting with Python for years, and I seldom use eval, almost never. Your use of it here caught my attention. Can you explain what var2 looks like before you start manipulating it?
Also, your filter line isn't doing what you think, but I will wait to comment on that until I see what you are trying to do with var2.
I declare var2 as parameter declared from script tool level. I want to var2 be a variable that stores name of list, that will be used in for loop (that is why I use eval - to iterate through the chosen list, not the var2 itself). In case I hardcode it (type var2 = 'l_1') then that loop is done correctly.
When it comes to filter, previously I wanted to use while loop
while '' in eval(var2): eval(var2).remove('')
but it did not work
OK, I get what you are doing with eval, but this line isn't doing what you expect.
#Delete empty values in list filter(None, eval(var2))
In fact, this line isn't doing anything because calling filter returns a filter object that you haven't assigned to anything. Filter does not work on the iterable in-place.
OK, that's correct, my code looks now like this:
import arcpy mxd = arcpy.mapping.MapDocument(r"E:\Kursy_Michal\ArcGIS\Proby\Proba1\Proba1.mxd") df = arcpy.mapping.ListDataFrames(mxd)[0] arcpy.env.workspace = r'E:\Kursy_Michal\ArcGIS\Proby\Proba1\Geobaza_1.mdb' import csv g = globals() i = 0 dct = {} with open('Warianty1.csv', 'r') as csvfile: reader = csv.reader(csvfile, delimiter=';') for row in reader: dct['l_%s' % i] = [] g['l_{0}'.format(i)] = row[:] i += 1 var2 = arcpy.GetParameterAsText(0) var3 = filter(None, eval(var2)) path1 = arcpy.GetParameterAsText(1) path2 = arcpy.GetParameterAsText(2) for fc in var3: ob = path2 + '\\' + fc arcpy.MakeFeatureLayer_management(ob, fc) arcpy.ApplySymbologyFromLayer_management(fc, path1 + '\\' + fc + '.lyr')
Error is still the very same as before. And, similarly, when I paste hardcoded version of this code into python console, it works fine, but when I use version with parameters as script tool, the error appears. Do you maybe know the reason behind this?
What type of script tool? Did you define the parameters and their type using the script tool dialog after adding it to your toolbox?
Well... just simple script tool in my custom toolbox. And yes, I defined those parameters. You can take a look:
Add an ArcPy AddMessage after var2 to see what exactly var2 is getting set to before you try to process it.
I have finally worked this out. There appears that in this case you do not want to have quotes in arcpy.GetParameterAsText(). Putting l_1 instead of 'l_1' made this script work.