3

I'm very new to ArcPy and programming in general. I'm trying to use UpdateLayer to add hatching to a selected layer from a template layer I've created.

The script runs (no errors), but it doesn't update my selected layer:

import arcpy

mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, 'Stationing')[0]
sourceLayer = arcpy.mapping.Layer(r'H:\Python\Data\Template.lyr')
updateLayer = arcpy.mapping.ListLayers(mxd, 'IAmRoute.lyr', df)[0]
arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, False)

mxd.saveACopy(r'H:\Python\Stationing2.mxd')
del mxd, sourceLayer

Am using ArcGIS Desktop 10.5.1.


When I save a copy of the mxd, I end up with two layers named Template. One appears to be the original Template layer, the second appears to be my original IAmRoute layer with its original properties.

Running from Arcmap w/mxd Stationing open.

edit: changed MapDocument to 'CURRENT'

  • Does the document contain a dataframe called 'Stationing' and is there a layer called 'IAmRoute.lyr' (perhaps it's called 'IAmRoute') in that dataframe? Try branching with a few conditions (if df != None etc..) with an AddError on the else statement, to be sure your objects are being retrieved correctly. If your document is open you should be accessing it using MapDocument('CURRENT') not by its file path. – Michael Stimson Apr 2 '18 at 22:27
  • In answer to your first two questions: Yes, the doc has a dataframe called 'Stationing,' and the layer I want to update is called 'IAmRoute.lyr' (that's also how it appears in the df). – SarahKelley Apr 2 '18 at 22:33
  • So, are the objects being retrieved correctly in the script? Have you tried using MapDocument('CURRENT')? You could be encountering a lock situation by accessing an open document by its file path. – Michael Stimson Apr 2 '18 at 22:39
  • (I took too long to edit the above comment and it locked - here's my response to your first comment) The doc has a dataframe called 'Stationing,' and the layer I want to update is called 'IAmRoute.lyr' (that's also how it appears in the df). Have now changed MapDocument to 'CURRENT' as recommended. When I run the script now, it seems to just change the name of IAmRoute to Template, without copying over the properties. Will try your other suggestion as well. – SarahKelley Apr 2 '18 at 22:44
1

This process will copy the hatching from one layer to another...well, technically, you end up with two copies of the layer, but you can add a line to delete the old one, if necessary.

import os
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, 'Stationing')[0]
sourceLayer = arcpy.mapping.Layer(r'H:\Python\Data\Template.lyr')
updateLayer = arcpy.mapping.ListLayers(mxd, 'IAmRoute.lyr', df)[0]

# Copy the source layer and make a layer object from the layer file
lyrCopy = r'H:\Python\Data\Copy.lyr'
sourceLayer.saveACopy(lyrCopy)
newLayer = arcpy.mapping.Layer(lyrCopy)

# Change the data to which the layer points; you may need to change some
#   arguments, depending on your storage. os.path.dirname is duplicated, because
#   you need the geodatabase path, not the feature dataset path.
newLayer.replaceDataSource(os.path.dirname(os.path.dirname(updateLayer.dataSource)), 'FILEGDB_WORKSPACE', os.path.basename(updateLayer.dataSource))

# Add the new layer to the top of your map
arcpy.mapping.AddLayer(df, newLayer, 'TOP')
# Get the new layer from the map
newLyrInMap = arcpy.mapping.ListLayers(mxd, newLayer.name, df)[0]
# Apply the layer properties from the original "updateLayer"
arcpy.mapping.UpdateLayer(df, newLyrInMap, updateLayer, False)
  • Hi, thank you for your response. The properties I ultimately want to apply are under the Hatches tab, not Symbology. I'm trying to replace all properties to apply those changes to the IAmRoute layer, which is why I had the argument set to False. Am I trying to do something with this function that isn't possible? – SarahKelley Apr 3 '18 at 18:01
  • I thought you meant hatching the generic sense...I don't deal with routes much. Since route hatching is not one of the layer properties, it won't be transferred via UpdateLayer. There doesn't appear to be a way to transfer route hatching in ArcPy. However, you may be able to copy the layer that has the hatching, replaceDataSource, and then copy the properties from the target layer. – Tom Apr 3 '18 at 18:25
  • @SarahKelley, I've updated my answer to demonstrate the process mentioned in my previous comment. – Tom Apr 3 '18 at 19:07
  • Wow, thank you for your assistance. I can follow most of that, but I need a little help with what to input for: newLayer.replaceDataSource() Also, to clarify terminology - what do you mean by geodatabase vs. feature dataset? Thank you again. – SarahKelley Apr 3 '18 at 19:36
  • @SarahKelley, Check out the replaceDataSource method from the Layer documentation. os.path.dirname simply gets the parent directory of a file; since routes must be within a feature dataset, then you must go to the grandparent directory. The second argument to replaceDataSource is a string of the worksapce type, because ESRI is too lazy to code tools that identify this for you. – Tom Apr 3 '18 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.