以下是从我的aspx.cs文件中拷贝出来的在ArcGIS Server中添加和删除shp图层的源码,用起来比较好用,代码出处是刘光的那本书。
如果使用,可以把添加图层的和删除的图层的代码写成一个通用的函数,如
private void addPPISHPFile(string shpFilePath, string shpFileName, string LayerName, string FieldName)
private void RemovePPISHPLayer(string LayerName)
使用就行,不多说了,关键是能解决问题。
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ESRI.ArcGIS.Server;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer;
public partial class Default2 : System.Web.UI.Page, ICallbackEventHandler
{
public string addLayerCallback;
private string callbackResponse;
MapResourceLocal localMapResource = null;
protected void Page_Load(object sender, EventArgs e)
{
addLayerCallback = ClientScript.GetCallbackEventReference(this, "message", "processCallbackresult", "context", "postBackError", true);
}
string ICallbackEventHandler.GetCallbackResult()
{
return callbackResponse;
}
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
if (eventArgument.Contains("addlayer"))
{
AddLayer();
}
else if (eventArgument.Contains("removelayer"))
{
Session["layeradded"] = false;
RemoveLayer();
}
}
#region 增加图层
//增加图层
private void AddLayer()
{
MapFunctionality agsMapFunctionality = Map1.GetFunctionality(0) as MapFunctionality;
localMapResource = (MapResourceLocal)agsMapFunctionality.Resource;
//使用ArcObjects增加图层
IServerContext mapContext = localMapResource.ServerContextInfo.ServerContext;
IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)mapContext.CreateObject("esriDataSourcesFile.ShapefileWorkspaceFactory");
IFeatureWorkspace featureWorkspace = workspaceFactory.OpenFromFile(@"D:\AGS_Data\GISData", 0) as IFeatureWorkspace;
IFeatureLayer featureLayer = (IFeatureLayer)mapContext.CreateObject("esriCarto.FeatureLayer");
featureLayer.FeatureClass = featureWorkspace.OpenFeatureClass("States.shp");
featureLayer.Name = "States";
//应用着色器
ApplyRenderer(featureLayer, mapContext);
IMapServerObjects mapServerObjects = localMapResource.MapServer as IMapServerObjects;
IMap map = mapServerObjects.get_Map(localMapResource.DataFrame);
map.AddLayer(featureLayer);
map.MoveLayer(featureLayer, 0);
//刷新服务器对象 响应添加图层
localMapResource.RefreshServerObjects();
//刷新地图与Toc
Toc1.Refresh();
Map1.CallbackResults.CopyFrom(Toc1.CallbackResults);
Map1.Refresh();
callbackResponse = Map1.CallbackResults.ToString();
}
#endregion
#region 渲染图层
//设置动态要素图层的着色器对象
private void ApplyRenderer(IFeatureLayer featureLayer, IServerContext mapContext)
{
IGeoFeatureLayer geoLayer = (IGeoFeatureLayer)featureLayer;
ISimpleRenderer simpleRender = (ISimpleRenderer)mapContext.CreateObject("esriCarto.SimpleRenderer");
IRgbColor rgbColor = (IRgbColor)mapContext.CreateObject("esriDisplay.RgbColor");
rgbColor.Red = 0;
rgbColor.Green = 0;
rgbColor.Blue = 210;
esriGeometryType geometryType = featureLayer.FeatureClass.ShapeType;
if (geometryType == esriGeometryType.esriGeometryPoint)
{
ISimpleMarkerSymbol symbol = (ISimpleMarkerSymbol)mapContext.CreateObject("esriDisplay.SimpleMarkerSymbol");
symbol.Color = (IColor)rgbColor;
simpleRender.Symbol = (ISymbol)symbol;
}
else if (geometryType == esriGeometryType.esriGeometryPolyline)
{
ISimpleLineSymbol symbol = (ISimpleLineSymbol)mapContext.CreateObject("esriDisplay.SimpleLineSymbol");
symbol.Color = (IColor)rgbColor;
simpleRender.Symbol = (ISymbol)symbol;
}
else if(geometryType == esriGeometryType.esriGeometryPolygon)
{
ISimpleFillSymbol symbol = (ISimpleFillSymbol)mapContext.CreateObject("esriDisplay.SimpleFillSymbol");
symbol.Color = (IColor)rgbColor;
simpleRender.Symbol = (ISymbol)symbol;
}
else
{
throw new Exception("不能确定图层类型!");
}
geoLayer.Renderer = (IFeatureRenderer)simpleRender;
}
#endregion
#region 删除图层
//删除图层
private void RemoveLayer()
{
MapFunctionality agsMapFunctionality = Map1.GetFunctionality(0) as MapFunctionality;
localMapResource = (MapResourceLocal)agsMapFunctionality.Resource;
//通过ArcObjects 删除动态添加的图层
IMapServerObjects mapServerObjects = localMapResource.MapServer as IMapServerObjects;
IMap map = mapServerObjects.get_Map(localMapResource.DataFrame);
ILayer workingLayer = null;
IEnumLayer enumLayer = map.get_Layers(null, true);
ILayer loopLayer = null;
while ((loopLayer = enumLayer.Next()) != null)
{
if (loopLayer.Name == "States")
{
workingLayer = loopLayer;
}
}
map.DeleteLayer(workingLayer);
//刷新服务器对象,响应删除图层
localMapResource.RefreshServerObjects();
Toc1.Refresh();
Map1.CallbackResults.CopyFrom(Toc1.CallbackResults);
Map1.Refresh();
callbackResponse = Map1.CallbackResults.ToString();
}
#endregion
protected void Button1_Click(object sender, EventArgs e)
{
AddLayer();
}
protected void Button2_Click(object sender, EventArgs e)
{
RemoveLayer();
}
}