C# soe 查询(IQueryFilter,ISpatialFilter,IQueryDef2,IIdentify)

  • IQueryFilter  属性查询
  • ISpatialFilter  几何空间查询
  •  IQueryDef2      IQueryDef 由IFeatureWorkspace.CreateQueryDef,注意事项: 

  1)只能在ArcSDE、PGDB、FGDB数据源上使用(注意:shp数据不支持),数据集的历史表示 不被QueryDef游标支持(翻译可能不太准确,详见官方文档)。 

  2)支持多表查询,但是查询的结果不能进行修改! 

  3)使用IQueryDef查询出来的游标,用此游标获取的字段的别名和字段名一致(如果是要获取字段的中文别名,请使用其他方法获取游标)

   

  • IIdentify接口  几何空间查询     使用IFeaturelayer接口QI到IIdentify接口查询,大数据的情况下使用此接口做空间查询比ISpatialFilter快,缺点:只有与查询图形相交一种查询方式没有IQueryfilter的查询方式多样

 

IQueryFilter查询

复制代码
IQueryFilter pQueryFilter = new QueryFilterClass();
pQueryFilter.WhereClause = where;

IFeatureClass fc = this.m_FeatureClass;
List<IFeature> listFeature = new List<IFeature>();
IFeatureCursor cursor = fc.Search(qy, false);
                        IFeature feature = null;while ((feature = cursor.NextFeature()) != null)
                        {
                 listFeature.Add(feature);
} Marshal.ReleaseComObject(cursor);
复制代码

 

ISpatialFilter  几何空间查询

复制代码
// 创建一个查询范围
IEnvelope envelope = new EnvelopeClass();
envelope.PutCoords( - 84.4078, 33.7787,  - 84.3856, 33.7997);

ISpatialFilter sf = new SpatialFilterClass();
            sf.WhereClause = where;
            sf.Geometry = envelope;
            sf.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
            sf.GeometryField = this.m_FeatureClass.ShapeFieldName;
            ISpatialReference sr = (this.m_FeatureClass as IGeoDataset).SpatialReference;
            sf.set_OutputSpatialReference(this.m_FeatureClass.ShapeFieldName, sr);

            IFeatureCursor pFCur = this.m_FeatureClass.Search(sf, false);
            IFeature pFea;
复制代码

 

IQueryDef2

复制代码
// 创建IQueryDef2对象
IQueryDef2 queryDef2 = (IQueryDef2)featureWorkspace.CreateQueryDef();

// 设置查询的表、字段及后缀查询条件
queryDef2.WhereClause = where;
queryDef2.Tables = "Cities";
queryDef2.SubFields = "Name, Pop1996";
queryDef2.PostfixClause = "ORDER BY Pop1996 DESC";

// 执行查询
属性及方法详解(ComReleaser comReleaser = new ComReleaser())
{
    ICursor cursor = queryDef2.Evaluate2(true);
    comReleaser.ManageLifetime(cursor);
    int nameIndex = cursor.FindField("Name");
    int pop1996Index = cursor.FindField("Pop1996");
    IRow row = null;
    while ((row = cursor.NextRow()) != null)
    {
        String cityName = Convert.ToString(row.get_Value(nameIndex));
        int population = Convert.ToInt32(row.get_Value(pop1996Index));
        Console.WriteLine("{0}: {1}", cityName, population);
    }
}
复制代码

 

IIdentify接口

复制代码
//QI FeatureLayer QI IIdentif

IIdentify pIdentify = needLayer as IIdentify;

IArray pIDs = pIdentify.Identify((IGeometry)pp);

if (pIDs == null || pIDs.Count == 0)

{

return;

}

//取第一个实体

IFeatureIdentifyObj pFeatIdObj = pIDs.get_Element(0) as IFeatureIdentifyObj;

// 1:获得IFeature对象

IFeature pFea= pFeatIdObj.Feature

myMapControl.FlashShape(needFeat.Shape, 3, 300, null);

//2:获得IRow对象

IRowIdentifyObject pRowObj = pFeatIdObj as IRowIdentifyObject;

IRow pRow= = pRowObj.Row ;

//例子二

//如果是矢量图层
if (layer is IFeatureLayer)
{
//图层转Identify对象
IIdentify pFL = layer as IIdentify;
//对点进行缓冲赋给几何图形,尽量减少对空白区域判定
ITopologicalOperator pTopo = SelectedPoint as ITopologicalOperator;
pGeometry = pTopo.Buffer(500);
//将几何图形作为输入传入Identify对象,进行识别
IArray id_result = pFL.Identify(pGeometry);
if (id_result != null)
{
//对识别结果中的记录进行遍历(由于缓冲扩大,可能识别到同一图层多条记录)
for (int i = 0; i < id_result.Count; i++)
{
//获取识别结果记录中的属性记录
IIdentifyObj featureIdentifyobj = (IIdentifyObj)id_result.get_Element(i);
IRowIdentifyObject iRowIdentifyObject = featureIdentifyobj as IRowIdentifyObject;
IRow pRow = iRowIdentifyObject.Row;//添加引用GeoDatabase
output += "\"" + featureIdentifyobj.Layer.Name + "\":\n";
//遍历属性记录,获取字段名、字段值
for (int a = 0; a < pRow.Fields.FieldCount; a++)
{
if (pRow.Fields.get_Field(a).Type != esriFieldType.esriFieldTypeGeometry)
{
output += String.Format("{0}={1} \n", pRow.Fields.get_Field(a).Name, pRow.get_Value(a).ToString());
}
}
}
}
}
else if (layer is IRasterLayer)
{
//图层转Identify对象
IIdentify pFL = layer as IIdentify;
//点转几何对象
pGeometry = SelectedPoint as IGeometry;
//将几何图形作为输入传入Identify对象,进行识别
IArray id_result = pFL.Identify(pGeometry);
if (id_result != null)
{
//遍历识别结果记录,获取图层名和对应位置的属性值
for (int i = 0; i < id_result.Count; i++)
{
IIdentifyObj featureIdentifyobj = (IIdentifyObj)id_result.get_Element(i);
IRasterIdentifyObj rasterIdentifyobj = featureIdentifyobj as IRasterIdentifyObj;
output += "\"" + featureIdentifyobj.Layer.Name + "\":" + "\n" + rasterIdentifyobj.MapTip + "\n";
}
}
}

复制代码

官方教材

转载:https://blog.csdn.net/yh0503/article/details/53493583

posted @   小鱼写代码的过往  阅读(314)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示