Select to view content in your preferred language

Python Script to Bulk Transfer Domains Between Geodatabases in ArcGIS

365
2
02-01-2025 04:20 AM
JEMOO-01
Occasional Contributor

Tired of transferring domains one by one using Domain to Table and Table to Domain? I created a Python script to automate the process! This script transfers all domains (or specific ones) between geodatabases in one go, saving time and effort. Perfect for file geodatabases, enterprise geodatabases, and ArcSDE connections.

How it works:

  1. Provide the source and target geodatabase paths.

  2. Run the script.

  3. All domains are transferred automatically!

2 Replies
MErikReedAugusta
MVP Regular Contributor

This looks great!  I did something vaguely similar for our team that created batch procedures to convert Domains to/from Excel files, because it's much faster to reorder and change individual domain entries in Excel than it is using the built-in Domain editor.

 

A few points on your script that I noticed while reading over it:

  1. I notice you essentially reinvented arcpy.ValidateTableName with your sanitize_table_name function.  Was that intentional, or just something you hadn't yet encountered in the endless libraries of arcpy?
  2. Those GDB inputs at lines 14 & 15 would be prime candidates for tool inputs, if you wanted to make this a more user-friendly geoprocessing tool.
  3. Line 26 is amusing to me, in that I'd often wondered if python's f-strings and raw strings could be combined like that, but I'd never actually taken the 2 seconds to test it.  Nice to finally know!
  4. Line 50: There are arguably use-cases here for whether the user might want to append or replace domains in this procedure.  I see 2–4 approaches, with increasing complexity:
    1. An input variable similar to the GDBs on Lines 14 & 15 that sets either "APPEND" or "REPLACE" at a global level.
    2. An input() command which asks the user to specify which to use for each domain as it comes up.  (N.B. I'm fairly certain this command doesn't work in the context of geoprocessing tools, if you take the advice of Point #2)
    3. If you go with Point #2, you could make use of the Validation section of that geoprocessing tool to pre-populate a table of inputs into the tool showing each domain found in GDB1, with the second column having a text input for "APPEND" or "REPLACE" for each entry.  I'd suggest having each domain set to "APPEND" by default, as a starting point.
    4. This combines Approaches 2 & 3 with a global input variable that sets the default value in the table-of-domain-names in Approach 3, which can then be toggled individually as per Approach 3.
  5. Line 53, Warning for ArcMap/Python 2: This will break for ArcMap users, because the  character is outside the bounds of basic ascii, and Python 2 didn't use Unicode strings by default, the way Python 3 does.
  6. You can also paste this script into the code block tool on the forums, rather than as a .txt attachment.  It's the </> button in the format buttons.
------------------------------
M Reed
"The pessimist may be right oftener than the optimist, but the optimist has more fun, and neither can stop the march of events anyhow." — Lazarus Long, in Time Enough for Love, by Robert A. Heinlein
JEMOO-01
Occasional Contributor

Thank you for the detailed feedback and suggestions! I really appreciate your insights. Here’s my response to your points:

  1. arcpy.ValidateTableName: You're absolutely right that arcpy.ValidateTableName exists and could be used here. I wrote the sanitize_table_name function as a learning exercise and to have more control over the sanitization process (e.g., ensuring the name starts with a letter and truncating to 30 characters). However, using arcpy.ValidateTableName would indeed make the code more concise and aligned with ArcPy's built-in functionality. I'll consider refactoring it in the next version!

  2. GDB Inputs as Tool Inputs: Great suggestion! Converting the GDB paths into tool inputs would make the script more user-friendly, especially for colleagues who might not be comfortable editing the script directly. I'll explore wrapping this script into a custom ArcGIS geoprocessing tool with input parameters for the source and target geodatabases.

  3. Combining f-strings and Raw Strings: Glad you found the f-string and raw string combination amusing! It’s one of those Python features that feels like a hidden gem once you realize how well they work together. I use it often for file paths to avoid escaping backslashes while still leveraging the power of f-strings.

  4. Append vs. Replace Domains: Thanks for the detailed suggestions on handling 'APPEND' vs. 'REPLACE'! I agree that this would add valuable flexibility. Here’s how I’m thinking of implementing it:

    • Add a global input variable to let users choose between 'APPEND' and 'REPLACE' for all domains.

    • For more granular control, I could create a table or dictionary to specify the behavior for each domain individually.

    • If I turn this into a geoprocessing tool, I’ll explore using the Validation section to pre-populate a table of domains with default options, as you suggested.
      I’ll start with the global input variable and work my way up to the more advanced options.

  5. Unicode Character Compatibility: That’s a great catch! I didn’t consider ArcMap/Python 2 compatibility when using the emoji. To ensure broader compatibility, I’ll replace it with a basic ASCII character like a checkmark (√) or simply use 'DONE' or 'SUCCESS'. Thanks for pointing that out!

  6. Code Formatting on Forums: Thanks for the tip about using the </> button to format the script on forums! I’ll make sure to do that in future posts to improve readability.

-------------------------

Thank you again for your insightful feedback and for sharing that wonderful quote by Robert A. Heinlein. It’s a great reminder that while we can’t control every outcome, maintaining an optimistic outlook makes the journey more enjoyable—and often more productive! I’ll definitely keep that in mind as I continue refining this script and exploring new ways to improve it.

Looking forward to more discussions like this in the future!

Best regards,
JIMMY