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?

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();
  • 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. – superrache Sep 9 at 7:37

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.