AnsweredAssumed Answered

Group Layer Definition Query - Python Script

Question asked by jcowell1201 on Sep 15, 2014
Latest reply on Sep 18, 2014 by mlewis22

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()

Outcomes