Example #1
0
        private static Point GetWgs84Point(Point point)
        {
            if (point.SRID == 4326)
            {
                return(point);
            }
            var cs = CoordinateSystemExtend.Get(point.SRID);

            if (cs is GeographicCoordinateSystem)
            {
                return(CoordinateTransformation.TransformPoint(point, GeographicCoordinateSystem.WGS84, false));
            }
            else
            {
                return(CoordinateTransformation.TransformPoint(point, GeographicCoordinateSystem.WGS84, false));
            }
        }
Example #2
0
        public static double Distance(Point point1, Point point2)
        {
            //一个有坐标系一个没有坐标系
            //if (point1.CoordinateSystem != null && point2.CoordinateSystem == null)
            //{
            //    point2 = point2.Clone() as Point;
            //    point2.SetCoordinateSystem(point1.CoordinateSystem, false);
            //}
            //else if (point1.CoordinateSystem == null && point2.CoordinateSystem != null)
            //{
            //    point1 = point1.Clone() as Point;
            //    point1.SetCoordinateSystem(point2.CoordinateSystem, false);
            //}
            ////两个坐标系不同
            //else if (point1.CoordinateSystem != point2.CoordinateSystem)
            //{
            //    if(point1.CoordinateSystem is ProjectedCoordinateSystem)
            //    point2 = CoordinateSystems.Transformations.CoordinateTransformation.TransformPoint(point2, point1.CoordinateSystem, false);
            //}
            //地理坐标系
            //if (point1.CoordinateSystem is GeographicCoordinateSystem)
            //{

            //如果两个都没有坐标系,直接求欧氏距离
            if (point1.SRID == 0 && point2.SRID == 0)
            {
                double x = point1.X - point2.X;
                double y = point1.Y - point2.Y;
                return(Math.Sqrt(x * x + y * y));
            }
            ////一个有坐标系一个没有坐标系
            //if (point1.CoordinateSystem != null && point2.CoordinateSystem == null)
            //{
            //    point2 = point2.Clone() as Point;
            //    point2.SetCoordinateSystem(point1.CoordinateSystem, false);
            //}
            //else if (point1.CoordinateSystem == null && point2.CoordinateSystem != null)
            //{
            //    point1 = point1.Clone() as Point;
            //    point1.SetCoordinateSystem(point2.CoordinateSystem, false);
            //}
            //else
            //{
            //    point1 = GetWgs84Point(point1);
            //    point2 = GetWgs84Point(point2);
            //}
            if (!point1.SRID.Equals(point2.SRID))
            {
                throw new Exception("坐标系不一致");
            }
            var cs = CoordinateSystemExtend.Get(point1.SRID);

            if (cs is IGeographicCoordinateSystem)
            {
                return(CalculateGeodeticCurve(
                           (cs as IGeographicCoordinateSystem).HorizontalDatum.Ellipsoid as Ellipsoid,
                           point1, point2).Length);
            }
            else
            {
                point1 = CoordinateTransformation.TransformPoint(point1, 4326);
                point2 = CoordinateTransformation.TransformPoint(point2, 4326);
                return(CalculateGeodeticCurve(Ellipsoid.WGS84, point1, point2).Length);
            }
            //double lat1 = point1.Latitude;
            //double lat2 = point2.Latitude;
            //double lng1 = point1.Longitude;
            //double lng2 = point2.Longitude;

            //var radLat1 = lat1 * Math.PI / 180.0;
            //var radLat2 = lat2 * Math.PI / 180.0;
            //var a = radLat1 - radLat2;
            //var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
            //var s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)));
            //s *= CoordinateSystem.WGS84.HorizontalDatum.Ellipsoid.SemiMajorAxis; //6378137;
            ////s = Math.Round(s * 10000) / 10000;

            //return s;
            //}
        }