参考代码:
http://hi.baidu.com/cavalry1/blog/item/e0d78f0a64ef961994ca6bef.html
#region "唯一值渲染"
/// <summary> 唯一值渲染:uvRenderer () </summary>
/// <Author>Cavalry</Author>
/// <param name="pFieldName">渲染的字段名</param>
/// <DateTime>2008-09-02 10:59</DateTime>
public static void uvRenderer(string pFieldName)
{
string servername = ConfigurationManager.AppSettings["MapHostName"];
//地图服务器的计算机名
string mapserverobject = ConfigurationManager.AppSettings["MapServiceName"];
//地图服务名称(ArcCatalog中服务的名称)
ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection agsconnection = new
ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection();
agsconnection.Host = servername;
agsconnection.Connect();
IServerObjectManager som = agsconnection.ServerObjectManager;
IServerContext serverContext = som.CreateServerContext(mapserverobject,
"MapServer");
IMapServer mapServer = serverContext.ServerObject as IMapServer;
IMapServerInfo mapInfo = mapServer.GetServerInfo(mapServer.DefaultMapName);
IMapDescription mapDesc = mapInfo.DefaultMapDescription;
IMapServerObjects mapServerObjects = mapServer as IMapServerObjects;
ILayer layer = mapServerObjects.get_Layer(mapServer.DefaultMapName, 0);
//获取面图层
IFeatureLayer pFLayer = layer as IFeatureLayer;
IGeoFeatureLayer geoLayer = layer as IGeoFeatureLayer;
IFeatureClass pFeatCls = geoLayer.FeatureClass as IFeatureClass;
//查询条件
IQueryFilter pQueryFilter = serverContext.CreateObject(
"esriGeodatabase.QueryFilter")as IQueryFilter;
IFeatureCursor pFeatCursor = pFeatCls.Search(pQueryFilter, false);
//设置一个随机颜色范围
IRandomColorRamp rx = serverContext.CreateObject(
"esriDisplay.RandomColorRamp")as IRandomColorRamp;
rx.MinSaturation = 20;
rx.MaxSaturation = 40;
rx.MinValue = 85;
rx.MaxValue = 100;
rx.StartHue = 76;
rx.EndHue = 188;
rx.Size = 360;
rx.UseSeed = true;
rx.Seed = 43;
//此处需要用CreateObject方有权限对IGeoLayer的Render属性进行设置(被这个问题困扰了N久)!!!
IUniqueValueRenderer pRender = (IUniqueValueRenderer)
serverContext.CreateObject("esriCarto.UniqueValueRenderer");
Int32 n;
//同样需要CreateObject方法
ISimpleFillSymbol symd = serverContext.CreateObject(
"esriDisplay.SimpleFillSymbol")as ISimpleFillSymbol;
symd.Style = ESRI.ArcGIS.Display.esriSimpleFillStyle.esriSFSSolid;
pRender.FieldCount = 1;
pRender.set_Field(0, pFieldName);
pRender.DefaultSymbol = symd as ISymbol;
pRender.UseDefaultSymbol = true;
IFeature pFeat = pFeatCursor.NextFeature();
n = pFeatCls.FeatureCount(pQueryFilter); //n为查询到的图元的数量
int i = 0;
bool ValFound;
int uh, iField;
IFields pFields = pFeatCursor.Fields;
iField = pFields.FindField(pFieldName); //获得要分析的字段的索引值
while (i < n)
{
ISimpleFillSymbol symx = serverContext.CreateObject(
"esriDisplay.SimpleFillSymbol")as ISimpleFillSymbol;
symx.Style = ESRI.ArcGIS.Display.esriSimpleFillStyle.esriSFSSolid;
symx.Outline.Width = 1;
string x = pFeat.get_Value(iField).ToString();
ValFound = false;
//先查询pRender之前是否已有添加过该值,有的话则不再添加
for (uh = 0; uh < pRender.ValueCount; uh++)
{
if (pRender.get_Value(uh) == x)
{
ValFound = true;
break;
}
}
//如果该值为尚未添加进pRender,则将值和符号样式添加给pRender
if (!ValFound)
{
pRender.AddValue(x, pFieldName, (ISymbol)symx);
pRender.set_Label(x, x);
pRender.set_Symbol(x, (ISymbol)symx);
}
pFeat = pFeatCursor.NextFeature();
i++;
}
//得到pRender里含有多少个不同的值,将其赋给rx的Size属性
rx.Size = pRender.ValueCount;
bool ok;
rx.CreateRamp(out ok);
Int32 ny;
IEnumColors RColors = rx.Colors;
RColors.Reset();
//为每个不同的值设置不同的颜色
for (ny = 0; ny < pRender.ValueCount; ny++)
{
string xv = pRender.get_Value(ny);
if (xv != "")
{
ISimpleFillSymbol jsy = pRender.get_Symbol(xv)as ISimpleFillSymbol;
jsy.Color = RColors.Next();
pRender.set_Symbol(xv, (ISymbol)jsy);
}
}
pRender.ColorScheme = "Custom";
pRender.set_FieldType(0, true);
geoLayer.Renderer = (IFeatureRenderer)pRender;
geoLayer.DisplayField = pFieldName;
}
#endregion
}
自己使用的唯一值渲染代码:
IMapServerObjects mapServerObjects = localMapResource.MapServer as IMapServerObjects;
IMap map = mapServerObjects.get_Map(localMapResource.DataFrame);
map.AddLayer(featureLayer);
map.MoveLayer(featureLayer, 0);
#region 渲染上色
//---------------------------
//渲染上颜色部分,根据dbz字段
//---------------------------
featureLayer = map.get_Layer(0) as IFeatureLayer;//0为图层序号,自己可以改
//获取面图层
IFeatureLayer pFLayer = featureLayer as IFeatureLayer;
IGeoFeatureLayer geoLayer = featureLayer as IGeoFeatureLayer;
IFeatureClass pFeatCls = geoLayer.FeatureClass as IFeatureClass;
//查询条件
IQueryFilter pQueryFilter = mapContext.CreateObject("esriGeodatabase.QueryFilter") as IQueryFilter;
IFeatureCursor pFeatCursor = pFeatCls.Search(pQueryFilter, false);
//设置一个随机颜色范围
IRandomColorRamp rx = mapContext.CreateObject("esriDisplay.RandomColorRamp") as IRandomColorRamp;
rx.StartHue = 40;
rx.EndHue = 120;
rx.MinValue = 65;
rx.MaxValue = 90;
rx.MinSaturation = 25;
rx.MaxSaturation = 45;
rx.Size = 20;
rx.Seed = 23;
//此处需要用CreateObject方有权限对IGeoLayer的Render属性进行设置(被这个问题困扰了N久)!!!
IUniqueValueRenderer pRender = (IUniqueValueRenderer)mapContext.CreateObject("esriCarto.UniqueValueRenderer");
Int32 n;
//同样需要CreateObject方法
ISimpleFillSymbol symd = mapContext.CreateObject("esriDisplay.SimpleFillSymbol") as ISimpleFillSymbol;
symd.Style = ESRI.ArcGIS.Display.esriSimpleFillStyle.esriSFSSolid;
pRender.FieldCount = 1;
pRender.set_Field(0, FieldName);
pRender.DefaultSymbol = symd as ISymbol;
pRender.UseDefaultSymbol = true;
IFeature pFeat = pFeatCursor.NextFeature();
n = pFeatCls.FeatureCount(pQueryFilter); //n为查询到的图元的数量
int i = 0;
bool ValFound;
int uh, iField;
IFields pFields = pFeatCursor.Fields;
iField = pFields.FindField(FieldName); //获得要分析的字段的索引值
//iField = 1;//需要渲染字段的id
while (i < n)
{
ISimpleFillSymbol symx = mapContext.CreateObject("esriDisplay.SimpleFillSymbol") as ISimpleFillSymbol;
symx.Style = ESRI.ArcGIS.Display.esriSimpleFillStyle.esriSFSSolid;
symx.Outline.Width = 1;
string x = pFeat.get_Value(iField).ToString();
ValFound = false;
//先查询pRender之前是否已有添加过该值,有的话则不再添加
for (uh = 0; uh < pRender.ValueCount; uh++)
{
if (pRender.get_Value(uh) == x)
{
ValFound = true;
break;
}
}
//如果该值为尚未添加进pRender,则将值和符号样式添加给pRender
if (!ValFound)
{
pRender.AddValue(x, FieldName, (ISymbol)symx);
pRender.set_Label(x, x);
pRender.set_Symbol(x, (ISymbol)symx);
}
pFeat = pFeatCursor.NextFeature();
i++;
}
//得到pRender里含有多少个不同的值,将其赋给rx的Size属性
rx.Size = pRender.ValueCount;
bool ok;
rx.CreateRamp(out ok);
Int32 ny;
IEnumColors RColors = rx.Colors;
RColors.Reset();
//Int32 TmpPPIColor;
int Tempred = 0;
int Tempgreen = 0;
int Tempblue = 0;
IRgbColor rgbColor = mapContext.CreateObject("esriDisplay.RGBColor") as IRgbColor;
//为每个不同的值设置不同的颜色
for (ny = 0; ny < pRender.ValueCount; ny++)
{
string xv = pRender.get_Value(ny);
if (xv != "")
{
ISimpleFillSymbol jsy = pRender.get_Symbol(xv) as ISimpleFillSymbol;
//jsy.Color = RColors.Next();
//----------------------
//自己添加的
//--------------------
Tempred = 0;
Tempgreen = 0;
Tempblue = 0;
//这个函数是根据xv的值返回RGB值,可根据自己需要设置颜色
PPIAndRHIFeaColor(xv, ref Tempred, ref Tempgreen, ref Tempblue);
rgbColor.Red = Tempred;
rgbColor.Green = Tempgreen;
rgbColor.Blue = Tempblue;
IColor color = rgbColor as IColor;
jsy.Color = color;// GetRGB(Tempred, Tempgreen, Tempblue);
jsy.Outline.Width = 0;
jsy.Outline.Color = color;
//--------------------------
//-------------------------
pRender.set_Symbol(xv, (ISymbol)jsy);
}
}
pRender.ColorScheme = "Custom";
pRender.set_FieldType(0, true);
geoLayer.Renderer = (IFeatureRenderer)pRender;
geoLayer.DisplayField = FieldName;
//-----------------------
//熏染结束
//-----------------------
#endregion
//刷新服务器对象 响应添加图层
localMapResource.RefreshServerObjects();
//刷新地图与Toc
Map1.Refresh();