PYTHON - Arcpy package

Innovation Experience With Python

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)
#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 on right)
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)






Website Design by Doug Wolfinger. © 2020 Doug Wolfinger