2

we use definition queries to filter data to field users. We have up to 40-50 MXDs that are identical except for 1 variable in the definition query that allows the data for that MXD to be unique. Example - "DeploymentNumber='AA'" where 'AA' is the variable. So, when we update MXDs, we would like to be able to use a python script to be able to update the variable - 'AA' component of our definition query in each layer without having to open each MXD, click on each layer and manually update (which in some cases is a 5 minute operation per MXD).

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Nov 18, 2015 at 5:38

1 Answer 1

3

You'll have to change this for your specific data since there might be layers that you don't want to change / don't have def queries set:

import os
import arcpy
# top level directory where all the map docs are
path = r"<path>" 
# recursively search the directory for .mxd files
def getMXD(path):
 for dirpath, dirnames, filenames in os.walk(path):
 for f in filenames:
 if f.endswith(".mxd"):
 yield os.path.join(dirpath, f)
for mapdoc in getMXD(path):
 # might need to change wildcarding here
 mxd = arcpy.mapping.MapDocument(mapdoc) 
 df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
 for lyr in arcpy.mapping.ListLayers(mxd, "*", df): 
 if lyr.supports("DEFINITIONQUERY"):
 lyr.definitionQuery = "DeploymentNumber = 'AA'"
 mxd.save() # or mxd.saveACopy(<newpath>)
 del mxd, df
answered Nov 18, 2015 at 6:29
7
  • 2
    Was on my way to paste almost identical code :) suggest using mxd_files = arcpy.ListFiles("*.mxd") and then mxd_file_path = os.path.join(mxd_files_folder,mxd_file), a bit less code? Commented Nov 18, 2015 at 6:39
  • @AlexTereshenkov, good point (and a lot simpler!) if all your map docs are in the same folder. You'll want os.walk() to recursively drill down into sub-folders as arcpy.ListFiles() only works on whatever is set to env.workspace. Commented Nov 18, 2015 at 6:50
  • ah you are right. You would need to change env.workspace every time you are in another directory.. was always wondering why a parameter input_folder was not implemented for the List* functions in arcpy, is there any specific reason for that? Would love to have arcpy.ListFeatureClasses(input_folder) :) Commented Nov 18, 2015 at 6:57
  • 1
    @AlexTereshenkov, likely because the various List* functions are essentially file globbers with special consideration for ArcGIS data types (e.g., a file geodatabase is a folder). See the glob module for something similar. Commented Nov 18, 2015 at 7:04
  • I will get into that tomorrow when I get back into the office and let you know how I go. Much appreciated. Commented Nov 18, 2015 at 7:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.