inconsistent Arcpy.Mapping behaviour

2342
28
07-31-2014 10:55 AM
JoshuaChan
New Contributor III

I've written a script using Arcpy.mapping module to automate map making. It works as a one-off for use in ArcMap if I use the ArcToolbox to call the script. But if I call it from python (eclipse) I get weird error messages ("ExecuteError: ERROR 000358: Invalid expression") pointing towards the "Select by Layer" tool. But it executes fine if run from ArcToolbox, no errors... same script.

Part 2: I gave up running it from Eclipse and ran it from ArcToolbox within ArcMap. Now I'm trying to run a batch - multiple maps. It used to work until this morning. this morning I made the most minor of changes... to check to see if a variable.upper() == 'YES'

again, this works in the "one-off" scenario within ArcMap called from ArcToolbox. But I'm trying to run my batch script (also called by ArcToolbox) and I get that same query error as above. Why is that? it's the exact same script. I don't want to interactively run this script in ArcMap for all 350+ maps. Why is arcpy.mapping so flaky? Is there a way to fix this???

0 Kudos
28 Replies
JoshuaChan
New Contributor III

I asked our server administrator and here's what patches we have:

ArcGIS 10.1 for Desktop
  ArcGIS 10.1 SP1 for Desktop
  ArcGIS 10.1 SP1 for Desktop Set Bing Key Patch
  ArcGIS 10.1 SP1 for Desktop Quality Improvement Patch

I'm not sure how this affects this strange bug where my program works one at a time but not in a batch. I've changed the query format as you suggested. 

So after using the string format syntax example, instead of the update layer error (as per yesterday) it is giving me the older query format error again:

Executing: batchMap4PMV

Start Time: Fri Aug 08 08:04:35 2014

Running script batchMap4PMV...

checking out inset map placement: None

no inset map

beginning layer filter of data frameLayers

CROWN_LANDS_FILE = '2411100'

Tantalis Files

defined the lyr for Tantalis Files?

ERROR 000358: Invalid expression

Failed to execute (SelectLayerByAttribute).

Traceback (most recent call last):

  File "W:\srm\sry\Local\projlib\2014-15\Auth_Lands_PMV\data\scripts\batchMapRobot.py", line 80, in <module>

    runMapRobotScript(filenum, outfile)

  File "W:\srm\sry\Local\projlib\2014-15\Auth_Lands_PMV\data\scripts\batchMapRobot.py", line 42, in runMapRobotScript

    obj.dataFrameFilter(obj.mainDF, obj.mainDefQry, inset_map)

  File "W:\srm\sry\Local\projlib\2014-15\Auth_Lands_PMV\data\scripts\map_robot_2014_PMV_v2_4batch.py", line 163, in dataFrameFilter

    arcpy.SelectLayerByAttribute_management(lyr,"NEW_SELECTION", defQry)

  File "e:\sw_nt\arcgis\desktop10.1\arcpy\arcpy\management.py", line 6498, in SelectLayerByAttribute

    raise e

ExecuteError: ERROR 000358: Invalid expression

Failed to execute (SelectLayerByAttribute).

Failed to execute (batchMap4PMV).

Failed at Fri Aug 08 08:04:43 2014 (Elapsed Time: 8.00 seconds)

0 Kudos
XanderBakker
Esri Esteemed Contributor

I suppose that if you manually apply the definition query CROWN_LANDS_FILE = '2411100' to the layer, it works without any problems, right? If so, then the definition query is not the problem. It might be the layer itself or other aspects of the environment you're working in. Those are hard to solve without access to the data and mxd you're using.

I would probably step away from using of classes and rewrite the code using the da cursors and set the extent using the geometry rather than selecting and zooming to the extent of the selection.

0 Kudos
JoshuaChan
New Contributor III

I don't understand why the code works as a one off but not in the looped scenario.

I'll have to read up on da cursors vs regular run of the mill cursors.

as for the geometry thing, can you send me a link to that? The one you sent previously was talking about exporting to shapefile, not zooming to an extent.

0 Kudos
JoshuaChan
New Contributor III

my time is pretty limited so I'm trying to avoid having to rewrite the entire thing. That may take me as much time as manually doing this one at a time.

0 Kudos
JoshuaChan
New Contributor III

plus without knowing what the real problem is, i'm hesitant to rewrite it not knowing if that will actually fix the (unknown) problem.

0 Kudos
JoshuaChan
New Contributor III

sorry, i see now the notes you put in there for lines 35-37 for map extent setting. more reading to do....

0 Kudos
XanderBakker
Esri Esteemed Contributor

Although it seems as a lot of work to rewrite the code, it might pay off in the long run...

0 Kudos
XanderBakker
Esri Esteemed Contributor

This is a good place to start reading on Accessing data using cursors‌ and obviously SearchCursor (arcpy.da)

0 Kudos
JoshuaChan
New Contributor III

I've discovered the reason behind the "select by attribute" error but not the reason behind the UpdateLayer error.

The SelectByAttribute kept popping up because I was not yet connected to the SDE. rather than give me a dialog box to log in - it just returned an error. I don't know how to fix that other than to open up the MXD, connect to the SDE, then close the MXD and run the script from the ArcToolbox in a blank new mxd. Seems kinda hokey but thats all i could think of for now.

BUt the Update Layer part is still broken... In my script instead of printing out the name of my "Dimensions" layer I decided to print out all the layer names. I can see all the layers EXCEPT for "Dimensions". I re-open my MXD and I can see a layer named "Dimensions". It has something in the Attribute table so it's not an empty layer. I remove it... I re-add it. and hit Save. I re run the script and same problem... ListLayers does not show "Dimensions" even though it really is there.

This might be the sticking point... why does the layer not want to show up in the batch version of the script?

0 Kudos
JoshuaChan
New Contributor III

so I haven't touched this for a week. I copied my old mxd and re-ran the program with no changes to the script. Now the error message is different:

"

Traceback (most recent call last):

  File "W:\srm\sry\Local\projlib\2014-15\Auth_Lands_PMV\data\scripts\batchMapRobot.py", line 80, in <module>

    runMapRobotScript(filenum, outfile)

  File "W:\srm\sry\Local\projlib\2014-15\Auth_Lands_PMV\data\scripts\batchMapRobot.py", line 46, in runMapRobotScript

    obj.doDimensions(obj.mainDF, tantlyr)

  File "W:\srm\sry\Local\projlib\2014-15\Auth_Lands_PMV\data\scripts\map_robot_2014_PMV_v2_4batch.py", line 230, in doDimensions

    arcpy.mapping.UpdateLayer(dFrame, upLyr, srcLyr, False)

  File "e:\sw_nt\arcgis\desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_

    return fn(*args, **kw)

  File "e:\sw_nt\arcgis\desktop10.1\arcpy\arcpy\mapping.py", line 1869, in UpdateLayer

    assert isinstance(update_layer, Layer)

AssertionError

Failed to execute (batchMap4PMV).

"

I placed a whole bunch of add message statements to see where exactly it was bombing and it's on the UpdateLayer line.

Again, this script runs without error when I'm doing it one at a time in ArcMap via ArcToolbox. But when trying to run as a batch - namely have a script call the original script with a bunch of arguments - it bombs. The error message doesn't make much sense to me and so far every online answer to similar queries point to people sending a text string to the UpdateLayer function instead of a map document object or a layer object.  If you check my script (it's still on the FTP site listed above) I'm fairly certain I have sent it map document object and layer object.

Any help much appreciated.

0 Kudos