5

I have ~600 lyr files that reference a table on an SDE database that has about 20,000,000 records. Some of these *lyr files have an incorrect definition query applied or is missing the query entirely.

I think I'm looking for a way to create a table with the Lyr file name and it's definition query, but I'm not sure if that's something I can do in python or model builder. Can anyone pt me in the right direction?

8

The arcpy.mapping module makes it pretty easy if you're comfortable doing a little Python coding. Here's the basic outline:

At the beginning of your Python script, get the list of .lyr files (using glob.glob("*.lyr"), for example).

Then for each file, call arcpy.mapping.ListLayers(filename), passing it the filename of your .lyr file. This returns a list of Layer objects.

Each of these Layer objects has a definitionQuery property that you could print or write to a file.

I imagine the code would be something like this (though I haven't actually tried to run this):

import glob
import arcpy
import arcpy.mapping
filenames = glob.glob("C:\\path\\to\\layer\\files\\*.lyr");
for filename in filenames:
    layers = arcpy.mapping.ListLayers(filename)
    for layer in layers:
        print filename + ',' + layer.longName + ',' + layer.definitionQuery

And you should get output like this:

C:\path\to\layer\files\foo.lyr,Some Layer Name,WHERE foo='xyz'
C:\path\to\layer\files\var.lyr,Another Layer Name,WHERE foo='abc'
4

This function (inventory_layers_and_mxds) will create a new table and populate it with definition queries

import os

import arcpy

def inventory_layers_and_mxds(out_table, folder_to_inventory, mxds=True, lyrs=True):
    # Set up inventory table
    print [os.path.dirname(out_table),
           os.path.basename(out_table)]
    arcpy.management.CreateTable(os.path.dirname(out_table),
                                 os.path.basename(out_table))
    arcpy.management.AddField(out_table, "mapdocument", "TEXT",
                              field_length=320)
    arcpy.management.AddField(out_table, "layername", "TEXT",
                              field_length=128)
    arcpy.management.AddField(out_table, "definitionquery", "TEXT",
                              field_length=320)

    # open cursor, loop over directory looking for MXDs
    with arcpy.da.InsertCursor(out_table,
                               ["mapdocument",
                                "layername",
                                "definitionquery"]) as inv_cursor:
        # Spider through folder
        for foldername, subdirectories, files in os.walk(folder_to_inventory):
            for filename in files:
                file_path = os.path.join(foldername, filename)
                item = None
                # Is file an MXD and are we looking at map docs?
                if os.path.splitext(filename)[1].lower() == ".mxd" and mxds:
                    mxd = arcpy.mapping.MapDocument(file_path)
                    item = mxd 
                # Is file a LYR and are we looking at layers?
                if os.path.splitext(filename)[1].lower() == ".lyr" and lyrs:
                    lyr = arcpy.mapping.Layer(file_path)
                    item = lyr
                if item is not None:
                    # Now loop over layers
                    for layer in arcpy.mapping.ListLayers(item):
                        # Only insert row if it does a definition query
                        # (ignore group layers etc)
                        if layer.supports("DEFINITIONQUERY"):
                            inv_cursor.insertRow([file_path, layer.longName,
                                                  layer.definitionQuery])
2

With the ArcPy Mapping module you can read or write a Layers definitionQuery. Here is a blog post.
Working with a Definition Query, ArcPy and Python

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.