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

📄 form1.cs

📁 AE+vb来实现的最短路径的实现功能!gis
💻 CS
📖 第 1 页 / 共 5 页
字号:
			SwitchView( e.KeyCode.ToString() == "90");
		}

		private void SwitchView(bool to3D)
		{
			if(to3D)
			{
				this.tabControl1.SelectedIndex = 1;
			}
			else
			{
				this.tabControl1.SelectedIndex = 0;
			}
		}

		private void ConfigureEvacuationObjects()
		{
			try
			{
				Log("Configuring Evacuation Objects...");
				INALayer pNALayer = ESRIControlUtils.GetLayerFromMap(this.Map2D,"Closest Facility") as INALayer;
				//Solve
				INAContext pNAContext = pNALayer.Context;

				//Initialize Traversal Result
				INATraversalResultQuery pTraverse = pNAContext.Result as INATraversalResultQuery;
				INATraversalResult pNATraversalResult =  pTraverse as INATraversalResult;
				IFeatureClass pFCEdges = pTraverse.get_FeatureClass(ESRI.ArcGIS.Geodatabase.esriNetworkElementType.esriNETEdge);
				int idxEdgeSourceID =  pFCEdges.Fields.FindField("SourceID");
				int idxEdgeSourceOID =  pFCEdges.Fields.FindField("SourceOID");
				int iFacilityIDIdx =  pFCEdges.Fields.FindField("PathID");

				//Configure Agents and Network Positions
				int iTotalAgents = MiscUtils.GetTotalPathIds(pNALayer);
				ArrayList pAgents = new ArrayList(iTotalAgents);
				Hashtable pNetworkPositions = new  Hashtable();
				
				Log("Configuring agents...");
				for (int iCurrentAgent = 1; iCurrentAgent <= iTotalAgents;iCurrentAgent++)
				{
					IAgent pAgent = null;
					string sAgentType = MiscUtils.GetAgentTypeByID(pNALayer,iCurrentAgent );
					switch (sAgentType)
					{
						case ("Young"):
							pAgent = new Young();
							break;
						case ("Agent"):
							pAgent = new Agent();
							break;
						case ("Child"):
							break;
						case ("Senior"):
							pAgent = new Senior();
							break;
					}
					pAgent.Type = sAgentType;
					pAgent.Name = sAgentType + iCurrentAgent.ToString();
					System.Collections.ArrayList positionsForAgent = new ArrayList();
					Log("Configuring agent (" + sAgentType + ") " + iCurrentAgent.ToString() + " of " +  iTotalAgents.ToString());
					
					long lFacilityID = MiscUtils.GetTargetFacilityByID(pNALayer,iCurrentAgent );
					pAgent.TargetFacility = lFacilityID;
					IFeature pSourceEdgeF = null;
					IFeatureClass pSourceEdgeFC = null;
					int iSourceID = 0,iSourceOID = 0;long lUniqueID = 0;int iCapacity = 0;double dRotation = 0;double dElev = 0;int iOneTimeDelay = 0;

					IFeatureCursor pFCursorEdges = MiscUtils.GetEdgesByPathID(pFCEdges,iCurrentAgent);
					IFeature pEdge = pFCursorEdges.NextFeature();
					while (pEdge!=null)
					{
						//GetSourceFeature
						iSourceID = Convert.ToInt32(pEdge.get_Value(idxEdgeSourceID));
						iSourceOID = Convert.ToInt32(pEdge.get_Value(idxEdgeSourceOID));
						pSourceEdgeFC = pNATraversalResult.get_SourceByID(iSourceID).Table as IFeatureClass;
						pSourceEdgeF = pSourceEdgeFC.GetFeature(iSourceOID);
						//iFacilityID = Convert.ToInt32(pEdge.get_Value(iFacilityIDIdx));
						//Get UniqueID
						lUniqueID = Convert.ToInt32(pSourceEdgeF.get_Value(7));
						//Find similar OID in other agents
						bool b = MiscUtils.GetSimilarRoute(pAgents,lFacilityID,lUniqueID,ref positionsForAgent);
						if(!b)
						{
							//Add UniqueID to positionsfor agent
							positionsForAgent.Add(lUniqueID);
							//Add to networkpositions if necessary
							if(!pNetworkPositions.ContainsKey(lUniqueID))
							{ 	
								//Get Rotation, Capacity, Calculate Locations and so on
								//Create new Position Object
								//Add it to the hashtable
								iCapacity = Convert.ToInt32(pSourceEdgeF.get_Value(5));
								dRotation = Convert.ToInt32(pSourceEdgeF.get_Value(6));
								dElev = Convert.ToDouble(pSourceEdgeF.get_Value(8));
								iOneTimeDelay = Convert.ToInt32(pSourceEdgeF.get_Value(9));
								NetworkPosition pNetworkPosition = new NetworkPosition();
								pNetworkPosition.Capacity = iCapacity;
								pNetworkPosition.UniqueID = lUniqueID;
								pNetworkPosition.Rotation = dRotation;
								pNetworkPosition.OneTimeDelay = iOneTimeDelay;
								ICurve pCurve = pSourceEdgeF.Shape as ICurve;
								switch(iCapacity)
								{
									case 1:
										IConstructPoint pPoint1 = new PointClass()as IConstructPoint;
										pPoint1.ConstructAlong(pCurve,ESRI.ArcGIS.Geometry.esriSegmentExtension.esriNoExtension,0.5,true);
										((IPoint)pPoint1).Z = dElev;
										pNetworkPosition.SetLocation(0,pPoint1 as IPoint);
										break;
									case 2:
										IConstructPoint pPoint2a = new PointClass()as IConstructPoint;
										pPoint2a.ConstructOffset(pCurve,ESRI.ArcGIS.Geometry.esriSegmentExtension.esriNoExtension,0.4,true,-0.7);
										((IPoint)pPoint2a).Z = dElev;
										IConstructPoint pPoint2b = new PointClass()as IConstructPoint;
										pPoint2b.ConstructOffset(pCurve,ESRI.ArcGIS.Geometry.esriSegmentExtension.esriNoExtension,0.55,true,0.7);
										((IPoint)pPoint2b).Z = dElev;

										pNetworkPosition.SetLocation(0,pPoint2a as IPoint);
										pNetworkPosition.SetLocation(1,pPoint2b as IPoint);
										break;
									case 3:
										IConstructPoint pPoint3a = new PointClass()as IConstructPoint;
										pPoint3a.ConstructOffset(pCurve,ESRI.ArcGIS.Geometry.esriSegmentExtension.esriNoExtension,0.4,true,-0.7);
										((IPoint)pPoint3a).Z = dElev;
										IConstructPoint pPoint3b = new PointClass()as IConstructPoint;
										pPoint3b.ConstructOffset(pCurve,ESRI.ArcGIS.Geometry.esriSegmentExtension.esriNoExtension,0.55,true,0.7);
										((IPoint)pPoint3b).Z = dElev;
										IConstructPoint pPoint3c = new PointClass()as IConstructPoint;
										pPoint3c.ConstructAlong(pCurve,ESRI.ArcGIS.Geometry.esriSegmentExtension.esriNoExtension,0.4,true);
										((IPoint)pPoint3c).Z = dElev;

										pNetworkPosition.SetLocation(0,pPoint3a as IPoint);
										pNetworkPosition.SetLocation(1,pPoint3b as IPoint);
										pNetworkPosition.SetLocation(2,pPoint3c as IPoint);
										break;
									default:
										System.Diagnostics.Debug.WriteLine("Capacity must be greater than zero and smaller than 4");
										break;
								}
								pNetworkPositions.Add(lUniqueID,pNetworkPosition);
							}
							pEdge = pFCursorEdges.NextFeature();
						}
						else
						{
							pEdge = null;
						}
					}
					//Load positions in agent
					pAgent.LoadPositionIds(positionsForAgent);
					pAgent.LoadPositions(pNetworkPositions);
					//Add Agent to collection
					pAgents.Add(pAgent);

					//Release
					System.Runtime.InteropServices.Marshal.ReleaseComObject(pFCursorEdges);
					
				}

				//Run Model
				IFeatureLayer pFLAgentMotion = ESRIControlUtils.GetLayerFromMap(this.Map2D,"AgentMotion") as IFeatureLayer;
				RunEvacuation(pAgents,pNetworkPositions,pFLAgentMotion.FeatureClass);
				//Enable buttons
				this.BtnAnimate.Enabled = true;
			}
			catch (Exception exx)
			{
				Log("ConfigureEvacuationObjects " + exx.Message);
			}
		}
		private void RunEvacuation(ArrayList pAgentArrayList,Hashtable pNetworkPositions,IFeatureClass pFCAgentMotion)
		{
			try
			{
				Log("Cleaning previous animations...");
				// Clean up animations
				MiscUtils.CleanupFC( ((IFeatureLayer)m_AgentAnimation2DLayer).FeatureClass );

				Log("Running evacuation...");
				((ITable)pFCAgentMotion).DeleteSearchedRows(null);
				IFeatureCursor pInsertCursor = pFCAgentMotion.Insert(true);
				IFeatureBuffer pFBuffer = null;
				//Run agents
				//Put agents in initial position
				int iTotalAgents = pAgentArrayList.Count;
				for (int i = 0;i<iTotalAgents;i++)
				{
					IAgent pAgent = pAgentArrayList[i] as IAgent;
					long iCurrentPos = pAgent.CurrentPositionId;
					NetworkPosition pNetNextPosition = pNetworkPositions[iCurrentPos] as NetworkPosition;
					if (pNetNextPosition==null)
					{
						throw new Exception("Position " + iCurrentPos.ToString() + " has not been found in the network");
					}
					else
					{
						pNetNextPosition.AddAgent(true);
					}
				}
				//Loop until solved
				int iTotalSteps = 0;
				while(iTotalAgents>0)
				{
					//Sort agents based on distance to exit
					pAgentArrayList.Sort();
					//Move forward agents based on sorted list
					for (int i = 0;i<iTotalAgents;i++)
					{
						IAgent pAgent = pAgentArrayList[i] as IAgent;
						pAgent.MoveForward();
						NetworkPosition pNetCurrentPosition = pAgent.CurrentPosition;
					
						//Log agent position
						pFBuffer = pFCAgentMotion.CreateFeatureBuffer();
						pFBuffer.Shape = pNetCurrentPosition.Location;
						pFBuffer.set_Value(3, pAgent.Name );//3 Agent Name
						pFBuffer.set_Value(4, iTotalSteps );//4 Step number
						pFBuffer.set_Value(5, pNetCurrentPosition.Rotation );//5 Rotation
						pFBuffer.set_Value(6, pAgent.Status );//6 Status
						pFBuffer.set_Value(7, pAgent.Type );//7 Type
						pInsertCursor.InsertFeature(pFBuffer);
					}
					//Remove Agents that have arrived
					ArrayList pNonArrivedAgents = new ArrayList();
					for (int i = 0;i<iTotalAgents;i++)
					{
						IAgent pAgent = pAgentArrayList[i] as IAgent;
						if (!pAgent.Arrived)
						{
							pNonArrivedAgents.Add(pAgentArrayList[i]);
						}
						else
						{
							long lCurrentPosition = pAgent.CurrentPositionId;
							NetworkPosition pNetNextPosition = pNetworkPositions[lCurrentPosition] as NetworkPosition;
							pNetNextPosition.RemoveAgent();
						}
					}
					pAgentArrayList = pNonArrivedAgents;
					//Count remaining Agents
					iTotalAgents = pAgentArrayList.Count;
					iTotalSteps++;
				}


				pInsertCursor.Flush();
				m_AgentAnimation2DLayer.DefinitionExpression = "Step = 0";


				//Log(("----------Step " + iTotalSteps.ToString() + " -----------------"));
				this.SliderSeconds.Minimum = 0;
				this.SliderSeconds.Maximum = iTotalSteps;
				this.SliderSeconds.Visible = true;
				this.SliderSeconds.Value = 0;
				this.groupClosestFacility.Visible = true;

				double dTotalSecs = iTotalSteps * 0.75; //One ste every 0.75 steps
				this.LblSlider.Text = MiscUtils.SecondsToString(dTotalSecs);;
				this.LblSlider.Visible = true;

				Log("");
			}
			catch (Exception exx)
			{
				Log("RunEvacuation " + exx.Message);
			}

		}

		private void Log(string sMessage)
		{
			this.statusBar1.Panels[0].Text = sMessage;
			System.Diagnostics.Debug.WriteLine(sMessage);
		}

		private void BtnAnimate_Click(object sender, System.EventArgs e)
		{
			this.timer1.Interval = 200;
			this.timer1.Enabled = (!this.timer1.Enabled);

			if (this.BtnAnimate.Text == "Animate")
			{
				
				this.BtnAnimate.Text = "Stop";
				//Toggle visibility of layers
				((ILayer)m_AgentAnimation2DLayer).Visible = true;
				ESRIControlUtils.GetLayerFromMap(this.Map2D,m_s2DRouteGroup).Visible = false;
				ESRIControlUtils.GetLayerFromMap(this.Map2D,m_sClosestFacilityLayer).Visible = false;

				((ILayer)m_AgentAnimation3DLayer).Visible = true;
				ESRIControlUtils.GetLayerFromMap(this.Map3D,m_sStops3D).Visible = false;
				ESRIControlUtils.GetLayerFromMap(this.Map3D,m_sRoute3D).Visible = false;	
			}
			else
			{
				this.SliderSeconds.Value = 0;
				this.BtnAnimate.Text = "Animate";
				//Toggle visibility of layers
				((ILayer)m_AgentAnimation2DLayer).Visible = false;
				ESRIControlUtils.GetLayerFromMap(this.Map2D,m_s2DRouteGroup).Visible = true;
				ESRIControlUtils.GetLayerFromMap(this.Map2D,m_sClosestFacilityLayer).Visible = true;

				((ILayer)m_AgentAnimation3DLayer).Visible = false;
				ESRIControlUtils.GetLayerFromMap(this.Map3D,m_sStops3D).Visible = true;
			

⌨️ 快捷键说明

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