zoukankan      html  css  js  c++  java
  • AE方法——查询(一)

    查询是GIS不可或缺的功能,今天我写一下我近期对查询的理解。先说说属性查询;

    A 查询主要涉及接口: 1.IQueryFilter 通过设置这个接口的WhereClause属性来作为查询条件,本属性的语句为SQL的where后的条件语句。

    2.IFeatureLayer 矢量图层,一般由ILayer进行转化。该接口的Search方法进行查询,Search方法的参数有两个,前面的是IQueryFilter对象 ,后面的是bool值,一般设为false.

    3.IFeature 要素接口,用来接收查询出来的要素。

    4.IFeatureCursor 游标接口,通过Search进行查询,可以讲结果保存在这里,从而利用NextFeature方法,遍历所有要素。

    B 基本的思想 获取图层,设置条件,进行查询,显示要素类。

    C 如何获取IFeatureLayer 地图的图层的索引从0开始,一般通过get_Layer方法或者Layer属性获取ILayer,然后将ILayer强制转换为IFeatureLayer。

    D 举例 下面是一个整体的过程。 首先定义一个方法:

    /// <summary> /// 查询并高亮显示

    /// </summary> /// <param name="sql">为WhereClause赋值</param> 

    /// <param name="pLayer">图层,ILayer对象</param> 

    public void QueryAndHight(string sql, ILayer pLayer) 

    {//查询                  

    this.axMapControl1.Map.ClearSelection();//清除地图的选择 

    IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;//定义矢量图层           

    IQueryFilter pQueryFilter = new QueryFilter();//实例化一个查询条件对象           

    pQueryFilter.WhereClause = sql;//将查询条件赋值           

    IFeatureCursor pFeatureCursor = pFeatureLayer.Search(pQueryFilter, false);//进行查询           

    IFeature pFeature;           

    pFeature = pFeatureCursor.NextFeature();//此步是将游标中的第一个交给pFeature           

    if (pFeature == null)//判断是否查到结果           

    {//如果没有查到报错并结束               

    MessageBox.Show("没有查询到地物!", "查询提示", MessageBoxButtons.OK, MessageBoxIcon.Information);               

    return;           

    }           

    axMapControl1.Map.SelectFeature(pLayer, pFeature);//将查询到的地物作为选择对象高亮显示在地图上          

    axMapControl1.CenterAt(pFeature.Shape as ESRI.ArcGIS.Geometry.IPoint);//设置当前查询到的要素为地图的中心          

    axMapControl1.MapScale = pLayer.MinimumScale;//将当前地图的比例尺设置为ILayer的最小显示比例尺          

    axMapControl1.ActiveView.Refresh();//刷新地图,这样才能显示出地物 

    } 下面我们进行方法的调用:

    Ilayer pLayer=axMapControl1.Map.get_Layer(0);//第一个图层,在地图中第一个图层是医院

    QueryAndHight("name='市一医院'",pLayer);//调用函数,在医院的图层上查询name为市一医院的要素并高量显示在地图上。

    上面的例子有一个问题,假设我们只知道图层的名字,而不知道索引为多少,这就比较复杂了。

    所以我们可以提前写一个方法 通过名字获得ILayer对象。 

    public ILayer GetLayerByName(string strLayerName)       

    {           

    ILayer pLayer = null;           

    for (int i = 0; i <= axMapControl1.LayerCount - 1; i++)           

    {               

      if (strLayerName == axMapControl1.get_Layer(i).Name)               

      {                   

        pLayer = axMapControl1.get_Layer(i); break;               

      }           

    }           

    return pLayer;       

    }

    这样,我们就可以用以下的方法获得ILayer对象进行查询:

    ILayer pLayer=GetLayerByName("医院");。。

    上面我们只是进行单要素的查询与显示,但是我们有时候需要获得多个查询结果,这样就需要用到NextFeature方法进行遍历搜索显示 ,我们在进行高亮显示的时候也能获取要素的属性值,可以将其保存起来。

    下面就是一个例子:

    List<string> strName=new List<string>();//用来保存name属性,这个可以换成listBox或者comoBox来接受要素的name 

    IFeatureLayer pFeatureLayer = mainfrm.GetLayerByName("此处为图层的名字") as IFeatureLayer; 

    IQueryFilter pQueryFilter = new QueryFilter(); 

    pQueryFilter.WhereClause = "name  like '%" + txtName.Text + "%'";//模糊查询 

    IFeatureCursor pFeatureCursor = pFeatureLayer.Search(pQueryFilter, false); 

    IFeature pFeature;  pFeature = pFeatureCursor.NextFeature();//获取第一个要素 

    if (pFeature == null)

    {//如果要素为空,则提示并返回         

    MessageBox.Show("没有查询到" + txtName.Text + "", "查询提示", MessageBoxButtons.OK, MessageBoxIcon.Information);         

    return;   

    }   

    while (pFeature != null)   

    {              

    strName.Add(pFeature.get_Value(pFeature.Fields.FindField("name")).ToString());//将查询出的要素的name属性加到strName中 

    //如果是listbox则可以用listBox1.items.add(....);来将name添加到listbox中。               

    mainfrm.axMapControl1.Map.SelectFeature(mainfrm.GetLayerByName(“图层的名字”), pFeature);//高亮显示               

    pFeature = pFeatureCursor.NextFeature();//遍历要素游标,直至要素为空时为止。    

    }

  • 相关阅读:
    Oracle SQL语句收集
    SqlParameter In 查询
    SQL 性能优化
    Entity Framework
    【XLL API 函数】 xlfSetName
    【XLL API 函数】xlfUnregister (Form 2)
    【XLL API 函数】xlfUnregister (Form 1)
    【Excel 4.0 函数】REGISTER 的两种形式以及VBA等效语句
    【Excel 4.0 函数】REGISTER
    【Bochs 官方手册翻译】 第一章 Bochs介绍
  • 原文地址:https://www.cnblogs.com/ahnucao/p/2640627.html
Copyright © 2011-2022 走看看