Skip to content

arcsmith.ws

arcsmith.ws

Workspace

Helpers for setting up geodatabase workspaces inside 'Tool.execute'.

  • Create a file geodatabase with automatic parent folder creation
  • Get a workspace path for intermediate outputs, switching between memory and a scratch .gdb

Functions of the ws module

  • init_gdb - create a file geodatabase, reusing or overwriting an existing one
  • temp_space - get a workspace path for intermediate outputs (in-memory or scratch GDB)

init_gdb

Creates a file geodatabase at the specified location, creating the parent folder if it does not already exist. If the geodatabase already exists, it is reused by default, or recreated when overwrite=True.

init_gdb(folder, gdb_name, overwrite=False) -> str
Parameter Type Default Description
folder str or Path required Folder in which to create the geodatabase.
gdb_name str required Name of the geodatabase. A trailing .gdb extension, if supplied, is stripped, so "glacier" and "glacier.gdb" behave identically.
overwrite bool False What to do when the target .gdb already exists. False (default) reuses the existing geodatabase and returns its path (idempotent). True deletes and recreates it empty.

Returns

str: absolute path to the geodatabase. This is the newly created .gdb unless an existing one was reused (overwrite=False).

Existing geodatabases

arcpy.env.overwriteOutput does not apply to geodatabase creation, so CreateFileGDB raises if the .gdb already exists. init_gdb handles this automatically: by default it reuses the existing workspace, and overwrite=True forces a fresh, empty one. Reuse hands back whatever schema is already on disk, so pass overwrite=True if a clean workspace matters.

Examples

# Create a geodatabase
gdb = arcsmith.ws.init_gdb(r"C:/Projects/Glacier", "glacier")
# C:/Projects/Glacier/glacier.gdb

# Create and set as the arcpy workspace in one line
arcpy.env.workspace = arcsmith.ws.init_gdb(folder, gdb_name)

# Force a fresh, empty geodatabase even if one already exists
gdb = arcsmith.ws.init_gdb(folder, "glacier", overwrite=True)

temp_space

Returns a workspace path for intermediate outputs, either the in-memory workspace or the session scratch geodatabase.

temp_space(use_memory=True) -> str
Parameter Type Default Description
use_memory bool True If True, returns 'memory'. If False, returns arcpy.env.scratchGDB for on-disk inspection.

Returns

str: 'memory' or the absolute path to the scratch geodatabase.

Development workflow

Set use_memory=False while building a tool to inspect intermediate outputs in ArcGIS Pro. Switch back to True (the default) for production runs; no other code changes needed.

memory limitations

The 'memory' workspace does not support everything an on-disk geodatabase does. Certain field types, attribute indexes, and a handful of tools cannot write to it. If a tool fails against 'memory', use use_memory=False for compatibility, not only for inspection.

Examples

# Production: intermediates go to memory (default)
ws = arcsmith.ws.temp_space()
tmp = f"{ws}/trails_temp"
# tmp = "memory/trails_temp"

# Development: intermediates written to scratch GDB for inspection
ws = arcsmith.ws.temp_space(use_memory=False)
tmp = f"{ws}/trails_temp"
# tmp = "C:/Users/.../scratch.gdb/trails_temp"