1. 绘直线
private void Map1_Line(object sender, LineEventArgs args)
{
if (args.ToolName == "Line")
{
IFeatureLayer flayer = GetFeatureLayer(ddlLayers.SelectedIndex); //获取当前活动图层了函数,就是调用GetFeatureLayer(int lyid)函数
if (flayer == null) return;
if (flayer.FeatureClass.ShapeType != ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
{
string sc;
sc = "<script language=javascript>alert('当前图层类型不对!')</script>";
Page.RegisterClientScriptBlock("ShapeTypeError",sc);
return;
}
//生成直线
ESRI.ArcGIS.Server.IServerContext context;
ESRI.ArcGIS.Server.WebControls.WebMap webmap = Map1.CreateWebMap();
webmap.ManageLifetime(flayer);
context = webmap.ServerContext;
ESRI.ArcGIS.Geometry.IPolyline Polyline = context.CreateObject("esriGeometry.Polyline") as ESRI.ArcGIS.Geometry.IPolyline;
webmap.ManageLifetime(Polyline);
ESRI.ArcGIS.Geometry.IPoint pt;
ESRI.ArcGIS.Geometry.IGeometryCollection ringcol = context.CreateObject("esriGeometry.Polyline") as ESRI.ArcGIS.Geometry.IGeometryCollection;
webmap.ManageLifetime(ringcol);
ESRI.ArcGIS.Geometry.IPointCollection ptcol =context.CreateObject("esriGeometry.Path") as ESRI.ArcGIS.Geometry.IPointCollection;// new PathClass();
webmap.ManageLifetime(ptcol);
object obj=Type.Missing;
pt = webmap.ToMapPoint(args.BeginPoint.X,args.BeginPoint.Y);
ptcol.AddPoint(pt,ref obj,ref obj);
pt = webmap.ToMapPoint(args.EndPoint.X,args.EndPoint.Y);
ptcol.AddPoint(pt,ref obj,ref obj);
ringcol.AddGeometry(ptcol as IGeometry,ref obj,ref obj);
Polyline =ringcol as IPolyline;
//将多直线到图层中
ESRI.ArcGIS.Geodatabase.IFeature feature = flayer.FeatureClass.CreateFeature();
feature.Shape = Polyline as IGeometry;
feature.Store();
webmap.Refresh();
webmap.Dispose();
}
}
2. 绘折线
private void Map1_Polyline(object sender, PolylineEventArgs args)
{
if (args.ToolName == "Polyline")
{
IFeatureLayer flayer = GetFeatureLayer(ddlLayers.SelectedIndex); //获取当前活动图层了函数,就是调用GetFeatureLayer(int lyid)函数
if (flayer == null) return;
if (flayer.FeatureClass.ShapeType != ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline)
{
string sc;
sc = "<script language=javascript>alert('当前图层类型不对!')</script>";
Page.RegisterClientScriptBlock("ShapeTypeError",sc);
return;
}
//生成直线
ESRI.ArcGIS.Server.IServerContext context;
ESRI.ArcGIS.Server.WebControls.WebMap webmap = Map1.CreateWebMap();
webmap.ManageLifetime(flayer);
context = webmap.ServerContext;
ESRI.ArcGIS.Geometry.IPolyline Polyline = context.CreateObject("esriGeometry.Polyline") as ESRI.ArcGIS.Geometry.IPolyline;
webmap.ManageLifetime(Polyline);
ESRI.ArcGIS.Geometry.IPoint pt;
ESRI.ArcGIS.Geometry.IGeometryCollection ringcol = context.CreateObject("esriGeometry.Polyline") as ESRI.ArcGIS.Geometry.IGeometryCollection;
webmap.ManageLifetime(ringcol);
ESRI.ArcGIS.Geometry.IPointCollection ptcol =context.CreateObject("esriGeometry.Path") as ESRI.ArcGIS.Geometry.IPointCollection;// new PathClass();
webmap.ManageLifetime(ptcol);
object obj=Type.Missing;
for (int i=0;i<=args.Vectors.Length-1;i++)
{
pt = webmap.ToMapPoint(args.Vectors[i].X,args.Vectors[i].Y);
ptcol.AddPoint(pt,ref obj,ref obj);
}
ringcol.AddGeometry(ptcol as IGeometry,ref obj,ref obj);
Polyline =ringcol as IPolyline;
//将折线保存到图层中
ESRI.ArcGIS.Geodatabase.IFeature feature = flayer.FeatureClass.CreateFeature();
feature.Shape = Polyline as IGeometry;
feature.Store();
webmap.Refresh();
webmap.Dispose();
}
}
3. 绘多边形
private void Map1_Polygon(object sender, PolygonEventArgs args)
{
if (args.ToolName == "Polygon")
{
IFeatureLayer flayer = GetFeatureLayer(ddlLayers.SelectedIndex); //获取当前活动图层了函数,就是调用GetFeatureLayer(int lyid)函数
if (flayer == null) return;
if (flayer.FeatureClass.ShapeType != ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
{
string sc;
sc = "<script language=javascript>alert('当前图层类型不对!')</script>";
Page.RegisterClientScriptBlock("ShapeTypeError",sc);
return;
}
//生成多边形
ESRI.ArcGIS.Server.IServerContext context;
ESRI.ArcGIS.Server.WebControls.WebMap webmap = Map1.CreateWebMap();
webmap.ManageLifetime(flayer);
context = webmap.ServerContext;
ESRI.ArcGIS.Geometry.IPolygon poly = context.CreateObject("esriGeometry.Polygon") as ESRI.ArcGIS.Geometry.IPolygon; //'new ag.PolygonClass();
webmap.ManageLifetime(poly);
ESRI.ArcGIS.Geometry.IPoint pt;
ESRI.ArcGIS.Geometry.IGeometryCollection ringcol = context.CreateObject("esriGeometry.Polygon") as ESRI.ArcGIS.Geometry.IGeometryCollection;// new PolygonClass();
webmap.ManageLifetime(ringcol);
ESRI.ArcGIS.Geometry.IPointCollection ptcol =context.CreateObject("esriGeometry.Ring") as ESRI.ArcGIS.Geometry.IPointCollection;// new RingClass();
webmap.ManageLifetime(ptcol);
object obj=Type.Missing;
for (int i=0;i<=args.Vectors.Length-1;i++)
{
pt = webmap.ToMapPoint(args.Vectors[i].X,args.Vectors[i].Y);
ptcol.AddPoint(pt,ref obj,ref obj);
}
ringcol.AddGeometry(ptcol as IGeometry,ref obj,ref obj);
poly = ringcol as IPolygon;
//将多边形写入到图层中
ESRI.ArcGIS.Geodatabase.IFeature feature = flayer.FeatureClass.CreateFeature();
feature.Shape = poly as IGeometry;
feature.Store();
webmap.Refresh();
webmap.Dispose();
}
}
4. GetFeatureLayer(int lyid) 函数
private IFeatureLayer GetFeatureLayer(int lyid)
{
WebMap webmap = Map1.CreateWebMap();
try
{
ILayer layer = (webmap.MapServer as IMapServerObjects).get_Layer(webmap.DataFrame,lyid);
if (layer==null)
return null;
else
{
return (layer as IFeatureLayer);
}
}
finally
{
webmap.Dispose();
}
}
5. 工作区编辑函数 StartEdit()
private void StartEdit()
{
using (WebMap webMap =Map1.CreateWebMap() )
{
IServerContext sc=webMap.ServerContext;
IMapServer map = sc.ServerObject as IMapServer;
IMapServerObjects mapobj = map as IMapServerObjects;
IMap fgmap = mapobj.get_Map(webMap.DataFrame);
IFeatureLayer fl = fgmap.get_Layer(ddlLayers.SelectedIndex) as IFeatureLayer;
IDataset ds = fl.FeatureClass as IDataset;
IWorkspaceEdit ws = ds.Workspace as IWorkspaceEdit;
ws.StartEditing(true);
ws.StartEditOperation();
}
}
6. 工作区停止编辑数据保存函数SaveEdit();
private void SaveEdit()
{
using (WebMap webMap =Map1.CreateWebMap() )
{
IServerContext sc=webMap.ServerContext;
IMapServer map = sc.ServerObject as IMapServer;
IMapServerObjects mapobj = map as IMapServerObjects;
IMap fgmap = mapobj.get_Map(webMap.DataFrame);
IFeatureLayer fl = fgmap.get_Layer(ddlLayers.SelectedIndex) as IFeatureLayer;
IDataset ds = fl.FeatureClass as IDataset;
IWorkspaceEdit ws = ds.Workspace as IWorkspaceEdit;
ws.StartEditOperation();
ws.StopEditing(true);
}
}
7. 绘图方法初始化设置
private void InitializeComponent()
{
this.Toolbar1.CommandClick += new ESRI.ArcGIS.Server.WebControls.ToolbarCommandClickEventHandler(this.Toolbar1_CommandClick);
this.Map1.Polygon += new ESRI.ArcGIS.Server.WebControls.MapPolygonEventHandler(this.Map1_Polygon);
this.Map1.Polyline += new ESRI.ArcGIS.Server.WebControls.MapPolylineEventHandler(this.Map1_Polyline);
this.Map1.Line += new ESRI.ArcGIS.Server.WebControls.MapLineEventHandler(this.Map1_Line);
this.Load += new System.EventHandler(this.Page_Load);
this.Map1.Point+=new MapPointEventHandler(Map1_Point);
}