コード例 #1
0
        public void CrossAndIntersectionTest()
        {
            // Arrange
            var gf = new GeometryFactory(new PrecisionModel(100000000));

            var closestCoordinate = new Coordinate(152608, 594957);
            var closestLine       = gf.CreateLineString(new[]
            {
                new Coordinate(152348, 595130),
                new Coordinate(152421, 595061),
                new Coordinate(152455, 595033),
                new Coordinate(152524, 595001),
                new Coordinate(152593, 594973),
                new Coordinate(152622, 594946),
                new Coordinate(152634, 594930),
                new Coordinate(152641, 594921),
                new Coordinate(152649, 594910),
                new Coordinate(152863, 594623),
                new Coordinate(152873, 594607)
            });
            var    indexedLine     = new NetTopologySuite.LinearReferencing.LengthIndexedLine(closestLine);
            double projectedIndex  = indexedLine.Project(closestCoordinate);
            var    coordinateToAdd = indexedLine.ExtractPoint(projectedIndex);

            gf.PrecisionModel.MakePrecise(coordinateToAdd);

            var line = gf.CreateLineString(new[] { new Coordinate(152503, 594904), coordinateToAdd });

            ToImage(0, closestLine, line, GeometryFactory.Default.CreatePoint(coordinateToAdd));

            // act
            var intersectionPt = line.Intersection(closestLine).Coordinate;

            gf.PrecisionModel.MakePrecise(intersectionPt);

            // assert intersection point is equal to coordinate to add
            Assert.AreEqual(coordinateToAdd, intersectionPt);

            // act insertion of coordinate to add
            var lip = new NetTopologySuite.LinearReferencing.LocationIndexOfPoint(closestLine);
            var ll  = lip.IndexOf(coordinateToAdd);

            if (!ll.IsVertex)
            {
                var cl  = (ILineString)closestLine;
                var cls = cl.Factory.CoordinateSequenceFactory.Create(cl.CoordinateSequence.Count + 1, cl.CoordinateSequence.Ordinates);
                CoordinateSequences.Copy(cl.CoordinateSequence, 0, cls, 0, ll.SegmentIndex + 1);
                cls.SetOrdinate(ll.SegmentIndex + 1, Ordinate.X, coordinateToAdd.X);
                cls.SetOrdinate(ll.SegmentIndex + 1, Ordinate.Y, coordinateToAdd.Y);
                CoordinateSequences.Copy(cl.CoordinateSequence, ll.SegmentIndex + 1, cls, ll.SegmentIndex + 2, cl.CoordinateSequence.Count - ll.SegmentIndex - 1);
                closestLine = gf.CreateLineString(cls);
            }

            ToImage(1, closestLine, line, GeometryFactory.Default.CreatePoint(coordinateToAdd));

            Assert.IsTrue(line.Touches(closestLine));
            Assert.IsFalse(line.Crosses(closestLine));
        }
コード例 #2
0
        /// <summary>
        /// Computes a snapped coordinate.  If the mouse is near the selected feature, the output
        /// location of the mouse will be the coordinates on the feature rather than the actual
        /// mouse coords.
        /// </summary>
        /// <param name="mouseRect">The event args.</param>
        /// <param name="feat">set if a coordinate is found</param>
        /// <param name="layer">the feature's layer</param>
        /// <param name="snappedCoord">the coordinate of the mouse</param>
        /// <returns>true if snap found</returns>
        protected bool ComputeSnappedLocation_ForSelectedFeature(Rectangle mouseRect, ref IFeature feat, IFeatureLayer layer, ref Coordinate snappedCoord)
        {
            SnappingType      = string.Empty;
            SnappedCoordIndex = 0;

            if (!DoSnapping)
            {
                return(false);
            }
            if (mouseRect == null || feat == null || layer == null || snappedCoord == null)
            {
                return(false);
            }

            Extent pix = Map.PixelToProj(mouseRect);

            if (pix == null)
            {
                return(false);
            }

            Envelope mouseEnv = pix.ToEnvelope();

            NetTopologySuite.Geometries.Point mouse_onEarth = new NetTopologySuite.Geometries.Point(snappedCoord);
            IGeometry featGeom = feat.Geometry;

            // If the feature is partially or totaly visible in the view.
            if (Map.ViewExtents.Intersects(featGeom.EnvelopeInternal))
            {
                bool doCoord_Snap;

                // System.Console.WriteLine(feat.Fid);
                int coordCounter = 0;
                foreach (Coordinate c in feat.Geometry.Coordinates)
                {
                    doCoord_Snap = true;

                    if (layer.SnapVertices)
                    {
                        if (coordCounter == 0 && !layer.SnapStartPoint)
                        {
                            doCoord_Snap = false;
                        }
                        if (coordCounter == (feat.Geometry.Coordinates.Length - 1) && !layer.SnapEndPoint)
                        {
                            doCoord_Snap = false;
                        }

                        if (doCoord_Snap)
                        {
                            // If the mouse envelope contains the current coordinate, we found a snap location.
                            if (mouseEnv.Contains(c))
                            {
                                snappedCoord      = c;
                                SnappedCoordIndex = coordCounter;
                                SnappedFeature    = feat;
                                SnappingType      = SnappingTypeVertex;
                                return(true);
                            }
                        }
                    }

                    if (coordCounter > 0 && layer.SnapEdges && feat.FeatureType != FeatureType.Point && feat.FeatureType != FeatureType.MultiPoint)
                    {
                        double edge_Distance = 0;
                        if (layer.DataSet.CoordinateType.Equals(CoordinateType.Z))
                        {
                            edge_Distance = feat.Geometry.Coordinates[coordCounter - 1].Distance3D(c);
                        }
                        else
                        {
                            edge_Distance = feat.Geometry.Coordinates[coordCounter - 1].Distance(c);
                        }

                        if (edge_Distance > 0)
                        {
                            List <Coordinate> edgeCoords = new List <Coordinate>();
                            edgeCoords.Add(feat.Geometry.Coordinates[coordCounter - 1]);
                            edgeCoords.Add(c);

                            LineString edge = new LineString(edgeCoords.ToArray());

                            if (mouse_onEarth.Distance(edge) <= (mouseEnv.Width / 2))
                            {
                                NetTopologySuite.LinearReferencing.LengthIndexedLine indexedEedge = new NetTopologySuite.LinearReferencing.LengthIndexedLine(edge);
                                double proj_Index = indexedEedge.Project(mouse_onEarth.Coordinate);

                                if (proj_Index > (mouseEnv.Width / 2) && proj_Index < (edge.Length - (mouseEnv.Width / 2)))
                                {
                                    snappedCoord       = indexedEedge.ExtractPoint(proj_Index);
                                    SnappedCoordKeeped = snappedCoord; /* c.Clone() as Coordinate;*/
                                    SnappedCoordIndex  = coordCounter;
                                    SnappedFeature     = feat;
                                    SnappingType       = SnappingTypeEdge;
                                    return(true);
                                }
                            }
                        }
                    }

                    coordCounter++;
                }
            }

            SnappedFeature = null;
            return(false);
        }