⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 networkutils.cs

📁 AE+vb来实现的最短路径的实现功能!gis
💻 CS
字号:

//Created by Ismael Chivite.  ArcGIS 9.1 Build 647 Nov 2004
using System;

using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.NetworkAnalyst;


//Ismael Chivite
//ESRI Redlands
//March 2005 (Updated)
namespace APL
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class NetworkUtils
	{
		public NetworkUtils()
		{
		}

		public static bool AddNetWorkLocation(INALayer pNALayer,string sNLocationClassName,IPoint pPoint, string sName,string sAgentType, double dTolerance)
		{
			try
			{
				//GetNAContext
				INAContext pNAContext = pNALayer.Context;
				//Locate Network Location

				INALocator pNALocator = pNAContext.Locator;
				pNALocator.SnapTolerance = dTolerance;
				
				int iTotalAgents = pNALocator.LocatorAgentCount;
				for (int n=0;n<iTotalAgents;n++)
				{
					INALocatorAgent pNALocAgent = pNALocator.get_LocatorAgent(n);
					INALocatorFeatureAgent pNALocFeatAgent = pNALocAgent as INALocatorFeatureAgent;
					if (pNALocAgent!=null)
					{
						if (pNALocFeatAgent.SourceName==sName)
						{
							pNALocFeatAgent.SnapType = ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartVertex;
						}
						else
						{
							pNALocFeatAgent.SnapType = ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartNone;
						}
					}
				}

				INALocation pOutNALocation = null;
				IPoint pOutPoint = null;
				double dOutDistanceToNetwork = 0;

			
				pNALocator.QueryLocationByPoint(pPoint,ref pOutNALocation,ref pOutPoint,ref dOutDistanceToNetwork);
				//Add new location
//				((INAContextEdit)pNAContext).StartEditing(false);
//				((INAContextEdit)pNAContext).StartEditOperation();
				INAClass pNAClass = pNAContext.NAClasses.get_ItemByName(sNLocationClassName)as INAClass;
				NetworkUtils.InsertNALocation(pNAClass,pPoint,pOutNALocation,sName,sAgentType);
//				((INAContextEdit)pNAContext).StopEditOperation();
//				((INAContextEdit)pNAContext).StopEditing(true);
				((INAContextEdit)pNAContext).ContextChanged();
				//
				return pOutNALocation.IsLocated;

			}
			catch (Exception exx)
			{
				throw new Exception(exx.Message);
			}
		}
		public static bool AddBarrier(INALayer pNALayer,IPoint pPoint,string sFloorName, bool PolygonBarrier, double dTolerance)
		{
			try
			{
				//GetNAContext
				INAContext pNAContext = pNALayer.Context;
				//Locate Network Location

				INALocator pNALocator = pNAContext.Locator;
				pNALocator.SnapTolerance = dTolerance;
				
				int iTotalAgents = pNALocator.LocatorAgentCount;
				for (int n=0;n<iTotalAgents;n++)
				{
					INALocatorAgent pNALocAgent = pNALocator.get_LocatorAgent(n);
					INALocatorFeatureAgent pNALocFeatAgent = pNALocAgent as INALocatorFeatureAgent;
					if (pNALocAgent!=null)
					{
						if (pNALocFeatAgent.SourceName==sFloorName)
						{
							pNALocFeatAgent.SnapType = ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartVertex;
						}
						else
						{
							pNALocFeatAgent.SnapType = ESRI.ArcGIS.Geometry.esriGeometryHitPartType.esriGeometryPartNone;
						}
					}
				}

				INALocation pOutNALocation = null;
				IPoint pOutPoint = null;
				double dOutDistanceToNetwork = 0;

			
				pNALocator.QueryLocationByPoint(pPoint,ref pOutNALocation,ref pOutPoint,ref dOutDistanceToNetwork);
				//Add new location
				//				((INAContextEdit)pNAContext).StartEditing(false);
				//				((INAContextEdit)pNAContext).StartEditOperation();
				INAClass pNAClass = pNAContext.NAClasses.get_ItemByName("Barriers")as INAClass;
//				NetworkUtils.InsertNALocation(pNAClass,pPoint,pOutNALocation,sName,sAgentType);

				IFeatureClass pFC  = pNAClass as IFeatureClass;
				IFeature pF = pFC.CreateFeature();
				IPoint pPoint2D = new PointClass();
				pPoint2D.PutCoords(pPoint.X,pPoint.Y);

				pF.Shape = pPoint2D;
				if(PolygonBarrier)
				{
					pF.set_Value(pF.Fields.FindField("Origin"),"FromPolygonBarrier");
				}
				else
				{
					pF.set_Value(pF.Fields.FindField("Origin"),"FromPoint");
				}
				INALocationObject pNALocationObject = pF as INALocationObject;
				pNALocationObject.NALocation = pOutNALocation;

				pF.set_Value(2,"Barrier");

				if (pOutNALocation.IsLocated)
				{
					pF.set_Value(8,0);
				}
				else
				{
					pF.set_Value(8,1);
				}


				pF.Store();

				((INAContextEdit)pNAContext).ContextChanged();

				return pOutNALocation.IsLocated;

			}
			catch (Exception exx)
			{
				throw new Exception(exx.Message);
			}
		}


		public static void RemoveNAObjects(INALayer pNALayer,string sNLocationClassName)
		{

			//This function does not work on the server
			try
			{
				//GetNAContext
				INAContext pNAContext = pNALayer.Context;
				//Remove all rows
				INAClass pNAClass = pNAContext.NAClasses.get_ItemByName(sNLocationClassName)as INAClass;
				if (pNAClass == null)
				{
					throw new Exception(sNLocationClassName + " class does not exist. No objects can be removed");
				}
				pNAClass.DeleteAllRows();
			}
			catch (Exception exx)
			{
				throw new Exception(exx.Message);
			}
		}
//		public static void SetSolverProperty(INALayer pNALayer,string sPropertyName,object sValue)
//		{
////			//GetNAContext
////			INAContext pNAContext = pNALayer.Context;
////
////			IPropertySet pPSet = pNAContext.InputProperties as IPropertySet;
////			pPSet.SetProperty(sPropertyName,sValue);
////
////			IGPMessages pGPMessages = new GPMessagesClass();
////			pNAContext.Solver.UpdateContext(pNAContext,NetworkUtils.GetDENetworkDataset(pNAContext.NetworkDataset),pGPMessages);
//
//		}
		public static void SetSolverRestrictions(INALayer pNALayer,string sRestrictions,char sSeparator)
		{
			//GetNAContext
			INAContext pNAContext = pNALayer.Context;

			INASolverSettings pSolverSettings = pNAContext.Solver as INASolverSettings;
		
			IStringArray pStringArray = pSolverSettings.RestrictionAttributeNames;//soc.CreateObject("esriSystem.StrArray") as  IStringArray;
			pStringArray.RemoveAll();

			string[] sArrayRestrictions = sRestrictions.Split(sSeparator);
			int iTotalRestrictions = sArrayRestrictions.Length;
			for (int i = 0;i<iTotalRestrictions;i++)
			{
				if(sArrayRestrictions[i] != "") pStringArray.Add(sArrayRestrictions[i]);
			}

			pSolverSettings.RestrictionAttributeNames = pStringArray;

			IGPMessages pGPMessages = new GPMessagesClass();
			//pSolverSettings.Bind(pNAContext.NetworkDataset,pGPMessages);
			pNAContext.Solver.UpdateContext(pNAContext,NetworkUtils.GetDENetworkDataset(pNAContext.NetworkDataset),pGPMessages);

		}
		public static void SetImpedance(INALayer pNALayer,string sImpedanceAttributeName)
		{
			//GetSolver
			INASolver pNASolver = pNALayer.Context.Solver;
			((INASolverSettings)pNASolver).ImpedanceAttributeName = sImpedanceAttributeName;

			IGPMessages pGPMessages = new GPMessagesClass();
			pNASolver.UpdateContext(pNALayer.Context,GetDENetworkDataset(pNALayer.Context.NetworkDataset),pGPMessages);
		}
		public static void Solve(INALayer pNALayer)
		{
			//GetNAContext
			INAContext pNAContext = pNALayer.Context;
			IGPMessages pGPMessages = new GPMessagesClass();
			pNAContext.Solver.Solve(pNAContext,pGPMessages,null);
		}
	


		private static IDENetworkDataset GetDENetworkDataset(INetworkDataset pNetDS)
		{
			IDatasetComponent pDatasetComponent = pNetDS as IDatasetComponent;
			return pDatasetComponent.DataElement as IDENetworkDataset;
		}


		private static void InsertNALocation(INAClass pNAClass,IPoint pPoint,INALocation pNALocation,string sName,string sAgentType)
		{
			string sNLocationClassName = pNAClass.ClassDefinition.Name;
			//System.Windows.Forms.MessageBox.Show("Floor is: " + sFloor);
			IFeatureClass pFC  = pNAClass as IFeatureClass;
			IFeature pF = pFC.CreateFeature();
			IPoint pPoint2D = new PointClass();
			pPoint2D.PutCoords(pPoint.X,pPoint.Y);

			pF.Shape = pPoint2D;

			INALocationObject pNALocationObject = pF as INALocationObject;
			pNALocationObject.NALocation = pNALocation;

			if (sName!="") pF.set_Value(2,sName);
			if (sNLocationClassName=="Stops")
			{
				pF.set_Value(3,pFC.FeatureCount(null));
				if (pNALocation.IsLocated)
				{
					pF.set_Value(15, 0);
				}
				else
				{
					pF.set_Value(15, 1);
				}
			}
			else if (sNLocationClassName=="Incidents")
			{
				if (pNALocation.IsLocated)
				{
					pF.set_Value(9,0);
				}
				else
				{
					pF.set_Value(9,1);
				}
				int iAgentTypeField = pF.Fields.FindField("AgentType");
				if (iAgentTypeField>0)
				{
					pF.set_Value(iAgentTypeField,sAgentType);
				}
			}
			else if (sNLocationClassName=="Barriers" || sNLocationClassName=="Facilities")
			{
				if (pNALocation.IsLocated)
				{
					pF.set_Value(8,0);
				}
				else
				{
					pF.set_Value(8,1);
				}
			}

			pF.Store();

		}
	}
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -