2

I created a script that adds definition queries to every layer in an mxd depending on the layer name.

This worked fine as a stand alone script, but when I try to use it as a script tool with arcpy.GetParameterAsText() it fails on the second query.

Here's the snippet:

import arcpy

arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument(arcpy.GetParameterAsText(0))

def applyDefQuery(mxd):
    df = arcpy.mapping.ListDataFrames(mxd, "")[0]
    layers = arcpy.mapping.ListLayers(mxd, "", df)

    for layer in layers:
        if layer.name == "PROJECT_POLYGON":
            layer.definitionQuery = arcpy.GetParameterAsText(1)
        else:
            layer.definitionQuery = arcpy.GetParameterAsText(2)
    mxd.save()

applyDefQuery(mxd)

The idea is to prep an mxd for QA/QC when a subcontractor returns a checkout with a ton of layers. The script should select all of the project area polygons that they edited within and for every other layer select the records that the subcontractor edited.

I've tried troubleshooting this in a variety of ways: adding a second function for the second query, taking it out of the for loop and running it before the conditional statement, searching for null values in the editor fields instead of the ones I'm looking for (I was concerned that the value format - 'COMPANY\editorname' was causing an issue since it contained an escape character). Nothing seems to work except for leaving the third field in the GUI blank and just running the first definition query.

Here are the parameters for the script tool:

and the input I'm passing to it:

and the error:

Executing: QCPrep "C:\Users\bhamson\Documents\Company Check-    INs\VisualQC_04142015\VisualQC_04162015_2 (1).mxd" ""CONTRACT'" = 'AAA-CO-11-001_005'"     ""EDITOR" LIKE 'COMPANY%'"
Start Time: Tue May 05 18:30:38 2015
Running script QCPrep...
Failed script QCPrep...

Traceback (most recent call last):
  File "C:\Users\bhamson\Documents\Company Check-INs\VisualQC_04142015\QCPrep.py", line  17, in <module>
    applyDefQuery(mxd)
  File "C:\Users\bhamson\Documents\Company Check-INs\VisualQC_04142015\QCPrep.py", line  14, in applyDefQuery
    layer.definitionQuery = arcpy.GetParameterAsText(2)
  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\arcobjects\_base.py",  line 89, in _set
    return setattr(self._arc_object, attr_name, cval(val))
RuntimeError: LayerObject: Set attribute definitionQuery does not exist

Failed to execute (QCPrep). Failed at Tue May 05 18:30:56 2015 (Elapsed Time: 17.58 seconds)

Any idea what the problem could be?

1

It's possible that you have layers that don't support definition queries. Like a raster layer or even a group layer.

I suggest adding some additional logic that only applies the definition query to feature layers. Something like this:

def applyDefQuery(mxd):
    df = arcpy.mapping.ListDataFrames(mxd, "")[0]
    layers = arcpy.mapping.ListLayers(mxd, "", df)

    for layer in layers:
        if layer.isFeatureLayer:
            if layer.name == "PROJECT_POLYGON":
                layer.definitionQuery = arcpy.GetParameterAsText(1)
            else:
                layer.definitionQuery = arcpy.GetParameterAsText(2)
    mxd.save()

You can read more about layer properties here.

  • Worked perfectly, thank you! Its just odd since there weren't any group layers or rasters or anything, just plain feature classes from a geodatabase. It also worked when I ran it as a python script. Not that I'm complaining or anything, just curious... – bradrulez69 May 5 '15 at 22:54
  • what about base layers? Like ESRI backgrounds? Also, if the answer worked for you, please accept it. – Fezter May 5 '15 at 22:58
  • 1
    Accepted! Actually, yes I did have a raster in there as a base layer. Totally forgot about it since I didn't have one in the mxd when I initially ran the script. I can't believe I missed that. Still a good bit of logic to add in the future just in case. – bradrulez69 May 5 '15 at 23:07

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.