Last active
October 19, 2023 13:32
-
-
Save jeffgreenca/68c816ced134d8c35ca73fd0bd42bb39 to your computer and use it in GitHub Desktop.
Create ArcGIS domains and codes from a CSV file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Create ArcGIS domains and codes from a CSV file | |
# | |
# Run either with this command, replacing 10.6 with your ArcGIS version: | |
# c:\python27\ArcGIS10.6\python.exe import-codes-v2.py | |
# or, for ArcGIS Pro, use this command: | |
# "c:\Program Files\ArcGIS\Pro\bin\Python\scripts\propy.bat" import-codes-v2.py | |
# | |
# For each unique domain identified in the csv file, a domain will be greated in the gdb workspace | |
# Takes as input a CSV file with exactly these headers: domain,code,code_description | |
#Example | |
#domain,code,code_description | |
#mydom1,CI,code one | |
#mydom1,CB,code two | |
#mydom2,AF,some other code | |
# | |
# Adapted from example code provided to the public by ArcGIS. | |
import arcpy | |
import csv | |
#Configure these settings as appropriate for your environment | |
arcpy.env.workspace = "C:/pytemp/" | |
gdb = "mydomains.gdb" | |
csvfile = "c:/pytemp/codes.csv" | |
domains = [] | |
rows = [] | |
with open(csvfile, 'r') as csvfile: | |
reader = csv.DictReader(csvfile) | |
for row in reader: | |
domains.append(row['domain']) | |
rows.append(row) | |
domains = list(set(domains)) | |
print("Identified %s unique domains" % len(domains)) | |
print("Identified %s codes" % len(rows)) | |
#If you do not wish to create domains, because for example they are already pre-created, comment out this for loop. | |
print("Creating domains:") | |
for domain in domains: | |
print(domain) | |
arcpy.CreateDomain_management(gdb, domain, "", "TEXT", "CODED") | |
print("Creating codes:") | |
for row in rows: | |
print("%s\t%s\t%s" % (row['domain'], row['code'], row['code_description']) | |
arcpy.AddCodedValueToDomain_management(gdb, row['domain'], row['code'], row['code_description']) | |
print("Done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment