inconsistent Arcpy.Mapping behaviour

2334
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

ps for the one-off my mxd is set to "CURRENT" but for the batch I did link it to the actual mxd file location "C:\blah\blah\awesome.mxd"

The batch worked this morning until I added the "upper()" part.

if you want to see the scripts you can get them at: ftp://ftp.geobc.gov.bc.ca/pub/outgoing/GeoBC_Regional/Coast/tmp_mapRobot/

thx

0 Kudos
JasonScheirer
Occasional Contributor III

Can you post a full traceback? There's a lot that could be going wrong here. arcpy.mapping should work the same in all environments, I suspect something else is at play here.

0 Kudos
JoshuaChan
New Contributor III

what is a traceback?

I'm not a real programmer - just a GIS guy thrown into the possibilities of python. A lot of my script had to be fixed by a real python guy but he stopped when he said that the errors that are ESRI based are out of his "scope".

If you mean the error messages it looks like this now:

"

Executing: batchMap4PMV

Start Time: Thu Jul 31 13:38:22 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 161, 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 Thu Jul 31 13:38:29 2014 (Elapsed Time: 7.00 seconds)

"

Oh and I've tried the quotes around the field names. It works OK without the quotes in the ArcMap interactive world.

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

no answers? no similar experiences?

I'll try rebuilding my mxd from scratch... maybe it's another corrupted MXD????

0 Kudos
JoshuaChan
New Contributor III

P.S. with the new forum format, where is this post supposed to go?

is this the wrong forum? where do the arcpy.mapping experts hang out?

0 Kudos
IanMurray
Frequent Contributor

Probably this should go in the python space. Python

Timothy Hales‌, can you get this moved?

Xander Bakker‌, you mind taking a look at this code?

0 Kudos
TimothyHales
Esri Notable Contributor

I've moved it to Python‌. Thanks for the heads up!

0 Kudos
XanderBakker
Esri Esteemed Contributor

Just had a "short" look at the code, and from the error message I see you're using version 10.1 of ArcGIS. Any service packs installed? At 10.1 you can use lot's of the goodies provided like the data access module, but you're using the old and slow cursors.

Code normally does the same thing each time over and over. If it doesn't then normally something of the environment it runs in (ArcGIS, underlying data, state of elements in your MXD, etc) changed.

Just to respond to the discussion on whether to use quotes around the field name or field values not. Don't think about this to much, since arcpy can do the work for you. As you know the way you have to specify the field with single quotes, square brackets or just without, depends on the data source. Arcpy comes with a tool ("AddFieldDelimiters") that will do the work for you. Have a look at the snippet below:

fc = r"C:\some\path\to\a\folder\or\fgdb.gdb\myFeatureClass"

fld_name = "myField"

a_value = 123456

qdef = "{0} = {1}".format(arcpy.AddFieldDelimiters(fc, fld_name), a_value)

When you want to append text into one string try to avoid this:

"CROWN_LANDS_FILE = '" + str(fileNum) + "'"

... rather use the string format syntax like:

"CROWN_LANDS_FILE = '{0}'".format(fileNum)

This makes it easier to construct complex strings.

If you want to zoom to a feature, you can also use the extent of the feature geometry, as I have explained in this thread:

https://community.esri.com/message/393455#393455

Then there's this thing about classes; the fact that you want to run a piece of code 350 times, for me isn't a reason to create a class for it. Sure you can, but the idea of classes (IMHO) is that they should contain reusable generic code and not all kinds of specifics as is the case in your code. It makes it very hard to read your code.

Kind regards, Xander

0 Kudos