0

After opening a MxD document and getting the main map then a feature layer, I want to duplicate the layer with ArcObjects in order to inherit its properties (symbology, layer def and labelling). Then, I want to edit the name of the copy and its layer defs query.

I managed to get both IMap and source feature layer.

IMap map = mapDocument.getMap(0);
IFeatureLayer sourceFeatureLayer = map.getLayer(0);
IFeatureLayer destinationFeatureLayer = ...?
destinationFeatureLayer.setName("layer_2");
map.addLayer(destinationFeatureLayer);

How do I duplicate the source layer?

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Sep 5, 2019 at 9:11

1 Answer 1

1

You should take a closer look at all the interfaces that IFeatureLayer implements for a lot of this information. That's .NET documentation, but it will mirror Java pretty closely.

A lot of what you want is on the IGeoFeatureLayer, IDataLayer, and IFeatureLayerDefinition. I have not tested the below code, but it's basically what you want to be doing.

IFeatureLayer old_fLayer = map.Layer[0];
var new_fLayer = new FeatureLayer() as IFeatureLayer;
//copy the data source to the new layer
new_fLayer.FeatureClass = old_fLayer.FeatureClass;
((new_fLayer as IDataLayer).DataSourceName = ((old_fLayer as IDataLayer).DataSourceName;
//copy the symbology (I'm 90% sure that there is no issue in simply assigning the renderer from one layer to another
(new_fLayer as IGeoFeatureLayer).Renderer = (old_fLayer as IGeoFeatureLayer).Renderer;
//move the labeling properties. AnnotationProperties implements IClone so we'll use that instead of direct assignment.
(new_fLayer as IGeoFeatureLayer).AnnotationProperties = 
(IAnnotateLayerPropertiesCollection)((old_fLayer as IGeoFeatureLayer).AnnotationProperties as IClone).Clone();
(new_fLayer as IGeoFeatureLayer).AnnotationPropertiesID = ((old_fLayer as IGeoFeatureLayer).AnnotationPropertiesID;
//update the new properties for this layer
(new_fLayer as IFeatureLayerDefintion).DefinitionExpression = "new expression";
new_fLayer.Name = "New Name";
//add the new layer to the map
map.AddLayer(new_fLayer);
(map as IActiveView).Refresh();
answered Sep 5, 2019 at 16:16
1
  • 1
    Creating a new layer then applying data source, renderer and annotation properties works well. Also I found this: IObjectCopy copier = new ObjectCopy(); IFeatureLayer destLayer = (IFeatureLayer)copier.copy(sourceLayer); Then add the layer to the map, change the name and layer def. Renderer and annotation properties are well copied too. Commented Sep 9, 2019 at 7:37

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.