using System;
using System.Collections;
using System.Collections.Generic;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;

namespace SouthSmart.GISService.Infrastructure.Module
{
    public static class FeatureClassHelper
    {
        //属性查询
        public static List<IFeature> FeatureSearch ( this IFeatureClass featureClass, IQueryFilter filter )
        {
            var features = new List<IFeature> ();
            IFeature pFeature;
            IFeatureCursor pFCursor = featureClass.Search ( filter, false );
            pFeature = pFCursor.NextFeature (); // 第一个Feature
            while ( pFeature != null ) // 如果是null就说明刚才那个是最后一个了，后面没有了
            {
                features.Add ( pFeature );
                // 对pFeature作相应处理
                pFeature = pFCursor.NextFeature (); // 下一个Feature
            }
            return features;
        }
        //空间查询
        public static List<IFeature> FeatureSearch ( this IFeatureClass featureClass, IGeometry queryGeometry, esriSpatialRelEnum spatialRel )
        {

            //构造空间过滤器
            ISpatialFilter spatialFilter = new SpatialFilterClass ();
            spatialFilter.Geometry = queryGeometry;
            spatialFilter.SpatialRel = spatialRel;
            //在要素类上用空间过滤器进行查询即可
            var features = new List<IFeature> ();
            IFeature pFeature;
            IFeatureCursor pFCursor = featureClass.Search ( spatialFilter, false );
            pFeature = pFCursor.NextFeature (); // 第一个Feature
            while ( pFeature != null ) // 如果是null就说明刚才那个是最后一个了，后面没有了
            {
                features.Add ( pFeature );
                // 对pFeature作相应处理
                pFeature = pFCursor.NextFeature (); // 下一个Feature
            }
            return features;
        }
        public static IPointCollection GetPointCollection ( this IGeometry drawGeo )
        {
            //判断绘制多边形是否为null，并不为空图形
            if ( drawGeo != null && !drawGeo.IsEmpty )
            {
                //图形转为点集
                IPointCollection pPointColl = drawGeo as IPointCollection;
                ////遍历点集
                //for ( int i = 0; i < pPointColl.PointCount; i++ )
                //{
                //    //获取点
                //    IPoint pPoint = pPointColl.get_Point ( i );
                //    //输出点坐标
                //    outputStr += ( "x:" + pPoint.X.ToString ( "f2" ) + ", y:" + pPoint.Y.ToString ( "f2" )+"\r\n" );
                //}
                return pPointColl;
            }
            return null;
        }

        public static List<IFeatureDataset> OpenCAD ( string workspacePath )
        {

            IWorkspaceFactory pWorkspaceFactory;
            IFeatureWorkspace pFeatureWorkspace;
            //打开CAD数据集
            pWorkspaceFactory = new CadWorkspaceFactoryClass ();
            pFeatureWorkspace = ( IFeatureWorkspace ) pWorkspaceFactory.OpenFromFile ( workspacePath, 0 );
            var dataset = GetAllFeatureDataSet ( pFeatureWorkspace as IWorkspace );
            return dataset;
        }
        /// <summary>
        /// 获取所有要素集
        /// </summary>
        /// <param name="workspace">工作空间对象</param>
        /// <returns>要素集列表</returns> 
        public static List<IFeatureDataset> GetAllFeatureDataSet ( IWorkspace workspace )
        {
            IEnumDataset dataset = workspace.get_Datasets ( esriDatasetType.esriDTFeatureDataset );
            IFeatureDataset featureDataset = dataset.Next () as IFeatureDataset;
            List<IFeatureDataset> featureDatasetList = new List<IFeatureDataset> ();
            
            while ( featureDataset != null )
            {
                string name = featureDataset.Name;
                featureDatasetList.Add ( featureDataset );
                featureDataset = dataset.Next () as IFeatureDataset;
            }
            return featureDatasetList;
        }
        /// <summary>
        /// 获取所有要素类
        /// </summary>
        /// <param name="featureDataset">要素集</param>
        /// <returns>要素类列表</returns>
        public static List<IFeatureClass> GetAllFeatureClass ( IFeatureDataset featureDataset )
        {
            IFeatureClassContainer featureClassContainer = ( IFeatureClassContainer ) featureDataset;
            IEnumFeatureClass enumFeatureClass = featureClassContainer.Classes;
            IFeatureClass featureClass = enumFeatureClass.Next ();

            List<IFeatureClass> featureClassList = new List<IFeatureClass> ();
            while ( featureClass != null )
            {
                featureClassList.Add ( featureClass );
                featureClass = enumFeatureClass.Next ();
            }
            return featureClassList;
        }
        /// <summary>
        /// 获取所有要素
        /// </summary>
        /// <param name="featureClass">要素类</param>
        /// <returns>要素列表</returns>
        public static List<IFeature> GetAllFeature ( IFeatureClass featureClass )
        {
            List<IFeature> featureList = new List<IFeature> ();
            IFeatureCursor featureCursor = featureClass.Search ( null, false );
            IFeature feature = featureCursor.NextFeature ();

            while ( feature != null )
            {
                featureList.Add ( feature );
                feature = featureCursor.NextFeature ();
            }
            return featureList;
        }
        //插入要素
        public static void InsertFeature ( this IFeatureClass featureClass, IGeometry geometry, Hashtable hashTable )
        {
            try
            {
                IFeatureCursor featureLineCursor = featureClass.Insert ( true );
                //将几何图形加入featureclass                                                       
                IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer ();
                featureBuffer.Shape = geometry;
                //遍历方法一：遍历哈希表中的键
                foreach ( string key in hashTable.Keys )
                {
                    int index = featureClass.Fields.FindField ( key );
                    if ( index == -1 )
                    {
                        continue;
                    }
                    featureBuffer.set_Value ( index, hashTable[key] );
                }

                featureLineCursor.InsertFeature ( featureBuffer );
                featureLineCursor.Flush ();
            }
            catch ( Exception ex )
            {
                throw ex;
            }

        }
        // 通过IFeature.Delete方法删除要素
        public static void DeleteFeatures ( this IFeatureClass pFeatureclass, string strWhereClause )
        {
            IQueryFilter pQueryFilter = new QueryFilterClass ();
            pQueryFilter.WhereClause = strWhereClause;
            IFeatureCursor pFeatureCursor = pFeatureclass.Search ( pQueryFilter, false );
            IFeature pFeature = pFeatureCursor.NextFeature ();
            while ( pFeature != null )
            {
                pFeature.Delete ();
                pFeature = pFeatureCursor.NextFeature ();
            }
        }
    }
}
