前几天在网上找到的代码,自己添加一些,支持单字段,两个字段,三个字段的渲染
//下面给出唯一值渲染的核心代码:
private void UniqueValueRenderer(IFeatureLayer pFeatLyr, string[] sFieldName)
{
IUniqueValueRenderer pUniqueValueRender;
IColor pNextUniqueColor;
IEnumColors pEnumRamp;
ITable pTable;
IRow pNextRow;
ICursor pCursor;
IQueryFilter pQueryFilter;
IRandomColorRamp pRandColorRamp = new RandomColorRampClass();
pRandColorRamp.StartHue = 0;
pRandColorRamp.MinValue = 0;
pRandColorRamp.MinSaturation = 15;
pRandColorRamp.EndHue = 360;
pRandColorRamp.MaxValue = 100;
pRandColorRamp.MaxSaturation = 30;
IQueryFilter pQueryFilter1 = new QueryFilterClass();
pRandColorRamp.Size = pFeatLyr.FeatureClass.FeatureCount(pQueryFilter1);
bool bSuccess = false;
pRandColorRamp.CreateRamp(out bSuccess);
if (sFieldName.Length == 1)
{
IFeatureLayer pFLayer = pFeatLyr as IFeatureLayer;
IGeoFeatureLayer geoLayer = pFeatLyr as IGeoFeatureLayer;
IFeatureClass fcls = pFLayer.FeatureClass;
IQueryFilter pqf = new QueryFilterClass();
IFeatureCursor fCursor = fcls.Search(pqf, false);
IRandomColorRamp rx = new RandomColorRampClass();
rx.MinSaturation = 15;
rx.MaxSaturation = 30;
rx.MinValue = 85;
rx.MaxValue = 100;
rx.StartHue = 0;
rx.EndHue = 360;
rx.Size = 100;
bool ok; ;
rx.CreateRamp(out ok);
IEnumColors RColors = rx.Colors;
RColors.Reset();
IUniqueValueRenderer pRender = new UniqueValueRendererClass();
pRender.FieldCount = 1;
pRender.set_Field(0, sFieldName[0]);
IFeature pFeat = fCursor.NextFeature();
int index = pFeat.Fields.FindField(sFieldName[0]);
while (pFeat != null)
{
ISimpleFillSymbol symd = new SimpleFillSymbolClass();
symd.Style = esriSimpleFillStyle.esriSFSSolid;
symd.Outline.Width = 1;
symd.Color = RColors.Next();
string valuestr = pFeat.get_Value(index).ToString();
pRender.AddValue(valuestr, valuestr, symd as ISymbol);
pFeat = fCursor.NextFeature();
}
geoLayer.Renderer = pRender as IFeatureRenderer;
m_Map.Refresh();
}
if (sFieldName.Length == 2)
{
string sFieldName1 = sFieldName[0];
string sFieldName2 = sFieldName[1];
IGeoFeatureLayer pGeoFeatureL = (IGeoFeatureLayer)pFeatLyr;
pUniqueValueRender = new UniqueValueRendererClass();
pTable = (ITable)pGeoFeatureL;
int pFieldNumber = pTable.FindField(sFieldName1);
int pFieldNumber2 = pTable.FindField(sFieldName2);
pUniqueValueRender.FieldCount = 2;
pUniqueValueRender.set_Field(0, sFieldName1);
pUniqueValueRender.set_Field(1, sFieldName2);
pEnumRamp = pRandColorRamp.Colors;
pNextUniqueColor = null;
pQueryFilter = new QueryFilterClass();
pQueryFilter.AddField(sFieldName1);
pQueryFilter.AddField(sFieldName2);
pCursor = pTable.Search(pQueryFilter, true);
pNextRow = pCursor.NextRow();
string codeValue;
while (pNextRow != null)
{
codeValue = pNextRow.get_Value(pFieldNumber).ToString() + pUniqueValueRender.FieldDelimiter + pNextRow.get_Value(pFieldNumber2).ToString();
pNextUniqueColor = pEnumRamp.Next();
if (pNextUniqueColor == null)
{
pEnumRamp.Reset();
pNextUniqueColor = pEnumRamp.Next();
}
IFillSymbol pFillSymbol;
ILineSymbol pLineSymbol;
IMarkerSymbol pMarkerSymbol;
switch (pGeoFeatureL.FeatureClass.ShapeType)
{
case esriGeometryType.esriGeometryPolygon:
{
pFillSymbol = new SimpleFillSymbolClass();
pFillSymbol.Color = pNextUniqueColor;
pUniqueValueRender.AddValue(codeValue, sFieldName1 + " " + sFieldName2, (ISymbol)pFillSymbol);
break;
}
case esriGeometryType.esriGeometryPolyline:
{
pLineSymbol = new SimpleLineSymbolClass();
pLineSymbol.Color = pNextUniqueColor;
pUniqueValueRender.AddValue(codeValue, sFieldName1 + " " + sFieldName2, (ISymbol)pLineSymbol);
break;
}
case esriGeometryType.esriGeometryPoint:
{
pMarkerSymbol = new SimpleMarkerSymbolClass();
pMarkerSymbol.Color = pNextUniqueColor;
pUniqueValueRender.AddValue(codeValue, sFieldName1 + " " + sFieldName2, (ISymbol)pMarkerSymbol);
break;
}
}
pNextRow = pCursor.NextRow();
}
pGeoFeatureL.Renderer = (IFeatureRenderer)pUniqueValueRender;
m_Map.Refresh();
}
else if (sFieldName.Length == 3)
{
string sFieldName1 = sFieldName[0];
string sFieldName2 = sFieldName[1];
string sFieldName3 = sFieldName[2];
IGeoFeatureLayer pGeoFeatureL = (IGeoFeatureLayer)pFeatLyr;
pUniqueValueRender = new UniqueValueRendererClass();
pTable = (ITable)pGeoFeatureL;
int pFieldNumber = pTable.FindField(sFieldName1);
int pFieldNumber2 = pTable.FindField(sFieldName2);
int pFieldNumber3 = pTable.FindField(sFieldName3);
pUniqueValueRender.FieldCount = 3;
pUniqueValueRender.set_Field(0, sFieldName1);
pUniqueValueRender.set_Field(1, sFieldName2);
pUniqueValueRender.set_Field(2, sFieldName3);
pEnumRamp = pRandColorRamp.Colors;
pNextUniqueColor = null;
pQueryFilter = new QueryFilterClass();
pQueryFilter.AddField(sFieldName1);
pQueryFilter.AddField(sFieldName2);
pQueryFilter.AddField(sFieldName3);
pCursor = pTable.Search(pQueryFilter, true);
pNextRow = pCursor.NextRow();
string codeValue;
while (pNextRow != null)
{
codeValue = pNextRow.get_Value(pFieldNumber).ToString() + pUniqueValueRender.FieldDelimiter + pNextRow.get_Value(pFieldNumber2).ToString() + pUniqueValueRender.FieldDelimiter + pNextRow.get_Value(pFieldNumber3).ToString();
pNextUniqueColor = pEnumRamp.Next();
if (pNextUniqueColor == null)
{
pEnumRamp.Reset();
pNextUniqueColor = pEnumRamp.Next();
}
IFillSymbol pFillSymbol;
ILineSymbol pLineSymbol;
IMarkerSymbol pMarkerSymbol;
switch (pGeoFeatureL.FeatureClass.ShapeType)
{
case esriGeometryType.esriGeometryPolygon:
{
pFillSymbol = new SimpleFillSymbolClass();
pFillSymbol.Color = pNextUniqueColor;
pUniqueValueRender.AddValue(codeValue, sFieldName1 + " " + sFieldName2 + "" + sFieldName3, (ISymbol)pFillSymbol);
break;
}
case esriGeometryType.esriGeometryPolyline:
{
pLineSymbol = new SimpleLineSymbolClass();
pLineSymbol.Color = pNextUniqueColor;
pUniqueValueRender.AddValue(codeValue, sFieldName1 + " " + sFieldName2 + "" + sFieldName3, (ISymbol)pLineSymbol);
break;
}
case esriGeometryType.esriGeometryPoint:
{
pMarkerSymbol = new SimpleMarkerSymbolClass();
pMarkerSymbol.Color = pNextUniqueColor;
pUniqueValueRender.AddValue(codeValue, sFieldName1 + " " + sFieldName2 + "" + sFieldName3, (ISymbol)pMarkerSymbol);
break;
}
}
pNextRow = pCursor.NextRow();
}
pGeoFeatureL.Renderer = (IFeatureRenderer)pUniqueValueRender;
m_Map.Refresh();
}
}