Definitition Query and Layer class - how to use?

313
1
12-19-2011 09:46 AM
JoshuaChan
New Contributor III
In the arcHelp there's a page under "Classes" that says that Layers have a property of Definition Query. How do I use that?

I want to be able to have the user input a value which will then be used as a query in a layer's definition query. And then I want to be able to zoom into that geographic extent.

does anyone have any examples of how I could do that?  The ArcHelp page mentions it but I don't know how it works
Tags (2)
0 Kudos
1 Reply
JeffBarrette
Esri Regular Contributor
Here is an example from the arcpy.mapping Sample Script Tools download from the Resource Center:
http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=A910AB18-1422-2418-3418-3885D...

The script replaces layer data sources from a personal GDB to a fGDB.  It also updates the layer defintion query so the SQL syntax is correct.

    #Read parameters from dialog
    inputGDB = arcpy.GetParameterAsText(0)    #Input personal GDB
    outputGDB = arcpy.GetParameterAsText(1)  #Output file GDB
    inputMXD = arcpy.GetParameterAsText(2)    # Input MXD
    outputMXD = arcpy.GetParameterAsText(3)  #Output MXD
    updateSQL = arcpy.GetParameter(4)           #Update SQL query

    #Update pGDB TO fGDB
    mxd = arcpy.mapping.MapDocument(inputMXD)
    mxd.replaceWorkspaces(inputGDB, "ACCESS_WORKSPACE", outputGDB, "FILEGDB_WORKSPACE")

    #Update query definitions
    if updateSQL:
        for lyr in arcpy.mapping.ListLayers(mxd):
            if lyr.supports("definitionQuery"):
                lyr.definitionQuery = lyr.definitionQuery.replace("[","\"")
                lyr.definitionQuery = lyr.definitionQuery.replace("]","\"")
            if lyr.supports("labelClasses"):
                for lblClass in lyr.labelClasses:
                    lblClass.SQLQuery = lblClass.SQLQuery.replace("[", "\"")
                    lblClass.SQLQuery = lblClass.SQLQuery.replace("]", "\"")

    #Save and open resulting MXD
    mxd.saveACopy(outputMXD)
    os.startfile(outputMXD)


You can download the sample and test it on the data it comes with.

Jeff
0 Kudos