0

I am trying to create a Python-Toolbox with following Tool:

  • inputFeature: A User has to load a Feature
  • inputField: Then he has to to select a field from the Input Feature
  • inputValue: At the end he has to put in a value (no matter which datatype: date, string, numeric...)

The execute method now should select all rows which are equal to the inputValue.

I already had a working code, but just for numeric inputValues. Another User recomended this code form a past post:

import arcpy

def buildWhereClause(table, field, value):
    """Constructs a SQL WHERE clause to select rows having the specified value
    within a given field and table."""

    # Add DBMS-specific field delimiters
    fieldDelimited = arcpy.AddFieldDelimiters(table, field)

    # Determine field type
    fieldType = arcpy.ListFields(table, field)[0].type

    # Add single-quotes for string field values
    if str(fieldType) == 'String':
        value = "'%s'" % value

    # Format WHERE clause
    whereClause = "%s = %s" % (fieldDelimited, value)
    return whereClause

if __name__ == "__main__":
    inputfc = r"C:\input.shp"
    outputfc = r"C:\output.shp"
    fieldname = "StudyID"
    fieldvalue = 101
    whereclause = buildWhereClause(inputfc, fieldname, fieldvalue)
    arcpy.Select_analysis(inputfc, outputfc, whereclause)

This is the link to the post: Including variable in where clause of arcpy.Select_analysis() It sounds that this would be the perfect solution. But I have no idea how to implement the method in my Python-Toolbox and how to run it with Input-Parameters. Does somebody know how to put the code into the standard Python-Toolbox Codeblock? I mean shouldn't it be used in def execute?

1 Answer 1

0

That's how I implemented the Method and how I used it in my Python Toolbox:

def buildWhereClause(self, table, field, value):
    """Methode baut eine SQL whereClause, um die Zeilen zu selektieren, die
    mit einem bestimmten Wert in der Tabelle eines Features mit einem bestimmten
    Wert übereinstimmen."""

    # Fügt DBMS-spezfische field delimiters hinzu
    fieldDelimited = ap.AddFieldDelimiters(table, field)

    # Wählt den field type aus
    fieldType = ap.ListFields(table, field)[0].type

    # Fügt einzelne quotes f?r die String field values hinzu
    if str(fieldType) == 'String':
        value = "'%s'"% value

    # Format der whereClause
    whereClause = "%s = %s" % (fieldDelimited, value)
    return whereClause


def execute(self, parameters, messages):
    """The source code of the tool."""
    inputFeature = parameters[0].valueAsText
    ap.AddMessage("Input Feature: "+ inputFeature)

    inputFieldname = parameters[1].valueAsText
    ap.AddMessage("Feldname: "+ inputFieldname)

    inputValue = parameters[2].valueAsText
    ap.AddMessage("Suchwert: "+ inputValue)

    outputFeature = parameters[3].valueAsText
    ap.AddMessage("Output Feature: "+ outputFeature)

    whereClause = self.buildWhereClause(inputFeature, inputFieldname, inputValue)
    ap.AddMessage("Where Clause: "+ whereClause)

    ap.SelectLayerByAttribute_management(inputFeature,"NEW_SELECTION", whereClause)
    return

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.