In this post we are going to add X and Y coordinates fields to existing tables and feature class in a File Geodatabase programatically.
1- We start by creating a xml document with the different field's properties, we'll be adding.
2- Now let's create a python list with the two fields by parsing the xml file
#parse Xml file from physical path into memory
root = elementTree.parse(pathToXmlFile)
#get the fields
fields = root.getiterator('field') # Returns list of all fields
#iterate thru each node
for node in fields:
field = node
theFieldList = []
for i in range(len(field)):
theFieldList.insert(i, field[i].text)
listOfFields.append(theFieldList)#append field to the list
3- Then we create a list of feature classes and tables we are going to be adding the fieldsfcList = ['fds\\fc1', 'fc2', 'table1']
4- Then we add the fieldsif len(Fields) > 0:
#iterate thru fields
for field in Fields:
#get fields here
field_name = str(field[0])
field_type = str(field[1])
field_precision = str(field[2])
field_scale = str(field[3])
field_length = str(field[4])
field_alias = str(field[5])
field_is_nullable = str(field[6])
field_is_required =str(field[7])
field_domain = str(field[8])
#add field
gp.addfield(fc, field_name, field_type, field_precision,
field_scale, field_length, field_alias,
field_is_nullable, field_is_required,
field_domain)
Here is the complete code
1- We start by creating a xml document with the different field's properties, we'll be adding.
2- Now let's create a python list with the two fields by parsing the xml file
#parse Xml file from physical path into memory
root = elementTree.parse(pathToXmlFile)
#get the fields
fields = root.getiterator('field') # Returns list of all fields
#iterate thru each node
for node in fields:
field = node
theFieldList = []
for i in range(len(field)):
theFieldList.insert(i, field[i].text)
listOfFields.append(theFieldList)#append field to the list
3- Then we create a list of feature classes and tables we are going to be adding the fieldsfcList = ['fds\\fc1', 'fc2', 'table1']
4- Then we add the fieldsif len(Fields) > 0:
#iterate thru fields
for field in Fields:
#get fields here
field_name = str(field[0])
field_type = str(field[1])
field_precision = str(field[2])
field_scale = str(field[3])
field_length = str(field[4])
field_alias = str(field[5])
field_is_nullable = str(field[6])
field_is_required =str(field[7])
field_domain = str(field[8])
#add field
gp.addfield(fc, field_name, field_type, field_precision,
field_scale, field_length, field_alias,
field_is_nullable, field_is_required,
field_domain)
Here is the complete code


No comments:
Post a Comment