(转)ArcGis Server开发Web GIS新手体验(四)

这一节主要贴代码算了,通过代码看一下一些简单功能的实现。偶快要放假了,静不下心来写了,写得也不好。说不定贴代码效果更好。

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、新建一个多边形

None.gif  private void Map1_Polygon(object sender, ESRI.ArcGIS.Server.WebControls.PolygonEventArgs args)
ExpandedBlockStart.gif ContractedBlock.gif   dot.gif{
InBlock.gif   if (args.ToolName == "newpolygon")
ExpandedSubBlockStart.gif ContractedSubBlock.gif    dot.gif{
InBlock.gif    IFeatureLayer flayer = GetCurFeatureLayer();        //获取当前活动图层了函数,这里就不贴了,就是调用GetFeatureLayer(int lyid)函数
InBlock.gif    if (flayer == null) return;
InBlock.gif    
InBlock.gif    if (flayer.FeatureClass.ShapeType != ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon) 
ExpandedSubBlockStart.gif ContractedSubBlock.gif     dot.gif{
InBlock.gif     string sc;
InBlock.gif     sc = "<script language=javascript>alert('当前图层何类型不对!')</script>";
InBlock.gif     Page.RegisterClientScriptBlock("ShapeTypeError",sc);
InBlock.gif     return;
ExpandedSubBlockEnd.gif    }
InBlock.gif
InBlock.gif    //生成多边形
InBlock.gif    ESRI.ArcGIS.Server.IServerContext context;
InBlock.gif    ESRI.ArcGIS.Server.WebControls.WebMap webmap = Map1.CreateWebMap();
InBlock.gif    webmap.ManageLifetime(flayer);
InBlock.gif    context = webmap.ServerContext;
InBlock.gif    ESRI.ArcGIS.Geometry.IPolygon poly = context.CreateObject("esriGeometry.Polygon") as ESRI.ArcGIS.Geometry.IPolygon; //'new ag.PolygonClass();
InBlock.gif    webmap.ManageLifetime(poly);
InBlock.gif    ESRI.ArcGIS.Geometry.IPoint pt;
InBlock.gif    ESRI.ArcGIS.Geometry.IGeometryCollection ringcol = context.CreateObject("esriGeometry.Polygon") as ESRI.ArcGIS.Geometry.IGeometryCollection;// new  PolygonClass();
InBlock.gif    webmap.ManageLifetime(ringcol);
InBlock.gif    ESRI.ArcGIS.Geometry.IPointCollection ptcol =context.CreateObject("esriGeometry.Ring") as ESRI.ArcGIS.Geometry.IPointCollection;// new RingClass();
InBlock.gif    webmap.ManageLifetime(ptcol);
InBlock.gif    object obj=Type.Missing;
InBlock.gif    for (int i=0;i<=args.Vectors.Length-1;i++)
ExpandedSubBlockStart.gif ContractedSubBlock.gif     dot.gif{
InBlock.gif     pt = webmap.ToMapPoint(args.Vectors[i].X,args.Vectors[i].Y);
InBlock.gif     ptcol.AddPoint(pt,ref obj,ref obj);
ExpandedSubBlockEnd.gif    }
InBlock.gif    ringcol.AddGeometry(ptcol as IGeometry,ref obj,ref obj);    
InBlock.gif    poly = ringcol as IPolygon;    
InBlock.gif
InBlock.gif    //将多边形写入到图层中
InBlock.gif    ESRI.ArcGIS.Geodatabase.IFeature feature =  flayer.FeatureClass.CreateFeature();
InBlock.gif    feature.Shape = poly as IGeometry;
InBlock.gif    feature.Store();
InBlock.gif    webmap.Refresh();
InBlock.gif
InBlock.gif    webmap.Dispose();
ExpandedSubBlockEnd.gif   }
ExpandedBlockEnd.gif  }
None.gif
None.gif


4、矩形选择,获得选择集,并在地图上显示选中的对象。(这个功能花费了偶一天多的时间。)

None.gif  private void Map1_DragRectangle(object sender, ESRI.ArcGIS.Server.WebControls.ToolEventArgs args)
ExpandedBlockStart.gif ContractedBlock.gif   dot.gif{
InBlock.gif   string strTool = args.ToolName.ToLower();
InBlock.gif   if (strTool=="rectsel") 
ExpandedSubBlockStart.gif ContractedSubBlock.gif    dot.gif{
InBlock.gif    //取得当前层
InBlock.gif    if (ddlLayers.SelectedValue=="")
InBlock.gif     return;
InBlock.gif    IFeatureLayer flayer = GetCurFeatureLayer(); 
InBlock.gif    if (flayer == null) return;
InBlock.gif    
InBlock.gif    //获得选择集
InBlock.gif    int t1=Environment.TickCount;
InBlock.gif    ESRI.ArcGIS.Server.WebControls.WebMap webmap = Map1.CreateWebMap();
InBlock.gif    ESRI.ArcGIS.Server.IServerContext ctx = webmap.ServerContext;
InBlock.gif    webmap.ManageLifetime(ctx);
InBlock.gif    ESRI.ArcGIS.Geodatabase.IWorkspace ws = (flayer.FeatureClass as ESRI.ArcGIS.Geodatabase.IDataset).Workspace;
InBlock.gif    ESRI.ArcGIS.Geometry.IEnvelope env = ctx.CreateObject("esriGeometry.Envelope") as ESRI.ArcGIS.Geometry.IEnvelope;
InBlock.gif    webmap.ManageLifetime(ws);
InBlock.gif    webmap.ManageLifetime(env);
InBlock.gif    IPoint pt = webmap.ToMapPoint(Convert.ToInt32(Request.Params.Get("maxx")),Convert.ToInt32(Request.Params.Get("maxy")));
InBlock.gif    env.XMax = pt.X;
InBlock.gif    env.YMin = pt.Y;
InBlock.gif    pt = webmap.ToMapPoint(Convert.ToInt32(Request.Params.Get("minx")),Convert.ToInt32(Request.Params.Get("miny")));
InBlock.gif    env.XMin= pt.X;
InBlock.gif    env.YMax = pt.Y;    
InBlock.gif    ESRI.ArcGIS.Geodatabase.ISpatialFilter filter = ctx.CreateObject("esriGeodatabase.SpatialFilter") as ESRI.ArcGIS.Geodatabase.ISpatialFilter;
InBlock.gif    webmap.ManageLifetime(filter);
InBlock.gif    filter.SpatialRel = ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelIntersects;
InBlock.gif    filter.Geometry = env as ESRI.ArcGIS.Geometry.IGeometry;
InBlock.gif    filter.GeometryField = flayer.FeatureClass.ShapeFieldName;
InBlock.gif    ESRI.ArcGIS.Geodatabase.ISelectionSet sset = flayer.FeatureClass.Select(filter,ESRI.ArcGIS.Geodatabase.esriSelectionType.esriSelectionTypeSnapshot,ESRI.ArcGIS.Geodatabase.esriSelectionOption.esriSelectionOptionNormal,ws);
InBlock.gif   
InBlock.gif    int t2=Environment.TickCount;
InBlock.gif    int t3=t2-t1;    //t3是查询响应的时间,可以用来测试一下性能,呵呵    
InBlock.gif
InBlock.gif    //显示选择集     
InBlock.gif    int id;
InBlock.gif    ESRI.ArcGIS.Geodatabase.IEnumIDs ids;
InBlock.gif    ids = sset.IDs;
InBlock.gif    webmap.ManageLifetime(ids);
InBlock.gif    ids.Reset();
InBlock.gif    ESRI.ArcGIS.Geodatabase.IFIDSet fidset = ctx.CreateObject("esriGeodatabase.FIDSet") as ESRI.ArcGIS.Geodatabase.IFIDSet;    
InBlock.gif    id = ids.Next(); 
InBlock.gif    while (id>=0)
ExpandedSubBlockStart.gif ContractedSubBlock.gif     dot.gif{
InBlock.gif     fidset.Add(id);
InBlock.gif     id =ids.Next();
ExpandedSubBlockEnd.gif    }
InBlock.gif    IMapDescription desc = webmap.MapDescription as IMapDescription;
InBlock.gif    webmap.ManageLifetime(desc);
InBlock.gif    ILayerDescription ldesc = desc.LayerDescriptions.get_Element(flyid);
InBlock.gif    webmap.ManageLifetime(ldesc);
InBlock.gif    ldesc.SelectionFeatures = fidset;
InBlock.gif
InBlock.gif    //将选择信息保存在session中
InBlock.gif    Session["selection"] = sset;
InBlock.gif    Session["layerid"] = flyid;
InBlock.gif
InBlock.gif    webmap.Refresh();
InBlock.gif
InBlock.gif    webmap.Dispose();
InBlock.gif     
ExpandedSubBlockEnd.gif   }
InBlock.gif
ExpandedBlockEnd.gif  }
None.gif
None.gif


5、删除选中的对象

None.gif  private void DeleteSel()
ExpandedBlockStart.gif ContractedBlock.gif   dot.gif{
InBlock.gif   if (Session["layerid"]==null) return;
InBlock.gif   if (Session["selection"] == null) return;
InBlock.gif
InBlock.gif   int layerid = (int)Session["layerid"];
InBlock.gif   if (layerid == -1) return;
InBlock.gif   ESRI.ArcGIS.Geodatabase.ISelectionSet sset = Session["selection"] as ESRI.ArcGIS.Geodatabase.ISelectionSet;
InBlock.gif   if (sset == null) return;
InBlock.gif
InBlock.gif   WebMap webmap = Map1.CreateWebMap();
InBlock.gif   IFeatureLayer layer = GetFeatureLayer(layerid);
InBlock.gif   if (layer==null) return;
InBlock.gif   webmap.ManageLifetime(layer);
InBlock.gif   webmap.ManageLifetime(sset);
InBlock.gif   ESRI.ArcGIS.Geodatabase.IEnumIDs ids = sset.IDs;
InBlock.gif   webmap.ManageLifetime(ids);
InBlock.gif   ids.Reset();
InBlock.gif   int id;
InBlock.gif   id = ids.Next();
InBlock.gif   ESRI.ArcGIS.Geodatabase.IFeature feature;
InBlock.gif   ESRI.ArcGIS.Server.IServerContext ctx = webmap.ServerContext;
InBlock.gif   webmap.ManageLifetime(ctx);
InBlock.gif   //将selectionset转化为featurecursor对象
InBlock.gif   ESRI.ArcGIS.Geodatabase.IFeatureCursor fcursor;
InBlock.gif   ESRI.ArcGIS.Geodatabase.ICursor cursor;
InBlock.gif   sset.Search(null,false,out cursor);
InBlock.gif   fcursor =  cursor as ESRI.ArcGIS.Geodatabase.IFeatureCursor;
InBlock.gif   ESRI.ArcGIS.esriSystem.ISet pDeleteSet = ctx.CreateObject("esriSystem.Set") as ESRI.ArcGIS.esriSystem.Set; 
InBlock.gif   webmap.ManageLifetime(pDeleteSet);
InBlock.gif
InBlock.gif   //设置ISet对象
InBlock.gif   feature = fcursor.NextFeature();
InBlock.gif   while (feature != null)
ExpandedSubBlockStart.gif ContractedSubBlock.gif    dot.gif{
InBlock.gif    pDeleteSet.Add(feature);
InBlock.gif    feature = fcursor.NextFeature();
ExpandedSubBlockEnd.gif   }
InBlock.gif
InBlock.gif   ESRI.ArcGIS.Geodatabase.IFeatureEdit fedit;
InBlock.gif   pDeleteSet.Reset();
InBlock.gif   fedit = pDeleteSet.Next() as ESRI.ArcGIS.Geodatabase.IFeatureEdit;
InBlock.gif   while (fedit != null)
ExpandedSubBlockStart.gif ContractedSubBlock.gif    dot.gif{
InBlock.gif    fedit.DeleteSet(pDeleteSet);
InBlock.gif    fedit = pDeleteSet.Next() as ESRI.ArcGIS.Geodatabase.IFeatureEdit;
ExpandedSubBlockEnd.gif   }
InBlock.gif
InBlock.gif   Session.Remove("layerid");
InBlock.gif   Session.Remove("selection");
InBlock.gif
InBlock.gif   webmap.Refresh();
InBlock.gif
InBlock.gif   webmap.Dispose();
ExpandedBlockEnd.gif  }

转载于:https://www.cnblogs.com/gentlewolf/archive/2007/06/29/800592.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值