如何为当前层或独立表创建一个Summary表

本例要实现的是如何按某一字段“分组”(dissolve),统计其它字段的数据信息摘要(创建Summary表)。可得到的主要信息包括该字段值相同的每组记录中的记录数量、最大值、最小值、和、平均值等。主要用到IBasicGeoprocessor接口的Dissolve方法。

l   要点

为当前层创建Summary表,要得到当前层的引用,并确定在其上执行Dissolve操作的字段。对独立表的操作方法与层的操作类似。

l   程序说明

过程UIBCreateSummaryTable_Click是实现模块,调用过程CreateSummaryTable实现功能。过程CreateSummaryTable中应先确认层(例中为states)和要“Dissolve”的字段(例中为SUB_REGION)存在,同时要定义摘要表的名字(本例为SumStates)。

然后指定执行Dissolve方法的操作符(如Minimum,Count,Average等)和在其上施行操作的字段名(例中为AREA)。操作结果作为独立表添加到当前Map。

因为Dissolve方法参数表中的“输入表”和“输出数据集的名字”都是引用,为了避免多次调用过程使最终SumStates表中的结果不唯一,每次执行Dissolve前,将SumStates的已存内容删除。

l   代码

Private Sub UIBCreateSummaryTable_Click()

    Call CreateSummaryTable

End Sub

Public Sub CreateSummaryTable()

    Dim pMxDocument             As IMxDocument

    Dim pMap                    As IMap

    Dim pLayer                  As ILayer

    Dim pFeatLayer              As IFeatureLayer

    Dim iCount                  As Integer

    Dim pFeatureClass           As IFeatureClass

    Dim pTable                  As ITable

    Dim pDataSet                As IDataset

    Dim pWorkspace              As IWorkspace

    Dim pWorkspaceDataset       As IDataset

    Dim pWorkspaceName          As IName

    Dim pOutTableName           As ITableName

    Dim pOutDatasetName         As IDatasetName

    Dim pEnumDataset            As IEnumDataset

    Dim pBasicGeoprocessor      As IBasicGeoprocessor

    Dim pSumTable               As ITable

    Dim pStandaloneTable        As IStandaloneTable

    Dim pStandaloneTableColl    As IStandaloneTableCollection

    ' Define current layer name and output table name

    Const sLayerName As String = "states"

    Const sSumTableName As String = "SumStates"

    Set pMxDocument = ThisDocument

    Set pMap = pMxDocument.FocusMap

    On Error GoTo ErrorHandler   

    Set pMxDocument = ThisDocument

    Set pMap = pMxDocument.FocusMap

    On Error GoTo ErrorHandler   

     ' Find the layer named states

    For iCount = 0 To pMap.LayerCount - 1

        Set pLayer = pMap.Layer(iCount)

        If TypeOf pLayer Is IFeatureLayer Then

            If pLayer.Name = sLayerName Then

                Set pFeatLayer = pLayer

                Exit For

            End If

        End If

    Next 

    If pFeatLayer Is Nothing Then

        MsgBox "The " & sLayerName & " layer was not found"

        Exit Sub

    End If

    ' Get the workspace of the states layer

    Set pFeatureClass = pFeatLayer.FeatureClass

    Set pTable = pFeatureClass

    Set pDataSet = pTable

    Set pWorkspace = pDataSet.Workspace

    Set pWorkspaceDataset = pWorkspace

    Set pWorkspaceName = pWorkspaceDataset.FullName

    ' Set up the output table

    Set pOutTableName = New TableName

    Set pOutDatasetName = pOutTableName

    pOutDatasetName.Name = sSumTableName

    Set pOutDatasetName.WorkspaceName = pWorkspaceName

    ' Make sure there is a field called SUB_REGION in the layer

    If pTable.FindField("SUB_REGION") = -1 Then

        MsgBox "There must be a field named SUB_REGION in states"

        Exit Sub

    End If

    ' Check if SumStates.dbf file already exist: if yes, delete it

    Set pEnumDataset = pWorkspace.Datasets(esriDTTable)

    Set pWorkspaceDataset = pEnumDataset.Next

    Do Until pWorkspaceDataset Is Nothing

        If pWorkspaceDataset.Name = pOutDatasetName.Name Then

            pWorkspaceDataset.Delete

            Exit Do

        End If

        Set pWorkspaceDataset = pEnumDataset.Next

    Loop

    ' Perform the summarize. Note the summary fields string (minimum.SUB_REGION ...)

    ' below. This is a comma-delimited string that lists the generated summary

    ' fields. Each field must start with a keyword, and be followed by .fieldName,

    ' where fieldName is the name of a field in the original table.

    '

    ' If you specify the Shape field, you must use the keyword 'Dissolve'. This

    ' is not used below since we are creating a non-spatial summary table.

 

    Set pBasicGeoprocessor = New BasicGeoprocessor

    Set pSumTable = pBasicGeoprocessor.Dissolve(pTable, False, "SUB_REGION", _

        "Minimum.SUB_REGION, Count.SUB_REGION, Sum.AREA, Average.AREA," & _

        "Minimum.AREA, Maximum.AREA, StdDev.AREA, Variance.AREA", _

        pOutDatasetName)

    ' add the table to map

    Set pStandaloneTable = New StandaloneTable

    Set pStandaloneTable.Table = pSumTable

    Set pStandaloneTableColl = pMap

    pStandaloneTableColl.AddStandaloneTable pStandaloneTable

    ' Refresh the TOC

    pMxDocument.UpdateContents

    Exit Sub

ErrorHandler:

    MsgBox Err.Number & " " & Err.Description

End Sub

posted on 2006-09-07 13:23  greatbird  阅读(660)  评论(0)    收藏  举报

导航