I am making a model with ModelBuilder and my question relates to a smaller part of the model (or like a beginning), where I need to check whether the user input file extension is SHP or GPX and order the model to do different series of geoprocessing for both cases. I didnt find a tool in ModelBuilder that would do exactly what I need, so I guessed that I will need to write a Python script and use script tool in my model.

Could you tell me what I´m doing wrong?

I wrote the following script in PythonWin and saved a .py file, and inserted the script tool into the model. But I don't get the result I expected, as whatever file the input gets, it says either true true for both, or false false for both.

And also how to connect then those "correct" true or false answer values to the next tools?

Is it over a precondition or what exactly?

Here is the script and a few screenshots for better understading. (im a very beginner in scripting....)

Also a link to another topic for reference from where I took the script and tried to adapt it: Checking field type in ModelBuilder using if else?

import arcpy
import os
input = arcpy.GetParameterAsText(0)
name = os.path.basename(input).rstrip(os.path.splitext(input)[1])
extension = os.path.splitext(input)[1].lstrip(".")

def CheckExtension(extension):    
  if extension == 'shp':    
    arcpy.SetParameterAsText(1, "true")    
    arcpy.SetParameterAsText(2, "false")  
  elif extension == 'gpx':
    arcpy.SetParameterAsText(2, "true")
    arcpy.SetParameterAsText(1, "false")


enter image description here enter image description here

  • Another way to achieve your aim might be to use a file type filter as described in the[ documentation](desktop.arcgis.com/en/arcmap/10.3/analyze/modelbuilder/…). you could just pass all your input to both processing tool chains, but have each chain only accept the appropriate file format. – RoperMaps Apr 20 '18 at 10:12
  • The following also makes your script a bit easier to read: name, extension = os.path.basename(input).split('.') in place of lines 4 and 5 – RoperMaps Apr 20 '18 at 10:16
  • Rename your file to remove the ' (02)' from the end. – RoperMaps Apr 23 '18 at 11:43
  • @RoperMaps hey thanks for your advice! However, I have a question - how to make each chain only accept the appropriate file format? Is there a specific tool for that? – Aurelija Viluckytė Apr 23 '18 at 11:47
  • @RoperMaps because the tools for the .shp file are such as "select features by layer" or "add surface information", and I can't just connect to these the parameter input, if its .gpx . Otherwise, I would need to do it by hand. I need some "middleman" to direct the input to one or another sequence... – Aurelija Viluckytė Apr 23 '18 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.