Last active
March 31, 2016 06:45
-
-
Save cindygis/3976371c048e7e15672d to your computer and use it in GitHub Desktop.
Adds a new field using a numpy array to layers in a map document.
This file contains 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
''' | |
@date 12/10/2015 | |
@author Cindy Williams | |
Adds a new field to layers in a map document, based | |
on a current field. | |
For use in the Python window in ArcMap. | |
''' | |
import arcpy | |
import numpy | |
mxd = arcpy.mapping.MapDocument("CURRENT") | |
lyrs = arcpy.mapping.ListLayers(mxd) | |
# Create a numpy array with a link oid field and the fields to be added | |
narray = numpy.array([], numpy.dtype([('objid', numpy.int), ('GIS_ID', '|S10'),])) | |
for lyr in lyrs: | |
if lyr.isFeatureLayer: | |
# Find the current field with the values | |
field = [field.name for field in arcpy.ListFields(lyr, "GIS_*")][0] | |
# Prevent the tool from failing | |
if field != "GIS_ID": | |
# Add the field | |
arcpy.da.ExtendTable(lyr,"OID@", narray, "objid") | |
# Copy the field values to the new field | |
with arcpy.da.UpdateCursor(lyr, ("GIS_ID", field)) as cursor: | |
for row in cursor: | |
row[0] = row[1] | |
cursor.updateRow(row) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment