Problems with UpdateLayer - exporting field aliases to shapefile

2564
2
Jump to solution
02-22-2013 04:55 AM
StefanHaglund1
Esri Contributor
Hello All,

I am running into problems with UpdateLayer that I can???t figure out.

My goal is to write a script that exports a shapefile from a FGDB and adds the field-aliases existing in the FGDB-FC to the shapefile. I have run through the steps in the interactive Python-window but now I am trying to make it into a tool. I only get as far as using arcpy.mapping.UpdateLayer before it crashes.

Here is my code:
# -*- coding: cp1252 -*- import arcview import arcpy, os arcpy.env.overwriteOutput = True  #Setting up... arcpy.env.transferDomains = True arcpy.env.workspace = r"C:\GIS\ArcGIS\aliastest"  #Getting parameters inFC = arcpy.GetParameterAsText(0) outshp = arcpy.GetParameterAsText(1) wp = arcpy.GetParameterAsText(2)  #Converting arcpy.FeatureClassToFeatureClass_conversion(inFC, wp, outshp)  #Saving original FC to layer arcpy.MakeFeatureLayer_management(inFC, "inFCLyr") arcpy.SaveToLayerFile_management("inFCLyr", "H_Orig04")  #Saving converted shape to layer arcpy.MakeFeatureLayer_management(os.path.join(wp, outshp), "outShpvLyr") arcpy.SaveToLayerFile_management("outShpvLyr", "H_shp04.lyr")  #Preparing to update and replace srcLayer = arcpy.mapping.Layer(r"inFCLyr") upLayer = arcpy.mapping.Layer(r"outShpvLyr") mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "*")[0]  arcpy.mapping.AddLayer(df, srcLayer, "BOTTOM") arcpy.RefreshTOC() arcpy.RefreshActiveView()  arcpy.mapping.UpdateLayer(df, upLayer, srcLayer, False)


It gets as far as the last line where it ends with:
 File "c:\program files\arcgis\desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_     return fn(*args, **kw)   File "c:\program files\arcgis\desktop10.1\arcpy\arcpy\mapping.py", line 1876, in UpdateLayer     tc.ReplaceLayer(tl, rl) ValueError: DataFrameObject: Unexpected error


Don't know what causes this! From what I can tell I am not doing anything arcpy.mapping wasn't designed for. I tested to make sure the parameters going into UpdateLayer where of the correct type, so it shouldn't be that.

For the last part of of the script, I will use replaceDatasoure to transfer aliases from the original FC to the exported shapefile. Once I get pass this...

I'd be really happy for any help on this.
Thanks!
0 Kudos
1 Solution

Accepted Solutions
JeffBarrette
Esri Regular Contributor
Is it possible that you have upLayer and scrLayer reversed?  I see you are adding the source layer to the MXD, not the update layer.

The layer you want to update needs to be in the mxd, the source layer does not.

Jeff

View solution in original post

0 Kudos
2 Replies
JeffBarrette
Esri Regular Contributor
Is it possible that you have upLayer and scrLayer reversed?  I see you are adding the source layer to the MXD, not the update layer.

The layer you want to update needs to be in the mxd, the source layer does not.

Jeff
0 Kudos
StefanHaglund1
Esri Contributor
Thanks Jeffrey, you sent me in the right direction.

I ended up specifying the updatelayer better throughout the script. Maybe overworked it a bit but it's working.

Here's my final script if someone runs into the same problem in the future. What it does is export a FC with domains and field aliasnames to a shapefile where domain descriptions are added through "arcpy.env.transferDomains = True" and also creating a .lyr-file where aliasnames are copied from the original FC.

# -*- coding: cp1252 -*-
import arcview
import arcpy, os

#Getting parameters
inFC = arcpy.GetParameterAsText(0)
wp = arcpy.GetParameterAsText(1)
outshp = arcpy.GetParameterAsText(2)

#Setting up...
arcpy.env.overwriteOutput = True
arcpy.env.transferDomains = True
arcpy.env.workspace = wp
outshpN = outshp + ".shp"
outlyr = outshp
outlyrN = outlyr + ".lyr"

#Converting
arcpy.FeatureClassToFeatureClass_conversion(inFC, wp, outshpN)

#Saving original FC to layer
arcpy.MakeFeatureLayer_management(inFC, "inFCLyr")
arcpy.SaveToLayerFile_management("inFCLyr", "origlyr")

#Saving converted shape to layer
arcpy.MakeFeatureLayer_management(os.path.join(wp, outshpN), "outShpvLyr")
arcpy.SaveToLayerFile_management("outShpvLyr", "shplyr.lyr")

#Preparing to update and replace
srcLayer = arcpy.mapping.Layer(r"inFCLyr")
upLayer = arcpy.mapping.Layer(r"outShpvLyr")
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
arcpy.mapping.AddLayer(df, upLayer, "BOTTOM")
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
upLayerDF = arcpy.mapping.ListLayers(mxd, "outShpvLyr", df)[0]

#Updating layer
arcpy.mapping.UpdateLayer(df, upLayerDF, srcLayer, False)

#Replacing datasource
lyr = arcpy.mapping.ListLayers(mxd, "inFCLyr", df) [0]
lyr.replaceDataSource(wp, "SHAPEFILE_WORKSPACE", outshp)
lyr.name = outlyr
lyr.description = " "

#Saving layer-file
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
arcpy.SaveToLayerFile_management("inFCLyr", outlyrN, "RELATIVE")

#Cleaning up
arcpy.Delete_management(os.path.join(wp, "origlyr.lyr"))
arcpy.Delete_management(os.path.join(wp, "shplyr.lyr"))
0 Kudos