AnsweredAssumed Answered

Replace Definition Query

Question asked by sev0302 on Dec 16, 2015
Latest reply on Dec 25, 2015 by Dan_Patterson

I've got a series of MXDs with definition queries with file gdb-specific syntax. I'd like to batch modify those DQs to work with enterprise gdb syntax. Not every layer has a DQ, and not all DQs are the same. The problem I'm running into is with the expressions portion... the function is passing 'SHAPE.STLength()' with the quotes to the definition query, which doesn't match the syntax (need it without the quotes). Changing them to double quotes returns similar results, and removing the quotes all together causes the function to fail.

 

Any ideas?

 

import arcpy

from arcpy import env

from os import path

 

env.workspace = ws = r"C:\temp\DefQueryScript\test1"

 

expressions = {

    'shape_length'  : 'SHAPE.STLength()',

    'shape_area'    : 'SHAPE.STArea()',

 

    'SHAPE_Length'  : 'SHAPE.STLength()',

    'SHAPE_Area'    : 'SHAPE.STArea()',

 

    'SHAPE_length' : 'SHAPE.STLength()',

    'SHAPE_area'   : 'SHAPE.STArea()',

}

 

def letsGetThisPartyStarted():

    ''' For each Map Document in the workspace, check each layer for a

        definition query, and if found, update it according to the contents of

        the expression dictionary.'''

    print "Let's get this party started!\n"

    mxds = getMXDs()

    for i, mxd in enumerate(mxds):

        for layer in getLayers(mxd):

            evaluateExpression(layer)

        saveMXD(mxd, i)

 

def getMXDs():

    ''' Create a list of each Map Document in the workspace(s).'''

    mxds = [arcpy.mapping.MapDocument(m) for m in arcpy.ListFiles("*.mxd")]

    return mxds

 

def getLayers(mxd):

    ''' Collects all feature layer in a Map Document.'''

    print "{0} layers:\n".format(mxd.filePath)

    layers = [l for l in arcpy.mapping.ListLayers(mxd) if l.isFeatureLayer]

    return layers

 

def evaluateExpression(lyr):

    ''' If the layer has a Definition Query, update that query using the

        dictionary of expressions.'''

    if lyr.definitionQuery:

        for exp in expressions.items():

            key, value = exp[0], exp[1]

            if key in lyr.definitionQuery:

                updateDefinitionQuery(lyr, key, value)

    else:

        print "        {0}: No Definition Query found\n".format(lyr.name)

        pass

 

def updateDefinitionQuery(lyr, old_exp, new_exp):

    ''' Replaces the old expression with the new one. Fill in the old/new

        associations in the "expressions" dictionary above.'''

    print "        {0}:".format(lyr.name)

    print "            Old expression: {0}".format(lyr.definitionQuery)

    lyr.definitionQuery = lyr.definitionQuery.replace(old_exp, new_exp)

    print "            New expression: {0}\n".format(lyr.definitionQuery)

 

def saveMXD(mxd, i):

    ''' Saves a copy of the Map Document which has had it's layers Definition

        Queries updated.'''

    mxd.saveACopy(path.join(ws, "COPY_" + mxd.filePath + "_{0}.mxd".format(i)))

    del mxd

 

if __name__ == "__main__":

    letsGetThisPartyStarted()

    print "'fin.'"

Outcomes