有时候需要根据外部关联表格的某一个字段的值来符号化一个图层,例如,有一个行政区的图层,它和一个属性表格关联,这个表格中记录有行政区的属性,比如“人口”,现我们需要根据人口值来符号化每一个要素。因为图层和属性表格不在同一个表格中,因此,需要将它们“Join”起来,保存在内存中,以便符号化图层的时候能读取到关联表格的字段值。这里需要注意的是取字段的时候要注明表格名称,如iField = pFields.FindField("tract_pop.POPULATION")
代码
Private Sub CreateAndApplyUVRenderer()
Dim pDpyRC As IDisplayRelationshipClass'内存关联类
Set pDpyRC = JionToTable(0)
Dim pTable As ITable
Set pTable = pDpyRC
Dim pMap As IMap
Set pMap = axMapControl.Map
Dim pLayer As ILayer
Set pLayer = pMap.Layer(0)
Dim pFLayer As IFeatureLayer
Set pFLayer = pLayer
Dim pLyr As IGeoFeatureLayer
Set pLyr = pFLayer
Dim pFeatCls As IFeatureClass
Set pFeatCls = pFLayer.FeatureClass
Dim pQueryFilter As IQueryFilter
Set pQueryFilter = New QueryFilter 'empty supports: SELECT *
Dim pFeatCursor As IFeatureCursor
Set pFeatCursor = pFeatCls.Search(pQueryFilter, False)
Dim pCursor As ICursor
Set pCursor = pTable.Search(pQueryFilter, False)
'** Make the color ramp we will use for the symbols in the renderer
Dim rx As IRandomColorRamp
Set rx = New RandomColorRamp
rx.MinSaturation = 20
rx.MaxSaturation = 40
rx.MinValue = 85
rx.MaxValue = 100
rx.StartHue = 76
rx.EndHue = 188
rx.UseSeed = True
rx.Seed = 43
'** Make the renderer
Dim pRender As IUniqueValueRenderer, n As Long
Set pRender = New UniqueValueRenderer
Dim symd As ISimpleFillSymbol
Set symd = New SimpleFillSymbol
symd.Style = esriSFSSolid
symd.Outline.Width = 0.4
'** These properties should be set prior to adding values
pRender.FieldCount = 1
pRender.Field(0) = "POPULATION"
pRender.DefaultSymbol = symd
pRender.UseDefaultSymbol = True
'Dim pFeat As IFeature
'n = pFeatCls.FeatureCount(pQueryFilter)
Dim pRow As IRow
n = pTable.RowCount(pQueryFilter)
'** Loop through the features
Dim i As Integer
i = 0
Dim ValFound As Boolean
Dim NoValFound As Boolean
Dim uh As Integer
Dim pFields As IFields
Dim iField As Integer
Set pFields = pCursor.Fields
iField = pFields.FindField("tract_pop.POPULATION")'注意取字段的时候要注明表格名称
Do Until i = n
Dim symx As ISimpleFillSymbol
Set symx = New SimpleFillSymbol
symx.Style = esriSFSSolid
symx.Outline.Width = 0.4
Set pRow = pCursor.NextRow
Dim x As String
x = pRow.Value(iField) '*new Cory*
'** Test to see if we've already added this value
'** to the renderer, if not, then add it.
ValFound = False
For uh = 0 To (pRender.ValueCount - 1)
If pRender.Value(uh) = x Then
NoValFound = True
Exit For
End If
Next uh
If Not ValFound Then
pRender.AddValue x, "POPULATION", symx
pRender.Label(x) = x
pRender.Symbol(x) = symx
End If
i = i + 1
Loop
'** now that we know how many unique values there are
'** we can size the color ramp and assign the colors.
rx.size = pRender.ValueCount
rx.CreateRamp (True)
Dim RColors As IEnumColors, ny As Long
Set RColors = rx.Colors
RColors.Reset
For ny = 0 To (pRender.ValueCount - 1)
Dim xv As String
xv = pRender.Value(ny)
If xv <> "" Then
Dim jsy As ISimpleFillSymbol
Set jsy = pRender.Symbol(xv)
jsy.Color = RColors.Next
pRender.Symbol(xv) = jsy
End If
Next ny
'** If you didn't use a color ramp that was predefined
'** in a style, you need to use "Custom" here, otherwise
'** use the name of the color ramp you chose.
pRender.ColorScheme = "Custom"
pRender.FieldType(0) = True
Set pLyr.Renderer = pRender
pLyr.DisplayField = "POPULATION"
'** This makes the layer properties symbology tab show
'** show the correct interface.
Dim hx As IRendererPropertyPage
Set hx = New UniqueValuePropertyPage
pLyr.RendererPropertyPageClassID = hx.ClassID
'** Refresh the TOC
axMapControl.ActiveView.ContentsChanged
'** Draw the map
axMapControl.ActiveView.Refresh
End Sub
Private Function JionToTable(i As Integer) As IDisplayRelationshipClass
Dim pMap As IMap
Dim pLayer As IGeoFeatureLayer
Dim pDpyRC As IDisplayRelationshipClass
Dim pTableColl As ITableCollection
Dim pTable As ITable
Dim pMemRCFact As IMemoryRelationshipClassFactory
Dim pRelClass As IRelationshipClass
Set pMap = axMapControl.Map
' get a reference to the layer
Set pLayer = pMap.Layer(0)
Set pDpyRC = pLayer
' get a reference to the table
Set pTableColl = pMap
Set pTable = pTableColl.Table(0)
' create a relationship class in memory
Set pMemRCFact = New MemoryRelationshipClassFactory
Set pRelClass = pMemRCFact.Open("Demo", pTable, "TRACT_ID", pLayer.FeatureClass, _
"TRACT_ID", "Forwards", "Backwards", esriRelCardinalityOneToOne)
' perform the join
pDpyRC.DisplayRelationshipClass pRelClass, esriLeftOuterJoin
Set JionToTable = pDpyRC
End Function