Esempio n. 1
0
        protected override bool IndexOfAfterCheck(Geometry linearGeom, Coordinate testPt)
        {
            var indexedLine = new LengthIndexedLine(linearGeom);

            // check locations are consecutive
            double loc1 = indexedLine.IndexOf(testPt);
            double loc2 = indexedLine.IndexOfAfter(testPt, loc1);

            if (loc2 <= loc1)
            {
                return(false);
            }

            // check extracted points are the same as the input
            var pt1 = indexedLine.ExtractPoint(loc1);
            var pt2 = indexedLine.ExtractPoint(loc2);

            if (!pt1.Equals2D(testPt))
            {
                return(false);
            }
            if (!pt2.Equals2D(testPt))
            {
                return(false);
            }

            return(true);
        }
Esempio n. 2
0
        private void HandleSelfClosingCase(GpxProlongerExecutorInput input, LineAndCoordinate current, LineAndCoordinate next, List <ILineString> linesToProlong)
        {
            var lengthIndexedLine             = new LengthIndexedLine(current.Line);
            var closestCoordinateCurrentIndex = lengthIndexedLine.Project(current.Coordinate);
            var closestCoordinateNextIndex    = lengthIndexedLine.Project(next.Coordinate);
            var segment     = lengthIndexedLine.ExtractLine(closestCoordinateCurrentIndex, closestCoordinateNextIndex);
            var coordinates = segment.Coordinates.Concat(new[] { segment.Coordinates.First() }).ToArray();

            if (coordinates.Length < 4)
            {
                return;
            }
            var polygon = new Polygon(new LinearRing(coordinates));

            if (polygon.Area < input.MinimalAreaSize)
            {
                return;
            }
            var currentCoordinate = lengthIndexedLine.ExtractPoint(closestCoordinateCurrentIndex);
            var nextCoordinate    = lengthIndexedLine.ExtractPoint(closestCoordinateNextIndex);

            if (!AddCoordinate(current.Line, currentCoordinate, nextCoordinate, linesToProlong, input.MinimalDistance))
            {
                linesToProlong.Add(_geometryFactory.CreateLineString(new[] { currentCoordinate, nextCoordinate }));
            }
        }
Esempio n. 3
0
        public void TestExtractPointBeyondRange()
        {
            var linearGeom  = Read("LINESTRING (0 0, 10 10)");
            var indexedLine = new LengthIndexedLine(linearGeom);
            var pt          = indexedLine.ExtractPoint(100);

            Assert.IsTrue(pt.Equals(new Coordinate(10, 10)));

            var pt2 = indexedLine.ExtractPoint(0);

            Assert.IsTrue(pt2.Equals(new Coordinate(0, 0)));
        }
Esempio n. 4
0
        private void HandleTwoLinesCase(GpxProlongerExecutorInput input, LineAndCoordinate current, LineAndCoordinate next, List <ILineString> linesToProlong)
        {
            var currentLengthIndexedLine = new LengthIndexedLine(current.Line);
            var currentCoordinate        = currentLengthIndexedLine.ExtractPoint(currentLengthIndexedLine.Project(current.Coordinate));
            var nextLengthIndexedLine    = new LengthIndexedLine(next.Line);
            var nextCoordinate           = nextLengthIndexedLine.ExtractPoint(nextLengthIndexedLine.Project(next.Coordinate));

            var bothLinesAreInList = linesToProlong.Contains(current.Line) && linesToProlong.Contains(next.Line);

            if (bothLinesAreInList && current.Line.Coordinates.Last().Distance(current.Coordinate) < input.MinimalDistance &&
                next.Line.Coordinates.First().Distance(next.Coordinate) < input.MinimalDistance)
            {
                linesToProlong.Remove(current.Line);
                linesToProlong.Remove(next.Line);
                linesToProlong.Add(_geometryFactory.CreateLineString(current.Line.Coordinates
                                                                     .Concat(next.Line.Coordinates).ToArray()));
            }
            else if (bothLinesAreInList &&
                     current.Line.Coordinates.First().Distance(current.Coordinate) < input.MinimalDistance &&
                     next.Line.Coordinates.Last().Distance(next.Coordinate) < input.MinimalDistance)
            {
                linesToProlong.Remove(current.Line);
                linesToProlong.Remove(next.Line);
                linesToProlong.Add(_geometryFactory.CreateLineString(next.Line.Coordinates
                                                                     .Concat(current.Line.Coordinates).ToArray()));
            }
            else if (!AddCoordinate(current.Line, currentCoordinate, nextCoordinate, linesToProlong, input.MinimalDistance))
            {
                if (!AddCoordinate(next.Line, currentCoordinate, nextCoordinate, linesToProlong, input.MinimalDistance))
                {
                    linesToProlong.Add(_geometryFactory.CreateLineString(new[] { currentCoordinate, nextCoordinate }));
                }
            }
        }
        public static IGeometry ExtractPoint(IGeometry g, double index)
        {
            var ll = new LengthIndexedLine(g);
            var p  = ll.ExtractPoint(index);

            return(g.Factory.CreatePoint(p));
        }
Esempio n. 6
0
        /// <summary>
        /// This methods tries to find the coordinate to add on the closest line and prefers to return one of the edges if possible.
        /// </summary>
        /// <param name="lineToTestAgainst"></param>
        /// <param name="closestLine"></param>
        /// <param name="minimalDistance"></param>
        /// <returns></returns>
        private Coordinate GetCoordinateToAdd(ILineString lineToTestAgainst, ILineString closestLine, double minimalDistance)
        {
            var closestCoordinate = lineToTestAgainst.Coordinates.Last();
            var edgeCoordinate    = GetEdgeCoordiante(closestLine, closestCoordinate, minimalDistance, 1.5);

            if (edgeCoordinate != null)
            {
                return(edgeCoordinate);
            }
            if (lineToTestAgainst.Intersects(closestLine))
            {
                var intecsectionCoordinate = lineToTestAgainst.Intersection(closestLine).Coordinates.First();
                edgeCoordinate = GetEdgeCoordiante(closestLine, intecsectionCoordinate, minimalDistance, 3);
                return(edgeCoordinate ?? intecsectionCoordinate);
            }
            var closestCoordinateOnExitingLine = closestLine.Coordinates.OrderBy(c => c.Distance(closestCoordinate)).First();

            if (closestCoordinateOnExitingLine.Distance(closestCoordinate) < minimalDistance)
            {
                return(closestCoordinateOnExitingLine);
            }

            var closestLineIndexed = new LengthIndexedLine(closestLine);
            var closestCoordinateProjectedIndex = closestLineIndexed.Project(closestCoordinate);
            var projectedCoordinate             = closestLineIndexed.ExtractPoint(closestCoordinateProjectedIndex);

            _geometryFactory.PrecisionModel.MakePrecise(projectedCoordinate);
            edgeCoordinate = GetEdgeCoordiante(closestLine, projectedCoordinate, minimalDistance, 3);
            return(edgeCoordinate ?? projectedCoordinate);
        }
Esempio n. 7
0
        protected override Coordinate ExtractOffsetAt(Geometry linearGeom, Coordinate testPt, double offsetDistance)
        {
            var    indexedLine = new LengthIndexedLine(linearGeom);
            double index       = indexedLine.IndexOf(testPt);

            return(indexedLine.ExtractPoint(index, offsetDistance));
        }
        public static IGeometry Project(IGeometry g, IGeometry g2)
        {
            var    ll    = new LengthIndexedLine(g);
            double index = ll.Project(g2.Coordinate);
            var    p     = ll.ExtractPoint(index);

            return(g.Factory.CreatePoint(p));
        }
Esempio n. 9
0
        public void TestComputeZNaN()
        {
            var    linearGeom  = Read("LINESTRING (0 0, 10 10 10)");
            var    indexedLine = new LengthIndexedLine(linearGeom);
            double projIndex   = indexedLine.Project(new Coordinate(5, 5));
            var    projPt      = indexedLine.ExtractPoint(projIndex);

            Assert.IsTrue(double.IsNaN(projPt.Z));
        }
Esempio n. 10
0
        public void TestProjectExtractPoint()
        {
            var    linearGeom  = Read("MULTILINESTRING ((0 2, 0 0), (-1 1, 1 1))");
            var    indexedLine = new LengthIndexedLine(linearGeom);
            double index       = indexedLine.Project(new Coordinate(1, 0));
            var    pt          = indexedLine.ExtractPoint(index);

            Assert.IsTrue(pt.Equals(new Coordinate(0, 0)));
        }
Esempio n. 11
0
        /// <summary>
        /// Tries to split a branch at the given chainage. It will not create empty branches ( split at chainage 0 or chainage is length branch)
        /// All branch features are updated
        /// Chainage is interpreted as chainage in geometry, even if IsLengthCustom is true for branch
        /// </summary>
        /// <param name="branch"></param>
        /// <param name="geometryOffset">Local (or geometry-based) chainage</param>
        public static SplitResult SplitBranchAtNode(IBranch branch, double geometryOffset)
        {
            bool startedEdit = false;
            BranchSplitAction branchSplitAction = null;

            //start editaction if not editing

            if (geometryOffset == 0.0)
            {
                return(null); //no split required
            }
            if (geometryOffset == branch.Geometry.Length)
            {
                return(null); //no split required
            }
            if (!branch.Network.IsEditing)
            {
                startedEdit       = true;
                branchSplitAction = new BranchSplitAction();
                branch.Network.BeginEdit(branchSplitAction);
            }

            var lengthIndexedLine = new LengthIndexedLine(branch.Geometry);
            var splitLocation     = lengthIndexedLine.ExtractPoint(geometryOffset);

            var node = (INode)Activator.CreateInstance(branch.Source.GetType());

            node.Name     = GetUniqueName("Node{0:D3}", branch.Network.Nodes, "Node");
            node.Geometry =
                new Point(new Coordinate(splitLocation.X, splitLocation.Y,
                                         Double.IsNaN(splitLocation.Z) ? 0.0 : splitLocation.Z));

            var         newBranch = SplitBranchAtNode(branch.Network, branch, node);
            SplitResult result    = null;

            if (null != newBranch)
            {
                branch.Network.Nodes.Add(node);
                result = new SplitResult
                {
                    NewNode   = node,
                    NewBranch = newBranch
                };
            }

            if (startedEdit)
            {
                branchSplitAction.SplittedBranch = branch;
                if (result != null)
                {
                    branchSplitAction.NewBranch = result.NewBranch;
                }
                branch.Network.EndEdit();
            }

            return(result);
        }
Esempio n. 12
0
        public void TestComputeZ()
        {
            IGeometry         linearGeom  = Read("LINESTRING (0 0 0, 10 10 10)");
            LengthIndexedLine indexedLine = new LengthIndexedLine(linearGeom);
            double            projIndex   = indexedLine.Project(new Coordinate(5, 5));
            Coordinate        projPt      = indexedLine.ExtractPoint(projIndex);

            //    System.out.println(projPt);
            Assert.IsTrue(projPt.Equals3D(new Coordinate(5, 5, 5)));
        }
Esempio n. 13
0
        public void TestComputeZ()
        {
            var    linearGeom  = Read("LINESTRING (0 0 0, 10 10 10)");
            var    indexedLine = new LengthIndexedLine(linearGeom);
            double projIndex   = indexedLine.Project(new Coordinate(5, 5));
            var    projPt      = indexedLine.ExtractPoint(projIndex);

            //    System.out.println(projPt);
            Assert.That(projPt, Is.InstanceOf <CoordinateZ>());
            Assert.IsTrue(((CoordinateZ)projPt).Equals3D(new CoordinateZ(5, 5, 5)));
        }
        /// <summary>
        /// return the coordinates along the gridProfile at stepSize intervals.
        /// </summary>
        /// <param name="gridProfile"></param>
        /// <param name="stepSize"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException">When <paramref name="stepSize"/> is 0.0 or less.</exception>
        public static IEnumerable<Coordinate> GetGridProfileCoordinates(ILineString gridProfile, double stepSize)
        {
            var lengthIndexedLine = new LengthIndexedLine(gridProfile);
            if (stepSize <= 0) throw new ArgumentException("Stepsize too small", "stepSize");

            var count = (int)((gridProfile.Length / stepSize) + 1);
            for (int i = 0; i < count; i++)
            {
                yield return (Coordinate)lengthIndexedLine.ExtractPoint(i * stepSize).Clone();
            }
        }
Esempio n. 15
0
        private void UpdateGeometry()
        {
            if (Branch == null || Branch.Geometry == null)
            {
                return;
            }

            var lengthIndexedLine = new LengthIndexedLine(Branch.Geometry);

            // thousand bombs and granates: ExtractPoint will give either a new coordinate or
            // a reference to an existing object
            Geometry = new Point((ICoordinate)lengthIndexedLine.ExtractPoint(Offset).Clone());
        }
        protected override void CalculateLocation(VehicleManager vehicleManager)
        {
            if (lengthIndexedLine == null)
            {
                return;
            }
            var oldCoordinate = vehicleManager.Position.Location;
            var newCoordinate = lengthIndexedLine.ExtractPoint(lengthIndexedLine.Project(oldCoordinate) + 0.000001 * Speed);

            vehicleManager.Position.Location = newCoordinate;
            vehicleManager.Position.Speed    = Speed;
            vehicleManager.Position.Angle    = vehicleManager.Position.Angle;
        }
Esempio n. 17
0
        private void UpdateGeometry()
        {
            if (Branch == null || Branch.Geometry == null)
            {
                return;
            }

            var lengthIndexedLine = new LengthIndexedLine(Branch.Geometry);

            var offset = Branch.IsLengthCustom ? (Branch.Geometry.Length / Branch.Length) * Offset : Offset;

            // always clone: ExtractPoint will give either a new coordinate or a reference to an existing object
            Geometry = new Point((ICoordinate)lengthIndexedLine.ExtractPoint(offset).Clone());
        }
Esempio n. 18
0
        private SegmentWithLines CreateSegmentWithLines(Coordinate[] segment, LineAndCoordinate current, LineAndCoordinate next)
        {
            var currentLengthIndexedLine = new LengthIndexedLine(current.Line);
            var currentCoordinate        = currentLengthIndexedLine.ExtractPoint(currentLengthIndexedLine.Project(current.Coordinate));
            var nextLengthIndexedLine    = new LengthIndexedLine(next.Line);
            var nextCoordinate           = nextLengthIndexedLine.ExtractPoint(nextLengthIndexedLine.Project(next.Coordinate));

            var segmentWithLines = new SegmentWithLines
            {
                OriginalCoordinates = segment,
                Start          = current,
                StartProjected = currentCoordinate,
                End            = next,
                EndProjected   = nextCoordinate
            };

            return(segmentWithLines);
        }
Esempio n. 19
0
        private void HandleIntersectionCase(GpxProlongerExecutorInput input, SegmentWithLines segment, List <ILineString> linesToProlong)
        {
            var intersection = segment.Start.Line.Intersection(segment.End.Line).Coordinates
                               .OrderBy(c => c.Distance(segment.Start.Coordinate) + c.Distance(segment.End.Coordinate)).FirstOrDefault();

            if (intersection == null)
            {
                var distance = new DistanceOp(segment.Start.Line, segment.End.Line);
                intersection = distance.NearestPoints().First();
            }

            var currentLengthIndexedLine                  = new LengthIndexedLine(segment.Start.Line);
            var closestCoordinateCurrentIndex             = currentLengthIndexedLine.Project(segment.Start.Coordinate);
            var closestCoordinateCurrentIntersectionIndex = currentLengthIndexedLine.Project(intersection);
            var currentSegment =
                currentLengthIndexedLine.ExtractLine(closestCoordinateCurrentIndex, closestCoordinateCurrentIntersectionIndex);

            var nextLengthIndexedLine                  = new LengthIndexedLine(segment.End.Line);
            var closestCoordinateNextIndex             = nextLengthIndexedLine.Project(segment.End.Coordinate);
            var closestCoordinateNextIntersectionIndex = nextLengthIndexedLine.Project(intersection);
            var nextSegment =
                nextLengthIndexedLine.ExtractLine(closestCoordinateNextIntersectionIndex, closestCoordinateNextIndex);

            var coordinates = currentSegment.Coordinates.Concat(nextSegment.Coordinates)
                              .Concat(new[] { currentSegment.Coordinates.First() }).ToArray();

            if (coordinates.Length < 4)
            {
                return;
            }
            var polygon = new Polygon(new LinearRing(coordinates));

            if (polygon.Area < input.MinimalAreaSize)
            {
                return;
            }
            var currentCoordinate = currentLengthIndexedLine.ExtractPoint(closestCoordinateCurrentIndex);
            var nextCoordinate    = nextLengthIndexedLine.ExtractPoint(closestCoordinateNextIndex);
            var line = CreateLineString(currentCoordinate, segment.OriginalCoordinates, nextCoordinate);

            linesToProlong.Add(line);
        }
Esempio n. 20
0
        public static Geometry Project(Geometry g, Geometry g2)
        {
            var ll = new LengthIndexedLine(g);

            if (g2.Dimension == Dimension.Curve)
            {
                var    line       = (LineString)g2.GetGeometryN(0);
                var    pStart     = line.GetCoordinateN(0);
                var    pEnd       = line.GetCoordinateN(line.NumPoints - 1);
                double indexStart = ll.Project(pStart);
                double indexEnd   = ll.Project(pEnd);
                return(ll.ExtractLine(indexStart, indexEnd));
            }
            else
            {
                double index = ll.Project(g2.Coordinate);
                var    p     = ll.ExtractPoint(index);
                return(g.Factory.CreatePoint(p));
            }
        }
Esempio n. 21
0
        /// <summary>
        /// Tries to split a branch at the given offset. It will not create empty branches ( split at offset 0 or offset is length branch)
        /// All branch features are updated
        /// </summary>
        /// <param name="branch"></param>
        /// <param name="offset">Local (or geometry-based) offset</param>
        public static INode SplitBranchAtNode(IBranch branch, double offset)
        {
            var    lengthIndexedLine = new LengthIndexedLine(branch.Geometry);
            double geometryOffest    = GetGeometryOffest(branch, offset);
            var    splitLocation     = lengthIndexedLine.ExtractPoint(geometryOffest);

            var node = (INode)Activator.CreateInstance(branch.Source.GetType());

            node.Name     = GetUniqueName(null, branch.Network.Nodes, "Node");
            node.Geometry = new Point((ICoordinate)splitLocation.Clone());

            var newBranch = SplitBranchAtNode(branch.Network, branch, node);

            if (null != newBranch)
            {
                branch.Network.Nodes.Add(node);
                return(node);
            }
            return(null);
        }
Esempio n. 22
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="wkt"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        public void RunExtractedLine(string wkt, double start, double end)
        {
            Console.WriteLine("=========================");
            IGeometry g1 = rdr.Read(wkt);

            Console.WriteLine("Input Geometry: " + g1);
            Console.WriteLine("Indices to extract: " + start + " " + end);

            LengthIndexedLine indexedLine = new LengthIndexedLine(g1);

            IGeometry subLine = indexedLine.ExtractLine(start, end);

            Console.WriteLine("Extracted Line: " + subLine);

            double[] index = indexedLine.IndicesOf(subLine);
            Console.WriteLine("Indices of extracted line: " + index[0] + " " + index[1]);

            Coordinate midpt = indexedLine.ExtractPoint((index[0] + index[1]) / 2);

            Console.WriteLine("Midpoint of extracted line: " + midpt);
        }