2

I am trying to clip a feature from one geodatabase into another and retain the editing feature templates I have assigned using subtypes for the original feature class in the original geodatabase. After clipping, the subtypes and editing feature templates are retained, but only when I drag and drop the layers into the map document. When I use arcpy.mapping.AddLayer(), the editing templates are lost.

How do I use python to add layers such that the editing feature templates are retained?

The clipped feature is named "Feature_Clip" in the code below.

workspace = arcpy.env.workspace
clippedFeature = os.path.join(workspace, "Feature_Clip")
mxd = arcpy.mapping.MapDocument("CURRENT")
df = mxd.activeDataFrame
layer = arcpy.mapping.Layer(clippedFeature)
arcpy.mapping.AddLayer (df, layer)
arcpy.ApplySymbologyFromLayer_management(layer.name, layerFile)
del mxd, df, layer

note layerFile points to a .lyr file I have created from the original feature class in hopes of using it's symbology, but it doesn't seem to have any effect.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Feb 15, 2018 at 18:48
0

1 Answer 1

1

The way that I would try to do this would be to switch the feature class under the layer that you clipped by setting lyr.dataSource to your clipped feature class.


The asker used this answer to resolve this. See updated code below for their final working solution. Note the layer name of the original is "Feature" in this example:

workspace = arcpy.env.workspace
clippedFeature = os.path.join(workspace, "Feature_Clip")
mxd = arcpy.mapping.MapDocument("CURRENT")
df = mxd.activeDataFrame
layer = arcpy.mapping.Layer(clippedFeature)
for existingLayer in arcpy.mapping.ListLayers(mxd, "", df):
 if existingLayer.name == layer.name[-5]: #Remove '_Clip" from new layer name to find original layer
 workspace_type = "FILEGDB_WORKSPACE"
 dataset_name = clippedFeature
 existingLayer.replaceDataSource(workspace, workspace_type,
 dataset_name)
arcpy.RefreshActiveView()
answered Mar 4, 2018 at 8:36
1
  • Thanks PolyGeo, this worked but I used the replaceDataSource() method instead of setting its attribute as you suggested. I've edited my question with the working code. Commented Mar 8, 2018 at 19:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.