arcgis server 9.2代码阅读笔记二:在页面上动态加载图层

代码来源 ARCGIS 9.2例子

// Copyright 2006 ESRI
//
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
//
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// AUTHER:糊涂虫 2007.9.20
// See use restrictions at /arcgis/developerkit/userestrictions.

using System;
using System.Data;
using System.Configuration;
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.Utility;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;

public partial class _Default : System.Web.UI.Page
{
//创建数据表,用于数据绑定
DataTable layertable;

protected void Page_Init(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
layertable = new DataTable("layertable");
DataColumn dc1 = new DataColumn("layername");
DataColumn dc2 = new DataColumn("layerpath");
layertable.Columns.Add(dc1);
layertable.Columns.Add(dc2);
//读取文件(是一些基本功能)
String rootPath = Server.MapPath("~");

System.IO.StreamReader sr = new System.IO.StreamReader(rootPath + "/layers.catalog");
string delimStr = "=";
char[] delimiter = delimStr.ToCharArray();
string[] readparts = null;
string readstring = null;

while (sr.Peek() != -1)
{
readstring = sr.ReadLine();
if (!readstring.Contains("="))
break;

readparts = readstring.Split(delimiter);
DataRow dr = layertable.NewRow();
dr[0] = readparts[0];
dr[1] = readparts[1];
layertable.Rows.Add(dr);
}
sr.Close();
}
}

protected void Page_PreRender(object sender, EventArgs e)
{
//数据绑定
DropDownList1.DataSource = layertable;
DropDownList1.DataTextField = "layername";
DropDownList1.DataValueField = "layerpath";
DropDownList1.DataBind();

if (!Page.IsPostBack)
{
if (Session.IsNewSession)
{
Session.Add("addedlayers", false);
Session.Add("addedlayersid", null);
//创建server上下文
IServerContext mapContext = this.getServerContext();
//使用server上下文来定义地图服务
IMapServer mapServer = mapContext.ServerObject as IMapServer;
//获得地图服务信息
IMapServerInfo mapInfo = mapServer.GetServerInfo(mapServer.DefaultMapName);
//获得地图描述信息
IMapDescription mapDesc = mapInfo.DefaultMapDescription;
//放到session中去
SetSessionMapDescription(mapContext, mapDesc);
//创建地图
CreateMapImage(mapContext, mapDesc);
//释放调server上下文
mapContext.ReleaseContext();

}
else
{
IServerContext mapContext = this.getServerContext();
mapContext = this.getServerContext();

IMapDescription mapDesc = GetSessionMapDescription(mapContext);
UpdateDataLayers(mapContext);

CreateMapImage(mapContext, mapDesc);

RemoveLayers(mapContext);
mapContext.ReleaseContext();
}
}
else
{
//if a postback, do something here during load
}
}

//添加图层
private void AddTheLayers(IServerContext in_mapcontext)
{
IMapServer ms = (IMapServer)in_mapcontext.ServerObject;

IMapServerObjects2 mso = (IMapServerObjects2)ms;
string mapname = ms.DefaultMapName;
IMap map = mso.get_Map(mapname);
//创建一个新的图层,和它想类似的还有IFeatureClass
IFeatureLayer layer = (IFeatureLayer)in_mapcontext.CreateObject("esriCarto.FeatureLayer");
//IFeatureLayer layer = (IFeatureLayer)in_mapcontext.CreateObject("{E663A651-8AAD-11D0-BEC7-00805F7C4268}");
//创建一个用于打开和创建工作空间的一个工厂接口类
//IFeatureLayer IFeatureClass IWorkspaceFactory 在AE中都有,他们用于打开和操作数据空间
IWorkspaceFactory factory = (IWorkspaceFactory)in_mapcontext.CreateObject("esriDataSourcesFile.ShapefileWorkspaceFactory");

ListItem listitem = DropDownList1.SelectedItem;
int lastslash = listitem.Value.LastIndexOf("/");
string filepath = listitem.Value.Substring(0, lastslash);
string filename = listitem.Value.Substring(lastslash + 1);

IWorkspace ws = factory.OpenFromFile(filepath, 0);
IFeatureWorkspace fws = (IFeatureWorkspace)ws;
layer.FeatureClass = fws.OpenFeatureClass(filename);
layer.Name = listitem.Text;
//IGeoFeatureLayer是由IFeatureLayer继承而来,IFeatureLayer是由ILayer继承而来,IGeoFeatureLayer比IFeatureLayer多了标注方面的东西
IGeoFeatureLayer iglayer = (IGeoFeatureLayer)layer;
//IFeatureRenderer中定义了一个叫做Draw的方法,其实ArcObjects中各种各样的渲染效果就是通过这个Draw方法画出来 的,因为各个渲染效果都实现了IFeatureRenderer接口。所以要实现"GroupRenderer"效果,只要实现了 IFeatureRenderer接口就可以了。
IFeatureRenderer renderer = iglayer.Renderer;
//创建简单渲染
ISimpleRenderer isr = (ISimpleRenderer)renderer;
IRgbColor irgbc = (IRgbColor)in_mapcontext.CreateObject("esriDisplay.RgbColor");
irgbc.Red = 0;
irgbc.Green = 0;
irgbc.Blue = 210;
//获得类别,各自进行设置。这里比较奇怪的是创建的ifs并没有被添加到图层中去,不是很明白,请教各位。
ESRI.ArcGIS.Geometry.esriGeometryType featype = layer.FeatureClass.ShapeType;
if (featype == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
{
ISimpleMarkerSymbol ifs = (ISimpleMarkerSymbol)isr.Symbol;
ifs.Color = (IColor)irgbc;
}
else if (featype == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
{
ISimpleLineSymbol ifs = (ISimpleLineSymbol)isr.Symbol;
ifs.Color = (IColor)irgbc;
}
else if (featype == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
{
ISimpleFillSymbol ifs = (ISimpleFillSymbol)isr.Symbol;
ifs.Color = (IColor)irgbc;
}
else
{
throw new Exception("No renderer or symbol selected. Shape type undetermined.");
}
//增加图层
map.AddLayer(layer);
mso.RefreshServerObjects();

Session["addedlayersid"] = mso.get_LayerID(mapname, layer);
}

private void CreateMapImage(IServerContext mapContext, IMapDescription md)
{
//分别创建图像类别 图像描述 图像显示
IImageType imageType;
IImageDescription imageDesc;
IImageDisplay imageDisp;

imageType = mapContext.CreateObject("esriCarto.ImageType") as IImageType;
imageDesc = mapContext.CreateObject("esriCarto.ImageDescription") as ImageDescription;
imageDisp = mapContext.CreateObject("esriCarto.ImageDisplay") as ImageDisplay;

imageType.Format = esriImageFormat.esriImageJPG;
imageType.ReturnType = esriImageReturnType.esriImageReturnURL;

imageDisp.Height = (int)imgOutput.Height.Value;
imageDisp.Width = (int)imgOutput.Width.Value;
imageDisp.DeviceResolution = 96;

imageDesc.Display = imageDisp;
imageDesc.Type = imageType;
//创建地图服务
IMapServer mapServer = (IMapServer)mapContext.ServerObject;

IImageResult mapImage = mapServer.ExportMapImage(md, imageDesc);
imgOutput.ImageUrl = mapImage.URL;
imgOutput.Visible = true;

}
//创建session
private void SetSessionMapDescription(IServerContext in_mapcontext, IMapDescription in_mapdesc)
{
string md = in_mapcontext.SaveObject(in_mapdesc);
Session.Add("md", md);
}
//获得session
private IMapDescription GetSessionMapDescription(IServerContext in_mapcontext)
{
string stringDesc = Session["md"] as string;
IMapDescription mapDesc = null;

if (stringDesc == null)
{
IMapServer mapServer = in_mapcontext.ServerObject as IMapServer;
IMapServerInfo mapInfo = mapServer.GetServerInfo(mapServer.DefaultMapName);
mapDesc = mapInfo.DefaultMapDescription;
}
else
{
mapDesc = in_mapcontext.LoadObject(stringDesc) as IMapDescription;
}

return mapDesc;
}
//创建server上下文,在这个上下文中,运用IServerObjectManager对象管理其生命周期
private IServerContext getServerContext()
{
string servername = "localhost";
string mapserverobject = "USA_data";
IServerObjectManager serverManager;

if (Session["SOM"] == null)
{
// *** Using Web ADF Common API
ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection gisconnection = new ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection();
gisconnection.Host = servername;
gisconnection.Connect();

serverManager = gisconnection.ServerObjectManager;
Session.Add("SOM", serverManager);

}
else
{
serverManager = Session["SOM"] as IServerObjectManager;
}

// *** Change the map server object name as needed
IServerContext mapContext = serverManager.CreateServerContext(mapserverobject, "MapServer");

return mapContext;

}

private void RemoveLayers(IServerContext in_mapcontext)
{
//判断是否添加了图层
bool tf = (bool)Session["addedlayers"];

if (tf)
{
IMapServer ms = (IMapServer)in_mapcontext.ServerObject;

IMapServerObjects mso = (IMapServerObjects)ms;

IMap map = mso.get_Map(ms.DefaultMapName);
//通过地图服务获得图层
ILayer layer = map.get_Layer(0);

map.DeleteLayer(layer);
mso.RefreshServerObjects();
}
}

private void UpdateDataLayers(IServerContext mapContext)
{
bool tf = (bool)Session["addedlayers"];

if (tf)
{
AddTheLayers(mapContext);

// Update dynamic data id, after initial addition of dynamic layer
IMapDescription mapDesc = GetSessionMapDescription(mapContext);
ILayerDescription newLayerDesc = mapDesc.LayerDescriptions.get_Element(0);
newLayerDesc.ID = (int)Session["addedlayersid"];
SetSessionMapDescription(mapContext, mapDesc);
}
}

protected void btnAddLayer_Click(object sender, EventArgs e)
{
Session.Add("addedlayers", true);

IServerContext mapContext = this.getServerContext();

UpdateDataLayers(mapContext);

IMapServer ms = (IMapServer)mapContext.ServerObject;

IMapServerInfo mapInfo = ms.GetServerInfo(ms.DefaultMapName);

IMapDescription NEWmapDesc = mapInfo.DefaultMapDescription;

CreateMapImage(mapContext, NEWmapDesc);

SetSessionMapDescription(mapContext, NEWmapDesc);

RemoveLayers(mapContext);

mapContext.ReleaseContext();

}

protected void btnRefresh_Click(object sender, EventArgs e)
{

IServerContext mapContext = this.getServerContext();
UpdateDataLayers(mapContext);

/* Test to see actual id of dynamic layer, compare with MapDescription stored in session (also used to generate map image).
*
IMapServer ms = (IMapServer)mapContext.ServerObject;
IMapServerInfo mapInfo = ms.GetServerInfo(ms.DefaultMapName);
IMapDescription NEWmapDesc = mapInfo.DefaultMapDescription;
ILayerDescription NEWLayerDesc = NEWmapDesc.LayerDescriptions.get_Element(0);
*/

IMapDescription mapDesc = GetSessionMapDescription(mapContext);
IMapArea mapArea = mapDesc.MapArea;
//创建IMapExtent 用于描述地图的范围.放大缩小地图
IMapExtent mapExtent = mapArea as IMapExtent;
//创建IEnvelope,用来确定一个空间范围,可以做空间查询
IEnvelope env = (IEnvelope)mapContext.CreateObject("esriGeometry.Envelope");
env.PutCoords(-120, 30, -50, 50);

mapExtent.Extent = env;

CreateMapImage(mapContext, mapDesc);

SetSessionMapDescription(mapContext, mapDesc);

RemoveLayers(mapContext);

mapContext.ReleaseContext();
}
}

这里比较奇怪的是创建的ifs并没有被添加到图层中去,不是很明白,请教各位。自己现在只是一个初学者,我在这里只是抛砖引玉,希望大家能发出更好的博客文章出来,同时支持原创。如有错误请交流jiyanliangcs@163.com。 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhangjie_xiaoke/archive/2008/03/05/2150732.aspx

posted @ 2009-08-06 17:19  googlegis  阅读(180)  评论(0编辑  收藏  举报

坐标合肥,非典型GIS开发人员 GitHub