Select to view content in your preferred language

Python UpdateLayer works in V10 but not in 10.1

1856
12
Jump to solution
10-17-2012 10:48 AM
DaveJordan1
Regular Contributor
I have a script written in v10 that compiles a series of maps and mailing labels based on a buffered distance around a parcel.  The script runs fine in V10.  In V10.1 it runs without error but does not symbolize correctly based on a layer files, specifically using UpdateLayer.  After UpdateLayer is processed, the screen refreshes but the symbols set in the layer file do not get processed. 

What has changed in 10.1 that would cause this?
Has anyone else come across this issue?

Here is an example that I copied from the python window in trying to emulate the script.  It does not apply the symbology either:

>>> SubjectLyr = arcpy.mapping.Layer("Y:\Notification Radius Pkgs\Dave3\Shapefiles\Dave3Subject.shp")
>>> mxd = arcpy.mapping.MapDocument("Current")
>>> df = arcpy.mapping.ListDataFrames(mxd, "Radius Map")[0]
>>> wPath = "Y:\Notification Radius Pkgs\\"
>>> arcpy.mapping.AddLayer(df, SubjectLyr,"AUTO_ARRANGE")
>>> sourceLayer = arcpy.mapping.Layer(wPath+"Setup10\LayerFiles\Subject.lyr")
>>> arcpy.mapping.UpdateLayer(df, SubjectLyr, sourceLayer, True)
>>>
Tags (2)
0 Kudos
12 Replies
DaveJordan1
Regular Contributor
Jeff,

I understand that I was referencing a shapefile via the layer function.  The logic that I am trying to build is to use layer files to drive the symbology for multiple mxds and shapefiles.  My script generates shapefiles based on user input, and then provides a standardized map series with consistant symbolization based on layer files. 

So, following your logic, "To fix this, add your shapefile into ArcMap, symboloize it however and then save out to a layer file (*.lyr). Then replace the path to the shapefile with the path to the lyr file"

I know how to do this manually, how would you scrpt it?
0 Kudos
JeffBarrette
Esri Regular Contributor
The code you had is was almost exactly what you needed.

mxd = arcpy.mapping.MapDocument( path to mxd )
df = arcpy.mapping.ListDataFrames(mxd)[0]                     ###UpdateLayer requires a data frame reference (returns 1st df)
lyrFile = arcpy.mapping.Layer( path to lyr file )                  ###This is a lyr file with pre-authored symbology you will apply to layer
lyr = arcpy.mapping.ListLayers(mxd, "layer name", df)[0]   ###A reference to the shapefile layer in MXD you want to update.
arcpy.mapping.UpdateLayer(df, lyr, lyrFile, True)                ###This will update ONLY the symbology


For more help on UpdateLayer, please refer to:
http://resources.arcgis.com/en/help/main/10.1/#/UpdateLayer/00s30000003p000000/

Jeff
0 Kudos
HannahP
New Contributor
Hello. I am having a similar issue with UpdateLayer in arcpy; however, the fix posted in this thread still isn't working for me 100% of the time.

I have a script that calculates kernel density for several sets of points, adds the output to a base map, updates the added layer to use the same color scheme and resampling function of a template layer, then zooms to the extent of the new layer.

This works fine for some of the resulting maps; the density output shows on the map, the color is as it should be, and it is sampled using bilinear interpolation as I wanted. The problem is that most of the resulting maps do not show the kernel output at all; only the basemap is visible.

I have isolated the code I am using and have altered it to compute one simple test case. Here is what I have:
SubjectLyr = arcpy.mapping.Layer(r"F:\\testFiles\\output\\Population23_KDens.lyr")
mxd = arcpy.mapping.MapDocument("F:\\testFiles\\testMap.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0] 
wPath = "F:\\testFiles\\output\\" # same as in line 1

#Add the layer to basemap
arcpy.mapping.AddLayer(df, SubjectLyr,"AUTO_ARRANGE") 

#Update the layer
SubjectLyr = arcpy.mapping.ListLayers(mxd, SubjectLyr.name, df)[0] # change reference to layer in mxd
sourceLayer = arcpy.mapping.Layer(wPath+"templates\\template.lyr") # get template layer
arcpy.mapping.UpdateLayer(df, SubjectLyr, sourceLayer, True)

###Zoom to selection
df.zoomToSelectedFeatures()
lyrExtent = SubjectLyr.getSelectedExtent()
df.extent = lyrExtent
arcpy.mapping.ExportToPDF(mxd, r"F:\\testFiles\\output\\Population23_KDens_Map.pdf")
print('DONE!')


As shown, my code is practically identical to the code that is a "workaround" to this problem described in the above posts, so I am not quite sure what is going wrong! This code works for some, but not all of my datasets.

I have also tried using the ApplySymbologyFromLayer_management() function instead of UpdateLayer, which does make all the kdens output layers apper; however, this does not change the resampling function used when drawing the output, and I need to to use bilinear interpolation.

Does anyone know what could be going on? Are there any other functions I could use?

Thanks!
0 Kudos