Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Using ModelBuilder, I want to create a tool that will let a user choose the value or values that will be used to extract a shapefile.

I have a parcel layer with a "Parcel_Type" field. I want the user to be able to choose the type of parcel to extract. So if the user wants to extract the parcels with a type "EL" then he would only have to enter "EL" as a variable. If he wants type "EL" and "CDD" he needs to enter them as multi-variables. That would be the only human interaction with the model.

So how can i use those variables in my model?

share|improve this question
    
I not see "Field" parameter & "Value" parameter for "Calculate Value" tools in ArcGIS 10.2.1 Thanks ![Expression for Calculate Value ](i.stack.imgur.com/j67bI.png) !Model Builder –  Mohammad Hosseini Apr 12 at 0:06
1  
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. –  BradHards Apr 12 at 0:48
    
This helped me start on the task of writing a model to extract features on a layer that have attributes within a range of interest (as in Selection | Select By Attributes). But when I run the model and choose the input layer, the list of fields/attributes is blank. And I coded the Code Block exactly as above. Could I be missing something? –  user3429841 Jul 16 at 13:22

2 Answers 2

up vote 6 down vote accepted

This type of operation almost always involves writing a WHERE Clause so I think using at least a little bit of Python is in order.

Also while this is possible to do with ModelBuilder, IMO, creating a Python script tool with custom validation and more control over parameter settings could provide a better user experience -- for example, by making the Field and MultiValue* parameters drop-down menus that you can pick values from instead of having to type them in.

*possibly, actually not sure about MultiValue parameters

However, since that is a more advanced exercise for another topic, I'll stick to the ModelBuilder approach, using Python only to create the WHERE clause:

  1. Create a new model
  2. Create variables (right-click the empty canvas and click Add Variable) for the input Feature Layer, Field, and MultiValue parameters.
  3. Right-click each of these and select Model Parameter
  4. Add a Calculate Value (Data Management) tool to the canvas. Connect the 3 variables to it as preconditions (just for show in this case, but it can make a difference in execution order in other situations).
  5. Double-click the Calculate Value tool to set it up:

    5a. Copy/paste the following into the Expression box (adjust to match your variable names):

    buildWhereClauseMultiValue(r"%Feature Layer%","%Field%","%Values%")
    

    5b. Copy/paste the following into the Code Block box:

    import arcpy
    
    def buildWhereClauseMultiValue(table, field, values):
        """Takes a semicolon-delimited list of values and constructs a SQL WHERE
        clause to select those values within a given field and table."""
    
        # Add DBMS-specific field delimiters
        fieldDelimited = arcpy.AddFieldDelimiters(arcpy.Describe(table).path, field)
    
        # Split multivalue at semicolons and strip quotes
        valueList = [value[1:-1] if (value.startswith("'") and value.endswith("'")) else value for value in values.split(';')]
    
        # Determine field type
        fieldType = arcpy.ListFields(table, field)[0].type
    
        # Add single-quotes for string field values
        if str(fieldType) == 'String':
            valueList = ["'%s'" % value for value in valueList]
    
        # Format WHERE clause in the form of an IN statement
        whereClause = "%s IN(%s)" % (fieldDelimited, ', '.join(valueList))
        return whereClause
    

    5c. Set the output data type to be a SQL Expression.

    5d. Click Ok and right click the output variable from the Calculate Value tool and rename it to something more descriptive like "WHERE Clause".

  6. Add a Select Layer By Attribute (Data Management) tool to the canvas. Connect the Feature Layer and WHERE Clause variables to the Select Layer By Attribute tool.
  7. Add a Copy Features (Data Management) tool to the canvas. Connect the output of the Select Layer By Attribute tool to the Copy Features tool. Right click the Output Feature Class variable and check the Model Parameter and (optionally) the Add to Display options.

    At this point it should look something like this: Model Canvas

  8. Under the Model -> Model Properties menu give it a good name and label and save and close it.

  9. Double-click the model in ArcToolbox to bring up the model dialog. Enter your parameters and click OK. It should export the selected features to a new feature class and add it to the map (if you checked "Add to Display" on the output feature class).

    Model Dialog

NOTES:

  • You can always hardcode values and not "parameterize" variables you don't want the user changing like the feature layer or field parameters. I just like having tools that are generic/reusable so I made those model parameters. In fact what I would do is simply drag and drop the generic model into a new model and then set your predefined parameters -- that way you can make several different predefined "wrapper" models, but only have one underlying model doing the work, so if you need to change its functionality, you only have to change it in one place.

  • If you're lucky like I was you won't have to validate the model and supply some dummy data only to remove it after validation (note how all the model elements are blank/white, meaning they're not "ready to run" -- but I was able to run it anyways upon filling out the parameters on the model dialog).

share|improve this answer
    
Thank you @blah238. It worked. –  Dom Mar 16 '12 at 4:26
    
Cool! Glad to help :) –  blah238 Mar 16 '12 at 4:37
    
@blah238 - At the beginning of your answer you state that this model would run better as a python script. I'm trying write a script that performs the exact task that you have laid out here in model builder. I'm stuck because I don't understand how to take user derived input (the multi values which may be one or more) and write a SQL expression for this. Thanks. –  rralbritton Oct 6 '12 at 0:34

You can also use one simple geoprocessing tool called Select to achieve this workflow if your GUI requirements are not for the precise dialog illustrated by @blah238.

Use the Search window to find it or go to ArcToolbox> Analysis Tools> Extract> Select.

The Select (Analysis) tool lets you extract features using any field (attribute) from a feature class.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.