Bug with arcpy.mapping.UpdateLayer() method ??

668
4
Jump to solution
07-22-2013 11:36 AM
ThomasMcCracken
New Contributor III
Greetings and Salutations Python Masters of the Universe,

I believe I have a minor bug here with the arcpy.mapping.UpdateLayer() method.  If anyone can help me correct it or work around it I would greatly appreciate it.

I'm not going to post the code because it is a simple script.  Basically I use the UpdateLayer method to update a whole bunch of layers, about 150, and the 'update symbology only' is set to False.  I'm updating everything; each layer comes from a different feature class for different counties so the database schema is the same for each.  I use the UpdateLayer to hide fields on all the layers.

So, after updating, the name for each layer is now based on the original layer that I used as the Source Layer.  Well, this is where the fun part comes in, whenever I go to change any of the names they all change based on whatever the one has been changed to. If I go to one of the layer's properties and change the name, it then changes the name of every single layer.  I've copied all the layers into another data frame to copy so I can loop through and set the names to be equal but it won't work because of this bug.

Again, the layer name changes for every layer when only one has been changed after using the UpdateLayer method on the layers.

Thanks,

GIS Python Padewan
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JeffBarrette
Esri Regular Contributor
I could reproduce your naming one / name all problem.  I fixed it by placing the reference to the layer file within my loop and deleting the variable before the next iteration.

import arcpy, os mxd = arcpy.mapping.MapDocument(r"C:\Temp\Test.mxd") df = arcpy.mapping.ListDataFrames(mxd)[0] for lyr in arcpy.mapping.ListLayers(mxd):   lyrFile = arcpy.mapping.Layer(r"C:\Temp\Test.lyr")    layerName = lyr.name   arcpy.mapping.UpdateLayer(df, lyr, lyrFile, False)   lyr.name = layerName   del lyrFile mxd.saveACopy(r"C:\Temp\Test2.mxd") del mxd os.startfile(r"C:\Temp\Test2.mxd")


I'd really like to understand the utility of updating 100+ layers so they are all identical except for their name.  When we designed UpdateLayer we were thinking of scenarios where a single layer either in one MXD or across many MXDs would be updated.  I'd like to learn more about your scenario.

Thanks,
Jeff

View solution in original post

0 Kudos
4 Replies
RhettZufelt
MVP Frequent Contributor
From what I can tell, setting  'update symbology only' is set to False' tells it to update everything in the layer, including name.  In fact, if you point to a different FC, it would replace it even.  As the help doc says, basically, it is doing a remove layer/add layer with the new properties as defined in the layer file.

I think, if you want to use the updatelayer for this, you might either have to re-create the layer file with python before applying it so that the name is representative of your data, OR, keep the original layer name in a variable, updateLayer, then rename the layer back to what it was before moving on to the next one.

R_
0 Kudos
ThomasMcCracken
New Contributor III
You're misunderstanding my bug...I know the UpdateLayer will change the name; that's not the problem.  The problem is the name will change for every layer after the method has been used whenever I change one of the layer's names.

So I did save the name in a variable and reapplied like you said, what happens is every layer gets the name of the last layer named whatever that name may be.

If I go manually into one of the layers, any of them, and change the name then every single layer that has been updated with the UpdateLayer method also changes its name.
0 Kudos
JeffBarrette
Esri Regular Contributor
I could reproduce your naming one / name all problem.  I fixed it by placing the reference to the layer file within my loop and deleting the variable before the next iteration.

import arcpy, os mxd = arcpy.mapping.MapDocument(r"C:\Temp\Test.mxd") df = arcpy.mapping.ListDataFrames(mxd)[0] for lyr in arcpy.mapping.ListLayers(mxd):   lyrFile = arcpy.mapping.Layer(r"C:\Temp\Test.lyr")    layerName = lyr.name   arcpy.mapping.UpdateLayer(df, lyr, lyrFile, False)   lyr.name = layerName   del lyrFile mxd.saveACopy(r"C:\Temp\Test2.mxd") del mxd os.startfile(r"C:\Temp\Test2.mxd")


I'd really like to understand the utility of updating 100+ layers so they are all identical except for their name.  When we designed UpdateLayer we were thinking of scenarios where a single layer either in one MXD or across many MXDs would be updated.  I'd like to learn more about your scenario.

Thanks,
Jeff
0 Kudos
ThomasMcCracken
New Contributor III
Woo ha it worked!  Thank you very much; I tried doing the same thing in my loop with the name but I always left the source layer variable outside the loop so it never deleted.  It's strange though because I did close the mxd and re-open so that the variable was lost and I still got the crazy name change bug.

Anyway, here is the scenario I'm working with.  I have a file geodatabase with parcel data divided into 150 features classes-one for each county.  These all have the same database schema; I guess they divided it by county for size or ease of distribution, who knows.  So these feature classes have around 100 records or so that our users don't need to see.  Some of it is the sales data and other information that is provided by local governments.  I needed to remove all the fields except for about 20 and this is why I used the UpdateLayer function in Python so I wouldn't have to do it 150 times.  Now that I have the layers updated properly I'm going to share it as a service with ArcGIS Server.  Maybe there's a more appropriate method in the ArcPy module but I couldn't find it.

If you have anymore questions feel free to shoot them at me.

Thanks again!
0 Kudos