It looks like NetworkHelper was very slow (especially on large networks) because of Distance() calls. Probably we should use time-consuming calls only after Envelope is checked:

Old code:

        public static IBranch GetNearestBranch(IEnumerable<IBranch> branches, IBranchFeature branchFeature, double tolerance)
        {
           ...
            var minDistance = double.MaxValue;
            var nearestBranch = (IBranch) null;

            foreach (var branch in branches)
            {
                var distance = branch.Geometry.Distance(branchFeature.Geometry);

                if (distance >= minDistance || distance >= tolerance)
                    continue;
                nearestBranch = branch;
                minDistance = distance;
            }
           ...
        }

Net code:

        public static IBranch GetNearestBranch(IEnumerable<IBranch> branches, IBranchFeature branchFeature, double tolerance)
        {
           ...
            var minDistance = double.MaxValue;
            var nearestBranch = (IBranch) null;

            // first select branches where envelope of branch overlaps with branchFeature
            var overlappingBranches = new List<IBranch>();
            foreach (var branch in branches)
            {
                if(branch.Geometry.EnvelopeInternal.Overlaps(branchFeature.Geometry.EnvelopeInternal))
                {
                    overlappingBranches.Add(branch);
                }
            }

            // then find nearest branch using Distance
            foreach (var branch in overlappingBranches)
            {
                var distance = branch.Geometry.Distance(branchFeature.Geometry);

                if (distance >= minDistance || distance >= tolerance)
                    continue;
                nearestBranch = branch;
                minDistance = distance;
            }
           ...
        }
  • No labels