Created
March 1, 2018 16:13
-
-
Save mu88/c4f87f87e809acfd63c7721474bd4111 to your computer and use it in GitHub Desktop.
ArcPy - Find duplicate Field Aliases within MXD
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
import arcpy, os | |
os.system('cls') | |
mxd = arcpy.mapping.MapDocument(r"\\Server\Share\MyMap.mxd") | |
tolerateFieldWhenDisabled = True | |
for layer in arcpy.mapping.ListLayers(mxd): | |
print "Checking Layer '" + layer.name + "'" | |
if not layer.isFeatureLayer: | |
print "Layer '" + layer.name + "' is no feature layer" | |
continue | |
layerDescription = arcpy.Describe(layer) | |
fieldInfo = layerDescription.fieldInfo | |
distinctAliasNames = [] | |
# unfortunately, this script does not work | |
# when using ListFields(), the initially used SDE connection has to be present to | |
# retrieve the feature class. | |
# when using layerDescription, no aliases will be returned | |
# | |
# see here for more http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/layer-class.htm | |
# => field alias of MXD is not supported, so the SDE file HAS TO EXIST | |
fields = arcpy.ListFields(layer.dataSource) | |
##fields = layerDescription.fields | |
for field in fields: | |
print field | |
aliasName = field.aliasName | |
if aliasName not in distinctAliasNames: | |
distinctAliasNames.append(aliasName) | |
else: | |
if tolerateFieldWhenDisabled == True: | |
fieldIndex = fieldInfo.findFieldByName(aliasName) | |
visibility = fieldInfo.getVisible(fieldIndex) | |
if visibility == "VISIBLE": | |
raise Exception("Layer '" + layer.name + "' contains duplicate field alias '" + aliasName + "'") | |
else: | |
raise Exception("Layer '" + layer.name + "' contains duplicate field alias '" + aliasName + "'") | |
print "Completed" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment