Skip to content

Instantly share code, notes, and snippets.

@morehavoc
Created December 24, 2014 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morehavoc/a3ab2bd98fe6848ac15e to your computer and use it in GitHub Desktop.
Save morehavoc/a3ab2bd98fe6848ac15e to your computer and use it in GitHub Desktop.
That Pesky CreateFeatureClass tool strikes again, this code will copy fields from one feature class to another so you don't need to use a template!
def _add_field(input_table, field):
import arcpy
if getattr(field, 'isNullable', True):
nullable = 'NULLABLE'
else:
nullable = 'NON_NULLABLE'
# there is a bug in 10.2.1 and lower (sum is 13) that prevents us from adding non_nullable fields, so we have
# to protect against that bug.
if nullable == 'NON_NULLABLE' and sum([int(i) for i in GetArcpyVersion().split('.') ]) <= 13:
nullable = 'NULLABLE'
arcpy.AddField_management(input_table,
field.name,
field.type,
field.precision,
field.scale,
field.length,
field.aliasName,
nullable,
'NON_REQUIRED',
field.domain)
def _remove_special_fields(field_list, layer_describe):
if getattr(layer_describe, 'hasOID', False) and layer_describe.OIDFieldName in field_list:
field_list.remove(layer_describe.OIDFieldName)
if getattr(layer_describe, 'hasGlobalID',
False) and layer_describe.globalIDFieldName in field_list:
field_list.remove(layer_describe.globalIDFieldName)
if hasattr(layer_describe, 'areaFieldName') and layer_describe.areaFieldName in field_list:
field_list.remove(layer_describe.areaFieldName)
if hasattr(layer_describe, 'lengthFieldName') and layer_describe.lengthFieldName in field_list:
field_list.remove(layer_describe.lengthFieldName)
if hasattr(layer_describe, 'shapeFieldName') and layer_describe.shapeFieldName in field_list:
field_list.remove(layer_describe.shapeFieldName)
return field_list
def sync_feature_class_columns(source_layer, destination_layer):
import arcpy
import eaglepy
source_layer_describe = arcpy.Describe(source_layer)
destination_layer_describe = arcpy.Describe(destination_layer)
source_field_names = [f.name for f in source_layer_describe.fields]
destination_field_names = [f.name for f in destination_layer_describe.fields]
fields_to_sync = [f for f in source_field_names if f not in destination_field_names]
## Remove special columns
fields_to_sync = _remove_special_fields(fields_to_sync, source_layer_describe)
fields_to_sync = _remove_special_fields(fields_to_sync, destination_layer_describe)
##Loop over each column and add it to the destination_layer, setting the proper type.
for field in source_layer_describe.fields:
if field.name not in fields_to_sync:
continue
_add_field(destination_layer, field)
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment