private void HandleBranchMerge(BranchMergeAction branchMergeAction) { IBranch extendedBranch = branchMergeAction.ExtendedBranch; IBranch removedBranch = branchMergeAction.RemovedBranch; var locationsOnExtendedBranch = Locations.Values.Where(l => l.Branch == extendedBranch).ToList(); var locationsOnRemovedBranch = Locations.Values.Where(l => l.Branch == removedBranch).ToList(); //remove the locations of the branches foreach (var networkLocation in locationsOnRemovedBranch.Concat(locationsOnExtendedBranch)) { Locations.Values.Remove(networkLocation); } //reinitialize segmentsInitialized = false; initialized = false; StoreBranchLengths(); }
/// <summary> /// Merge 2 branches by removing the node that connects them. /// </summary> /// <param name="node"></param> /// <param name="network"></param> public static void MergeNodeBranches(INode node, INetwork network) { // The first branch will be the incoming branch IBranch targetBranch = node.IncomingBranches[0]; IBranch sourceBranch = node.OutgoingBranches[0]; var canRemoveNodeAndMergeBranches = (node.OutgoingBranches.Count == 1) && (node.IncomingBranches.Count == 1); if (!canRemoveNodeAndMergeBranches) { throw new ArgumentException("Can only merge node with exactly 1 incoming and 1 outgoing branch and belonging to the same reach."); } if (targetBranch.IsLengthCustom != sourceBranch.IsLengthCustom) { throw new ArgumentException(string.Format("Cannot merge branch '{0}' and '{1}'. One has a custom length and the other does not.", targetBranch.Name, sourceBranch.Name)); } var branchMergeAction = new BranchMergeAction { RemovedBranch = sourceBranch, RemovedNode = node, ExtendedBranch = targetBranch }; //update the action and end the action so 'listeners' can react network.BeginEdit(branchMergeAction); //the action might have been cancelled if (network.EditWasCancelled) { network.CancelEdit(); //not one of my prettier hacks, sorry return; } var vertices = new List <ICoordinate>(); for (int i = 0; i < targetBranch.Geometry.Coordinates.Length; i++) { vertices.Add(new Coordinate(targetBranch.Geometry.Coordinates[i].X, targetBranch.Geometry.Coordinates[i].Y)); } for (int i = 1; i < sourceBranch.Geometry.Coordinates.Length; i++) { vertices.Add(new Coordinate(sourceBranch.Geometry.Coordinates[i].X, sourceBranch.Geometry.Coordinates[i].Y)); } var chainage = targetBranch.Geometry.Length; targetBranch.Geometry = new LineString(vertices.ToArray()); // endGeometry; if (targetBranch.IsLengthCustom) { // Use TypeUtils to circumvent chainage correction for BranchFeatures on targetBranch. TypeUtils.SetField(targetBranch, "length", targetBranch.Length + sourceBranch.Length); } MergeBranchFeatures(sourceBranch, targetBranch, chainage); // before removing the source branch reconnect the end of the target branch to the end node of the source branch targetBranch.Target = sourceBranch.Target; network.Branches.Remove(sourceBranch); network.Nodes.Remove(node); network.EndEdit(); }