Sunday 26 November 2017

Adding layers with subtypes and feature templates using ArcPy?


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.



Answer




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()

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...