public static bool SegmentIntersects(IGeometry g1, IGeometry g2)
 {
     Coordinate[] pt1 = g1.Coordinates;
     Coordinate[] pt2 = g2.Coordinates;
     RobustLineIntersector ri = new RobustLineIntersector();
     ri.ComputeIntersection(pt1[0], pt1[1], pt2[0], pt2[1]);
     return ri.HasIntersection;
 }
 public void TestCollinear4() {
     RobustLineIntersector i = new RobustLineIntersector();
     Coordinate p1 = new Coordinate(30, 10);
     Coordinate p2 = new Coordinate(20, 10);
     Coordinate q1 = new Coordinate(10, 10);
     Coordinate q2 = new Coordinate(30, 10);
     i.ComputeIntersection(p1, p2, q1, q2);
     Assert.AreEqual(RobustLineIntersector.Collinear, i.IntersectionNum);
     Assert.IsTrue(i.HasIntersection);
 }
Пример #3
0
        public static IGeometry MCIndexNodingWithPrecision(Geometry geom, double scaleFactor)
        {
            List<ISegmentString> segs = CreateNodedSegmentStrings(geom);

            LineIntersector li = new RobustLineIntersector();
            li.PrecisionModel = new PrecisionModel(scaleFactor);
            INoder noder = new MCIndexNoder(new IntersectionAdder(li));
            noder.ComputeNodes(segs);
            IList<ISegmentString> nodedSegStrings = noder.GetNodedSubstrings();
            return FromSegmentStrings(nodedSegStrings);
        }
 public void Test2Lines() {
     RobustLineIntersector i = new RobustLineIntersector();
     Coordinate p1 = new Coordinate(10, 10);
     Coordinate p2 = new Coordinate(20, 20);
     Coordinate q1 = new Coordinate(20, 10);
     Coordinate q2 = new Coordinate(10, 20);
     Coordinate x = new Coordinate(15, 15);
     i.ComputeIntersection(p1, p2, q1, q2);
     Assert.AreEqual(RobustLineIntersector.DoIntersect, i.IntersectionNum);
     Assert.AreEqual(1, i.IntersectionNum);
     Assert.AreEqual(x, i.GetIntersection(0));
     Assert.IsTrue(i.IsProper);
     Assert.IsTrue(i.HasIntersection);
 }
Пример #5
0
        /// <summary>
        /// Tests whether a point lies on the line defined by a list of
        /// coordinates.
        /// </summary>
        /// <param name="p">The point to test</param>
        /// <param name="line">The line coordinates</param>
        /// <returns>
        /// <c>true</c> if the point is a vertex of the line or lies in the interior
        /// of a line segment in the line
        /// </returns>
        public static bool IsOnLine(Coordinate p, Coordinate[] line)
        {
            var lineIntersector = new RobustLineIntersector();

            for (int i = 1; i < line.Length; i++)
            {
                var p0 = line[i - 1];
                var p1 = line[i];
                lineIntersector.ComputeIntersection(p, p0, p1);
                if (lineIntersector.HasIntersection)
                {
                    return(true);
                }
            }
            return(false);
        }
        /// <summary>
        /// Tests whether a point lies on the line segments defined by a
        /// list of coordinates.
        /// </summary>
        /// <param name="p"></param>
        /// <param name="pt"></param>
        /// <returns>true if the point is a vertex of the line
        /// or lies in the interior of a line segment in the linestring
        /// </returns>
        public static bool IsOnLine(Coordinate p, Coordinate[] pt)
        {
            LineIntersector lineIntersector = new RobustLineIntersector();

            for (int i = 1; i < pt.Length; i++)
            {
                Coordinate p0 = pt[i - 1];
                Coordinate p1 = pt[i];
                lineIntersector.ComputeIntersection(p, p0, p1);
                if (lineIntersector.HasIntersection)
                {
                    return(true);
                }
            }
            return(false);
        }
        public static IGeometry SegmentIntersectionDd(IGeometry g1, IGeometry g2)
        {
            Coordinate[] pt1 = g1.Coordinates;
            Coordinate[] pt2 = g2.Coordinates;

            // first check if there actually is an intersection
            RobustLineIntersector ri = new RobustLineIntersector();
            ri.ComputeIntersection(pt1[0], pt1[1], pt2[0], pt2[1]);
            if (!ri.HasIntersection)
            {
                // no intersection => return empty point
                return g1.Factory.CreatePoint((Coordinate)null);
            }

            Coordinate intPt = CGAlgorithmsDD.Intersection(pt1[0], pt1[1], pt2[0], pt2[1]);
            return g1.Factory.CreatePoint(intPt);
        }
Пример #8
0
        /// <summary>
        /// Tests whether a point lies on the line defined by a list of
        /// coordinates.
        /// </summary>
        /// <param name="p">The point to test</param>
        /// <param name="line">The line coordinates</param>
        /// <returns>
        /// <c>true</c> if the point is a vertex of the line or lies in the interior
        /// of a line segment in the line
        /// </returns>
        public static bool IsOnLine(Coordinate p, ICoordinateSequence line)
        {
            var lineIntersector = new RobustLineIntersector();
            var p0 = new Coordinate();
            var p1 = new Coordinate();
            int n  = line.Count;

            for (int i = 1; i < n; i++)
            {
                line.GetCoordinate(i - 1, p0);
                line.GetCoordinate(i, p1);
                lineIntersector.ComputeIntersection(p, p0, p1);
                if (lineIntersector.HasIntersection)
                {
                    return(true);
                }
            }
            return(false);
        }
 public static IGeometry SegmentIntersection(IGeometry g1, IGeometry g2)
 {
     Coordinate[] pt1 = g1.Coordinates;
     Coordinate[] pt2 = g2.Coordinates;
     RobustLineIntersector ri = new RobustLineIntersector();
     ri.ComputeIntersection(pt1[0], pt1[1], pt2[0], pt2[1]);
     switch (ri.IntersectionNum)
     {
         case 0:
             // no intersection => return empty point
             return g1.Factory.CreatePoint((Coordinate)null);
         case 1:
             // return point
             return g1.Factory.CreatePoint(ri.GetIntersection(0));
         case 2:
             // return line
             return g1.Factory.CreateLineString(
                 new Coordinate[] {
                     ri.GetIntersection(0),
                     ri.GetIntersection(1)
                 });
     }
     return null;
 }
Пример #10
0
 /// <summary>
 /// Computes an intersection point between two segments, if there is one.
 /// There may be 0, 1 or many intersection points between two segments.
 /// If there are 0, null is returned. If there is 1 or more, a single one
 /// is returned (chosen at the discretion of the algorithm).  If
 /// more information is required about the details of the intersection,
 /// the {RobustLineIntersector} class should be used.
 /// </summary>
 /// <param name="line">A line segment</param>
 /// <returns> An intersection point, or <c>null</c> if there is none.</returns>
 /// <see cref="RobustLineIntersector"/>
 public Coordinate Intersection(LineSegment line)
 {
     LineIntersector li = new RobustLineIntersector();
     li.ComputeIntersection(_p0, _p1, line._p0, line._p1);
     if (li.HasIntersection)
         return li.GetIntersection(0);
     return null;
 }
Пример #11
0
        private bool IsSimpleLinearGeometry(IGeometry geom)
        {
            if (geom.IsEmpty) 
                return true;

            GeometryGraph graph = new GeometryGraph(0, geom);
            LineIntersector li = new RobustLineIntersector();
            SegmentIntersector si = graph.ComputeSelfNodes(li, true);
            // if no self-intersection, must be simple
            if (!si.HasIntersection) return true;
            if (si.HasProperIntersection)
            {
                _nonSimpleLocation = si.ProperIntersectionPoint;
                return false;
            }
            if (HasNonEndpointIntersection(graph)) return false;
            if (_isClosedEndpointsInInterior)
            {
                if (HasClosedEndpointIntersection(graph)) return false;
            }
            return true;
        }
Пример #12
0
 /// <summary> 
 /// Tests whether a point lies on the line segments defined by a
 /// list of coordinates.
 /// </summary>
 /// <param name="p"></param>
 /// <param name="pt"></param>
 /// <returns>true if the point is a vertex of the line
 /// or lies in the interior of a line segment in the linestring
 /// </returns>
 public static bool IsOnLine(Coordinate p, Coordinate[] pt) 
 {
     LineIntersector lineIntersector = new RobustLineIntersector();
     for (int i = 1; i < pt.Length; i++) 
     {
         Coordinate p0 = pt[i - 1];
         Coordinate p1 = pt[i];
         lineIntersector.ComputeIntersection(p, p0, p1);
         if (lineIntersector.HasIntersection) 
             return true;                
     }
     return false;
 }
        public void CheckInputNotAltered(Coordinate[] pt, int scaleFactor)
        {
            // save input points
            Coordinate[] savePt = new Coordinate[4];
            for (int i = 0; i < 4; i++)
            {
                savePt[i] = new Coordinate(pt[i]);
            }

            LineIntersector li = new RobustLineIntersector();
            li.PrecisionModel = new PrecisionModel(scaleFactor);
            li.ComputeIntersection(pt[0], pt[1], pt[2], pt[3]);

            // check that input points are unchanged
            for (int i = 0; i < 4; i++)
            {
                Assert.AreEqual(savePt[i], pt[i], "Input point " + i + " was altered - ");
            }
        }
        /// <summary>
        /// Check that intersection of segment defined by points in pt array
        /// is equal to the expectedIntPt value (up to the given distanceTolerance)
        /// </summary>
        /// <param name="pt"></param>
        /// <param name="expectedIntersectionNum"></param>
        /// <param name="expectedIntPt">the expected intersection points (maybe null if not tested)</param>
        /// <param name="distanceTolerance">tolerance to use for equality test</param>
        private void CheckIntersection(Coordinate[] pt,
            int expectedIntersectionNum,
            Coordinate[] expectedIntPt,
            double distanceTolerance)
        {
            LineIntersector li = new RobustLineIntersector();
            li.ComputeIntersection(pt[0], pt[1], pt[2], pt[3]);

            int intNum = li.IntersectionNum;
            Assert.AreEqual(expectedIntersectionNum, intNum, "Number of intersections not as expected");

            if (expectedIntPt != null)
            {
                Assert.AreEqual(intNum, expectedIntPt.Length, "Wrong number of expected int pts provided");
                // test that both points are represented here
                if (intNum == 1)
                {
                    CheckIntPoints(expectedIntPt[0], li.GetIntersection(0), distanceTolerance);
                }
                else if (intNum == 2)
                {
                    CheckIntPoints(expectedIntPt[1], li.GetIntersection(0), distanceTolerance);
                    CheckIntPoints(expectedIntPt[1], li.GetIntersection(0), distanceTolerance);

                    if (!(Equals(expectedIntPt[0], li.GetIntersection(0), distanceTolerance)
                          || Equals(expectedIntPt[0], li.GetIntersection(1), distanceTolerance)))
                    {
                        CheckIntPoints(expectedIntPt[0], li.GetIntersection(0), distanceTolerance);
                        CheckIntPoints(expectedIntPt[0], li.GetIntersection(1), distanceTolerance);
                    }
                    else if (!(Equals(expectedIntPt[1], li.GetIntersection(0), distanceTolerance)
                               || Equals(expectedIntPt[1], li.GetIntersection(1), distanceTolerance)))
                    {
                        CheckIntPoints(expectedIntPt[1], li.GetIntersection(0), distanceTolerance);
                        CheckIntPoints(expectedIntPt[1], li.GetIntersection(1), distanceTolerance);
                    }
                }
            }
        }
Пример #15
0
        /// <summary>
        /// Checks validity of a LinearRing.
        /// </summary>
        /// <param name="g"></param>
        private void CheckValid(ILinearRing g)
        {
            CheckInvalidCoordinates(g.Coordinates);
            if (validErr != null) return;
            CheckClosedRing(g);
            if (validErr != null) return;

            GeometryGraph graph = new GeometryGraph(0, g);
            CheckTooFewPoints(graph);
            if (validErr != null) return;
            LineIntersector li = new RobustLineIntersector();
            graph.ComputeSelfNodes(li, true);
            CheckNoSelfIntersectingRings(graph);
        }
Пример #16
0
        public static IGeometry MCIndexNodingWithPrecision(IGeometry geom, double scaleFactor)
        {
            IPrecisionModel fixedPM = new PrecisionModel(scaleFactor);

            LineIntersector li = new RobustLineIntersector();
            li.PrecisionModel = fixedPM;

            INoder noder = new MCIndexNoder(new IntersectionAdder(li));
            noder.ComputeNodes(SegmentStringUtil.ExtractNodedSegmentStrings(geom));
            return SegmentStringUtil.ToGeometry(noder.GetNodedSubstrings(), geom.Factory);
        }