Assuming I have two SDEs or other datasources with the same datamodel. One contains features the other one is empty. In ArcMap I have layers from both datasources in my table of contents. Now I would like to copy some features that are already selected in the map view from one sde to the other using C# and ArcObjects. What would be the best way to do this?
-
1Have you considered using arcpy for this? Yes you can do it in ArcObjects. No, you probably shouldn't.– blah238Oct 7, 2013 at 22:25
-
I agree with this comment. My answer below requires quite a bit of code. This can actually be done with less code by writing an ArcPy script and invoking it from Visual Studio.– ConorOct 9, 2013 at 16:11
2 Answers
Assuming that your features are non-complex, you will want to use an Insert Cursor (method 1). If your features are complex, you must use IFeature.Store() (method 2).
See the following code from this link (It's in VBA, but shouldn't take long to translate over)
Public Sub LoadFeatures()
Dim pMxDoc As IMxDocument
Dim pFeatureLayer As IFeatureLayer
Dim pInFeatureClass As IFeatureClass
Dim pOutFeatureClass As IFeatureClass
Dim pQueryFilter As IQueryFilter
Dim pFeatureCursor As IFeatureCursor
Dim pFeature As IFeature
'For simplicity sake, load features into the feature class
'selected in the table of contents
Set pMxDoc = Application.Document
If pMxDoc.SelectedItem Is Nothing Then Exit Sub
If Not TypeOf pMxDoc.SelectedItem Is IFeatureLayer Then Exit Sub
Set pFeatureLayer = pMxDoc.SelectedItem
Set pOutFeatureClass = pFeatureLayer.FeatureClass
'Open a shapefile containing the features to load into the geodatabase
Set pInFeatureClass = OpenFeatureClass("d:\data\usa", "counties")
If pInFeatureClass Is Nothing Then Exit Sub
Set pQueryFilter = New QueryFilter
Set pFeatureCursor = pInFeatureClass.Search(pQueryFilter, True)
Set pFeature = pFeatureCursor.NextFeature
Do While Not pFeature Is Nothing
'The next two lines control which loading method is used
'To use an insert cursor, comment out the second line
'To use CreateFeature/Store, comment out the first line
InsertFeature pOutFeatureClass, pFeature.Shape
'CreateFeature pOutFeatureClass, pFeature.Shape
Set pFeature = pFeatureCursor.NextFeature
Loop
'Refresh the display
pMxDoc.ActiveView.PartialRefresh esriViewGeography, Nothing, Nothing
End Sub
Method 1: an insert cursor
Private Sub InsertFeature(pFeatureClass As IFeatureClass, pGeometry As IGeometry)
Dim pFeatureBuffer As IFeatureBuffer
Dim pFeatureCursor As IFeatureCursor
Set pFeatureCursor = pFeatureClass.Insert(True)
Set pFeatureBuffer = pFeatureClass.CreateFeatureBuffer
Set pFeatureBuffer.Shape = pGeometry
pFeatureCursor.InsertFeature pFeatureBuffer
End Sub
Method 2: CreateFeature/Store
Private Sub CreateFeature(pFeatureClass As IFeatureClass, pGeometry As IGeometry)
Dim pFeature As IFeature
Set pFeature = pFeatureClass.CreateFeature
Set pFeature.Shape = pGeometry
pFeature.Store
End Sub
Opening the feature class:
Public Function OpenFeatureClass(strWorkspace As String, strFeatureClass As String) As IFeatureClass
On Error GoTo ErrorHandler
Dim pShpWorkspaceName As IWorkspaceName
Dim pDatasetName As IDatasetName
Dim pName As IName
'Create the workspace name object
Set pShpWorkspaceName = New WorkspaceName
pShpWorkspaceName.PathName = strWorkspace
pShpWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.shapefileworkspacefactory.1"
'Create the feature class name object
Set pDatasetName = New FeatureClassName
pDatasetName.Name = strFeatureClass
Set pDatasetName.WorkspaceName = pShpWorkspaceName
'Open the feature class
Set pName = pDatasetName
Set OpenFeatureClass = pName.Open
Exit Function
ErrorHandler:
Set OpenFeatureClass = Nothing
End Function
-
1Way to put in the effort! Two comments though. When you use an Insert Cursor, you should try to create the Feature Cursor and Buffer outside your load loop. In the example above, I think you are creating a new cursor and buffer for every feature. Also, you may want to release the COM objects (Marshel.ReleaseCOMObject) so that they release the locks faster.– travisOct 8, 2013 at 1:20
The easiest way I can think of is to either copy and paste the features or use the Copy Features GP tool. That doesn't use C#.
If you really want to do with using C#, you could create an add-in or GP tool. You could use search and insert cursors to actually copy the features. Or, you could call the Copy Features GP tool from C# to do the work.