操作方法
此代码示例显示了如何使用 ArcMap 加载项更改 ArcGIS Server 要素服务中要素的填充和轮廓颜色。 通过保存 ArcMap 文档,可以在 ArcMap 中保留更改。
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
namespace Add_FeatureService_to_ArcMap
{
public class Add_FeatureService_to_ArcMap : ESRI.ArcGIS.Desktop.AddIns.Button
{
public Add_FeatureService_to_ArcMap()
{
}
protected override void OnClick()
{
IMxDocument pMx = ArcMap.Application.Document as IMxDocument;
//您可以将此 URL 替换为自己的要素服务 URL。 在我的案例中,ArcGIS Server 要素服务在如下所示的 URL 中包含面要素:
string url = "http://localhost:6080/arcgis/rest/services/FC1_Polygons/FeatureServer";
IPropertySet pFeatServProp = new PropertySet();
pFeatServProp.SetProperty("DATABASE", url);
IWorkspaceFactory pFeatWorkspaceFact = new FeatureServiceWorkspaceFactory() as IWorkspaceFactory;
IFeatureWorkspace pFeatureWorkspace = pFeatWorkspaceFact.Open(pFeatServProp, 0) as IFeatureWorkspace;
IFeatureClass pfeatClass = pFeatureWorkspace.OpenFeatureClass("0") as IFeatureClass;
//“0”对应于索引/ID 为 0 的第一个图层,同理,“1”和“2”指的是图层 ID 1
// 和 ID 2,您可以查看服务 REST 端点以确保 OpenFeatureClass 中的参数有效。
IFeatureLayer pfeatLayer = new FeatureLayer();
pfeatLayer.FeatureClass = pfeatClass;
pfeatLayer.Name = "Sami_FC1";
IGroupLayer pGroupLayer = new GroupLayer();
pGroupLayer.Name = "GroupLayerName";
// -------------------------------- RENDERER ---------------------------------------------------
// 通过从 Microsoft 颜色对象提取值来创建 RGB 颜色
System.Drawing.Color c = System.Drawing.Color.FromName("SlateBlue");
IRgbColor rgbColor_Fill = CreateRgbColor(c);
System.Drawing.Color c2 = System.Drawing.Color.FromName("Red");
IRgbColor rgbColor_Outline = CreateRgbColor(c2);
ISimpleLineSymbol simpleLineSymbol_Outline = new SimpleLineSymbol();
simpleLineSymbol_Outline.Color = rgbColor_Outline;
simpleLineSymbol_Outline.Width = 3.0;
ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbol();
simpleFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
simpleFillSymbol.Color = rgbColor_Fill;
simpleFillSymbol.Outline = simpleLineSymbol_Outline;
ISimpleRenderer simpleRenderer = new SimpleRenderer();
simpleRenderer.Label = "Simple Renderer";
simpleRenderer.Symbol = simpleFillSymbol as ISymbol;
IFeatureRenderer featureRenderer = simpleRenderer as IFeatureRenderer;
IGeoFeatureLayer geoFeatureLayer = pfeatLayer as IGeoFeatureLayer;
geoFeatureLayer.Renderer = featureRenderer;
//-------------------------------- END RENDERER ---------------------------------------------------
pGroupLayer.Add(pfeatLayer);
pMx.FocusMap.AddLayer(pGroupLayer);
ArcMap.Application.CurrentTool = null;
}
// 用于从 Microsoft 颜色对象(结构)中提取 RGB 组件的方法
protected static IRgbColor CreateRgbColor(System.Drawing.Color c)
{
// 请注意 c 为 Microsoft 颜色对象,它具有 4 个组件:alpha、R、G、B
// System.Drawing.Color 对象的 R、G、B 组件分配至 ArcObjects IRgbColor 对象
IRgbColor rgb = new RgbColorClass();
rgb.Red = c.R; rgb.Green = c.G; rgb.Blue = c.B;
rgb.UseWindowsDithering = false;
return rgb;
}
}
}
文章 ID: 000013843
获取来自 ArcGIS 专家的帮助
立即开始聊天