/// <summary> /// Snaps the points to the network coverage, given the tolerance. Existing locations/values will be cleared only /// when there are new points mapped to that branch. /// </summary> /// <param name="pointValuePairs"></param> /// <param name="networkCoverage"></param> /// <param name="tolerance"></param> public static void SnapToCoverage(IEnumerable <Tuple <IPoint, double> > pointValuePairs, INetworkCoverage networkCoverage, double tolerance) { var tree = new Quadtree <IBranch>(); networkCoverage.Network.Branches.ForEach(b => tree.Insert(b.Geometry.EnvelopeInternal, b)); // match points to branch buffers var locations = new List <Tuple <INetworkLocation, double> >(); foreach (var pointValue in pointValuePairs) { var envelope = pointValue.Item1.EnvelopeInternal; envelope.ExpandBy(tolerance); var branches = tree.Query(envelope).Cast <IBranch>(); var location = MapPointToClosestBranch(pointValue.Item1, branches, tolerance); if (location != null) { locations.Add(new Tuple <INetworkLocation, double>(location, pointValue.Item2)); } } // remove values for all branches that have new values imported var branchesToClear = locations.Select(l => l.Item1.Branch).Distinct(); branchesToClear.ForEach(b => NetworkHelper.ClearLocations(networkCoverage, b)); // add new values/locations to coverage locations.ForEach(l => networkCoverage[l.Item1] = l.Item2); }