コード例 #1
0
        private bool NodeAt(LineString candidate, Coordinate coord)
        {
            var lip = LocationIndexOfPoint.IndexOf(candidate, coord);

            if (IsStartOrEnd(lip, candidate))
            {
                return(false);
            }

            if (!lip.IsVertex)
            {
                return(false);
            }

            int candidateIndex = (int)candidate.UserData;

            RemoveAt(candidateIndex);

            var csFactory = candidate.Factory.CoordinateSequenceFactory;
            CoordinateSequence cs;

            cs = csFactory.Create(lip.SegmentIndex + 1, candidate.CoordinateSequence.Dimension);
            NetTopologySuite.Geometries.CoordinateSequences.Copy(candidate.CoordinateSequence, 0, cs, 0, cs.Count);
#if DEBUG
            System.Diagnostics.Debug.Assert(cs.GetCoordinate(cs.Count - 1).Equals(coord));
#endif
            var l1 = candidate.Factory.CreateLineString(cs);
            Add(l1);

            cs = csFactory.Create(candidate.CoordinateSequence.Count - lip.SegmentIndex, candidate.CoordinateSequence.Dimension);
            NetTopologySuite.Geometries.CoordinateSequences.Copy(candidate.CoordinateSequence, lip.SegmentIndex, cs, 0, cs.Count);
#if DEBUG
            System.Diagnostics.Debug.Assert(cs.GetCoordinate(0).Equals(coord));
#endif

            var l2 = candidate.Factory.CreateLineString(cs);
            Add(l2);

#if DEBUG
            System.Diagnostics.Debug.Assert(l1.CoordinateSequence.Count + l2.CoordinateSequence.Count == candidate.CoordinateSequence.Count + 1);
            System.Diagnostics.Debug.Assert(l1.EndPoint.EqualsExact(l2.StartPoint));
            System.Diagnostics.Debug.Assert(l1.StartPoint.EqualsExact(candidate.StartPoint));
            System.Diagnostics.Debug.Assert(l2.EndPoint.EqualsExact(candidate.EndPoint));
            System.Diagnostics.Debug.Assert(Math.Abs(l1.Length + l2.Length - candidate.Length) < 1e-9);
#endif

            return(true);
        }
コード例 #2
0
        public ILineString[] SplitTheLine(ILineString toBeSplit, IPoint splitPoint)
        {
            var llStart = new LinearLocation(0, 0);
            var llSplit = LocationIndexOfPoint.IndexOf(toBeSplit, splitPoint.Coordinate);
            var llEnd   = new LinearLocation(0, 1);

            var res = new[]
            {
                (ILineString)ExtractLineByLocation.Extract(toBeSplit, llStart, llSplit),
                (ILineString)ExtractLineByLocation.Extract(toBeSplit, llSplit, llEnd)
            };

            return(res);

            var        cDistance = double.MaxValue;
            Coordinate cPoint    = null;

            // From this point on it becomes a bit of a 'fudge'. Because the NTS functionality is a map matching dream (ie it returns
            // a point that may not be on the line. But We need to split the line into two. Therefore we have to get the distance
            // from the intersection to the cloest point on the line. More on this when we get there.
            // ToDo: Perhaps theres a much better way of doing this already in either SharpMap/ or more likely NTS?????????????????
            for (var i = 0; i < toBeSplit.Coordinates.Length; i++)
            {
                var pointOnTheLine = toBeSplit.Coordinates[i];

                double lDistance = pointOnTheLine.Distance(splitPoint.Coordinate);

                if (lDistance < cDistance)
                {
                    cDistance = lDistance;
                    cPoint    = pointOnTheLine;
                }
            }

            // Now We Need To Try And Calculate The Length Along The Line Of That Node.
            var indexAlongLine = 0;
            var firstPart      = new List <Coordinate>();
            var secondPart     = new List <Coordinate>();


            for (var i = 0; i < toBeSplit.Coordinates.Length; i++)
            {
                var pointOnTheLine =
                    toBeSplit.Coordinates[i];

                firstPart.Add(pointOnTheLine);

                if (ReferenceEquals(pointOnTheLine, cPoint))
                {
                    break;
                }

                indexAlongLine++;
            }

            for (int i = indexAlongLine; i < toBeSplit.Coordinates.Length; i++)
            {
                var pointOnTheLine = toBeSplit.Coordinates[i];

                secondPart.Add(pointOnTheLine);
            }

            var firstPartAsString  = _geomFactory.CreateLineString(firstPart.ToArray());
            var secondPartAsString = _geomFactory.CreateLineString(secondPart.ToArray());

            var splitLineParts = new ILineString[2];

            splitLineParts[0] = firstPartAsString;
            splitLineParts[1] = secondPartAsString;

            return(splitLineParts);
        }