AnsweredAssumed Answered

Create a copy of a layer

Question asked by sfarley on Jan 22, 2020
Latest reply on Jan 24, 2020 by Hornbydd

I am trying to create a deep copy of my layers. These layers could be either shape files or part of a geodatabase file. I need all the data to remain the same because I am creating tools to allow the user to clean up the data and the changes are saved to the new layers and not the original.

 

Here is the code that I am using, but it has a problem with geodatabase files where it does not seem to copy the shapes correctly. For instance, a few of the polylines go from having two points or more to having zero points. I am hoping that you have a better solution.

 

(code may have slight errors as I cannot do a direct copy from development machine and am retyping the code)

IFeatureClassName pInFeatureClassName, pFeatureClassName;

IDataset pDataset;

IDatasetName pInDsName, pOutDatasetName;

IWorkspaceName pWorkspaceName;

 

IFeatureLayer pFLayer = selectedLayer as IFeatureLayer; //selected layer is either a node or link layer

IFeatureClass pFc = pFLayer.FeatureClass;

IQueryFilter qf = new QueryFilterClass();

pDataset = pFc as IDataset;

pInFeatureClassName = pDataset.FullName as IFeatureClassName;

pInDsNamepInFeatureClassName as IDatasetName;

pFeatureClassName = new FeatureClassName() as IFeatureClassName;

pOutDatasetName = pFeatureClassName as IDatasetName;

pOutDatasetName.Name = "someNameWithGuid";

pWorkspaceName = new WorkspaceName() as IWorkspaceName;

 

IWorkspace pWorkspace = pDataset.Workspace;

 

string tempPath  = pWorkspace.PathName;

pWorkspaceName.PathName = tempPath;

pWorkspaceName .WorkspaceFacoryProgId = "esricCore.shapefileworkspacefactory.1";

pOutDatasetName.WorkspaceName = pWorkspaceName;

pFeatureClassName .ShapeType = esriGeometryType.esriGeometryAny;

pFeatureClassName .ShapeFieldName = "Shape";  //how to we programatically determine this?

 

IFeatureSelection pFSel = pFLayer as IFeatureSelection;

pFSel.SelectFeatures(qf, esriSelectionResultEnum.esriSelectionResultAdd, false); //selects everything on the map

ISelectionSet pSelSet = pFSel.SelectionSet as ISelectionSet;

IExportOperation pExportOp = new ExportOperation() as IExportOperation;

 

pExportOp.ExportFeatureClass(pInDsName, null, pSelSet, null, pOutDatasetName as IFeatureClassName, 0);

IWorkspaceFactory workspacefac = new ShapefileWorkspaceFactoryClass();

IFeatureWorkspace featworkspace = (IFeatureWorkspace)workspacefac .OpenFromFile(tempPath, 0);

IFeatureLayer lyr = new FeatureLayerClass();

IFeatureClass featclass = featworkspace.OpenFeatureClass(pOutDatasetName.Name + ".shp");

lyr.FeatureClass = featclass;

lyr.Name = pOutDatasetName.Name;

lyr.Visible = true;

currdoc.ActiveView.FocusMap.AddLayer(lyr);

Outcomes