Select to view content in your preferred language

Applying Definition Query to Layer

1221
3
02-20-2014 10:16 AM
MikeMacRae
Frequent Contributor
I have some code that goes through and adds layers to a Map Document. I also want to apply definition queries to some layers:

arcpy.MakeFeatureLayer_management(dataSource,dataSource_lyr)
addLayer = arcpy.mapping.Layer(dataSource_lyr)
arcpy.mapping.AddLayer(df,addLayer)
if row.getValue("Definition_Query") <> None:
     addLayer.definitionQuery = "FIELD1" = 'Test'


I think I may be confusing myself, but I'm thinking I set a feature layer on a data source of some kind (arcpy.MakeFeatureLayer_management), and then set that feature layer to a layer object (arcpy.mapping.Layer) so that I have access to the layers properties? At that point, can I not use the addLayer variable to set a definition query?

When I go into the resulting ArcMap and look at the definition query, it's empty.
Tags (2)
0 Kudos
3 Replies
AdamMarinelli
Deactivated User
I believe that the expression syntax is wrong for the Definition Query.

You have:
addLayer.definitionQuery = "FIELD1" = 'Test'

Change to:
addLayer.definitionQuery = ' "FIELD1" = \'Test\' '
0 Kudos
JohnDye
Deactivated User
Where are you establishing a reference to the 'dataSource_lyr' variable?
As it shows,you have clearly established a reference to it above this section of code since you're using the 'dataSource_lyr' to call the layer in your 'MakeFeatureLayer_management()' function.
0 Kudos
MikeMacRae
Frequent Contributor
Thanks Adam, I think that was one of my issues. The second issue, was I was doing a clip between setting the definition query and the adding the layer to the map, which appears to have deleted the query during the clip.

John, the dataSource_lyr variable was just a string concatenation I set earlier to get a dynamic name for the feature layer output.
0 Kudos