public GraphTableRows QueryN1(int n1) { GraphTableRows rows = new GraphTableRows(); foreach (GraphTableRow row in _rows) { if (row.N1 == n1) { rows.Add(row); } } return(rows); }
public Features QueryNodeEdgeFeatures(int n1) { Features features = new Features(); GraphTableRows rows = QueryN1(n1); if (rows == null) { return(features); } foreach (GraphTableRow row in rows) { IFeature feature = _nfc.GetEdgeFeature(row.EID); if (feature != null) { feature.Fields.Add(new FieldValue("NETWORK#EID", row.EID)); features.Add(feature); } } return(features); }
public NetworkTracerOutputCollection Trace(INetworkFeatureClass network, NetworkTracerInputCollection input, ICancelTracker cancelTraker) { if (network == null || !CanTrace(input)) { return(null); } GraphTable gt = new GraphTable(network.GraphTableAdapter()); NetworkSourceInput sourceNode = null; NetworkSourceEdgeInput sourceEdge = null; if (input.Collect(NetworkTracerInputType.SourceNode).Count == 1) { sourceNode = input.Collect(NetworkTracerInputType.SourceNode)[0] as NetworkSourceInput; } else if (input.Collect(NetworkTracerInputType.SoruceEdge).Count == 1) { sourceEdge = input.Collect(NetworkTracerInputType.SoruceEdge)[0] as NetworkSourceEdgeInput; } else { return(null); } Dijkstra dijkstra = new Dijkstra(cancelTraker); dijkstra.reportProgress += this.ReportProgress; dijkstra.ApplySwitchState = input.Contains(NetworkTracerInputType.IgnoreSwitches) == false && network.HasDisabledSwitches; Dijkstra.ApplyInputIds(dijkstra, input); if (sourceNode != null) { dijkstra.Calculate(gt, sourceNode.NodeId); } else if (sourceEdge != null) { IGraphEdge graphEdge = gt.QueryEdge(sourceEdge.EdgeId); if (graphEdge == null) { return(null); } bool n1_2_n2 = gt.QueryN1ToN2(graphEdge.N1, graphEdge.N2) != null; bool n2_2_n1 = gt.QueryN1ToN2(graphEdge.N2, graphEdge.N1) != null; bool n1switchState = dijkstra.ApplySwitchState ? gt.SwitchState(graphEdge.N1) : true; bool n2switchState = dijkstra.ApplySwitchState ? gt.SwitchState(graphEdge.N2) : true; if (n1_2_n2 && n1switchState == true) { dijkstra.Calculate(gt, graphEdge.N1); } else if (n2_2_n1 && n2switchState == true) { dijkstra.Calculate(gt, graphEdge.N2); } else { return(null); } } Dijkstra.Nodes dijkstraNodes = dijkstra.DijkstraNodesWithMaxDistance(double.MaxValue); if (dijkstraNodes == null) { return(null); } ProgressReport report = (ReportProgress != null ? new ProgressReport() : null); #region Collect EdgedIds if (report != null) { report.Message = "Collected Edges..."; report.featurePos = 0; report.featureMax = dijkstraNodes.Count; ReportProgress(report); } NetworkInputForbiddenEdgeIds forbiddenEdgeIds = (input.Contains(NetworkTracerInputType.ForbiddenEdgeIds)) ? input.Collect(NetworkTracerInputType.ForbiddenEdgeIds)[0] as NetworkInputForbiddenEdgeIds : null; NetworkInputForbiddenStartNodeEdgeIds forbiddenStartNodeEdgeIds = (input.Contains(NetworkTracerInputType.ForbiddenStartNodeEdgeIds)) ? input.Collect(NetworkTracerInputType.ForbiddenStartNodeEdgeIds)[0] as NetworkInputForbiddenStartNodeEdgeIds : null; int counter = 0; List <int> edgeIds = new List <int>(); foreach (Dijkstra.Node dijkstraNode in dijkstraNodes) { if (dijkstra.ApplySwitchState) { if (gt.SwitchState(dijkstraNode.Id) == false) // hier ist Schluss!! { continue; } } GraphTableRows gtRows = gt.QueryN1(dijkstraNode.Id); if (gtRows == null) { continue; } foreach (IGraphTableRow gtRow in gtRows) { int eid = gtRow.EID; if (sourceNode != null && forbiddenStartNodeEdgeIds != null && dijkstraNode.Id == sourceNode.NodeId && forbiddenStartNodeEdgeIds.Ids.Contains(eid)) { continue; } if (forbiddenEdgeIds != null && forbiddenEdgeIds.Ids.Contains(eid)) { continue; } int index = edgeIds.BinarySearch(eid); if (index < 0) { edgeIds.Insert(~index, eid); } } counter++; if (report != null && counter % 1000 == 0) { report.featurePos = counter; ReportProgress(report); } } #endregion NetworkTracerOutputCollection output = new NetworkTracerOutputCollection(); if (report != null) { report.Message = "Add Edges..."; report.featurePos = 0; report.featureMax = edgeIds.Count; ReportProgress(report); } counter = 0; NetworkPathOutput pathOutput = new NetworkPathOutput(); foreach (int edgeId in edgeIds) { pathOutput.Add(new NetworkEdgeOutput(edgeId)); counter++; if (report != null && counter % 1000 == 0) { report.featurePos = counter; ReportProgress(report); } //var x = network.GetEdgeFeatureAttributes(edgeId, null); } output.Add(pathOutput); if (input.Collect(NetworkTracerInputType.AppendNodeFlags).Count > 0) { Helper.AppendNodeFlags(network, gt, Helper.NodeIds(dijkstraNodes), output); } return(output); }
public bool Calculate(GraphTable graph, int fromNode, int toNode) { //_dNodes.Add(new Node(fromNode, 0.0)); Node startNode = _dNodes.ByIdOrAdd(fromNode); if (double.IsNaN(startNode.Dist)) { startNode.Dist = 0.0; } if (_barrierNodeIds != null && _barrierNodeIds.BinarySearch(fromNode) >= 0) { return(true); } bool found = false; int nodeId = fromNode; DistanceNodeStack distStack = new DistanceNodeStack(); distStack.Add(startNode); #region Progress ProgressReport report = new ProgressReport(); report.Message = "Network Calculations..."; report.featureMax = 1000; report.featurePos = 0; #endregion while (true) { //Node minDistNode = _dNodes.MinDistNode(); Node minDistNode = distStack.Pop(); if (minDistNode == null) // Keine Knoten mehr -> Ziel ist nicht ereichbar! { break; } if (minDistNode.Id == toNode) // Ziel mit kürzesten Weg erreicht! { found = true; break; } minDistNode.Used = true; if (minDistNode.Dist >= _maxDistance) { continue; } GraphTableRows graphRows = graph.QueryN1(minDistNode.Id); foreach (GraphTableRow graphRow in graphRows) { if (_applySwitchState && graph.SwitchState(graphRow.N1) == false) { continue; } if (_targetNodeFcIds != null && graphRow.N1 != fromNode && _targetNodeFcIds.Contains(graph.GetNodeFcid(graphRow.N1))) { if (_targetNodeType != null) { NetworkNodeType nodeType = graph.GetNodeType(graphRow.N1); if (nodeType == (NetworkNodeType)_targetNodeType) { continue; } } else { continue; } } if (_allowedNodeIds != null && _allowedNodeIds.BinarySearch(graphRow.N2) < 0) { continue; } if (_forbiddenTargetNodeIds != null && _forbiddenTargetNodeIds.BinarySearch(graphRow.N2) >= 0) { continue; } if (_forbiddenEdgeIds != null && _forbiddenEdgeIds.BinarySearch(graphRow.EID) >= 0) { continue; } if (_forbiddenStartNodeEdgeIds != null && graphRow.N1 == fromNode && _forbiddenStartNodeEdgeIds.BinarySearch(graphRow.EID) >= 0) { continue; } if (_barrierNodeIds != null && _barrierNodeIds.BinarySearch(graphRow.N2) >= 0) { continue; } Node n = _dNodes.ByIdOrAdd(graphRow.N2); double length = graphRow.LENGTH; if (_weight != null) { double weight = graph.QueryEdgeWeight(_weight.Guid, graphRow.EID); switch (_weightApplying) { case WeightApplying.Weight: length /= weight; break; case WeightApplying.ActualCosts: length = weight; break; } } double dist = minDistNode.Dist + length; double geodist = minDistNode.GeoDist + graphRow.LENGTH; if (double.IsNaN(n.Dist) || n.Dist > dist) { if (!double.IsNaN(n.Dist)) { distStack.Remove(n); } n.Dist = dist; n.GeoDist = geodist; n.Pre = minDistNode.Id; n.EId = graphRow.EID; n.Used = false; distStack.Add(n); } } if (_cancelTracker != null && _cancelTracker.Continue == false) { return(false); } if (reportProgress != null && _dNodes.Count % 20 == 0) { report.featurePos += 20; if (report.featurePos > report.featureMax) { // report.featurePos = 0; report.featureMax += 1000; } reportProgress(report); } } return(found); }
async public Task <NetworkTracerOutputCollection> Trace(INetworkFeatureClass network, NetworkTracerInputCollection input, gView.Framework.system.ICancelTracker cancelTraker) { if (network == null || !CanTrace(input)) { return(null); } GraphTable gt = new GraphTable(network.GraphTableAdapter()); NetworkSourceInput sourceNode = null; NetworkSourceEdgeInput sourceEdge = null; if (input.Collect(NetworkTracerInputType.SourceNode).Count == 1) { sourceNode = input.Collect(NetworkTracerInputType.SourceNode)[0] as NetworkSourceInput; } else if (input.Collect(NetworkTracerInputType.SoruceEdge).Count == 1) { sourceEdge = input.Collect(NetworkTracerInputType.SoruceEdge)[0] as NetworkSourceEdgeInput; } else { return(null); } Dijkstra dijkstra = new Dijkstra(cancelTraker); dijkstra.reportProgress += this.ReportProgress; dijkstra.ApplySwitchState = input.Contains(NetworkTracerInputType.IgnoreSwitches) == false && network.HasDisabledSwitches; Dijkstra.ApplyInputIds(dijkstra, input); if (sourceNode != null) { dijkstra.Calculate(gt, sourceNode.NodeId); } else if (sourceEdge != null) { IGraphEdge graphEdge = gt.QueryEdge(sourceEdge.EdgeId); if (graphEdge == null) { return(null); } bool n1_2_n2 = gt.QueryN1ToN2(graphEdge.N1, graphEdge.N2) != null; bool n2_2_n1 = gt.QueryN1ToN2(graphEdge.N2, graphEdge.N1) != null; bool n1switchState = dijkstra.ApplySwitchState ? gt.SwitchState(graphEdge.N1) : true; bool n2switchState = dijkstra.ApplySwitchState ? gt.SwitchState(graphEdge.N2) : true; if (n1_2_n2 && n1switchState == true) { dijkstra.Calculate(gt, graphEdge.N1); } else if (n2_2_n1 && n2switchState == true) { dijkstra.Calculate(gt, graphEdge.N2); } else { return(null); } } Dijkstra.Nodes dijkstraNodes = dijkstra.DijkstraNodesWithMaxDistance(double.MaxValue); if (dijkstraNodes == null) { return(null); } ProgressReport report = (ReportProgress != null ? new ProgressReport() : null); int counter = 0; #region Collect Disconnected Nodes int maxNodeId = network.MaxNodeId; if (report != null) { report.Message = "Collected Disconnected Nodes..."; report.featurePos = 0; report.featureMax = maxNodeId; ReportProgress(report); } List <int> connectedNodeIds = dijkstraNodes.IdsToList(); connectedNodeIds.Sort(); List <int> disconnectedNodeIds = new List <int>(); for (int id = 1; id <= maxNodeId; id++) { counter++; if (report != null && counter % 1000 == 0) { report.featurePos = counter; ReportProgress(report); } if (connectedNodeIds.BinarySearch(id) >= 0) { continue; } disconnectedNodeIds.Add(id); } #endregion #region Collect EdgedIds if (report != null) { report.Message = "Collected Edges..."; report.featurePos = 0; report.featureMax = dijkstraNodes.Count; ReportProgress(report); } List <int> edgeIds = new List <int>(); foreach (int id in disconnectedNodeIds) { GraphTableRows gtRows = gt.QueryN1(id); if (gtRows == null) { continue; } foreach (IGraphTableRow gtRow in gtRows) { int index = edgeIds.BinarySearch(gtRow.EID); if (index < 0) { edgeIds.Insert(~index, gtRow.EID); } } counter++; if (report != null && counter % 1000 == 0) { report.featurePos = counter; ReportProgress(report); } } #endregion NetworkTracerOutputCollection output = new NetworkTracerOutputCollection(); if (report != null) { report.Message = "Add Edges..."; report.featurePos = 0; report.featureMax = edgeIds.Count; ReportProgress(report); } counter = 0; NetworkPathOutput pathOutput = new NetworkPathOutput(); foreach (int edgeId in edgeIds) { pathOutput.Add(new NetworkEdgeOutput(edgeId)); counter++; if (report != null && counter % 1000 == 0) { report.featurePos = counter; ReportProgress(report); } } output.Add(pathOutput); if (input.Collect(NetworkTracerInputType.AppendNodeFlags).Count > 0) { await Helper.AppendNodeFlags(network, gt, disconnectedNodeIds, output); } return(output); }