Last active
August 6, 2021 14:39
-
-
Save AlexArcPy/ccebf25468a01faf53fa8d00b06fa250 to your computer and use it in GitHub Desktop.
Update field aliases of map layers within an ArcGIS map document file and geodatabase feature class fields
This file contains hidden or 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
| ## -*- coding: UTF-8 -*- | |
| import os | |
| from collections import defaultdict | |
| from comtypes.client import GetModule, CreateObject | |
| from snippets102 import GetStandaloneModules, InitStandalone | |
| GetStandaloneModules() | |
| InitStandalone() | |
| esriCarto = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.4\com\esriCarto.olb") | |
| esriGeodatabase = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.4\com\esriGeoDatabase.olb") | |
| esriDataSourcesGDB = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.4\com\esriDataSourcesGDB.olb") | |
| file_gdb_pointer = CreateObject(progid=esriDataSourcesGDB.FileGDBWorkspaceFactory, | |
| interface=esriGeodatabase.IWorkspaceFactory) | |
| #0 is The hWnd argument is the parent window or application's window. The hWnd will | |
| #guarantee that the connection dialog, if presented to you because of insufficient | |
| #properties, has the correct parent. | |
| file_gdb = file_gdb_pointer.OpenFromFile(r'C:\GIS\Temp\ArcGISHomeFolder\Default.gdb',hWnd=0) | |
| #access contents inside gdb | |
| feature_workspace = file_gdb.QueryInterface(esriGeodatabase.IFeatureWorkspace) | |
| #open feature class inside gdb | |
| fc = feature_workspace.OpenFeatureClass('Buffer1km') | |
| #edit field alias | |
| schema = fc.QueryInterface(interface=esriGeodatabase.IClassSchemaEdit3) | |
| for k, v in {'Shape': u'Shape representation', | |
| 'Shape_Length': u'Feature perimeter', | |
| 'Shape_Area': u'Feature area'}.iteritems(): | |
| schema.AlterFieldAliasName(k, v) |
This file contains hidden or 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 os | |
| from collections import defaultdict | |
| from comtypes.client import GetModule, CreateObject | |
| from snippets102 import GetStandaloneModules, InitStandalone | |
| GetStandaloneModules() | |
| InitStandalone() | |
| esriCarto = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.4\com\esriCarto.olb") | |
| #---------------------------------------------------------------------- | |
| def update_mxd_layers_field_aliases(mxd_path,output_mxd_path=None): | |
| """update field aliases for layers in a map document based on the field aliases stored in geodatabase""" | |
| pMapDocument = CreateObject(esriCarto.MapDocument, interface=esriCarto.IMapDocument) | |
| pMapDocument.Open(mxd_path) | |
| pMap = pMapDocument.Map(0) | |
| pEnumLayer = pMap.Layers(None,True) | |
| pLayer = pEnumLayer.Next() | |
| source_aliases = defaultdict(list) | |
| edited_aliases = defaultdict(list) | |
| while pLayer: | |
| print "Layer: ", pLayer.Name | |
| fields = pLayer.QueryInterface(esriCarto.ILayerFields) | |
| for i in xrange(fields.FieldCount): | |
| print "Name:", fields.Field(i).name, "Alias:", fields.FieldInfo(i).alias | |
| source_aliases[pLayer.Name].append((fields.Field(i).Name,fields.FieldInfo(i).alias)) | |
| fields.FieldInfo(i).alias = fields.Field(i).AliasName | |
| edited_aliases[pLayer.Name].append((fields.Field(i).Name,fields.Field(i).AliasName)) | |
| pLayer = pEnumLayer.Next() | |
| if not output_mxd_path: | |
| output_mxd_path = os.path.join(os.path.dirname(mxd_path),os.path.splitext(os.path.basename(mxd_path))[0] + '_upd' + | |
| os.path.splitext(os.path.basename(mxd_path))[1]) | |
| pMapDocument.SaveAs(output_mxd_path) | |
| return (source_aliases,edited_aliases) | |
| mxd_path = r"C:\GIS\Temp\alias_mxd.mxd" | |
| print update_mxd_layers_field_aliases(mxd_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment