예제 #1
0
        public ParameterizedLocation GetClosestParameterizedLocation(PointD location, out double distance)
        {
            if (location == null)
            {
                distance = 0;
                return(null);
            }

            double closestDistance = 0.0;
            ParameterizedLocation closestDistanceParameterizedLocation = new ParameterizedLocation(0, 0);
            bool   closestDistanceSet = false;
            double limit = 2 * 32;

            for (int i = 0; i < segments.Count; i++)
            {
                List <AdjustedWaypoint> waypoints = segments[i].Waypoints;
                PointD p0 = waypoints[0].Location;
                for (int j = 1; j < waypoints.Count; j++)
                {
                    PointD p1 = waypoints[j].Location;

                    // long distance between p0 and p1? then we need to check more time-consuming ClosestDistancePointToLine even if p0 is far from location
                    bool isLongLineSegment = (LinearAlgebraUtil.DistancePointToPoint(p0, p1) > limit);

                    if (LinearAlgebraUtil.DistancePointToPoint(location, p1) < limit || isLongLineSegment)
                    {
                        double t;
                        double tmpDistance = LinearAlgebraUtil.ClosestDistancePointToLine(location, p0, p1, out t);
                        if (tmpDistance < closestDistance || !closestDistanceSet)
                        {
                            closestDistance = tmpDistance;
                            closestDistanceParameterizedLocation =
                                new ParameterizedLocation(i,
                                                          waypoints[j - 1].ParameterizedLocation.Value + t * (waypoints[j].ParameterizedLocation.Value - waypoints[j - 1].ParameterizedLocation.Value));
                            closestDistanceSet = true;
                        }
                    }
                    p0 = p1;
                }
            }

            distance = closestDistance;
            return(closestDistanceSet ? closestDistanceParameterizedLocation : null);
        }
예제 #2
0
        public static LongLat Deproject(PointD coordinate, LongLat projectionOrigin)
        {
            if (LinearAlgebraUtil.DistancePointToPoint(coordinate, new PointD(0, 0)) < 0.0000001)
            {
                return(new LongLat(projectionOrigin.Longitude, projectionOrigin.Latitude));
            }
            const double r       = 6378200; // earth radius in metres
            var          longLat = new LongLat();
            var          rho     = Math.Sqrt(coordinate.X * coordinate.X + coordinate.Y * coordinate.Y);
            var          c       = Math.Asin(rho / r);
            var          lambda0 = projectionOrigin.Longitude * Math.PI / 180.0;
            var          phi1    = projectionOrigin.Latitude * Math.PI / 180.0;

            longLat.Latitude =
                Math.Asin(Math.Cos(c) * Math.Sin(phi1) +
                          (coordinate.Y * Math.Sin(c) * Math.Cos(phi1) / rho)) / Math.PI * 180.0;
            longLat.Longitude = (lambda0 +
                                 Math.Atan(coordinate.X * Math.Sin(c) /
                                           (rho * Math.Cos(phi1) * Math.Cos(c) -
                                            coordinate.Y * Math.Sin(phi1) * Math.Sin(c)))) / Math.PI * 180.0;
            return(longLat);
        }