Problem with UpdateLayer

584
2
05-30-2014 09:13 PM
RobertDicker
New Contributor
I'm trying to update a polygon layer from some default symbology held in a group layer in an mxd file. The polygon shape file is created from a raster immediately before I try to apply the default symbology. However, the program does not recognise the new shape file as a layer and therefore fails.

My understanding is that I do not have to explicitly save the shape file as a layer before trying to update it with new symbology. Therefore after the shape file is created it should be the first layer in the mxd. In the following script the source layer is the fifth layer inside the the group layer of symbology

# set environment setting to overwrite output
arcpy.env.overwriteOutput = True
# add outputs to map
arcpy.env.addOutputsToMap = True
# set workspace
arcpy.env.workspace = r'C:\Temp\myfile'

mxd = arcpy.mapping.MapDocument(r'C:\Temp\myfile\mymxd.mxd')
df = arcpy.mapping.ListDataFrames(mxd)[0]
       

inRaster = 'hSr_' + str(azimuth) + '_' + str(altitude)
outPolygons = str(azimuth) + '_' + str(altitude) + '.shp'
field = 'VALUE'
# Execute RasterToPolygon
arcpy.RasterToPolygon_conversion(inRaster, outPolygons, 'SIMPLIFY', field)

# this works fine up to here but fails at UpdateLayer

updateLayer = arcpy.mapping.ListLayers(mxd)[0]
sourceLayer = arcpy.mapping.ListLayers(mxd)[7]
arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True)
mxd.save()

This generates the following error:

LayerObject: Unexpected error

Any help with this would be appreciated.
0 Kudos
2 Replies
JimCousins
MVP Regular Contributor
I can not see your layers, but perhaps you are miscounting the layers? The group counts as a layer item in the table of contents, SO if you had your first layer and then a group layer, and within the group layer you 3 layers, Roads, Streams, Houses, a pointer to the Houses layer would be [4]. You can verify the layer pointer with a simple print or arcpy.AddMessage statment like print arcpy.mapping.ListLayers(mxd)[7], and see if it returns what you expect.
Regards,
Jim
0 Kudos
RobertDicker
New Contributor
Thanks Jim, the problem seems to me that even though I have set the addOutputsToMap = True, the shape file is not being added as a layer to the map document. Interestingly, when I tried using this script inside an Add In it appears to work but when deployed from a python IDE it doesn't?
0 Kudos