接(三)
这一节主要贴代码算了,通过代码看一下一些简单功能的实现。偶快要放假了,静不下心来写了,写得也不好。说不定贴代码效果更好。
1、获取所有图层
ESRI.ArcGIS.Server.WebControls.WebMap webmap
=
Map1.CreateWebMap();
try
{
ESRI.ArcGIS.Carto.IMapDescription descr
=
webmap.MapDescription;
ddlLayers.Items.Clear();
int
id;
for
(
int
i
=
0
;i
<=
descr.LayerDescriptions.Count
-
1
;i
++
)
{
id
=
descr.LayerDescriptions.get_Element(i).ID;
ddlLayers.Items.Add(id.ToString()
+
"
,
"
+
webmap.LayerNameFromID(id));
//
ddlLayers是一个DropDownList控件
}
if
(ddlLayers.Items.Count
>
0
)
{
ddlLayers.SelectedIndex
=
0
;
}
}
finally
{
webmap.Dispose();
}
2、通过图层的ID获取图层对象
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();
}
}
3、新建一个多边形
private
void
Map1_Polygon(
object
sender, ESRI.ArcGIS.Server.WebControls.PolygonEventArgs args)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (args.ToolName == "newpolygon")
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IFeatureLayer flayer = GetCurFeatureLayer(); //获取当前活动图层了函数,这里就不贴了,就是调用GetFeatureLayer(int lyid)函数
if (flayer == null) return;
if (flayer.FeatureClass.ShapeType != ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string sc;
sc = "<script language=javascript>alert('当前图层何类型不对!')</script>";
Page.RegisterClientScriptBlock("ShapeTypeError",sc);
return;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//生成多边形
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++)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//将多边形写入到图层中
ESRI.ArcGIS.Geodatabase.IFeature feature = flayer.FeatureClass.CreateFeature();
feature.Shape = poly as IGeometry;
feature.Store();
webmap.Refresh();
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
webmap.Dispose();
}
}
![None.gif](/Images/OutliningIndicators/None.gif)
4、矩形选择,获得选择集,并在地图上显示选中的对象。(这个功能花费了偶一天多的时间。)
private
void
Map1_DragRectangle(
object
sender, ESRI.ArcGIS.Server.WebControls.ToolEventArgs args)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
string strTool = args.ToolName.ToLower();
if (strTool=="rectsel")
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//取得当前层
if (ddlLayers.SelectedValue=="")
return;
IFeatureLayer flayer = GetCurFeatureLayer();
if (flayer == null) return;
//获得选择集
int t1=Environment.TickCount;
ESRI.ArcGIS.Server.WebControls.WebMap webmap = Map1.CreateWebMap();
ESRI.ArcGIS.Server.IServerContext ctx = webmap.ServerContext;
webmap.ManageLifetime(ctx);
ESRI.ArcGIS.Geodatabase.IWorkspace ws = (flayer.FeatureClass as ESRI.ArcGIS.Geodatabase.IDataset).Workspace;
ESRI.ArcGIS.Geometry.IEnvelope env = ctx.CreateObject("esriGeometry.Envelope") as ESRI.ArcGIS.Geometry.IEnvelope;
webmap.ManageLifetime(ws);
webmap.ManageLifetime(env);
IPoint pt = webmap.ToMapPoint(Convert.ToInt32(Request.Params.Get("maxx")),Convert.ToInt32(Request.Params.Get("maxy")));
env.XMax = pt.X;
env.YMin = pt.Y;
pt = webmap.ToMapPoint(Convert.ToInt32(Request.Params.Get("minx")),Convert.ToInt32(Request.Params.Get("miny")));
env.XMin= pt.X;
env.YMax = pt.Y;
ESRI.ArcGIS.Geodatabase.ISpatialFilter filter = ctx.CreateObject("esriGeodatabase.SpatialFilter") as ESRI.ArcGIS.Geodatabase.ISpatialFilter;
webmap.ManageLifetime(filter);
filter.SpatialRel = ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelIntersects;
filter.Geometry = env as ESRI.ArcGIS.Geometry.IGeometry;
filter.GeometryField = flayer.FeatureClass.ShapeFieldName;
ESRI.ArcGIS.Geodatabase.ISelectionSet sset = flayer.FeatureClass.Select(filter,ESRI.ArcGIS.Geodatabase.esriSelectionType.esriSelectionTypeSnapshot,ESRI.ArcGIS.Geodatabase.esriSelectionOption.esriSelectionOptionNormal,ws);
int t2=Environment.TickCount;
int t3=t2-t1; //t3是查询响应的时间,可以用来测试一下性能,呵呵
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//显示选择集
int id;
ESRI.ArcGIS.Geodatabase.IEnumIDs ids;
ids = sset.IDs;
webmap.ManageLifetime(ids);
ids.Reset();
ESRI.ArcGIS.Geodatabase.IFIDSet fidset = ctx.CreateObject("esriGeodatabase.FIDSet") as ESRI.ArcGIS.Geodatabase.IFIDSet;
id = ids.Next();
while (id>=0)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
fidset.Add(id);
id =ids.Next();
}
IMapDescription desc = webmap.MapDescription as IMapDescription;
webmap.ManageLifetime(desc);
ILayerDescription ldesc = desc.LayerDescriptions.get_Element(flyid);
webmap.ManageLifetime(ldesc);
ldesc.SelectionFeatures = fidset;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//将选择信息保存在session中
Session["selection"] = sset;
Session["layerid"] = flyid;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
webmap.Refresh();
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
webmap.Dispose();
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
![None.gif](/Images/OutliningIndicators/None.gif)
5、删除选中的对象
private
void
DeleteSel()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (Session["layerid"]==null) return;
if (Session["selection"] == null) return;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
int layerid = (int)Session["layerid"];
if (layerid == -1) return;
ESRI.ArcGIS.Geodatabase.ISelectionSet sset = Session["selection"] as ESRI.ArcGIS.Geodatabase.ISelectionSet;
if (sset == null) return;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
WebMap webmap = Map1.CreateWebMap();
IFeatureLayer layer = GetFeatureLayer(layerid);
if (layer==null) return;
webmap.ManageLifetime(layer);
webmap.ManageLifetime(sset);
ESRI.ArcGIS.Geodatabase.IEnumIDs ids = sset.IDs;
webmap.ManageLifetime(ids);
ids.Reset();
int id;
id = ids.Next();
ESRI.ArcGIS.Geodatabase.IFeature feature;
ESRI.ArcGIS.Server.IServerContext ctx = webmap.ServerContext;
webmap.ManageLifetime(ctx);
//将selectionset转化为featurecursor对象
ESRI.ArcGIS.Geodatabase.IFeatureCursor fcursor;
ESRI.ArcGIS.Geodatabase.ICursor cursor;
sset.Search(null,false,out cursor);
fcursor = cursor as ESRI.ArcGIS.Geodatabase.IFeatureCursor;
ESRI.ArcGIS.esriSystem.ISet pDeleteSet = ctx.CreateObject("esriSystem.Set") as ESRI.ArcGIS.esriSystem.Set;
webmap.ManageLifetime(pDeleteSet);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//设置ISet对象
feature = fcursor.NextFeature();
while (feature != null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pDeleteSet.Add(feature);
feature = fcursor.NextFeature();
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
ESRI.ArcGIS.Geodatabase.IFeatureEdit fedit;
pDeleteSet.Reset();
fedit = pDeleteSet.Next() as ESRI.ArcGIS.Geodatabase.IFeatureEdit;
while (fedit != null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
fedit.DeleteSet(pDeleteSet);
fedit = pDeleteSet.Next() as ESRI.ArcGIS.Geodatabase.IFeatureEdit;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Session.Remove("layerid");
Session.Remove("selection");
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
webmap.Refresh();
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
webmap.Dispose();
}
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)