[GIS] python conditional statement to allow either two inputs or only one input going into a merge

arcpyconditionalmerge

I have a ModelBuilder process that I am converting to python for additional editing. In the process I have both a point and line features(input variables) being buffered and then the resulting polygons merged into a single feature for additional processes. But I want to make the model more versatile so it will run if only one of the inputs (point OR lines) is supplied. Currently it crashes if only one input is supplied. Is there a way to direct the model so that if only one input is provided, it skips the merge step (or successfully merges with null) and then goes to the subsequent steps.

Here's my code:

# Process: Buffer (6)
arcpy.Buffer_analysis(in_points_shp, point_buffer_shp, "buffer", "FULL", "ROUND", "NONE", "", "PLANAR")

# Process: Buffer (11)
arcpy.Buffer_analysis(in_LINE_shp, line_buffer_shp, "buffer", "FULL", "ROUND", "NONE", "", "PLANAR")

# Process: Merge (2)
arcpy.Merge_management("Z:\\supporting_docs\\point_buffer.shp;Z:\\supporting_docs\\line_buffer.shp", merge_output_shp,

…merge output then goes on to other processes. My python skills are currently zero.

Best Answer

There are at least two things you could do.

You could define default values for your parameters, and then, if the default does not occur you will execute a certain piece of code.

For example, let's say the first parameter asks for a Point. If the first parameter was entered then it will not be default, so you can go ahead and execute a piece of code (I just used print statements for simplicity, of course you would include your real code instead):

def printSomething(a="default", b="default"):
    if a != "default" and b != "default":
        print a + b
    elif a != "default":
        print a
    else:
        print b

Or you could use something like *args. It might be the "better" approach but that might be a little harder to set up.

Surely there will be other ways, but I think that the first one should be easy to implement and would work fine.

Related Question