C# GIS & GPS
2012-11-19 14:18:10 2131

ArcGIS添加要素

using System;
using System.Collections.Generic;

using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;


namespace ClsLDGisLib
{
    public class CGISGPS
    {
       
        private string Fileworkspacestring;
        List<xlname> xlnames = new List<xlname>();
        List<gtzb> gtzbcolltion = new List<gtzb>();
        private ILayer m_pCurrentLayer;
        private IMap m_pMap;
        public string EditFolder

        {
            set
            {
                Fileworkspacestring = value;
            }
        }
        public IMap Map
        {
            set
            {
                m_pMap = (IMap)value;
            }
        }
        /// <summary>
        /// 当前层
        /// </summary>
        public ILayer CurrentLayer
        {
            set
            {
                m_pCurrentLayer = (ILayer)value;
            }
        }
        private struct xlname
        {
           public  string xlmc;
           public  string zlmc;

        }
        private struct gtzb
        {
            public double longitude;
            public double latitude;
            public double elevation;
            public string gh;
            public string xlmc;
            public string zlmc;
            public string zjgh;

        }
        private List<string> listnames;// 文件名
        public List<string> filenames
        {
            set
            {
                listnames = value;

            }
        }

        public  IFeatureLayer GetLayer(IMap pmap, string layerName)
        {
            IEnumLayer pLayers;
            IFeatureLayer m_SearchFeatureLayer;
            pLayers = pmap.get_Layers(null, true);// get_GlobeLayers(null, true, true, true);

            pLayers.Reset();
            ILayer player = pLayers.Next();

            while (player != null)
            {
                if (player.Name == layerName)
                {
                    m_SearchFeatureLayer = (IFeatureLayer)player;
                    return m_SearchFeatureLayer;

                }

                player = pLayers.Next();

            }
            return null;
        }

        public  int GetLayerid(IMap pmap, string layerName)
        {
            IEnumLayer pLayers;
            IFeatureLayer m_SearchFeatureLayer;
            pLayers = pmap.get_Layers(null, true);// get_GlobeLayers(null, true, true, true);

            pLayers.Reset();
            ILayer player = pLayers.Next();


            for (int i = 0; i < pmap.LayerCount; i++)
            {
                if (player.Name == layerName)
                {
                    m_SearchFeatureLayer = (IFeatureLayer)player;
                    return i;

                }

                player = pLayers.Next();

            }
            return 0;
        }
        /// <summary>
        /// 获取shp文件的工作空间
        /// </summary>
        /// <returns></returns>
        private IFeatureWorkspace Getshpworkspace()
        {
            IFeatureWorkspace pFeatWS;
            IWorkspaceFactory pWorkspaceFactory;
            pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();

            if (Fileworkspacestring == null)
            {
                return null;
            }
            pFeatWS = pWorkspaceFactory.OpenFromFile(Fileworkspacestring, 0) as IFeatureWorkspace;
            return pFeatWS;
  
        }

        /// <summary>
        /// 获取文件所有要添加的杆塔的所有的线路名称
        /// /// </summary>
        /// 
        public void Getlinenames()
        {                                 

            foreach (String file in listnames)
            {
                // Create a PictureBox.
                if (file == "Export_Output.shp")
                {
                    try
                    {
                        IQueryFilter queryFilter = new QueryFilterClass();
                        queryFilter.SubFields = "xlmc,zlmc";
                        IFeatureClass pFeatureClass = Getshpworkspace().OpenFeatureClass(file);
                        ISelectionSet selectionSet = pFeatureClass.Select(null, esriSelectionType.esriSelectionTypeIDSet, esriSelectionOption.esriSelectionOptionNormal, null);

                        IEnumIDs enumIDs = selectionSet.IDs;
                        int fieldIndex = pFeatureClass.FindField("xlmc");
                        int fieldIndex1 = pFeatureClass.FindField("zlmc");

                        IFeature feature;
                        int iD = enumIDs.Next();
                        xlname xl = new xlname();
                        int isfirst = 0;
                        while (iD != -1) //-1 is reutned after the last valid ID has been reached     
                        {                            
                            if (isfirst == 0)
                            {
                                feature = pFeatureClass.GetFeature(iD);
                                xl.xlmc = feature.get_Value(fieldIndex).ToString();
                                xl.zlmc = feature.get_Value(fieldIndex1).ToString();
                                xlnames.Add(xl);
                                isfirst = 1;
                            }
                            else
                            {
                                feature = pFeatureClass.GetFeature(iD);
                                xl.xlmc = feature.get_Value(fieldIndex).ToString();
                                xl.zlmc = feature.get_Value(fieldIndex1).ToString();
                                bool iscz = true;
                                foreach (xlname xlmc in xlnames)
                                {

                                    if ((xlmc.xlmc == xl.xlmc) && (xlmc.zlmc == xl.zlmc))
                                    {
                                        iscz = false;//是否存在

                                    }
                                }
                                if (iscz == true)   //不存在就添加
                                {
                                    xlnames.Add(xl);
                                }
                            }

                            iD = enumIDs.Next();
                        }
                        int dd = xlnames.Count;

                    }

                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message.ToString());
                    }

                }
            }   
        }
        /// <summary>
        /// 绘制点状设备
        /// </summary>
        /// <param name="iflayer"></param>
        /// <param name="pPoint"></param>
        private void Drawpoint(ILayer iflayer,IPoint pPoint)
        {
            try
            {
                if (iflayer == null) return;
                if (!(iflayer is IGeoFeatureLayer)) return;
                IFeatureLayer pFeatureLayer = (IFeatureLayer)iflayer;
                IFeatureClass mfeatureClass = pFeatureLayer.FeatureClass;
                IDataset pDataset = (IDataset)pFeatureLayer.FeatureClass;
                if (pDataset == null) return;
                
                // 开始编辑,并设置Undo/Redo 为可用
                IWorkspaceEdit pWorkspaceEdit = (IWorkspaceEdit)pDataset.Workspace;
                if (!pWorkspaceEdit.IsBeingEdited())
                {
                    pWorkspaceEdit.StartEditing(true);
                    pWorkspaceEdit.EnableUndoRedo();
                    IFeature myfeature;

                    IFeatureClassWrite fr = mfeatureClass as IFeatureClassWrite;//绘制杆塔
                    pWorkspaceEdit.StartEditOperation();
                    myfeature = mfeatureClass.CreateFeature();                 

                    myfeature.Shape = pPoint;                 

                    myfeature.Store();
                    fr.WriteFeature(myfeature);

                    pWorkspaceEdit.StopEditOperation();
                    pWorkspaceEdit.StopEditing (true );
                    m_pMap.ClearSelection();
                    IActiveView pActiveView = (IActiveView)m_pMap;
                    pActiveView.Refresh();


                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
            }

 
        }
        /// <summary>
        /// 画线路
        /// </summary>
        public void Drawline(ILayer iflayer, IPointCollection pointCollection)
        {
            try
            {
                if (iflayer == null) return;
                if (!(iflayer is IGeoFeatureLayer)) return;
                IFeatureLayer pFeatureLayer = (IFeatureLayer)iflayer;
                IFeatureClass mfeatureClass = pFeatureLayer.FeatureClass;
                IDataset pDataset = (IDataset)pFeatureLayer.FeatureClass;
                if (pDataset == null) return;

                // 开始编辑,并设置Undo/Redo 为可用
                IWorkspaceEdit pWorkspaceEdit = (IWorkspaceEdit)pDataset.Workspace;
                if (!pWorkspaceEdit.IsBeingEdited())
                {
                    pWorkspaceEdit.StartEditing(true);
                    pWorkspaceEdit.EnableUndoRedo();
                    IFeature myfeature;

                    IFeatureClassWrite fr = mfeatureClass as IFeatureClassWrite;//绘制杆塔
                    pWorkspaceEdit.StartEditOperation();
                    myfeature = mfeatureClass.CreateFeature();            
                    pWorkspaceEdit.StartEditOperation();
                    myfeature = mfeatureClass.CreateFeature();
                    //IGeometry gGeometry = (IGeometry)pointCollection ;
                    IPolyline pPolyline = pointCollection as IPolyline;

                    myfeature.Shape = pPolyline;                   

                    myfeature.Store();
                    fr.WriteFeature(myfeature);
                    IComplexEdgeFeature Ccomplexedte = myfeature as IComplexEdgeFeature;//建立连通性
                    Ccomplexedte.ConnectAtIntermediateVertices();

                    pWorkspaceEdit.StopEditOperation();
                    pWorkspaceEdit.StopEditing(true);

                   
                   

                    m_pMap.ClearSelection();
                    IActiveView pActiveView = (IActiveView)m_pMap;
                    pActiveView.Refresh();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
            }
        }        
      




        private void Inserttz()
        {
 
        }
        /// <summary>
        /// 绘制gps数据,杆塔,线路
        /// </summary>

        public void Draw()
        {
                  
            foreach (String file in listnames)
            {               
                if (file == "Export_Output.shp")//布绘杆塔                
                {
                    foreach (xlname xlmc in xlnames)
                    {
                        IFeature featurepoint;
                       
                        int idline = GetLayerid(m_pMap, "GIS.lines");//获取线图层
                        ILayer ilayerline = m_pMap.get_Layer(idline);

                        int idpoint = GetLayerid(m_pMap, "gt");//获取点图层
                        ILayer iflayer = m_pMap.get_Layer(idpoint);

                        IPolyline mypolylin = new PolylineClass();
                        IPointCollection pointCollection = mypolylin as IPointCollection;
                        object missingVal = System.Reflection.Missing.Value;

                        IQueryFilter queryFilter = new QueryFilterClass();
                        queryFilter.WhereClause = "xlmc='"+xlmc .xlmc+"'"+" and zlmc='" + xlmc.zlmc+"'";
                        IFeatureClass pFeatureClass = Getshpworkspace().OpenFeatureClass(file);
                        //IQueryFilterDefinition queryFilterDefinition = (IQueryFilterDefinition)queryFilter; 
                        //queryFilterDefinition.PostfixClause = "ORDER BY gh";
                        IFields fField = pFeatureClass.Fields;
                        for (int fieldindex = 0; fieldindex < fField.FieldCount;fieldindex++ )
                        {
                            string dd = fField.get_Field(fieldindex).Name;
                            string ddd = pFeatureClass.ShapeFieldName;
                        }
                        
                        ISelectionSet selectionSet = pFeatureClass.Select(queryFilter, esriSelectionType.esriSelectionTypeIDSet, esriSelectionOption.esriSelectionOptionNormal, null);
                        int fieldIndexgh = pFeatureClass.FindField("gh");
                        int fieldIndexzjgh = pFeatureClass.FindField("zjgh");
                        int fieldindexxlmc = pFeatureClass.FindField("xlmc");
                        int fieldindexzlmc = pFeatureClass.FindField("zlmc");
                        int fieldindextjxl = pFeatureClass.FindField("tjxl");
                        int fieldindextjgh = pFeatureClass.FindField("tjgh");
                        IEnumIDs enumIDs = selectionSet.IDs;                 

                        for (int j = 1; j <= selectionSet.Count;j++ )
                        {
                        
                            bool istjgh = false;
                            enumIDs.Reset();
                            int iD = enumIDs.Next();
                            while (iD != -1) //-1 is reutned after the last valid ID has been reached     
                            {
                                featurepoint = pFeatureClass.GetFeature(iD);
                                IGeometry geometrygt = featurepoint.Shape;//获取shp文件中的对象
                                IPoint pointgt = geometrygt as IPoint;
                                gtzb gtxx = new gtzb();                                
                                gtxx.gh = featurepoint.get_Value(fieldIndexgh).ToString();
                                gtxx.xlmc = featurepoint.get_Value(fieldindexxlmc).ToString();
                                gtxx.zlmc = featurepoint.get_Value(fieldindexzlmc).ToString();
                                gtxx.zjgh = featurepoint.get_Value(fieldIndexzjgh).ToString();
                                if (gtxx.gh == j.ToString() &&  gtxx.zjgh.ToString().Trim() =="")
                                {                                   
                                    Drawpoint(iflayer, pointgt);    //将shp文件中的对象绘制到数据库                             
                                    pointCollection.AddPoint(pointgt, ref missingVal, ref missingVal);
                                }
                                else if (gtxx.gh == j.ToString() && gtxx.zjgh.ToString().Trim() != "")
                                {
                                    
                                    istjgh = true;                         
                                
                                }
                                iD = enumIDs.Next();
                            }
                               if (istjgh)
                                {
                                    IQueryFilter queryFiltersub = new QueryFilterClass();
                                    queryFiltersub.WhereClause = "xlmc='" + xlmc.xlmc + "'" + " and zlmc='" + xlmc.zlmc + "'and zjgh='" + j + "'";
                                    ISelectionSet selectionSetsub = pFeatureClass.Select(queryFiltersub, esriSelectionType.esriSelectionTypeIDSet, esriSelectionOption.esriSelectionOptionNormal, null);
                                    
                                    for (int subj = 1; subj <= selectionSetsub.Count; subj++)
                                     {
                                          IEnumIDs enumsubIDs = selectionSetsub.IDs; 
                                          enumsubIDs.Reset();
                                          int iDsub = enumsubIDs.Next();
                                          while (iDsub != -1)    //-1 is reutned after the last valid ID has been reached     
                                          {
                                              featurepoint = pFeatureClass.GetFeature(iDsub);
                                              IGeometry geometrygt = featurepoint.Shape;
                                              IPoint pointgt = geometrygt as IPoint;
                                              gtzb gtxx = new gtzb();
                                              gtxx.longitude = pointgt.X;
                                              gtxx.latitude = pointgt.Y;
                                              gtxx.elevation = pointgt.Z;
                                              gtxx.gh = featurepoint.get_Value(fieldIndexgh).ToString();
                                              gtxx.zjgh = featurepoint.get_Value(fieldIndexzjgh).ToString();
                                              gtxx.xlmc = featurepoint.get_Value(fieldindexxlmc).ToString();
                                              gtxx.zlmc = featurepoint.get_Value(fieldindexzlmc).ToString();
                                              if (gtxx.zjgh  == subj.ToString())
                                              {
                                                  Drawpoint(iflayer, pointgt);                                                
                                                  pointCollection.AddPoint(pointgt, ref missingVal, ref missingVal);
                                               }
                                                  iDsub = enumsubIDs.Next();
                                           }
                                     } 
                                }
                        }

                        //绘制线路
                        Drawline(ilayerline, pointCollection);
                    }
                }
                    ///绘制变压器设备,同时联线
                else if (file == "byq.shp")
                {
                    try
                    {
                        IPolyline mypolylin = new PolylineClass();
                        IPointCollection pointCollection = mypolylin as IPointCollection;
                        object missingVal = System.Reflection.Missing.Value;

                        int idpoint = GetLayerid(m_pMap, "byq");//获取点图层
                        ILayer iflayer = m_pMap.get_Layer(idpoint);


                        int idline = GetLayerid(m_pMap, "GIS.lines");//获取线图层
                        ILayer ilayerline = m_pMap.get_Layer(idline);


                        int idpointgt = GetLayerid(m_pMap, "gt");//获取杆塔图层
                        ILayer iflayergt = m_pMap.get_Layer(idpoint);
                        IFeatureLayer pFeatureLayer = (IFeatureLayer)iflayergt;
                        IFeatureClass mfeatureClass = pFeatureLayer.FeatureClass;



                        IQueryFilter queryFilter = new QueryFilterClass();
                        IFeatureClass pFeatureClass = Getshpworkspace().OpenFeatureClass(file);
                        ISelectionSet selectionSet = pFeatureClass.Select(null, esriSelectionType.esriSelectionTypeIDSet, esriSelectionOption.esriSelectionOptionNormal, null);
                        IEnumIDs enumIDs = selectionSet.IDs;
                        int fieldIndexxlmc = pFeatureClass.FindField("xlmc");
                        int fieldIndexzlmc = pFeatureClass.FindField("zlmc");
                        int fieldIndextjgh = pFeatureClass.FindField("tjgh");

                        IFeature featurebyq;
                        int iD = enumIDs.Next();

                        while (iD != -1) //-1 is reutned after the last valid ID has been reached     
                        {
                            featurebyq = pFeatureClass.GetFeature(iD);
                            IGeometry geometrybyq = featurebyq.Shape;
                            IPoint pointbyq = geometrybyq as IPoint;

                            Drawpoint(iflayer, pointbyq);

                            string xlmc = featurebyq.get_Value(fieldIndexxlmc).ToString();
                            string zlmc = featurebyq.get_Value(fieldIndexzlmc).ToString();
                            string tjgh = featurebyq.get_Value(fieldIndextjgh).ToString();
                            ///在杆塔层中查找T接杆塔
                            IQueryFilter queryFiltersub = new QueryFilterClass();
                            queryFiltersub.WhereClause = "xlmc='" + xlmc + "'" + " and zlmc='" + zlmc + "'and tjgh='" + tjgh + "'";
                            ///featureclass 为杆塔层,如果没有绘制杆塔,则绘制孤立变压器
                            ISelectionSet selectionSetsub = mfeatureClass.Select(queryFiltersub, esriSelectionType.esriSelectionTypeIDSet, esriSelectionOption.esriSelectionOptionNormal, null);


                            if (selectionSetsub.Count == 1)
                            {
                                 IEnumIDs enumIDsub = selectionSetsub.IDs;
                                 IFeature featuregt;
                                 int iDsub = enumIDs.Next();

                                 while (iDsub != -1) //-1 is reutned after the last valid ID has been reached     
                                {
                                       featuregt = pFeatureClass.GetFeature(iDsub);
                                       IGeometry geometrygt = featuregt.Shape;
                                       IPoint pPointgt = geometrygt as IPoint;
                                       pointCollection.AddPoint(pPointgt, ref missingVal, ref missingVal);
                                       pointCollection.AddPoint(pointbyq, ref missingVal, ref missingVal);
                                }
                                 Drawline(ilayerline, pointCollection);
                               
                            }
                            else return;                       
                        }
                    } 
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message.ToString());
                    }
                }
                else if (file == "kg.shp")
                {

                }
                else if (file == "dz.shp")
                {

                }
            }           
        }     

    }
}