Monday 25 March 2019

arcgis desktop - Using MultiValue in Python script tool parameters?


My script works fine with a single feature class selected for clip_fc = arcpy.GetParameter(0) .



However when I set clip_fc to MultiValue in the script properties and then select several feature classes, I get an error "Parameters are not valid.".


Can anyone show me where I am going wrong?


import arcpy
import os.path
from arcpy import env

arcpy.env.workspace = r"C:\GIS Home\project_1\clipshps\Combined_mf.gdb"
arcpy.env.overwriteOutput = True
clip_fc = arcpy.GetParameter(0)
clip_name = str(clip_fc)

output_dir = r"C:\GIS Home\project_1\clipshps\outputs_clip"

for fds in arcpy.ListDatasets():
for fc in arcpy.ListFeatureClasses('','',fds):
out = os.path.join(output_dir, fc + "_" + clip_name[40:] + "_clip.shp")
arcpy.Clip_analysis(fc, clip_fc, out, "")

Answer



You need to loop through your inputs. Multivalue is semicolon delimited. Split on that and loop through them. (AddMessages to show how the fcs are presented)


import arcpy


ins = arcpy.GetParameterAsText(0)
arcpy.AddMessage(ins)

for fc in ins.split(';'):
arcpy.AddMessage(fc)
arcpy.Clip_analysis(fc, clipfeats, out)

Though I'm not entirely sure of your workflow. You're passing in multiple features to clip on, but also iterating through multiple features in a GDB? You'll have to take the logic I placed above and figure out what combination of inputs you want (1 or many FCs inside a GDB + 1 or many inputs to your tool)


EDIT ...Based on your comment you want many feature classes clipped by many feature class. Try this mash up of code based on yours and mine:


import arcpy, os


ins = arcpy.GetParameterAsText(0)
arcpy.AddMessage(ins)

arcpy.env.workspace = r"C:\GIS Home\project_1\clipshps\Combined_mf.gdb"
output_dir = r"C:\GIS Home\project_1\clipshps\outputs_clip"


for fds in arcpy.ListDatasets():


# will loop for every featureclass
for fc in arcpy.ListFeatureClasses('','',fds):

# will loop for each input fc from the tool
for fc_clip in ins.split(';'):
outName = os.path.join(output_dir, str(os.path.basename(fc)) + "_" + str(os.path.basename(fc_clip)))
arcpy.Clip_analysis(fc, fc_clip, outName, "")

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...