Select to view content in your preferred language

Fill causes syntax error in parsing grid expression when run in background

3726
5
03-16-2017 08:39 AM
Jelmervan_der_Linde
Deactivated User

I'm trying to use arcpy.sa.Fill in a python toolbox command, however it always fails with the following error:

Executing: FillFailTest C:\proef\mv_gron
Start Time: Fri Apr 07 10:19:57 2017
Running script FillFailTest...

Workspace was C:\Users\jelmer\Documents\ArcGIS\Default.gdb
Workspace is C:\WorkSpace

Traceback (most recent call last):
 File "<string>", line 34, in execute
 File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\sa\Functions.py", line 2089, in Fill
 z_limit)
 File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\sa\Utils.py", line 53, in swapper
 result = wrapper(*args, **kwargs)
 File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\sa\Functions.py", line 2085, in Wrapper
 z_limit)
 File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\geoprocessing\_base.py", line 513, in _callback
 return val(*gp_fixargs(args, True))
ExecuteError: ERROR 010328: Syntax error at or near symbol ).
ERROR 010267: Syntax error in parsing grid expression.
Failed to execute (Fill).

Failed to execute (FillFailTest).
Failed at Fri Apr 07 10:19:59 2017 (Elapsed Time: 1,45 seconds)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The odd thing is, the fill command works fine if I run it from the embedded arcpy console:

from arcpy.sa import Raster
from arcpy.sa import Fill
x = Raster(r'C:\test\mv_gron')
y = Fill(x, 40)‍‍‍‍

(Updated) To test Shaun's solution, I've also added these lines:

arcpy.env.workspace = r"C:\WorkSpace"
arcpy.env.scratchWorkspace = r"C:\WorkSpace"‍‍

It also works fine when I set the canRunInBackground property of the tool to False. Is there a way to run the too with the Fill call in the background?

I've attached a small toolbox example that fails for me.

(Update 2) I'm starting to doubt whether it has something to do with canRunInBackground or not. Currently, the Fill tool and arcpy.sa.Fill only work when used directly in ArcMap. If I call arcpy.sa.Fill in a python toolbox from inside ArcMap, or from IDLE, I encounter the syntax error. All paths are already short and simple, except for the install location of ArcMap (which is in the default C:\Program Files (x86)\Ar..etc).

Tags (3)
0 Kudos
5 Replies
ShaunWalbridge
Esri Regular Contributor

Older spatial analyst tools use GRID files as intermediate data. My guess is, the output location that it's trying to write these two when using background geoprocessing is invalid. Try adding something like

arcpy.env.workspace = r"c:\workspace"
arcpy.env.scratchWorkspace = r"c:\workspace"

In the initialization of your script, where the location is a) a short name b) contains no spaces.

Cheers,
Shaun

Jelmervan_der_Linde
Deactivated User

Unfortunately, setting the workspace env variable didn't solve it for me. I've updated the question and attachment with the code I tried.

I've also compared all the other environment variables in arcpy.env in the internal python window in ArcMap with the values in a stand-alone Python session, and except for the workspace one they were all equal.

Here is the minimum test case I can come up with:

import arcpy
arcpy.CheckOutExtension("Spatial")
arcpy.env.workspace = r'c:\filltest'
arcpy.env.scratchWorkspace = r'c:\ws'
out = arcpy.sa.Fill('mv', 40)

Copy & pasting these lines in ArcMap's Python console works just fine. Running them from IDLE results in the exception I mentioned in the first post.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Is there any specific reason for implementing this functionality in a Python Toolbox rather than using a script added to a Toolbox? I couldn't find any reason for that based on your code. 

The other thing I noticed is that in the Python Toolbox you create a raster object that you provide to the Fill operation when in the test snippet you provide a string (which is what is used in the sample code on the page). Could you try with simply passing the "parameters[0].valueAsText" as first parameter rather than the Raster object?

Based on the fact that you mention "Maaiveld" in the code, I assume that you are working with Dutch data (AHN#). Isn't 40 meters a rather high value to use for a fill, in a flat country like the Netherlands or are you using cm as unit?

Jelmervan_der_Linde
Deactivated User

The python toolboxes are easy to track in git, including all the parameter types etc.

I've tried to add a script to the a normal toolbox, using GetParameterAsText directly, with the following script and defining a single Raster Layer parameter in the toolbox:

import arcpy
import os

os.chdir(r'c:\ws')  # otherwise Fill tries to write temp stuff to the working directory of ArcMap
arcpy.CheckOutExtension("Spatial")

arcpy.env.workspace = r'c:\filltest'
arcpy.env.scratchWorkspace = r'c:\ws'

inp = arcpy.GetParameterAsText(0)
out = arcpy.sa.Fill(inp, 40)

print("Win!")

If I check the "Always run in foreground" checkbox, it works. Do I remove that check, running the script results in the same error again:

ExecuteError: ERROR 010328: Syntax error at or near symbol ).
ERROR 010267: Syntax error in parsing grid expression.
Failed to execute (Fill).

The data is indeed (based on) the AHN2 data, but the heights are encoded in millimeters so 40 isn't that much  The model itself works quite nicely (albeit slow) but only in the foreground. Because of that slowness I would like to push it to the background (or even better, a stand-alone script!)

0 Kudos
XanderBakker
Esri Esteemed Contributor

I'm sure running it as a stand alone script will not be a problem. Probably the error is caused by the setting to enable background processing. 

Disabling the background processing is normally the first thing I would try.

0 Kudos