arcEngine 路径规划

//作者 疯狂的乌龟
//2015-5-26
//arcEngine 10.1
//功能:路径规划
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.NetworkAnalyst;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;

namespace NetworkPlan
{
    public  class Canylist
    {
        private INAContext M_NAContext;
        private IFeatureClass PointFeatureClass;
        private IFeatureWorkspace NetworkFeatureWorkspace;    
        INetworkDataset networkDataset;

        /// <param name=”gdbFilePath”>路网所在的工作空间路径</param>
        /// <param name=”featureDatasetName”>网络数据集名称</param>
        /// <param name=”networkLayerName”>路网图层名称</param>     
        public CNetworkanylist(string gdbFilePath, string featureDatasetName,string networkLayerName,string pointLayerName)
        {
            this.NetworkFeatureWorkspace = this.OpenGdbWorkspace(gdbFilePath) as IFeatureWorkspace;
            this.PointFeatureClass = this.NetworkFeatureWorkspace.OpenFeatureClass(pointLayerName);

            this.networkDataset = this.OpenNetworkDataset(networkLayerName, featureDatasetName);
            this.CreateNaContext(networkDataset);
        }

        public INALayer GetRouteLayer()
         {
             INALayer pNaLayer = null;
             if (LoadLocation())
             {
                 IGPMessages gpMessages = new GPMessagesClass();
                 try
                 {
                     //路网分析
                     pNaLayer = M_NAContext.Solver.CreateLayer(M_NAContext);
                     M_NAContext.Solver.Solve(M_NAContext, gpMessages, new TrackCancelClass());                    
                 }
                 catch (Exception exp)
                 {                   
                     System.Diagnostics.Debug.WriteLine(“GetRouteLayer方法出错  ” + exp.Message);
                     return null;
                 }
             }
             return pNaLayer;
         }

         public IPolyline GetRouteLine()
         {
             IPolyline pline = null;
             ILayer Ly1 = GetRouteLayer() as ILayer;

             if (Ly1 is IGroupLayer || Ly1 is ICompositeLayer)
             {
                 ICompositeLayer Igrouplayer = (ICompositeLayer)Ly1;
                 for (int j = 0; j < Igrouplayer.Count; j++)
                 {
                     ILayer ly = Igrouplayer.get_Layer(j);
                     if (ly.Name.Equals(“Routes”))
                     {
                         ShpEngine shpHelper = new ShpEngine((ly as IFeatureLayer).FeatureClass);
                         List<IFeature> feaList = shpHelper.FeatureList;
                         if (null != feaList && feaList.Count > 0)
                         {
                             pline = feaList[0].Shape as IPolyline;
                         }
                     }
                 }
             }

             return pline;
         }

        /// <summary>
         /// 创建网络分析上下文
        /// </summary>
        /// <param name=”networkDataset”></param>
        void CreateNaContext(INetworkDataset networkDataset)
        {
            if (networkDataset == null) return;

            IDENetworkDataset deNDS = (networkDataset as IDatasetComponent).DataElement as IDENetworkDataset;

            INASolver naSolver = new NARouteSolverClass();
            M_NAContext = naSolver.CreateContext(deNDS, naSolver.Name);
            ((INAContextEdit)M_NAContext).Bind(networkDataset, new GPMessagesClass());
        }       

        /// <summary>
        /// 装载网络分析类
        /// </summary>
        /// <param name=”featureWorkspace”></param>
        /// <returns></returns>
        bool LoadLocation()
        {   
            try
            {
                INamedSet pNamedSet = M_NAContext.NAClasses;
                INAClass pNAClass = pNamedSet.get_ItemByName(“Stops”) as INAClass;

                INAClassLoader2 pNAClassLoader = new NAClassLoaderClass();

                INALocator3 locator = M_NAContext.Locator as INALocator3;
                locator.ExcludeRestrictedElements = true;
                locator.CacheRestrictedElements(M_NAContext);

                INAClassFieldMap naClassFieldMap = new NAClassFieldMapClass();
                naClassFieldMap.CreateMapping(pNAClass.ClassDefinition, PointFeatureClass.Fields);
                pNAClassLoader.FieldMap = naClassFieldMap;

                pNAClassLoader.Locator = locator;
                pNAClassLoader.NAClass = pNAClass;

                int rowsIn = 0;
                int rowsLocated = 0;
                ICursor pCursor = PointFeatureClass.Search(null, true) as ICursor;
                pNAClassLoader.Load(pCursor, null, ref rowsIn, ref rowsLocated);
            }
            catch(Exception exp)
            {
                System.Diagnostics.Debug.WriteLine(“LoadLocation方法出错  ” + exp.Message);
                return false;
            }
            
            return true;
        }

        /// <summary>
        /// 打开GDB工作空间
        /// </summary>
        /// <param name=”gdbFileName”></param>
        /// <returns></returns>
        IWorkspace OpenGdbWorkspace(string gdbFileName)
        {
            IWorkspaceFactory wf = new FileGDBWorkspaceFactoryClass();
            if (!System.IO.Directory.Exists(gdbFileName))
            {               
                System.Diagnostics.Debug.WriteLine(“LoadLocation方法出错  ” + “文件数据库: ” + gdbFileName + ” 不存在,请检查配置文件”);
                return null;
            }
            IWorkspace workspace = null;
            try
            {
                workspace = wf.OpenFromFile(gdbFileName, 0);
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine(“OpenGdbWorkspace方法出错  ” + exp.Message);
            }
            finally
            {
                wf = null;
            }

            return workspace;
        }

        /// <summary>
        /// 打开网络数据集
        /// </summary>
        /// <param name=”networkName”>路网图层名称</param>
        /// <param name=”networkDatasetName”>网络数据集名称</param>
        /// <returns></returns> 
        INetworkDataset OpenNetworkDataset(string networkName, string featureDatasetName)
        {
            try
            {                
                IFeatureDataset featureDataset = this.NetworkFeatureWorkspace.OpenFeatureDataset(featureDatasetName);
                IFeatureDatasetExtensionContainer featureDatasetExtensionContainer = featureDataset as IFeatureDatasetExtensionContainer;
                IFeatureDatasetExtension featureDatasetExtension = featureDatasetExtensionContainer.FindExtension(esriDatasetType.esriDTNetworkDataset);
                IDatasetContainer3 datasetContainer3 = featureDatasetExtension as ESRI.ArcGIS.Geodatabase.IDatasetContainer3;

                IDataset dataset = datasetContainer3.get_DatasetByName(esriDatasetType.esriDTNetworkDataset, networkName);

                return dataset as INetworkDataset;
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine(“OpenNetworkDataset方法出错  ” + exp.Message);
                return null;
            }
        }

    }
}

转载自:https://blog.csdn.net/herogui/article/details/46004625

You may also like...