示例#1
0
        public void Test()
        {
            var rdr   = new WKTReader();
            var geom0 = rdr.Read(
                @"LINESTRING(37.7224985654 -0.9555178485, 37.7226943974 -0.9554535447, 37.7229352226 -0.9553825995, 37.723181128 -0.955341998,
37.7234384814 -0.9552771203, 37.7237481242 -0.9552264134, 37.7241927331 -0.9551429879, 37.7247465157 -0.9550458111,
37.7252906081 -0.9549025103, 37.7260073939 -0.9547008659, 37.7266961443 -0.9544463063, 37.7271975705 -0.9542677211,
37.7275247714 -0.9541600877, 37.7278960455 -0.9540067672, 37.7279562199 -0.9545918853, 37.7280248243 -0.9553574602,
37.7281082916 -0.9560703664, 37.7281785921 -0.9567628655, 37.728241961 -0.9574851179, 37.7282899333 -0.9579211604,
37.728406797 -0.9591002592, 37.7285116219 -0.9600593024, 37.7286095979 -0.9609905579, 37.7286740215 -0.9616597345,
37.7287325308 -0.9622897772, 37.7288044601 -0.9630997691, 37.7288937412 -0.9643689763, 37.7290427091 -0.9657958785,
37.7291385693 -0.9669357838, 37.7292087963 -0.9677718666, 37.7293148069 -0.9689180115, 37.7293959591 -0.9697720861,
37.7294683595 -0.9706299638, 37.7295365948 -0.9707750019, 37.7300980277 -0.9706875215, 37.7312241207 -0.9704554138,
37.7322156915 -0.9702482864, 37.7329937243 -0.9700788735, 37.7336441253 -0.9699348312, 37.7345998334 -0.969702662, 
37.7354980229 -0.9695187068, 37.7362988481 -0.969273056, 37.7366833873 -0.9691528675, 37.7367509613 -0.9692824889,
37.7370790114 -0.9703683815, 37.7370889012 -0.9720775065, 37.737434433 -0.9727208544, 37.7377221441 -0.973204707,
37.7393064553 -0.9745088077, 37.7403573898 -0.9755188846, 37.7408684447 -0.97669274, 37.7414433551 -0.9779952721)");
            var geom1 = rdr.Read("POINT(37.7280838934 -0.9558619735)");

            var intersection = new DistanceOp(geom0, geom1).NearestPoints()[0];
            var testPoint    = geom0.Factory.CreatePoint(intersection);

            double distance = geom0.Distance(testPoint);

            Assert.That(distance, Is.LessThan(1e10));
        }
        public async Task <AppResult> Distance([FromBody] PositionModel model)
        {
            if (ModelState.IsValid == false)
            {
                return(ErrorMessage("error validation "));
            }

            if (model.FromLat == null || model.FromLng == null ||
                model.DistLat == null || model.DistLng == null)
            {
                return(ErrorMessage("error . enter 2 point latlng "));
            }

            var d = new DistanceOp(
                new Point(model.FromLat.Value, model.FromLng.Value),
                new Point(model.DistLat.Value, model.DistLng.Value));
            var distance = d.Distance();

            var histories = new List <GeoHistory>();

            _dbContext.GeoHistories.Add(new GeoHistory
            {
                Date     = DateTime.UtcNow,
                FromLng  = model.FromLng ?? 0,
                FromLat  = model.FromLat ?? 0,
                DistLat  = model.DistLat ?? 0,
                DistLng  = model.DistLng ?? 0,
                UserId   = CurrentUserId,
                Distance = distance
            });
            await _dbContext.SaveChangesAsync();

            return(SuccessfullMessage(distance));
        }
        private void DoNearestPointsTest(string wkt0, string wkt1, double distance,
                                         Coordinate p0, Coordinate p1)
        {
            var    op        = new DistanceOp(new WKTReader().Read(wkt0), new WKTReader().Read(wkt1));
            double tolerance = 1E-10;

            Assert.AreEqual(distance, op.NearestPoints()[0].Distance(op.NearestPoints()[1]), tolerance);
            Assert.AreEqual(p0.X, op.NearestPoints()[0].X, tolerance);
            Assert.AreEqual(p0.Y, op.NearestPoints()[0].Y, tolerance);
            Assert.AreEqual(p1.X, op.NearestPoints()[1].X, tolerance);
            Assert.AreEqual(p1.Y, op.NearestPoints()[1].Y, tolerance);
        }
示例#4
0
        /// <summary>
        /// Checks that two geometries are at least a minimum distance apart.
        /// </summary>
        /// <param name="g1">A geometry</param>
        /// <param name="g2">A geometry</param>
        /// <param name="minDist">The minimum distance the geometries should be separated by</param>
        private void CheckMinimumDistance(Geometry g1, Geometry g2, double minDist)
        {
            var distOp = new DistanceOp(g1, g2, minDist);

            _minDistanceFound = distOp.Distance();

            if (_minDistanceFound < minDist)
            {
                _isValid = false;
                var pts = distOp.NearestPoints();
                _errorLocation  = pts[1];
                _errorIndicator = g1.Factory.CreateLineString(pts);
                _errMsg         = "Distance between buffer curve and input is too small "
                                  + "(" + _minDistanceFound
                                  + " at " + WKTWriter.ToLineString(pts[0], pts[1]) + " )";
            }
        }
示例#5
0
        /**
         * verbindet die zwei Linien bei den nähesten Punkten der Linien:
         * erster Teil it Anfang der Linie <code>line0</code> bis zum (ersten) nähesten Punkt zu <code>line1</code>
         * zweitert Teilverbindet die zwei nähesten Punkte
         * dritter Teil ist der Endteil von <code>line1</code> ab dem nähesten Punkt
         * @param line0
         * @param line1
         * @return
         */
        private static List <Coordinate> concat(ILineString line0, ILineString line1)
        {
            List <Coordinate> listCoords   = new List <Coordinate>();
            IGeometry         intersection = line0.Intersection(line1);
            LengthIndexedLine lil0         = new LengthIndexedLine(line0);
            LengthIndexedLine lil1         = new LengthIndexedLine(line1);

            Coordinate[] closestPoints = DistanceOp.NearestPoints(line0, line1);
            if (!intersection.IsEmpty)
            { // die 2 Linien kreuzen sich
                double maxPos = 0;
                for (int i = 0; i < intersection.NumPoints; i++)
                { // den letzten Schnittpunkt suchen
                    Coordinate crossPoint = intersection.Coordinates[i];
                    double     pos        = lil0.Project(crossPoint);
                    if (maxPos < pos)
                    {
                        maxPos           = pos;
                        closestPoints[0] = crossPoint;
                        closestPoints[1] = crossPoint;
                    }
                }
            }
            for (int i = 0; i < line0.Coordinates.Length; i++)
            {
                if (lil0.Project(line0.Coordinates[i]) < lil0.Project(closestPoints[0]))
                {
                    listCoords.Add(line0.Coordinates[i]);
                }
            }
            listCoords.Add(closestPoints[0]);
            if (closestPoints[0] != closestPoints[1])
            { // die 2 Linien kreuzen sich nicht
                listCoords.Add(closestPoints[1]);
            }
            for (int i = 0; i < line1.Coordinates.Length; i++)
            {
                if (lil1.Project(line1.Coordinates[i]) > lil1.Project(closestPoints[1]))
                {
                    listCoords.Add(line1.Coordinates[i]);
                }
            }
            return(listCoords);
        }
示例#6
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);
        }
        public bool TryCalculate(Coordinate c1, Coordinate c2, Units unit, out double distance)
        {
            distance = 0;

            if (TryGetTransformation(c1, c2, out ICoordinateTransformation transformation))
            {
                var geoCoord1 = Transform(c1, transformation);
                var geoCoord2 = Transform(c2, transformation);

                var point1 = _factory.CreatePoint(geoCoord1);
                var point2 = _factory.CreatePoint(geoCoord2);

                distance = DistanceOp.Distance(point1, point2);
                distance = unit == Units.Meters ? distance : Constants.MilesPerMeter * distance;

                return(true);
            }

            return(false);
        }
示例#8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="wktA"></param>
        /// <param name="wktB"></param>
        public virtual void  FindClosestPoint(string wktA, string wktB)
        {
            Console.WriteLine("-------------------------------------");
            try
            {
                var A = wktRdr.Read(wktA);
                var B = wktRdr.Read(wktB);
                Console.WriteLine("Geometry A: " + A);
                Console.WriteLine("Geometry B: " + B);
                var distOp = new DistanceOp(A, B);

                double distance = distOp.Distance();
                Console.WriteLine("Distance = " + distance);

                var closestPt     = distOp.NearestPoints();
                var closestPtLine = fact.CreateLineString(closestPt);
                Console.WriteLine("Closest points: " + closestPtLine + " (distance = " + closestPtLine.Length + ")");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
        }
 public void RunSimpleLinePoint()
 {
     double dist = DistanceOp.Distance(geom1, pt2);
 }
 public void RunSimpleLines()
 {
     double dist = DistanceOp.Distance(geom1, geom2);
 }
        public static IGeometry nearestPoints(IGeometry a, IGeometry b)
        {
            var pts = DistanceOp.NearestPoints(a, b);

            return(a.Factory.CreateLineString(pts));
        }
示例#12
0
 private static double GetDistance(Point point1, Point point2) => DistanceOp.Distance(point1, point2);
示例#13
0
		/// <summary>
		/// Returns the minimum distance between this Geometry and the Geometry g.
		/// </summary>
		/// <param name="g">The Geometry from which to compute the distance.</param>
		/// <returns>Returns the minimum distance between this Geometry and the Geometry g.</returns>
		public double Distance(Geometry g)
		{
			return DistanceOp.Distance(this, g);
		}
示例#14
0
 private static double GetDistance(IPoint point1, IPoint point2)
 {
     return(DistanceOp.Distance(point1, point2));
 }