rhyck.m

Summarize a field grouped  by values in the field

Discussion created by rhyck.m on May 8, 2014
Hi all,

I am trying to summarize a field and populate the result in a listview. below is the code of what I am trying to accomplish but with no success. I want it to work as the summarize tool in ArcMap when right clicking the Field to summarize. I am wondering if there is a Select COUNT(fieldName) Group By (fieldName).


Private Sub btnCheckData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckData.Click
        'Load result in the listbox
        Dim selectedFieldName As String = Nothing

        If cboBoxFields.SelectedIndex <> -1 Then

            ListViewDataresult.Enabled = True

            selectedFieldName = cboBoxFields.SelectedItem.ToString

            ListViewDataresult.Items.Clear()
            ListViewDataresult.BeginUpdate()

            Dim newlist As ListViewItem

            Try
                'check if we have the fieldName
                If selectedFieldName = Nothing Then
                    Exit Sub
                End If

                Dim flayerName As String = cboBoxLayers.SelectedItem.ToString

                Dim mxDoc As IMxDocument = My.ArcMap.Application.Document
                Dim map As IMap = mxDoc.FocusMap

                Dim enumLayer As IEnumLayer = map.Layers

                Dim layer As ILayer = enumLayer.Next()

                Dim flayer As IFeatureLayer = Nothing

                Do While Not layer Is Nothing
                    If layer.Name = flayerName Then
                        flayer = CType(layer, IFeatureLayer)

                    End If
                    layer = enumLayer.Next()
                Loop

                If flayer Is Nothing Then
                    MessageBox.Show("No layer")
                    Exit Sub

                End If

                Dim fclass As IFeatureClass = flayer.FeatureClass
              
                'define the cursor to store data
                Dim pCursor As ESRI.ArcGIS.Geodatabase.ICursor
                'define datastat to query unique values
                Dim pDataStats As New ESRI.ArcGIS.Geodatabase.DataStatistics
                'define data collection
                Dim pEnum As System.Collections.IEnumerator
             
                pCursor = fclass.Search(Nothing, False)
                pDataStats.Field = selectedFieldName
                pDataStats.Cursor = pCursor
                pEnum = pDataStats.UniqueValues

                Dim dataStatResult As IStatisticsResults = pDataStats.Statistics

                pEnum.Reset()

                Do While pEnum.MoveNext

                    'This part works fine. it will add unique attributes in the listview in the 1st column

                    newlist = ListViewDataresult.Items.Add(pEnum.Current)

                    ' I wish to have here a number of  values that have the same attribute like the summarize in ArcMap, but instead it gives me the total of unique values in the current pDataStats
                  'here is where I want to add the count of duplicate attributes in the 2nd column
                    newlist.SubItems.Add(pDataStats.UniqueValueCount)
                Loop




                ListViewDataresult.EndUpdate()
                ListViewDataresult.Refresh()

                'MessageBox.Show(statResut.Count.ToString)

            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

        End If


    End Sub

Outcomes