ArcGIS Engine代码共享-投影(SpatialReference)对象操作

http://blog.csdn.net/linghe301/article/details/7204975

  1. public class SpatialReferenceHelper  
  2.    {  
  3.        // Fields  
  4.        private const double ParaC = 6399596.65198801;  
  5.        private const double ParaE1 = 0.00669438499958795;  
  6.        private const double Parak0 = 1.57048687472752E-07;  
  7.        private const double Parak1 = 0.00505250559291393;  
  8.        private const double Parak2 = 2.98473350966158E-05;  
  9.        private const double Parak3 = 2.41627215981336E-07;  
  10.        private const double Parak4 = 2.22241909461273E-09;  
  11.   
  12.        // Methods  
  13.       
  14.        public static double DistanceOfTwoEarthPoints(double lng1, double lat1, double lng2, double lat2, GaussSphere gs)  
  15.        {  
  16.            double d = Rad(lat1);  
  17.            double num2 = Rad(lat2);  
  18.            double num3 = d - num2;  
  19.            double num4 = Rad(lng1) - Rad(lng2);  
  20.            double num5 = 2.0 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(num3 / 2.0), 2.0) + ((Math.Cos(d) * Math.Cos(num2)) * Math.Pow(Math.Sin(num4 / 2.0), 2.0))));  
  21.            num5 *= (gs == GaussSphere.WGS84) ? 6378137.0 : ((gs == GaussSphere.Xian80) ? 6378140.0 : 6378245.0);  
  22.            return (Math.Round((double)(num5 * 10000.0)) / 10000.0);  
  23.        }  
  24.   
  25.        public static string FormatJWD(double pJWD)  
  26.        {  
  27.            int num = (int)pJWD;  
  28.            double num2 = (pJWD - num) * 60.0;  
  29.            int num3 = (int)num2;  
  30.            num2 = (num2 - num3) * 60.0;  
  31.            int num4 = (int)num2;  
  32.            return string.Concat(new object[] { num, "\x00b0", num3, "'", num4, "\"" });  
  33.        }  
  34.   
  35.        public static string FormatJWDEx(double pJWD)  
  36.        {  
  37.            int num = (int)pJWD;  
  38.            double num2 = (pJWD - num) * 60.0;  
  39.            int num3 = (int)num2;  
  40.            num2 = (num2 - num3) * 60.0;  
  41.            int num4 = (int)num2;  
  42.            return string.Concat(new object[] { num, "度", num3, "分", num4, "秒" });  
  43.        }  
  44.   
  45.        public static string FormatSpatialReference(ISpatialReference sr)  
  46.        {  
  47.            try  
  48.            {  
  49.                if (sr == null)  
  50.                {  
  51.                    return "";  
  52.                }  
  53.                StringBuilder builder = new StringBuilder();  
  54.                if (sr is UnknownCoordinateSystemClass)  
  55.                {  
  56.                    builder.Append("UnknownCoordinateSystem");  
  57.                }  
  58.                else  
  59.                {  
  60.                    if (sr.Name == "Unknown")  
  61.                    {  
  62.                        builder.Append("UnknownCoordinateSystem");  
  63.                    }  
  64.                    else  
  65.                    {  
  66.                        IGeographicCoordinateSystem system2;  
  67.                        if (sr is IProjectedCoordinateSystem)  
  68.                        {  
  69.                            Exception exception;  
  70.                            IProjectedCoordinateSystem system = sr as IProjectedCoordinateSystem;  
  71.                            builder.Append("投影坐标系:\n");  
  72.                            builder.Append("  Name:").Append(system.Name).Append("\n");  
  73.                            builder.Append("  Alias:").Append(system.Alias).Append("\n");  
  74.                            builder.Append("  Abbreviation:").Append(system.Abbreviation).Append("\n");  
  75.                            builder.Append("  Remarks:").Append(system.Remarks).Append("\n");  
  76.                            builder.Append("投影:").Append(system.Projection.Name).Append("\n");  
  77.                            builder.Append("投影参数:\n");  
  78.                            builder.Append("   False_Easting:").Append(system.FalseEasting).Append("\n");  
  79.                            builder.Append("   False_Northing:").Append(system.FalseNorthing).Append("\n");  
  80.                            builder.Append("   Central_Meridian:").Append(system.get_CentralMeridian(true)).Append("\n");  
  81.                            try  
  82.                            {  
  83.                                builder.Append("   Scale_Factor:").Append(system.ScaleFactor).Append("\n");  
  84.                            }  
  85.                            catch { }  
  86.                            builder.Append("   Latitude_Of_Origin:0\n");  
  87.                            builder.Append("Linear Unit:").Append(system.CoordinateUnit.Name).Append("(").Append(system.CoordinateUnit.MetersPerUnit).Append(")\n");  
  88.                            builder.Append("Geographic Coordinate System:\n");  
  89.                            system2 = system.GeographicCoordinateSystem;  
  90.                            builder.Append("  Name:").Append(system2.Name).Append("\n");  
  91.                            builder.Append("  Alias:").Append(system2.Alias).Append("\n");  
  92.                            builder.Append("  Abbreviation:").Append(system2.Abbreviation).Append("\n");  
  93.                            builder.Append("  Remarks:").Append(system2.Remarks).Append("\n");  
  94.                            builder.Append("  Angular Unit:").Append(system2.CoordinateUnit.Name).Append("(").Append(system2.CoordinateUnit.RadiansPerUnit).Append(")\n");  
  95.                            builder.Append("  Prime Meridian:").Append(system2.PrimeMeridian.Name).Append("(").Append(system2.PrimeMeridian.Longitude).Append(")\n");  
  96.                            builder.Append("  Datum:").Append(system2.Datum.Name).Append("\n");  
  97.                            builder.Append("    Spheroid:").Append(system2.Datum.Spheroid.Name).Append("\n");  
  98.                            builder.Append("      Semimajor Axis:").Append(system2.Datum.Spheroid.SemiMajorAxis).Append("\n");  
  99.                            builder.Append("      Semiminor Axis:").Append(system2.Datum.Spheroid.SemiMinorAxis).Append("\n");  
  100.                            builder.Append("      Inverse Flattening:").Append((double)(1.0 / system2.Datum.Spheroid.Flattening)).Append("\n");  
  101.                            builder.Append("X/Y Domain:\n");  
  102.                            try  
  103.                            {  
  104.                                double num = 0.0;  
  105.                                double num2 = 0.0;  
  106.                                double num3 = 0.0;  
  107.                                double num4 = 0.0;  
  108.                                double num5 = 0.0;  
  109.                                sr.GetDomain(out num, out num3, out num2, out num4);  
  110.                                sr.GetFalseOriginAndUnits(out num, out num2, out num5);  
  111.                                builder.Append(" Min X:").Append(num).Append("\n");  
  112.                                builder.Append(" Min Y:").Append(num2).Append("\n");  
  113.                                builder.Append(" Max X:").Append(num3).Append("\n");  
  114.                                builder.Append(" Max Y:").Append(num4).Append("\n");  
  115.                                builder.Append(" XYScale:").Append(num5).Append("\n");  
  116.                                builder.Append("\n");  
  117.                            }  
  118.                            catch (Exception exception1)  
  119.                            {  
  120.                                exception = exception1;  
  121.                            }  
  122.                            builder.Append("Z Domain:\n");  
  123.                            try  
  124.                            {  
  125.                                double num6;  
  126.                                double num7;  
  127.                                double num8 = 0.0;  
  128.                                sr.GetZDomain(out num6, out num7);  
  129.                                sr.GetZFalseOriginAndUnits(out num6, out num8);  
  130.                                builder.Append("  Min Z:").Append(num6).Append("\n");  
  131.                                builder.Append("  Max Z:").Append(num7).Append("\n");  
  132.                                builder.Append("  ZScale:").Append(num8).Append("\n");  
  133.                                builder.Append("\n");  
  134.                            }  
  135.                            catch (Exception exception2)  
  136.                            {  
  137.                                exception = exception2;  
  138.                            }  
  139.                            try  
  140.                            {  
  141.                                double num9;  
  142.                                double num10;  
  143.                                builder.Append("M Domain:\n");  
  144.                                double num11 = 0.0;  
  145.                                sr.GetMDomain(out num9, out num10);  
  146.                                sr.GetMFalseOriginAndUnits(out num9, out num11);  
  147.                                builder.Append("  Min M:").Append(num9).Append("\n");  
  148.                                builder.Append("  Max M:").Append(num10).Append("\n");  
  149.                                builder.Append("  MScale:").Append(num11).Append("\n");  
  150.                            }  
  151.                            catch (Exception exception3)  
  152.                            {  
  153.                                exception = exception3;  
  154.                            }  
  155.                        }  
  156.                        else if (sr is IGeographicCoordinateSystem)  
  157.                        {  
  158.                            builder.Append("Geographic Coordinate System:\n");  
  159.                            system2 = sr as IGeographicCoordinateSystem;  
  160.                            builder.Append("  Name:").Append(system2.Name).Append("\n");  
  161.                            builder.Append("  Alias:").Append(system2.Alias).Append("\n");  
  162.                            builder.Append("  Abbreviation:").Append(system2.Abbreviation).Append("\n");  
  163.                            builder.Append("  Remarks:").Append(system2.Remarks).Append("\n");  
  164.                            builder.Append("  Angular Unit:").Append(system2.CoordinateUnit.Name).Append("(").Append(system2.CoordinateUnit.RadiansPerUnit).Append(")\n");  
  165.                            builder.Append("  Prime Meridian:").Append(system2.PrimeMeridian.Name).Append("(").Append(system2.PrimeMeridian.Longitude).Append(")\n");  
  166.                            builder.Append("  Datum:").Append(system2.Datum.Name).Append("\n");  
  167.                            builder.Append("    Spheroid:").Append(system2.Datum.Spheroid.Name).Append("\n");  
  168.                            builder.Append("      Semimajor Axis:").Append(system2.Datum.Spheroid.SemiMajorAxis).Append("\n");  
  169.                            builder.Append("      Semiminor Axis:").Append(system2.Datum.Spheroid.SemiMinorAxis).Append("\n");  
  170.                            builder.Append("      Inverse Flattening:").Append((double)(1.0 / system2.Datum.Spheroid.Flattening)).Append("\n");  
  171.                        }  
  172.                    }  
  173.                }  
  174.                return builder.ToString();  
  175.            }  
  176.            catch (Exception ex)  
  177.            { return ""; }  
  178.        }  
  179.   
  180.        public static void GaussToGeo(double y, double x, short DH, out double L, out double B, double LP)  
  181.        {  
  182.            double num9 = 3.14159265358979;  
  183.            double num10 = 0.0067385254147;  
  184.            double num11 = 6399698.90178271;  
  185.            double num8 = (x / 1000000.0) - 3.0;  
  186.            double num6 = (((((27.11115372595 + (9.02468257083 * num8)) - (0.00579740442 * Math.Pow(num8, 2.0))) - (0.00043532572 * Math.Pow(num8, 3.0))) + (4.857285E-05 * Math.Pow(num8, 4.0))) + (2.15727E-06 * Math.Pow(num8, 5.0))) - (1.9399E-07 * Math.Pow(num8, 6.0));  
  187.            double num2 = Math.Tan((num6 * num9) / 180.0);  
  188.            double num7 = num10 * Math.Pow(Math.Cos((num6 * num9) / 180.0), 2.0);  
  189.            double num3 = (y * Math.Sqrt(1.0 + num7)) / num11;  
  190.            double num5 = num6 - ((((1.0 + num7) * num2) / num9) * (((90.0 * Math.Pow(num3, 2.0)) - ((7.5 * (((5.0 + (3.0 * Math.Pow(num2, 2.0))) + num7) - ((9.0 * num7) * Math.Pow(num2, 2.0)))) * Math.Pow(num3, 4.0))) + ((0.25 * ((61.0 + (90.0 * Math.Pow(num2, 2.0))) + (45.0 * Math.Pow(num2, 4.0)))) * Math.Pow(num3, 6.0))));  
  191.            double num4 = (((180.0 * num3) - ((30.0 * ((1.0 + (2.0 * Math.Pow(num2, 2.0))) + num7)) * Math.Pow(num3, 3.0))) + ((1.5 * ((5.0 + (28.0 * Math.Pow(num2, 2.0))) + (24.0 * Math.Pow(num2, 4.0)))) * Math.Pow(num3, 5.0))) / (num9 * Math.Cos((num6 * num9) / 180.0));  
  192.            double num = num4 * 3600.0;  
  193.            if (LP == -1000.0)  
  194.            {  
  195.                L = (((DH * 6) - 3) * 3600.0) + num;  
  196.            }  
  197.            else  
  198.            {  
  199.                L = (LP * 3600.0) + num;  
  200.            }  
  201.            B = num5 * 3600.0;  
  202.        }  
  203.   
  204.        public static void GeoToGauss(double jd, double wd, short DH, short DH_width, out double y, out double x, double LP)  
  205.        {  
  206.            double num2;  
  207.            double num12 = 3.14159265358979;  
  208.            double num13 = 0.0067385254147;  
  209.            double num14 = 6399698.90178271;  
  210.            double num4 = ((jd / 3600.0) * num12) / 180.0;  
  211.            double a = ((wd / 3600.0) * num12) / 180.0;  
  212.            if (LP == -1000.0)  
  213.            {  
  214.                num2 = (DH - 0.5) * DH_width;  
  215.            }  
  216.            else  
  217.            {  
  218.                num2 = LP;  
  219.            }  
  220.            double num3 = (jd / 3600.0) - num2;  
  221.            double num10 = Math.Sin(a);  
  222.            double num11 = Math.Cos(a);  
  223.            double num8 = (30.870794749999998 * wd) - (((((32005.7799 * num10) + (133.9238 * Math.Pow(num10, 3.0))) + (0.6976 * Math.Pow(num10, 5.0))) + (0.0039 * Math.Pow(num10, 7.0))) * num11);  
  224.            double num6 = num13 * Math.Pow(num11, 2.0);  
  225.            double num7 = num14 / Math.Sqrt(1.0 + num6);  
  226.            double num = Math.Tan(a);  
  227.            double num9 = ((num12 / 180.0) * num3) * num11;  
  228.            x = num8 + ((num7 * num) * (((0.5 * Math.Pow(num9, 2.0)) + (((((5.0 - Math.Pow(num, 2.0)) + (9.0 * num6)) + (4.0 * Math.Pow(num6, 2.0))) * Math.Pow(num9, 4.0)) / 24.0)) + ((((61.0 - (58.0 * Math.Pow(num, 2.0))) + Math.Pow(num, 4.0)) * Math.Pow(num9, 6.0)) / 720.0)));  
  229.            y = num7 * ((num9 + ((((1.0 - Math.Pow(num, 2.0)) + num6) * Math.Pow(num9, 3.0)) / 6.0)) + ((((((5.0 - (18.0 * Math.Pow(num, 2.0))) + Math.Pow(num, 4.0)) + (14.0 * num6)) - ((58.0 * num6) * Math.Pow(num, 2.0))) * Math.Pow(num9, 5.0)) / 120.0));  
  230.        }  
  231.   
  232.        private void GKTransform(double x, double y, ref double B, ref double L, double center, int n)  
  233.        {  
  234.            double num = (y - 500000.0) - (0xf4240 * n);  
  235.            double a = 1.57048687472752E-07 * x;  
  236.            double num4 = Math.Sin(a);  
  237.            double num2 = a + (Math.Cos(a) * ((((0.00505250559291393 * num4) - (2.98473350966158E-05 * Math.Pow(num4, 3.0))) + (2.41627215981336E-07 * Math.Pow(num4, 5.0))) - (2.22241909461273E-09 * Math.Pow(num4, 7.0))));  
  238.            double num6 = Math.Tan(num2);  
  239.            double num8 = 0.00669438499958795 * Math.Pow(Math.Cos(num2), 2.0);  
  240.            double num5 = Math.Sqrt(1.0 + num8);  
  241.            double num7 = 6399596.65198801 / num5;  
  242.            double num10 = num / num7;  
  243.            double num9 = Math.Pow(num5, 2.0) * num6;  
  244.            double num11 = Math.Pow(num6, 2.0);  
  245.            B = ((num2 - ((num9 * Math.Pow(num10, 2.0)) / 2.0)) + ((((((5.0 + (3.0 * num11)) + num8) - ((9.0 * num8) * num11)) * num9) * Math.Pow(num10, 4.0)) / 24.0)) - (((((61.0 + (90.0 * num11)) + (45.0 * Math.Pow(num11, 2.0))) * num9) * Math.Pow(num10, 6.0)) / 720.0);  
  246.            double num13 = 1.0 / Math.Cos(num2);  
  247.            L = (((num13 * num10) - (((((1.0 + (2.0 * num11)) + num8) * num13) * Math.Pow(num10, 3.0)) / 6.0)) + (((((((5.0 + (28.0 * num11)) + (24.0 * Math.Pow(num11, 2.0))) + (6.0 * num8)) + ((8.0 * num8) * num11)) * num13) * Math.Pow(num10, 5.0)) / 120.0)) + center;  
  248.        }  
  249.   
  250.        public static IPoint JWD2XY(IPoint pJWDPoint, int gcsType, int pcsType)  
  251.        {  
  252.            try  
  253.            {  
  254.                ISpatialReferenceFactory factory = new SpatialReferenceEnvironmentClass();  
  255.                pJWDPoint.SpatialReference = factory.CreateGeographicCoordinateSystem(gcsType);  
  256.                pJWDPoint.Project(factory.CreateProjectedCoordinateSystem(pcsType));  
  257.                return pJWDPoint;  
  258.            }  
  259.            catch  
  260.            {  
  261.                return null;  
  262.            }  
  263.        }  
  264.   
  265.        private static double Rad(double d)  
  266.        {  
  267.            return ((d * 3.1415926535897931) / 180.0);  
  268.        }  
  269.   
  270.        public static IPoint XY2JWD(double x, double y, int gcsType, int pcsType)  
  271.        {  
  272.            try  
  273.            {  
  274.                IPoint point = new PointClass();  
  275.                point.PutCoords(x, y);  
  276.                ISpatialReferenceFactory factory = new SpatialReferenceEnvironmentClass();  
  277.                point.SpatialReference = factory.CreateProjectedCoordinateSystem(pcsType);  
  278.                point.Project(factory.CreateGeographicCoordinateSystem(gcsType));  
  279.                return point;  
  280.            }  
  281.            catch  
  282.            {  
  283.                return null;  
  284.            }  
  285.        }  
  286.   
  287.        public static IPoint XY2JYD(ISpatialReference aSR, IPoint pXY)  
  288.        {  
  289.            IPoint point = new PointClass();  
  290.            point = (pXY as IClone).Clone() as IPoint;  
  291.            if (aSR == null)  
  292.            {  
  293.                return point;  
  294.            }  
  295.            if (aSR.Name!= "Unknown")  
  296.            {  
  297.                point.SpatialReference = aSR;  
  298.                point.SnapToSpatialReference();  
  299.                if (aSR is IProjectedCoordinateSystem)  
  300.                {  
  301.                    IGeographicCoordinateSystem system = (aSR as IProjectedCoordinateSystem).GeographicCoordinateSystem;  
  302.                    point.Project(system);  
  303.                }  
  304.                return point;  
  305.            }  
  306.            return null;  
  307.        }  
  308.   
  309.        // Nested Types  
  310.        public enum GaussSphere  
  311.        {  
  312.            Beijing54,  
  313.            Xian80,  
  314.            WGS84  
  315.        }  
  316.   
  317.        public static ISpatialReference SelectSR(String pFolderPath)  
  318.        {  
  319.            OpenFileDialog ofd = new OpenFileDialog();  
  320.            ofd.Filter = "ESRI 投影信息文件(*.prj)|*.prj";  
  321.            ofd.Title = "读取ESRI投影定义文件";  
  322.            ofd.Multiselect = false;  
  323.            if (pFolderPath != null &&  
  324.                Directory.Exists(pFolderPath))  
  325.            {  
  326.                ofd.InitialDirectory = pFolderPath;  
  327.            }  
  328.            if (ofd.ShowDialog() == DialogResult.OK)  
  329.            {  
  330.                string filePath = ofd.FileName;  
  331.                return ReadSR(filePath);  
  332.            }  
  333.            else return null;  
  334.        }  
  335.        public static ISpatialReference ReadSR(string pPrjFilePath)  
  336.        {  
  337.            if (File.Exists(pPrjFilePath))  
  338.            {  
  339.                StreamReader aReader = new StreamReader(pPrjFilePath);  
  340.                string aLine = aReader.ReadToEnd();  
  341.                aReader.Close();  
  342.                int byteCount = 0;  
  343.                if (aLine.StartsWith("GEOGCS"))  
  344.                {  
  345.                    GeographicCoordinateSystemClass sr = new GeographicCoordinateSystemClass();  
  346.                    (sr as IESRISpatialReference).ImportFromESRISpatialReference(aLine, out byteCount);  
  347.                    return sr;  
  348.                }  
  349.                else  
  350.                {  
  351.                    ProjectedCoordinateSystemClass sr = new ProjectedCoordinateSystemClass();  
  352.                    (sr as IESRISpatialReference).ImportFromESRISpatialReference(aLine, out byteCount);  
  353.                    return sr;  
  354.                }  
  355.            }  
  356.            else  
  357.            {  
  358.                return null;  
  359.            }  
  360.        }  
  361.    } 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值