AE中查询FeatureClass

时间:2024-03-02 18:36:46

图层FeatureClass查询:
        public string LayerSelection(IFeatureLayer pFeatureLayer,IGeometry pGeometry)
        {
            string strRecords = pFeatureLayer.Name + ":";
            string displayField = string.Empty;
            IFeatureClass pFeatureClass = null;
            ISpatialFilter pSpatialFilter = null;
            IFeatureCursor pFeatureCursor = null;
            IFeature pFeature = null;
            try
            {
                pFeatureClass = pFeatureLayer.FeatureClass;
                pSpatialFilter = new SpatialFilterClass();
                pSpatialFilter.Geometry = pGeometry;
                pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
                pSpatialFilter.GeometryField = pFeatureClass.ShapeFieldName;
                pFeatureCursor = pFeatureClass.Search(pSpatialFilter, false);
                pFeature = pFeatureCursor.NextFeature();
                while (pFeature != null)
                {
                    strRecords += pFeature.get_Value(pFeature.Fields.FindField("OBJECTID")).ToString()+",";
                    pFeature = pFeatureCursor.NextFeature();
                }
                if (pFeature != null)
                    Marshal.ReleaseComObject(pFeature);
                if (pFeatureCursor != null)
                    Marshal.ReleaseComObject(pFeatureCursor);
                if(pSpatialFilter!=null)
                    Marshal.ReleaseComObject(pSpatialFilter);
                if (pFeatureClass != null)
                    Marshal.ReleaseComObject(pFeatureClass);
                strRecords = strRecords.Substring(0, strRecords.Length - 1);
                return strRecords;
            }
            catch (System.Exception ex)
            {
                if (pFeature != null)
                    Marshal.ReleaseComObject(pFeature);
                if (pFeatureCursor != null)
                    Marshal.ReleaseComObject(pFeatureCursor);
                if (pSpatialFilter != null)
                    Marshal.ReleaseComObject(pSpatialFilter);
                if (pFeatureClass != null)
                    Marshal.ReleaseComObject(pFeatureClass);
                strRecords = "err:" + ex.Message;
                return strRecords;
            }
        }


FeatureClass属性字段模糊查询:
       public List<string[]> GetMatchedValues(IFeatureClass pFeatureClass, string strQuery,string strFieldName)
        {
            List<string[]> lstValue = null;
            string[] temp = null;
            IQueryFilter pQueryFilter = null;
            IFeatureCursor pFeatureCursor = null;
            IFeature pFeature = null;
            try
            {
                pQueryFilter = new QueryFilterClass();
                pQueryFilter.WhereClause = strQuery;
                pFeatureCursor = pFeatureClass.Search(pQueryFilter, false);
                pFeature = pFeatureCursor.NextFeature();
                lstValue = new List<string[]>();
                while (pFeature != null)
                {
                    temp = new string[2];
                    temp[0] = pFeature.get_Value(pFeature.Fields.FindField("OBJECTID")).ToString();
                    temp[1]=pFeature.get_Value(pFeature.Fields.FindField(strFieldName)).ToString();                    
                    lstValue.Add(temp);
                    pFeature = pFeatureCursor.NextFeature();
                }
                if (pFeature != null)
                    Marshal.ReleaseComObject(pFeature);
                if (pFeatureCursor != null)
                    Marshal.ReleaseComObject(pFeatureCursor);
                if (pQueryFilter != null)
                    Marshal.ReleaseComObject(pQueryFilter);
                return lstValue;
            }
            catch (System.Exception ex)
            {
                if (pFeature != null)
                    Marshal.ReleaseComObject(pFeature);
                if (pFeatureCursor != null)
                    Marshal.ReleaseComObject(pFeatureCursor);
                if (pQueryFilter != null)
                    Marshal.ReleaseComObject(pQueryFilter);
                return null;
            }
        }         

相关文章