Hi Damiano,
Here's the code below that should work for you. I can see couple of things in there that need tidying up, e.g. the error trapping, and changing the hardcoded file names to parameters. This script below does what I mentioned in my twelve steps above. The only issue I had was I couldn't copy the advanced label expressions and I couldn't tick the advanced label expression box (but this can be done via ArcObjects, if you don't want to do it manually). The script basically rebuilds your MXD with the new layer aliases that were updated in the geodatabase using the previous script I sent through above. This is not quite what you are after since you can't change the aliases in the GDB, however, hopefully this is enough to put you on the right track.
If I understand what you want to do, I would probably create a feature layer with field aliases, and use the code below. If you're really stuck, what you could do is use ArcPy to recreate your feature classes in a file geodatabase but without the data, e.g. use the Make Feature Layer tool and use '1=2' for the where clause, then use the Copy Features tool to create the FCs from the feature layers. Then use the Alter Field tool to create the Aliases in the FGDB, and use the script below to update the MXD. Then once you've done that, close your MXD, rename your FGDB, open your MXD and repair the datasources to point to Oracle. This sounds complicated but is quite easy - unless you're working with complex data sets, e.g. geometric networks, topology, etc. however, since you're using native Oracle, not SDE, I doubt this would be an issue for you.
Regards,
Rhys
I have added the code here, which should have the correct indenting: Your Python Trinket
import os, time, arcpy, csv
from datetime import datetime
def main(layers,visibleFields,directory,results_folder,layer_files_folder):
arcpy.env.overwriteOutput = True
for folder, dirs, files in os.walk(directory):
for file in files:
if file.endswith('test.mxd'): # add your MXD name here
try:
path = os.path.join(folder, file)
mxd = arcpy.mapping.MapDocument(path)
data_frame = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
i = 0
for lyr in arcpy.mapping.ListLayers(mxd, "", data_frame):
# todo - add properties from group layers
if lyr.supports("DATASOURCE"):
field_info = ''
if lyr in layers:
desc = arcpy.Describe(lyr)
field_info = desc.fieldInfo
for i in range(0, field_info.count):
if field_info.getFieldName(i) not in visibleFields:
field_info.setVisible(i, "HIDDEN")
# make a feature layer
in_features=lyr.dataSource
out_layer=lyr.name
where_clause=lyr.definitionQuery if lyr.supports("definitionQuery") else ''
layer = arcpy.MakeFeatureLayer_management(in_features,out_layer,where_clause,"",field_info)
# save the feature layer to a layer file
in_layer = layer
out_layer = os.path.join(layer_files_folder, str(i) + '.lyr')
arcpy.SaveToLayerFile_management(in_layer, out_layer)
# create a new layer
reference_layer = lyr
insert_layer = arcpy.mapping.Layer(out_layer)
insert_position = "AFTER"
# set the min and max scales for the layer
insert_layer.minScale = lyr.minScale
insert_layer.maxScale = lyr.maxScale
# copy the label classes from the original layer to the new layer
if lyr.supports("LABELCLASSES"):
className = ''
expression = ''
SQLQuery = ''
for lblClass in lyr.labelClasses:
className = lblClass.className
expression = lblClass.expression
SQLQuery = lblClass.SQLQuery
if expression[:3] == 'def':
print 'Warning: layer "%s" users an Advanced Label Expression' % (lyr.name)
for lblClass in insert_layer.labelClasses:
lblClass.className = className
lblClass.expression = expression
lblClass.SQLQuery = SQLQuery
# show the labels
if lyr.supports("SHOWLABELS") and expression[:3] != 'def':
insert_layer.showLabels = lyr.showLabels
# apply the symbology of the original layer to the new layer
in_layer=insert_layer
in_symbology_layer = lyr
arcpy.ApplySymbologyFromLayer_management(in_layer, in_symbology_layer)
# add the layer to the map document
arcpy.mapping.InsertLayer(data_frame, reference_layer, insert_layer, insert_position)
# remove the original layer
arcpy.mapping.RemoveLayer(data_frame, lyr)
i += 1
mxd.saveACopy(os.path.join(directory, 'output.mxd'))
del mxd
except OSError:
print 'Error with ' + file
if __name__ == '__main__':
layers = ['','',''] # add your layer names here
visibleFields = ['','',''] # add your field names here
directory = r'C:\dev\map documents (mxds)'
results_folder = r'C:\dev\scripts\Python\output'
layer_files_folder = r'C:\dev\gisdata\layer files\temp'
main(layers,visibleFields,directory,results_folder,layer_files_folder)