I have a shapefile in which some features shares same values for all the attributes.

I can merge them manually by going into edit session and, selecting features to be merged, and then using merge button under Editor.

However, since number of such features are very large, a programmatic solution is required.


2 Answers 2


You can use the Geoprocessing tools programmatically. Here's an example (in VB.NET) from one of my applications (which uses many geoprocessing tools). The InputName can be a string or a feature class and it returns a feature class.

   Friend Function DissolveDataset(ByVal InputName As Object, ByVal DissolveField As String, ByVal StatsFields As String, ByVal OutputName As String) As ESRI.ArcGIS.Geodatabase.IFeatureClass

    Dim DissolveDS As New ESRI.ArcGIS.DataManagementTools.Dissolve
    Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        Using releaser As New ESRI.ArcGIS.ADF.ComReleaser
            DissolveDS.in_features = InputName
            DissolveDS.dissolve_field = DissolveField
            DissolveDS.statistics_fields = StatsFields
            DissolveDS.out_feature_class = OutputName

            Result = RunTool(DissolveDS, Nothing)
            If Result Is Nothing Then
                System.Windows.Forms.MessageBox.Show("Could not dissolve dataset")
                Return Nothing
            End If

            Return ReturnObjectfromResult(Result, "Feature Class")
        End Using

    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.ToString, "Dissolve error")
        Return Nothing
    End Try

End Function

Friend Function RunTool(ByVal Process As ESRI.ArcGIS.Geoprocessor.IGPProcess, ByVal TC As ESRI.ArcGIS.esriSystem.ITrackCancel2, Optional ByVal AddOutput As Boolean = False) As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

    Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        System.Windows.Forms.Cursor.Current = Windows.Forms.Cursors.WaitCursor
        Dim GP As New ESRI.ArcGIS.Geoprocessor.Geoprocessor

        GP.AddOutputsToMap = AddOutput

        Result = CType(GP.Execute(Process, Nothing), ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2)
        If Result.Status <> ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded Then ReturnMessages(Result, "Geoprocessing Error")
    Catch ex As Exception
        ReturnMessages(Result, "Fail")
        System.Windows.Forms.MessageBox.Show(ex.ToString, "Run Geoprocessor")
    End Try

    Return Result

End Function

Private Sub ReturnMessages(ByVal pResult As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2, ByVal Title As String)

    Dim ErrorMessage As String

    If pResult Is Nothing Then Exit Sub

    If pResult.MessageCount > 0 Then
        For Count As Integer = 0 To pResult.MessageCount - 1
            ErrorMessage += pResult.GetMessage(Count)
    End If

    System.Windows.Forms.MessageBox.Show(ErrorMessage, Title)

End Sub

Friend Function ReturnObjectfromResult(ByVal result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2, ByVal Type As String) As Object

    Dim GPVal As ESRI.ArcGIS.Geodatabase.IGPValue
    Dim InMemFC As String
    Dim GPUtil As ESRI.ArcGIS.Geoprocessing.IGPUtilities3 = New ESRI.ArcGIS.Geoprocessing.GPUtilities

        GPVal = result.GetOutput(0)
        InMemFC = GPVal.GetAsText()

        Select Case Type
            Case "Feature Class"
                Return GPUtil.OpenFeatureClassFromString(InMemFC)
            Case "Table"
                Return GPUtil.OpenTableFromString(InMemFC)
            Case "Feature Layer"
                Return GPUtil.OpenFeatureLayerFromString(InMemFC)
            Case Else
                Return Nothing
        End Select

    Catch ex As Exception
        System.Windows.Forms.MessageBox.Show(ex.ToString, "ReturnObjectfromResult error")
        Return Nothing
    End Try

End Function

If you would like to actually use the Merge_management() tool then it is possible to use the FieldMappings() object with the merge tool to alter your fields that have the same attributes but different field names; I only use these within python so i can't pass on any code. I did find this discussion which may be helpful in addition to the other suggestions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.