Python Experience

Arcpy package

Script tool to add and populate fields
Tool dialog box to add and populate fields

# **** THIS TOOL ADDS FIELDS TO A FEATURE CLASS SO THAT ITS DATA CAN
# **** BE LOADED INTO AN SDE FEATURE CLASS
# **** THE TYPE, NAME AND LENGTH OF THE FIELDS IN THE TARGET FEATURE
# **** CLASS MUST CONFORM TO THOSE OF THE MATCHING FIELDS IN THE SDE FEATURE CLASS
import arcpy
import datetime
import arcgisscripting
import os
import sys
import types
import locale
gp = arcgisscripting.create(10.4) # **** 10.4 is for ArcMap 10.4 ****

# **** Get the inpurt argument Values ****
# **** Input FC ****

sourceFC = gp.GetParameterAsText(0)
# **** sourceFC = r"G:\ (directory folder)\(directory subfolder)\Backup.gdb\muni_provided_utilities_points_copy" ****
# **** Output FC ****

targetFC = gp.GetParameterAsText(1) #It's a FC in this script
# **** If no field name is specified, use the name "SchemaCheck" by default ****
fieldsFC01 = arcpy.ListFields(sourceFC)
fieldsFC02 = arcpy.ListFields(targetFC)
newFields = []

# **** Loop through the source file to copy each of its fields to add them to a list.
# **** fieldsFC01 is populated with the fields

for field01 in fieldsFC01:
exists = False
# **** Temporarily assign the field name current to this iteration to fieldname01_copy ****
fieldname01_copy = field01.name
# **** Loop through the target file to try to find a field with the same ****
for field02 in fieldsFC02:
if (field02.name == fieldname01_copy):
field02.name += "2"
if (fieldname01_copy == field02.name and field01.type == field02.type and field01.length == field02.length):
exists = True

# **** Add the field to the target file if it is not already in there ****
# **** OBJECTID, etc... do not need to be added
****

if (exists == False):
if (field01.name != 'OBJECTID' and field01.name != 'SHAPE' and field01.name != 'CHECKED'):
newFields.append([fieldname01_copy, field01.type, field01.length])
if (field01.name == 'STATE'):
newFields.append([field01.name, field01.type, field01.length])
exists = True

# **** print field01.name, field02.name Diagnostic print ****
# **** Arcpy populates the fields ****

for field in newFields:
fieldnames = field[0]
arcpy.AddField_management(targetFC, fieldnames, field[1], "", "", field[2], "", "NULLABLE","NON_REQUIRED","")
arcpy.AddField_management(targetFC, "STATE", "TEXT", field_length = 2)
field_MUNI = arcpy.GetParameterAsText(2)
field_uploader = arcpy.GetParameterAsText(3)
field_date = datetime.date.today()
field_method = "loaded"
field_utility_ty = arcpy.GetParameterAsText(4)
field_feature_ty = arcpy.GetParameterAsText(5)
field_state = arcpy.GetParameterAsText(6)
fields = ['MUNICIPALITY', 'DATE_PROVIDED', 'UPLOADER', 'METHOD', 'UTILITY_TYPE', 'FEATURE_TYPE', 'STATE']
# **** The user enters the values into the dialog box (shown above) ****
with arcpy.da.UpdateCursor(targetFC, fields) as cursor:
for row in cursor:
row[0] = field_MUNI #from the user
row[1] = field_date
row[2] = field_uploader
row[3] = field_method
row[4] = field_utility_ty
row[5] = field_feature_ty
row[6] = field_state
cursor.updateRow(row)

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

Scripts used in Field Calculator

# **** This script strips the city and state away from the zip code and leaves the zip code in the target field ***
# **** Enter this into the field above the program block: get_zip(!MAILADDR2!) ****
# **** where MAILADDR2 is the field that contains the city, state and zip ****
*** Then enter this in the code block ***

import re
def get_zip(address):
zip_pattern = r'\b\d{5}(?:-\d{4})?\b'
match = re.search(zip_pattern, address)
if match:
return match.group(0)
else:
return None
Removing everything before zipcode

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

# ****This script deletes the comma from text ****

before_first_comma(!CityState!)
def before_first_comma(text):
if text is None:
return None
idx = text.find(",")
if idx == -1:
return text.strip()
return text[:idx].strip()

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

# ****This script extracts the city name by removing all text to the right of the last character in the first word ****

!FieldName!.split(" ", 1)[0]
Town name only





Website Design by Doug Wolfinger, using Notepad++ originally, now Visual Studio. © 2025 Doug Wolfinger