这段代码是根据别人的代码,稍加了修改。
#region "唯一值渲染"
/// <summary> 唯一值渲染:uvRenderer () </summary>
/// <Author>Cavalry</Author>
/// <param name="pFieldName">渲染的字段名</param>
public void uvRenderer(string pFieldName)
{
string servername = "ZHOUWEN";
//地图服务器的计算机名
string mapserverobject = "DCM2";
//地图服务名称(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, 10);//10为图层序号,自己可以改
//获取面图层
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 = 0;
rx.MaxSaturation = 100;
rx.MinValue = 0;
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); //获得要分析的字段的索引值
iField = 11;//需要渲染字段的id
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;
}
自己加一个按钮,调用以上函数,就可以实现唯一值渲染了。不过颜色配置可能有点不太好。自己改一下。