private LineString GetLineWithExtraPoints(LineString currentLine, Coordinate coordinateToAddIfNeeded) { var point = new Point(coordinateToAddIfNeeded); if (currentLine.Distance(point) >= _options.MaxDistanceToExisitngLineForMerge * 2) { // coordinate not close enough return(currentLine); } var closestCoordinateOnGpx = currentLine.Coordinates.OrderBy(c => c.Distance(coordinateToAddIfNeeded)).First(); if (closestCoordinateOnGpx.Distance(coordinateToAddIfNeeded) < 2 * _options.MaxDistanceToExisitngLineForMerge) { // line already has a close enough coordinate return(currentLine); } // need to add a coordinate to the line var coordinates = currentLine.Coordinates.ToList(); var line = new LocationIndexedLine(currentLine); var projectedLocation = line.Project(coordinateToAddIfNeeded); var coordinateToAdd = line.ExtractPoint(projectedLocation); coordinates.Insert(projectedLocation.SegmentIndex + 1, coordinateToAdd); return(_geometryFactory.CreateLineString(coordinates.ToArray()) as LineString); }
IGeometry InsertPoint(IGeometry geom, Coordinate point) { if (!(geom is ILineal)) { throw new InvalidOperationException(); } var lil = new LocationIndexedLine(geom); var ll = lil.Project(point); var element = (ILineString)geom.GetGeometryN(ll.ComponentIndex); var oldSeq = element.CoordinateSequence; var newSeq = element.Factory.CoordinateSequenceFactory.Create( oldSeq.Count + 1, oldSeq.Dimension); var j = 0; if (ll.SegmentIndex == 0 && ll.SegmentFraction == 0) { if (ll.GetSegment(element).P0.Distance(point) == 0) { return(geom); } newSeq.SetCoordinate(0, point); CoordinateSequences.Copy(oldSeq, 0, newSeq, 1, oldSeq.Count); } else if (ll.SegmentIndex == oldSeq.Count - 1 && ll.SegmentFraction == 0) { if (ll.GetSegment(element).P0.Distance(point) == 0) { return(geom); } CoordinateSequences.Copy(oldSeq, 0, newSeq, 0, oldSeq.Count); newSeq.SetCoordinate(oldSeq.Count, point); } else { if (ll.IsVertex) { return(geom); } CoordinateSequences.Copy(oldSeq, 0, newSeq, 0, ll.SegmentIndex + 1); newSeq.SetCoordinate(ll.SegmentIndex + 1, point); CoordinateSequences.Copy(oldSeq, ll.SegmentIndex + 1, newSeq, ll.SegmentIndex + 2, newSeq.Count - 2 - ll.SegmentIndex); } var lines = new List <IGeometry>(); LineStringExtracter.GetLines(geom, lines); lines[ll.ComponentIndex] = geom.Factory.CreateLineString(newSeq); if (lines.Count == 1) { return(lines[0]); } return(geom.Factory.BuildGeometry(lines)); }