Select to view content in your preferred language

Group Layer Definition Query - Python Script

3490
3
09-15-2014 07:40 AM
JasonCowell
Emerging Contributor

Hello,

I'm trying to develop a quick tool to perform a definition query on a group layer based on the users input (Field, Value in Field, and Group Layer). I did some searching around and came up with the script that I have below. Originally I set the field, value, and group parameters directly equal to a particular value and the code worked perfectly. But now I want the user to be able to enter the field and value they want to perform the definition query with as well as which group layer to use. Every time I run the tool it says that "field" is undefined which means some how my parameters are not being returned. Is there some syntax that I'm missing or do I have any of the parameters set up incorrectly. I'm just starting to learn python so any help is welcome.

Thanks.

import arcpy

#Variables to form defintion query

def getParameterInfo(self):

    field = arcpy.Parameter(

        displayName="Field",

        name="field",

        datatype="Any Value",

        parameterType="Required",

        direction="Input")

  

    value = arcpy.Parameter(

        displayName="Value in Field",

        name="value",

        datatype="Any Value",

        parameterType="Required",

        direction="Input")

    group = arcpy.Parameter(

        displayName="Group Layer Name",

        name="group",

        datatype="Group Layer",

        parameterType="Required",

        direction="Input")

    params = [field, value, group]

   

    return params

#concatenate query syntax

queryStr = str(field) + "=" + str(value)

#Specify the MXD project (CURRENT), dataframe (Layers)

mxd = arcpy.mapping.MapDocument("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

#Apply defintion query to specified layer group (Test)

for lyr in arcpy.mapping.ListLayers(mxd, group, df)[0]:

    if lyr.supports("DEFINITIONQUERY"):

        lyr.definitionQuery = queryStr

arcpy.RefreshActiveView()

0 Kudos
3 Replies
MatthewLewis
Deactivated User

Could you use?

field = arcpy.GetParameterAsText(0)

value  = arcpy.GetParameterAsText(1)

group = arcpy.GetParameterAsText(2)

0 Kudos
JasonCowell
Emerging Contributor

That did the trick. Thanks.

0 Kudos
MatthewLewis
Deactivated User

might be worth marking as answered just so it gets filtered off the unanswered list 🙂

0 Kudos