I am pretty new to ArcPy so bear with me. I am having a hard time understanding how to feed a list of FCs from a "GetParameterAsText" into something like the merge tool. I have my GetParamterAsText window set to Feature Class and multivalue so I can drag a group of layers into it in the tool dialogue.
inputLayer = arcpy.GetParameterAsText(1)
When I run the merge command, it looks like it is messing up the expected syntax by putting ";" as a separator instead of ",".
Doing this:
arcpy.Merge_management(inputLayer, outputName, "")
Throws me this error:
"C:\Development\Merge_Dissolve\GA0combined.shp;C:\Development\Merge_Dissolve\GA25combined.shp" does not exist
I have tried to create a function to replace ";" with ",", but it doesn't seem to work on my inputLayer variable, but I'm guessing there's a more standardized way to working with a list of FCs with tools that accept multiple inputs such as Merge.
-
please try to form one question. Make it have a question mark at the end. A clear question will likely get an answer quicker than asking for guidance.Brad Nesom– Brad Nesom2015年05月27日 18:50:40 +00:00Commented May 27, 2015 at 18:50
-
Take a look at this: gis.stackexchange.com/questions/35553/…KHibma– KHibma2015年05月27日 20:20:36 +00:00Commented May 27, 2015 at 20:20
5 Answers 5
try
parameter = arcpy.GetParameter(1)
inputLayers = parameter.values()
This should give you the different layers as a list Afterwards you can access the individual layers by index or in a loop
layer = inputLayers[0]
or
for layer in inputLayers:
(do something with your layer)
-
This was very helpful. Thanks! I figured out something that is working...now to just fully understand WHY. =)GYS– GYS2015年05月27日 20:45:52 +00:00Commented May 27, 2015 at 20:45
-
@GYS can you elaborate for future reference?DWynne– DWynne2015年05月27日 23:32:24 +00:00Commented May 27, 2015 at 23:32
-
@GYS don't forget to choose an answer when you get a chance.ianbroad– ianbroad2015年05月28日 00:42:23 +00:00Commented May 28, 2015 at 0:42
Use split to convert parameter to list expected by Merge.
inputLayer = arcpy.GetParameterAsText(1)
list2merge=inputLayer.split(";")
-
Could you please elaborate on how you would use split?2015年05月27日 19:52:52 +00:00Commented May 27, 2015 at 19:52
There are several ways of successfully formatting values for a tool that accepts multivalues in Python.
- A semi-colon delimited string of data is a valid input for any geoprocessing parameter that accepts multiple inputs. This is valid, and is what you will be getting from
GetParameterAsText
on a multivalue parameter. - A list of strings of the feature classes (or layers, or table views, or whatever the tool parameter accepts).
- A list of
arcpy.Value
objects. If you useGetParameter
this is what you should be getting. - Create and populate an
arcpy.ValueTable
object. I don't recommend this per se as it's more effort than it's worth, but it will work.
If you've done #1, and it's not working, I would question whether the data does exist.
So the answer was that I needed to create a list and then feed that into the merge command. I'm not sure why #1 didn't work from @DWynne as that has worked for me in the past (and how I approached this at first) on another tool that takes multiple inputs.
This was my basic flow:
inputLayer = arcpy.GetParameterAsText(1)
layerList = inputLayer.split(";")
arcpy.Merge_management(layerList, tempmergeName, "")
This is what I needed to do in order to use a list of feature classes listed in a text file. A little off topic since the OP is specifically asking about GetParameterAsText() and I'm side-stepping the function. It is however directed at the same end goal: feeding Merge_managment a long list of inputs.
import arcpy
#inputLayer = arcpy.GetParameterAsText(1)
#outputFC = arcpy.GetParameterAsText(2)
inputLayer = r"X:\source\all-footprint-featureclasses.txt"
outputFC = r"X:\work\default.gdb\all_sat_footprints"
ff = open(inputLayer)
# Open text file and put each line into a list element
# --> ["path1","path2", ...]
layerList = ff.read().split()
print(f"{layerList[0:1]}...")
arcpy.Merge_management(";".join(layerList), outputFC, "")
# ";".join() turns ["path1","path2"] into --> "path1;path2"
print(arcpy.GetMessages())