#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
}
}
Arcgis Server+Asp.net(C#)的唯一值渲染
最新推荐文章于 2020-05-28 10:24:00 发布