Select to view content in your preferred language

Workspace Environment Issue for ArcPy Spatial Analyst

1996
7
05-20-2020 10:01 AM
VinceE
by
Frequent Contributor

I'm having issues using arcpy and spatial analyst functions. The workspace environment seems to get selectively ignored. I am running this code directly in the Python window in ArcGIS Pro 2.5.1. The behavior appears the same in an existing project versus a brand new unsaved project.

import arcpy as ap
from arcpy import env
from arcpy.sa import *

testing = True

env.workspace = r"C:\Users\Desktop\Prop\Prop_Slope.gdb"

dem = r"F:\5_Tools\SlopeClass\AARM.gdb\DEM_AOI"
demfs1 = FocalStatistics(dem, "Circle 5 CELL", "MEAN")
demfs2 = FocalStatistics(demfs1, "Circle 5 CELL", "MEAN")
demfs3 = FocalStatistics(demfs2, "Circle 5 CELL", "MEAN")

#with arcpy.EnvManager(workspace = env.workspace):
Contour(demfs3, "Contour", 10, 0, 1)

remap = "0 19.999 1;20 49.999 2;50 10000 3"
rclass = Reclassify(demfs3, "VALUE", remap, "NODATA")

#with arcpy.EnvManager(workspace = env.workspace):
ap.conversion.RasterToPolygon(rclass, "Reclass_Polygon", "SIMPLIFY", "VALUE",
        "SINGLE_OUTER_PART")

if testing:
    demfs1.save("FS1")
    demfs2.save("FS2")
    demfs3.save("FS3")
    rclass.save("Reclassified")
del demfs1, demfs2, demfs3, rclass‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The code itself executes without a hitch, however the outputs get saved in different locations. Calling the .save() method on the returned raster objects will create permanent saves in the location specified in "env.workspace". However, both the outputs of "Contour" and "RasterToPolygon", get saved to the Default geodatabase for the project (either a named default, or just the "Default.gdb" in an unsaved project).

Using the currently commented-out "EnvManager" and "with" blocks corrects this problem. Why does it appear necessary to explicitly define the workspace for these tools, when I have already done it globally at the top? What aspect of the workspace environment am I missing? Perhaps the use of spatial analyst tools here is coincidental, but it's when using these tools that I tend to experience strange behavior with the workspace environment.

Any help would be greatly appreciated.

7 Replies
JoeBorgione
MVP Emeritus

I always explicitly create a variable called outWS and direct my outputs there.  This is the first time I've seen a reference to the arcpy.EnvManager; I don't know how I've missed that.

I think when you set the workspace env as you do line 7 it allows you to refer to access existing data in that workspace. However, the geoprocessing tools probably just look at the default workspace / gdb for out put.

That should just about do it....
VinceE
by
Frequent Contributor

Thanks Joe. I usually do that as well (f"{outWS}\output") and completely skip the env.workspace setting because it isn't reliable. It would just be cleaner to use the workspace if possible. I've used the workspace setting successfully in other scripts, but it seems to have issues with spatial analyst, maybe other scenarios, but I can't diagnose what causes it. Also, RasterToPolygon isn't an SA tool, so that might invalidate my theory that spatial analyst is to blame.

The documentation says "Tools that honor the Current Workspace environment setting use the workspace specified as the default location for geoprocessing tool inputs and outputs." Both Contour and RasterToPolygon are supposed to respect Current Workspace. All I'm doing with EnvManager is restating the same workspace that I initialized at the top, so it's not clear to me why that works.

0 Kudos
DavidPike
MVP Frequent Contributor

Be inetreresting to see Esri's pitch on this, thanks for the arcpy.EnvManager  **steals**

VinceE
by
Frequent Contributor

EnvManager is definitely helpful for temporarily changing the workspace (when it works!), disabling Z or M values, changing raster cell size, etc. Glad you'll find that useful.

Hopefully we'll hear something from Esri, curious to see what could be happening here.

0 Kudos
TPuett
by
Emerging Contributor

Agreed! It still seems to have an issue at 3.3!!

0 Kudos
NicholasKlein-Baer
Occasional Contributor

I'm having very a vary similar issue with a custom python toolbox in ArcGIS Pro 2.5, described here on stack exchange. As mentioned above, there are several workarounds, but it would be nice to get a response from ESRI staff about what is going on ...

Vince Edwards I believe you are correct that the spatial analyst tools are to blame. In both of our cases the problem occurs with non-SA tools, but only AFTER using an SA tool. A couple of other observations:

1) Simply printing  `arcpy.env.workspace` to the console immediately before the offending GP tools (in this case contour and rasterToPolygon) fixes the issue. This is strange to me, because it shows the it env.workspace has not been somehow 'unset' - the correct value is printed and suddenly arcpy is 'reminded' of the correct location.

2) This only happens in an ArcGIS Pro project - everything seems to work as expected if running a script from the python command prompt or an IDE

VinceE
by
Frequent Contributor

Nicholas,

Thanks for sharing these workarounds/fixes and the your observations. I had the same experience when printing the workspace to verify it hadn't been "unset." At this point, I've opted to always use my own workspace variable for outputs, or alternatively, use EnvManager, which seems to fix the issue (although is even more cumbersome than using full paths when dealing with lots of outputs).