Change definition query

1481
3
Jump to solution
04-20-2013 08:28 PM
MarkPaulson
New Contributor III
I have three feature classes that have multiple features in each layer. Each feature class has a field called parcel_no. I turn different parcels on by a definition query on each layer such as parcel_no = 1. This will show all three layer for parcel 1. I would like to run a script that will change the definition query for all three layers, say from parcel_no = 1 to parcel_no = 5. New to python and need to be pointed in the right direction.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor
This is done by modifying the Layer object's definitionQuery property. There are several examples to get you started in the help here:

Desktop 10.1 Help: (arcpy.mapping)

import arcpy mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd") df = arcpy.mapping.ListDataFrames(mxd)[0] # first data frame lyrs = arcpy.mapping.ListLayers(mxd, "" , df) fixLayers = ["Test1 polygon","Test2 polygon"] newParcel = 5 for lyr in lyrs:   if lyr.name in fixLayers:       lyr.definitionQuery = "parcel_no = {0}".format(newParcel) arcpy.RefreshActiveView() del mxd # release the object (the map will not be deleted)

View solution in original post

0 Kudos
3 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Mark,

I would recommend using a Python Add-In Combo Box.  You can select the query from the drop down and apply it to the desired layers.  Here is an example of the code to use for the combo box:

import arcpy
import pythonaddins

class ComboBoxClass1(object):
    """Implementation for Query_addin.combobox (ComboBox)"""
    def __init__(self):
        self.items = ['"parcel_no" = 1', '"parcel_no" = 2', '"parcel_no" = 3', '"parcel_no" = 4', '"parcel_no" = 5']
        self.editable = False
        self.enabled = True
        self.dropdownWidth = 'WWWWWWWWWW'
        self.width = 'WWWWWWWWWW'
    def onSelChange(self, selection):
        self.mxd = arcpy.mapping.MapDocument("CURRENT")
        for lyr in arcpy.mapping.ListLayers(self.mxd):
            if lyr.name.lower() == "parcels1":
                lyr.definitionQuery = selection
            if lyr.name.lower() == "parcels2":
                lyr.definitionQuery = selection
            if lyr.name.lower() == "parcels3":
                lyr.definitionQuery = selection
        arcpy.RefreshTOC()
        arcpy.RefreshActiveView()
0 Kudos
curtvprice
MVP Esteemed Contributor
This is done by modifying the Layer object's definitionQuery property. There are several examples to get you started in the help here:

Desktop 10.1 Help: (arcpy.mapping)

import arcpy mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd") df = arcpy.mapping.ListDataFrames(mxd)[0] # first data frame lyrs = arcpy.mapping.ListLayers(mxd, "" , df) fixLayers = ["Test1 polygon","Test2 polygon"] newParcel = 5 for lyr in lyrs:   if lyr.name in fixLayers:       lyr.definitionQuery = "parcel_no = {0}".format(newParcel) arcpy.RefreshActiveView() del mxd # release the object (the map will not be deleted)
0 Kudos
MarkPaulson
New Contributor III
Thanks for the direction. Ended up setting 15 bookmarks (1-15) and used a for loop to set the definition query for each parcel. Many thanks.
0 Kudos