【转】C# ArcgisEngine开发中,对一个图层进行过滤,只显示符合条件的要素

有时候,我们要对图层上的地物进行有选择性的显示,以此来满足实际的功能要求。

按下面介绍的方法可轻松实现图层属性过滤显示:

  1、当图层已经加载时

 private void ShowByFilter(AxMapControl sMapCtr, IFeatureLayer sFlyr, string sFilter)

        {

            ESRI.ArcGIS.Carto.IFeatureLayerDefinition pDef = (ESRI.ArcGIS.Carto.IFeatureLayerDefinition)sFlyr;

 

            pDef.DefinitionExpression = sFilter;

 

            sMapCtr.ActiveView.Refresh();

        }

2、当图层尚未加载时

private IFeatureClass GetFeatureClass(IWorkspace pWS, string sTabName, string sAttrFilter)

        {

            try

            {

                if (pWS == null || sTabName == null) return null;

 

                IFeatureWorkspace pFWS = pWS as IFeatureWorkspace;

                IFeatureClass pFC = null;

 

                pFC = pFWS.OpenFeatureClass(sTabName);

 

                IQueryDef pQueryDef = pFWS.CreateQueryDef();

                pQueryDef.Tables = sTabName;

                pQueryDef.WhereClause = sAttrFilter;

 

                IQueryName2 pQueryName = new FeatureQueryNameClass();

                pQueryName.PrimaryKey = pFC.OIDFieldName;

                pQueryName.QueryDef = pQueryDef;

 

                IDataset pDS = (IDataset)pFWS;

 

                IWorkspaceName pWSName = (IWorkspaceName)pDS.FullName;

                IDatasetName pDSName = (IDatasetName)pQueryName;

 

                pDSName.WorkspaceName = pWSName;

                IName pName = (IName)pQueryName;

                pFC = pName.Open() as IFeatureClass;

 

                return pFC;

            }

            catch

            {

                return null;

            }

        }

 

posted @ 2015-06-16 16:35  Creatisan  阅读(1462)  评论(0编辑  收藏  举报