RasterWorkspace: optimize environment for faster arcpy map algebra

2729
0
09-03-2015 11:48 AM

RasterWorkspace: optimize environment for faster arcpy map algebra

Because temporary rasters that must be written to disk in arcpy map algebra are written in Esri grid format to a folder, it is best practice for heavy map algebra to set the workspace and scratchWorkspace environments to the same folder.

If the scratch and current are written to the same folder, the operation:

raster = CreateConstantRaster(1)
raster.save("mygrid")

will rename the temp grid (named something like createconsta_1) to "mygrid". If the workspace is not a folder or scratchWorkspace is somewhere else, the raster will have to be copied (and maybe converted, say from grid to .gdb), not renamed. This can take a while for large rasters, and since heavy map algebra can be very processing intensive, why not cut a corner!

Here's an example run from the ArcMap Python window:

>>> env.workspace = r"D:\Users\cprice\work\Default.gdb"
>>> RasterWorkspace(1)
  workspace:        D:\Users\cprice\work\Default.gdb
  scratchWorkspace: D:\Users\cprice\work\scratch
Set to:
  workspace:        D:\Users\cprice\work\scratch
  scratchWorkspace: D:\Users\cprice\work\scratch
>>> env.workspace = r"D:\Users\cprice\work"
>>> RasterWorkspace(1)
  workspace:        D:\Users\cprice\work
  scratchWorkspace: D:\Users\cprice\work\scratch
Set to:
  workspace:        D:\Users\cprice\work
  scratchWorkspace: D:\Users\cprice\work

def RasterWorkspace(report=False):
    """Set a raster workspace (folder) based the current workspace.
    If the current workspace is a valid folder,
    workspace, and scratchWorkpace will be set to that location.
    Otherwise, the current workspace is set to env.scratchFolder.
    
    This is done because arcpy map algebra work most efficiently if the 
    current and scratch workspace are set to the same system folder
    (not a geodatabase)
    """
    import arcpy
    from arcpy import env
    if report:
        print("  workspace:        {}\n  scratchWorkspace: {}".format(
            env.workspace, env.scratchWorkspace))
    try:
        if env.workspace:
            if not arcpy.Exists(env.workspace):
                raise Exception("Current workspace '{}' not found".format(env.workspace))
            if arcpy.Describe(env.workspace).DataType == "Folder":
                env.scratchWorkspace = env.workspace
            else:
                env.workspace = env.scratchFolder
                env.scratchWorkspace = env.scratchFolder
        else:
            raise Exception("Current workspace not set")
    except Exception as msg:
        print("Warning: " +  msg)
        print("Warning: " + "Using {}".format(env.scratchFolder))
        env.workspace = env.scratchFolder
        env.scratchWorkspace = env.workspace
    if report:
        print("\nSet to:\n"
              "  workspace:        {}\n  scratchWorkspace: {}".format(
                  env.workspace, env.scratchWorkspace))
    return
Version history
Last update:
‎12-12-2021 03:43 AM
Updated by: