//关闭工作空间 private void CloseWorkspace() { m_ipGeometricNetwork = null; m_ipPoints = null; m_ipPointToEID = null; m_ipEnumNetEID_Junctions = null; m_ipEnumNetEID_Edges = null; m_ipPolyline = null; }
private bool InitializeNetworkAndMap(IFeatureDataset FeatureDataset) { bool flag = false; try { IFeatureLayer layer; int num; this.m_ipGeometricNetwork = ((INetworkCollection)FeatureDataset).get_GeometricNetwork(0); INetwork network = this.m_ipGeometricNetwork.Network; if (this.m_ipMap == null) { this.m_ipMap = new MapClass(); IFeatureClassContainer ipGeometricNetwork = (IFeatureClassContainer)this.m_ipGeometricNetwork; for (num = 0; num < ipGeometricNetwork.ClassCount; num++) { IFeatureClass class2 = ipGeometricNetwork.get_Class(num); layer = new FeatureLayerClass { FeatureClass = class2 }; this.m_ipMap.AddLayer(layer); } } IEnvelope envelope = new EnvelopeClass(); for (num = 0; num < this.m_ipMap.LayerCount; num++) { layer = (IFeatureLayer)this.m_ipMap.get_Layer(num); IGeoDataset dataset = (IGeoDataset)layer; IEnvelope extent = dataset.Extent; envelope.Union(extent); } this.m_ipPointToEID = new PointToEIDClass(); this.m_ipPointToEID.SourceMap = this.m_ipMap; this.m_ipPointToEID.GeometricNetwork = this.m_ipGeometricNetwork; double width = envelope.Width; double height = envelope.Height; if (width > height) { this.m_ipPointToEID.SnapTolerance = width / 100.0; } else { this.m_ipPointToEID.SnapTolerance = height / 100.0; } flag = true; } catch (Exception exception) { MessageBox.Show(exception.ToString()); } return(flag); }
//初始化 private bool InitializeNetworkAndMap(IFeatureDataset Featuredataset,System.Collections.ArrayList array1,System.Collections.ArrayList array2,int flag) { IFeatureClassContainer ipFeatureClassContainer; IFeatureClass ipFeatureClass; IGeoDataset ipGeoDataset; ILayer ipLayer; IFeatureLayer ipFeatureLayer; IEnvelope ipEnvelope, ipMaxEnvelope; double dblSearchTol; INetworkCollection ipNetworkCollection = Featuredataset as INetworkCollection; int count = ipNetworkCollection.GeometricNetworkCount; barrierarray1 =(System.Collections.ArrayList)array1.Clone(); barrierarray2 = (System.Collections.ArrayList)array2.Clone(); barrierflag = flag; //获取几何网络工作空间 m_ipGeometricNetwork = ipNetworkCollection.get_GeometricNetwork(0); INetwork ipNetwork = m_ipGeometricNetwork.Network; if (m_ipMap != null) { m_ipMap = new MapClass(); ipFeatureClassContainer = m_ipGeometricNetwork as IFeatureClassContainer; count = ipFeatureClassContainer.ClassCount; for (int i = 0; i < count; i++) { ipFeatureClass = ipFeatureClassContainer.get_Class(i); ipFeatureLayer = new FeatureLayerClass(); ipFeatureLayer.FeatureClass = ipFeatureClass; m_ipMap.AddLayer(ipFeatureLayer); } } count = m_ipMap.LayerCount; ipMaxEnvelope = new EnvelopeClass(); for (int i = 0; i < count; i++) { ipLayer = m_ipMap.get_Layer(i); ipFeatureLayer = ipLayer as IFeatureLayer; ipGeoDataset = ipFeatureLayer as IGeoDataset; ipEnvelope = ipGeoDataset.Extent; ipMaxEnvelope.Union(ipEnvelope); } m_ipPointToEID = new PointToEIDClass(); m_ipPointToEID.SourceMap = m_ipMap; m_ipPointToEID.GeometricNetwork = m_ipGeometricNetwork; double dblWidth = ipMaxEnvelope.Width; double dblHeight = ipMaxEnvelope.Height; if (dblWidth > dblHeight) dblSearchTol = dblWidth / 100; else dblSearchTol = dblHeight / 100; m_ipPointToEID.SnapTolerance = dblSearchTol; return true; }
private void CloseWorkspace() { m_ipGeometricNetwork = null; m_ipPoints = null; m_ipPointToEID = null; m_ipEnumNetEID_Junctions = null; m_ipEnumNetEID_Edges = null; m_ipPolyline = null; }
private void Network_Load(object sender, EventArgs e) { //获取几何网络文件路径 //注意修改此路径为当前存储路径 string strPath = Application.StartupPath + @"\nanjingroad.mdb"; //打开工作空间 IWorkspaceFactory pWorkspaceFactory = new AccessWorkspaceFactory(); IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(strPath, 0) as IFeatureWorkspace; //获取要素数据集 //注意名称的设置要与上面创建保持一致 IFeatureDataset pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset("road_merge"); //获取network集合 INetworkCollection pNetWorkCollection = pFeatureDataset as INetworkCollection; //获取network的数量,为零时返回 int intNetworkCount = pNetWorkCollection.GeometricNetworkCount; //if (intNetworkCount < 1) // return; //FeatureDataset可能包含多个network,我们获取指定的network //注意network的名称的设置要与上面创建保持一致 mGeometricNetwork = pNetWorkCollection.get_GeometricNetworkByName("road_merge_Net"); //将Network中的每个要素类作为一个图层加入地图控件 IFeatureClassContainer pFeatClsContainer = mGeometricNetwork as IFeatureClassContainer; //获取要素类数量,为零时返回 int intFeatClsCount = pFeatClsContainer.ClassCount; if (intFeatClsCount < 1) { return; } IFeatureClass pFeatureClass; IFeatureLayer pFeatureLayer; for (int i = 0; i < intFeatClsCount; i++) { //获取要素类 pFeatureClass = pFeatClsContainer.get_Class(i); pFeatureLayer = new FeatureLayerClass(); pFeatureLayer.FeatureClass = pFeatureClass; pFeatureLayer.Name = pFeatureClass.AliasName; //加入地图控件 this.axMapControl1.AddLayer((ILayer)pFeatureLayer, 0); } //计算snap tolerance为图层最大宽度的1/100 //获取图层数量 int intLayerCount = this.axMapControl1.LayerCount; IGeoDataset pGeoDataset; IEnvelope pMaxEnvelope = new EnvelopeClass(); for (int i = 0; i < intLayerCount; i++) { //获取图层 pFeatureLayer = this.axMapControl1.get_Layer(i) as IFeatureLayer; pGeoDataset = pFeatureLayer as IGeoDataset; //通过Union获得较大图层范围 pMaxEnvelope.Union(pGeoDataset.Extent); } double dblWidth = pMaxEnvelope.Width; double dblHeight = pMaxEnvelope.Height; double dblSnapTol; if (dblHeight < dblWidth) { dblSnapTol = dblWidth * 0.05; } else { dblSnapTol = dblHeight * 0.05; } //设置源地图,几何网络以及捕捉容差 mPointToEID = new PointToEIDClass(); mPointToEID.SourceMap = this.axMapControl1.Map; mPointToEID.GeometricNetwork = mGeometricNetwork; mPointToEID.SnapTolerance = dblSnapTol; }
////几何网络 //private IGeometricNetwork mGeometricNetwork; ////给定点的集合 //private IPointCollection mPointCollection; ////获取给定点最近的Network元素 //private IPointToEID mPointToEID; ////返回结果变量 //private IEnumNetEID mEnumNetEID_Junctions; //private IEnumNetEID mEnumNetEID_Edges; //private double mdblPathCost; /// <summary> /// 实现路径分析 /// </summary> /// <param name="weightName">路径权重字段</param> public void SolvePath(string weightName, IGeometricNetwork mGeometricNetwork, IPointCollection mPointCollection, IPointToEID mPointToEID, out IEnumNetEID mEnumNetEID_Junctions, out IEnumNetEID mEnumNetEID_Edges, out double mdblPathCost) { //创建ITraceFlowSolverGEN ITraceFlowSolverGEN pTraceFlowSolverGEN = new TraceFlowSolverClass(); INetSolver pNetSolver = pTraceFlowSolverGEN as INetSolver; //初始化用于路径计算的Network INetwork pNetWork = mGeometricNetwork.Network; pNetSolver.SourceNetwork = pNetWork; //获取分析经过的点的个数 int intCount = mPointCollection.PointCount; if (intCount < 1) { MessageBox.Show("未获取到车辆点或目标点!"); } INetFlag pNetFlag; //用于存储路径计算得到的边 IEdgeFlag[] pEdgeFlags = new IEdgeFlag[intCount]; IPoint pEdgePoint = new PointClass(); int intEdgeEID; IPoint pFoundEdgePoint; double dblEdgePercent; //用于获取几何网络元素的UserID, UserClassID,UserSubID INetElements pNetElements = pNetWork as INetElements; int intEdgeUserClassID; int intEdgeUserID; int intEdgeUserSubID; for (int i = 0; i < intCount; i++) { pNetFlag = new EdgeFlagClass(); //获取用户点击点 pEdgePoint = mPointCollection.get_Point(i); //获取距离用户点击点最近的边 mPointToEID.GetNearestEdge(pEdgePoint, out intEdgeEID, out pFoundEdgePoint, out dblEdgePercent); if (intEdgeEID <= 0) { continue; } //根据得到的边查询对应的几何网络中的元素UserID, UserClassID,UserSubID pNetElements.QueryIDs(intEdgeEID, esriElementType.esriETEdge, out intEdgeUserClassID, out intEdgeUserID, out intEdgeUserSubID); if (intEdgeUserClassID <= 0 || intEdgeUserID <= 0) { continue; } pNetFlag.UserClassID = intEdgeUserClassID; pNetFlag.UserID = intEdgeUserID; pNetFlag.UserSubID = intEdgeUserSubID; pEdgeFlags[i] = pNetFlag as IEdgeFlag; } //设置路径求解的边 pTraceFlowSolverGEN.PutEdgeOrigins(ref pEdgeFlags); //路径计算权重 INetSchema pNetSchema = pNetWork as INetSchema; INetWeight pNetWeight = pNetSchema.get_WeightByName(weightName); if (pNetWeight == null) { MessageBox.Show("权重设置有误!"); } //设置权重,这里双向的权重设为一致 INetSolverWeights pNetSolverWeights = pTraceFlowSolverGEN as INetSolverWeights; pNetSolverWeights.ToFromEdgeWeight = pNetWeight; pNetSolverWeights.FromToEdgeWeight = pNetWeight; object[] arrResults = new object[intCount - 1]; //执行路径计算 pTraceFlowSolverGEN.FindPath(esriFlowMethod.esriFMConnected, esriShortestPathObjFn.esriSPObjFnMinSum, out mEnumNetEID_Junctions, out mEnumNetEID_Edges, intCount - 1, ref arrResults); //获取路径计算总代价(cost) mdblPathCost = 0; for (int i = 0; i < intCount - 1; i++) { mdblPathCost += (double)arrResults[i]; } }
//初始化几何网络和地图 private bool InitializeNetworkAndMap(IFeatureDataset FeatureDataset) { IFeatureClassContainer ipFeatureClassContainer; IFeatureClass ipFeatureClass; IGeoDataset ipGeoDataset; ILayer ipLayer; IFeatureLayer ipFeatureLayer; IEnvelope ipEnvelope, ipMaxEnvelope; double dblSearchTol; INetworkCollection ipNetworkCollection = FeatureDataset as INetworkCollection; int count = ipNetworkCollection.GeometricNetworkCount; //获取第一个几何网络工作空间 m_ipGeometricNetwork = ipNetworkCollection.get_GeometricNetwork(0); INetwork ipNetwork = m_ipGeometricNetwork.Network; if (m_ipMap != null) { //m_ipMap = new MapClass(); ipFeatureClassContainer = m_ipGeometricNetwork as IFeatureClassContainer; count = ipFeatureClassContainer.ClassCount; for (int i = 0; i < count; i++) { ipFeatureClass = ipFeatureClassContainer.get_Class(i); ipFeatureLayer = new FeatureLayerClass(); ipFeatureLayer.FeatureClass = ipFeatureClass; for (int j = 0; j < m_ipMap.LayerCount; j++) { if (m_ipMap.get_Layer(j).Name.ToUpper() == ipFeatureLayer.Name.ToUpper()) { continue; } } m_ipMap.AddLayer(ipFeatureLayer); } m_ipActiveView.Refresh(); } count = m_ipMap.LayerCount; ipMaxEnvelope = new EnvelopeClass(); for (int i = 0; i < count; i++) { ipLayer = m_ipMap.get_Layer(i); ipFeatureLayer = ipLayer as IFeatureLayer; ipGeoDataset = ipFeatureLayer as IGeoDataset; ipEnvelope = ipGeoDataset.Extent; ipMaxEnvelope.Union(ipEnvelope); } m_ipPointToEID = new PointToEIDClass(); m_ipPointToEID.SourceMap = m_ipMap; m_ipPointToEID.GeometricNetwork = m_ipGeometricNetwork; double dblWidth = ipMaxEnvelope.Width; double dblHeight = ipMaxEnvelope.Height; if (dblWidth > dblHeight) dblSearchTol = dblWidth / 100; else dblSearchTol = dblHeight / 100; m_ipPointToEID.SnapTolerance = dblSearchTol; return true; }
//初始化几何网络和地图 private bool InitializeNetworkAndMap(IFeatureDataset FeatureDataset) { IFeatureClassContainer ipFeatureClassContainer; IFeatureClass ipFeatureClass; IGeoDataset ipGeoDataset; ILayer ipLayer; IFeatureLayer ipFeatureLayer; IEnvelope ipEnvelope, ipMaxEnvelope; double dblSearchTol; INetworkCollection ipNetworkCollection = FeatureDataset as INetworkCollection; int count = ipNetworkCollection.GeometricNetworkCount; //获取第一个几何网络工作空间 m_ipGeometricNetwork = ipNetworkCollection.get_GeometricNetwork(0); INetwork ipNetwork = m_ipGeometricNetwork.Network; if (m_ipMap != null) { //m_ipMap = new MapClass(); ipFeatureClassContainer = m_ipGeometricNetwork as IFeatureClassContainer; count = ipFeatureClassContainer.ClassCount; for (int i = 0; i < count; i++) { ipFeatureClass = ipFeatureClassContainer.get_Class(i); ipFeatureLayer = new FeatureLayerClass(); ipFeatureLayer.FeatureClass = ipFeatureClass; for (int j = 0; j < m_ipMap.LayerCount; j++) { if (m_ipMap.get_Layer(j).Name.ToUpper() == ipFeatureLayer.Name.ToUpper()) { continue; } } m_ipMap.AddLayer(ipFeatureLayer); } m_ipActiveView.Refresh(); } count = m_ipMap.LayerCount; ipMaxEnvelope = new EnvelopeClass(); for (int i = 0; i < count; i++) { ipLayer = m_ipMap.get_Layer(i); ipFeatureLayer = ipLayer as IFeatureLayer; ipGeoDataset = ipFeatureLayer as IGeoDataset; ipEnvelope = ipGeoDataset.Extent; ipMaxEnvelope.Union(ipEnvelope); } m_ipPointToEID = new PointToEIDClass(); m_ipPointToEID.SourceMap = m_ipMap; m_ipPointToEID.GeometricNetwork = m_ipGeometricNetwork; double dblWidth = ipMaxEnvelope.Width; double dblHeight = ipMaxEnvelope.Height; if (dblWidth > dblHeight) { dblSearchTol = dblWidth / 100; } else { dblSearchTol = dblHeight / 100; } m_ipPointToEID.SnapTolerance = dblSearchTol; return(true); }
private bool slovePath() { try { double costLength = 0; m_ipPointToEID = new PointToEIDClass(); m_ipPointToEID.SourceMap = m_HookHelper.ActiveView.FocusMap; m_ipPointToEID.GeometricNetwork = m_ipGeometricNetwork; m_ipPointToEID.SnapTolerance = m_HookHelper.ActiveView.Extent.Envelope.Width / 100; INetwork ipNetwork; INetElements ipNetElements; INetFlag ipNetFlag; //IEdgeFlag ipEdgeFlag; IEdgeFlag [] EdgeFlagArray; int pEdgeID; IPoint pLocation; double dblEdgePercent; int iUserClassID, iUserID, iUserSubID; // look up the EID for the current point (this will populate intEdgeID and dblEdgePercent) ipTraceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolver; //get the inetsolver interface ipNetSolver = ipTraceFlowSolver as INetSolver; ipNetwork = m_ipGeometricNetwork.Network; //set the net solver's source network ipNetSolver.SourceNetwork = ipNetwork; //make edge flags from the points //the INetElements interface is needed to get UserID, UserClassID, //and UserSubID from an element id EdgeFlagArray = new IEdgeFlag[mPointArray.PointCount]; ipNetElements = ipNetwork as INetElements; for (int ii = 0; ii < mPointArray.PointCount; ii++) { ipNetFlag = new EdgeFlagClass(); point = mPointArray.get_Point(ii); m_ipPointToEID.GetNearestEdge(point, out pEdgeID, out pLocation, out dblEdgePercent); //iUserClassID是featureclass ID,userID是objectID,userSubID ipNetElements.QueryIDs(pEdgeID, esriElementType.esriETEdge, out iUserClassID, out iUserID, out iUserSubID); ipNetFlag.UserClassID = iUserClassID; ipNetFlag.UserID = iUserID; ipNetFlag.UserSubID = iUserSubID; EdgeFlagArray[ii] = ipNetFlag as IEdgeFlag; //MessageBox.Show(iUserID.ToString()); } //添加到TraceFlowSolver中 ITraceFlowSolverGEN pTFSolverGEN = ipTraceFlowSolver as ITraceFlowSolverGEN; pTFSolverGEN.PutEdgeOrigins(ref EdgeFlagArray); //get the INetSchema interface INetSchema ipNetSchema = ipNetwork as INetSchema; INetWeight ipNetWeight = ipNetSchema.get_Weight(0); //MessageBox.Show(ipNetWeight.WeightName); INetSolverWeights ipNetSolverWeights = ipTraceFlowSolver as INetSolverWeights; //来回使用同一个权重 ipNetSolverWeights.FromToEdgeWeight = ipNetWeight; ipNetSolverWeights.ToFromEdgeWeight = ipNetWeight; object [] totalCost = new object[mPointArray.PointCount - 1]; pTFSolverGEN.FindPath(esriFlowMethod.esriFMConnected, esriShortestPathObjFn.esriSPObjFnMinSum, out m_ipEnumNetEID_Junctions, out m_ipEnumNetEID_Edges, mPointArray.PointCount - 1, ref totalCost); //MessageBox.Show(totalCost[0].ToString()); for (int ii = 0; ii < totalCost.Length; ii++) { costLength += (double)(totalCost[ii]); } pStatusBar.Panels[0].Text = "水管长度:" + costLength.ToString(); return(true); } catch (Exception e) { MessageBox.Show(e.Message); return(false); } }
private void setUpstreamJunctionFlag(IPoint pPoint) { try { if (pGeometricNetwork == null) { openGeoNetwork(); } IEnumNetEID m_ipEnumNetEID_Junctions, m_ipEnumNetEID_Edges; //用来存放搜索结果的Junctions和Edges ipNetwork = pGeometricNetwork.Network; /* * IMarkerSymbol pMarkerSym=SpatialHelperFunction.FindMarkerSym("Civic.ServerStyle", "Default", "Pin Flag Square"); * if(pMarkerSym==null){ * MessageBox.Show("无法取得Symbol"); * return; * } * * IRgbColor pRGBColor=Utility.getRGBColor(255, 0, 0); * * pMarkerSym.Color=pRGBColor; * pMarkerSym.Size=26; * pMarkerSym.XOffset=10; * pMarkerSym.YOffset=10; * //无法使用 IFlagDisplay,只有 INetworkAnalysisExtFlags才可以使用IFlagDisplay * * Utility.drawPointToGraphicLayer(m_HookHelper.ActiveView,pPoint,pMarkerSym); */ int pJunctionID; IPoint pLocation; int iUserClassID, iUserID, iUserSubID; INetElements ipNetElements; ipNetElements = ipNetwork as INetElements; //得到network中所有的Elements mPointToEID = new PointToEIDClass(); //设置它的GeometricNetwork mPointToEID.GeometricNetwork = pGeometricNetwork; //设置容限值 mPointToEID.SnapTolerance = m_HookHelper.ActiveView.Extent.Envelope.Width / 100; //设置Source Map mPointToEID.SourceMap = m_HookHelper.ActiveView.FocusMap; //得到最近的JunctionID mPointToEID.GetNearestJunction(pPoint, out pJunctionID, out pLocation); //Each element in a logical network has a unique element ID (EID). ipNetElements.QueryIDs(pJunctionID, esriElementType.esriETJunction, out iUserClassID, out iUserID, out iUserSubID); //得到所在Junction的ID //无法使用 IFlagDisplay,只有 INetworkAnalysisExtFlags才可以使用IFlagDisplay INetFlag ipNetFlag = new JunctionFlagClass(); ipNetFlag.UserClassID = iUserClassID; ipNetFlag.UserID = iUserID; ipNetFlag.UserSubID = iUserSubID; IJunctionFlag [] pArrayJFlag = new IJunctionFlag[1]; pArrayJFlag[0] = ipNetFlag as IJunctionFlag; ipTraceFlowSolver = new TraceFlowSolverClass() as ITraceFlowSolverGEN; //get the inetsolver interface ipNetSolver = ipTraceFlowSolver as INetSolver; ipNetSolver.SourceNetwork = pGeometricNetwork.Network; ipTraceFlowSolver.PutJunctionOrigins(ref pArrayJFlag); ipTraceFlowSolver.TraceIndeterminateFlow = false; object[] totalCost = new object[1]; //ipTraceFlowSolver.FindSource(esriFlowMethod.esriFMUpstream,esriShortestPathObjFn.esriSPObjFnMinSum,out m_ipEnumNetEID_Junctions,out m_ipEnumNetEID_Edges,1,ref totalCost); ipTraceFlowSolver.FindFlowElements(esriFlowMethod.esriFMUpstream, esriFlowElements.esriFEEdges, out m_ipEnumNetEID_Junctions, out m_ipEnumNetEID_Edges); SpatialHelperFunction.pathToPolyline(pGeometricNetwork, m_HookHelper.ActiveView, m_ipEnumNetEID_Edges); //ipTraceFlowSolver.FindFlowEndElements(esriFlowMethod.esriFMUpstream,esriFlowElements.esriFEJunctionsAndEdges,out m_ipEnumNetEID_Junctions,out m_ipEnumNetEID_Edges); m_HookHelper.ActiveView.Refresh(); } catch (Exception e) { MessageBox.Show(e.Message); } }
private void networkAnalysisToolStripMenuItem_Click(object sender, EventArgs e) { //mTool = "network analysis"; //文件路径名称,包含文件名称和路径名称 string strPath = null; //定义OpenFileDialog,获取并打开地图文档 OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = "打开MDB"; openFileDialog.Filter = "MDB文件(*.mdb)|*.mdb"; if (openFileDialog.ShowDialog() == DialogResult.OK) { strPath = openFileDialog.FileName; } else { return; } //修改当前工具 mTool = "Network"; //获取几何网络文件路径 //注意修改此路径为当前存储路径 //打开工作空间 IWorkspaceFactory pWorkspaceFactory = new AccessWorkspaceFactory(); IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(strPath, 0) as IFeatureWorkspace; //获取要素数据集 //注意名称的设置要与上面创建保持一致 IFeatureDataset pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset("high"); //获取network集合 INetworkCollection pNetWorkCollection = pFeatureDataset as INetworkCollection; //获取network的数量,为零时返回 int intNetworkCount = pNetWorkCollection.GeometricNetworkCount; if (intNetworkCount < 1) { return; } //FeatureDataset可能包含多个network,我们获取指定的network //注意network的名称的设置要与上面创建保持一致 mGeometricNetwork = pNetWorkCollection.get_GeometricNetworkByName("high_net"); //将Network中的每个要素类作为一个图层加入地图控件 IFeatureClassContainer pFeatClsContainer = mGeometricNetwork as IFeatureClassContainer; //获取要素类数量,为零时返回 int intFeatClsCount = pFeatClsContainer.ClassCount; if (intFeatClsCount < 1) { return; } IFeatureClass pFeatureClass; IFeatureLayer pFeatureLayer; for (int i = 0; i < intFeatClsCount; i++) { //获取要素类 pFeatureClass = pFeatClsContainer.get_Class(i); pFeatureLayer = new FeatureLayerClass(); pFeatureLayer.FeatureClass = pFeatureClass; pFeatureLayer.Name = pFeatureClass.AliasName; //加入地图控件 this.axMapControl1.AddLayer((ILayer)pFeatureLayer, 0); } //计算snap tolerance为图层最大宽度的1/100 //获取图层数量 int intLayerCount = this.axMapControl1.LayerCount; IGeoDataset pGeoDataset; IEnvelope pMaxEnvelope = new EnvelopeClass(); for (int i = 0; i < intLayerCount; i++) { //获取图层 pFeatureLayer = this.axMapControl1.get_Layer(i) as IFeatureLayer; pGeoDataset = pFeatureLayer as IGeoDataset; //通过Union获得较大图层范围 pMaxEnvelope.Union(pGeoDataset.Extent); } double dblWidth = pMaxEnvelope.Width; double dblHeight = pMaxEnvelope.Height; double dblSnapTol; if (dblHeight < dblWidth) { dblSnapTol = dblWidth * 0.01; } else { dblSnapTol = dblHeight * 0.01; } //设置源地图,几何网络以及捕捉容差 mPointToEID = new PointToEIDClass(); mPointToEID.SourceMap = this.axMapControl1.Map; mPointToEID.GeometricNetwork = mGeometricNetwork; mPointToEID.SnapTolerance = dblSnapTol; }
private void setUpstreamJunctionFlag(IPoint pPoint) { try { if(pGeometricNetwork==null){ openGeoNetwork(); } IEnumNetEID m_ipEnumNetEID_Junctions,m_ipEnumNetEID_Edges;//����������������Junctions��Edges ipNetwork=pGeometricNetwork.Network; /* IMarkerSymbol pMarkerSym=SpatialHelperFunction.FindMarkerSym("Civic.ServerStyle", "Default", "Pin Flag Square"); if(pMarkerSym==null){ MessageBox.Show("��ȡ��Symbol"); return; } IRgbColor pRGBColor=Utility.getRGBColor(255, 0, 0); pMarkerSym.Color=pRGBColor; pMarkerSym.Size=26; pMarkerSym.XOffset=10; pMarkerSym.YOffset=10; //��ʹ�� IFlagDisplay��ֻ�� INetworkAnalysisExtFlags�ſ���ʹ��IFlagDisplay Utility.drawPointToGraphicLayer(m_HookHelper.ActiveView,pPoint,pMarkerSym); */ int pJunctionID; IPoint pLocation; int iUserClassID,iUserID,iUserSubID; INetElements ipNetElements; ipNetElements = ipNetwork as INetElements; //�õ�network�����е�Elements mPointToEID=new PointToEIDClass(); //��������GeometricNetwork mPointToEID.GeometricNetwork=pGeometricNetwork; //��������ֵ mPointToEID.SnapTolerance=m_HookHelper.ActiveView.Extent.Envelope.Width/100; //����Source Map mPointToEID.SourceMap=m_HookHelper.ActiveView.FocusMap; //�õ������JunctionID mPointToEID.GetNearestJunction(pPoint,out pJunctionID,out pLocation); //Each element in a logical network has a unique element ID (EID). ipNetElements.QueryIDs(pJunctionID,esriElementType.esriETJunction,out iUserClassID,out iUserID,out iUserSubID); //�õ�����Junction��ID //��ʹ�� IFlagDisplay��ֻ�� INetworkAnalysisExtFlags�ſ���ʹ��IFlagDisplay INetFlag ipNetFlag=new JunctionFlagClass(); ipNetFlag.UserClassID=iUserClassID; ipNetFlag.UserID=iUserID; ipNetFlag.UserSubID=iUserSubID; IJunctionFlag [] pArrayJFlag=new IJunctionFlag[1]; pArrayJFlag[0]=ipNetFlag as IJunctionFlag; ipTraceFlowSolver=new TraceFlowSolverClass() as ITraceFlowSolverGEN; //get the inetsolver interface ipNetSolver=ipTraceFlowSolver as INetSolver; ipNetSolver.SourceNetwork=pGeometricNetwork.Network; ipTraceFlowSolver.PutJunctionOrigins(ref pArrayJFlag); ipTraceFlowSolver.TraceIndeterminateFlow=false; object[] totalCost=new object[1]; //ipTraceFlowSolver.FindSource(esriFlowMethod.esriFMUpstream,esriShortestPathObjFn.esriSPObjFnMinSum,out m_ipEnumNetEID_Junctions,out m_ipEnumNetEID_Edges,1,ref totalCost); ipTraceFlowSolver.FindFlowElements(esriFlowMethod.esriFMUpstream, esriFlowElements.esriFEEdges, out m_ipEnumNetEID_Junctions, out m_ipEnumNetEID_Edges); SpatialHelperFunction.pathToPolyline(pGeometricNetwork,m_HookHelper.ActiveView,m_ipEnumNetEID_Edges); //ipTraceFlowSolver.FindFlowEndElements(esriFlowMethod.esriFMUpstream,esriFlowElements.esriFEJunctionsAndEdges,out m_ipEnumNetEID_Junctions,out m_ipEnumNetEID_Edges); m_HookHelper.ActiveView.Refresh(); } catch(Exception e){ MessageBox.Show(e.Message); } }
private bool slovePath() { try { double costLength=0; m_ipPointToEID = new PointToEIDClass(); m_ipPointToEID.SourceMap=m_HookHelper.ActiveView.FocusMap; m_ipPointToEID.GeometricNetwork = m_ipGeometricNetwork; m_ipPointToEID.SnapTolerance=m_HookHelper.ActiveView.Extent.Envelope.Width/100; INetwork ipNetwork; INetElements ipNetElements; INetFlag ipNetFlag; //IEdgeFlag ipEdgeFlag; IEdgeFlag [] EdgeFlagArray; int pEdgeID; IPoint pLocation; double dblEdgePercent; int iUserClassID,iUserID,iUserSubID; // look up the EID for the current point (this will populate intEdgeID and dblEdgePercent) ipTraceFlowSolver=new TraceFlowSolverClass() as ITraceFlowSolver; //get the inetsolver interface ipNetSolver=ipTraceFlowSolver as INetSolver; ipNetwork=m_ipGeometricNetwork.Network; //set the net solver's source network ipNetSolver.SourceNetwork=ipNetwork; //make edge flags from the points //the INetElements interface is needed to get UserID, UserClassID, //and UserSubID from an element id EdgeFlagArray=new IEdgeFlag[mPointArray.PointCount]; ipNetElements = ipNetwork as INetElements; for(int ii=0;ii<mPointArray.PointCount;ii++) { ipNetFlag = new EdgeFlagClass(); point=mPointArray.get_Point(ii); m_ipPointToEID.GetNearestEdge(point,out pEdgeID,out pLocation,out dblEdgePercent); //iUserClassID��featureclass ID,userID��objectID,userSubID ipNetElements.QueryIDs(pEdgeID,esriElementType.esriETEdge,out iUserClassID,out iUserID,out iUserSubID); ipNetFlag.UserClassID=iUserClassID; ipNetFlag.UserID=iUserID; ipNetFlag.UserSubID=iUserSubID; EdgeFlagArray[ii]=ipNetFlag as IEdgeFlag; //MessageBox.Show(iUserID.ToString()); } //��ӵ�TraceFlowSolver�� ITraceFlowSolverGEN pTFSolverGEN=ipTraceFlowSolver as ITraceFlowSolverGEN; pTFSolverGEN.PutEdgeOrigins(ref EdgeFlagArray); //get the INetSchema interface INetSchema ipNetSchema = ipNetwork as INetSchema; INetWeight ipNetWeight = ipNetSchema.get_Weight(0); //MessageBox.Show(ipNetWeight.WeightName); INetSolverWeights ipNetSolverWeights=ipTraceFlowSolver as INetSolverWeights; //����ʹ��ͬһ��Ȩ�� ipNetSolverWeights.FromToEdgeWeight=ipNetWeight; ipNetSolverWeights.ToFromEdgeWeight=ipNetWeight; object [] totalCost=new object[mPointArray.PointCount-1]; pTFSolverGEN.FindPath(esriFlowMethod.esriFMConnected, esriShortestPathObjFn.esriSPObjFnMinSum, out m_ipEnumNetEID_Junctions, out m_ipEnumNetEID_Edges, mPointArray.PointCount-1,ref totalCost); //MessageBox.Show(totalCost[0].ToString()); for(int ii=0;ii<totalCost.Length;ii++){ costLength+=(double)(totalCost[ii]); } pStatusBar.Panels[0].Text="ˮ�ܳ���:"+costLength.ToString(); return true; } catch(Exception e){ MessageBox.Show(e.Message); return false; } }