OSDN Git Service

LLH座標系に加えECEF座標系を追加
[yubeshi/yubeshi.git] / Yubeshi / GeodeticCoordinate.cs
1 /*\r
2  *      Yubeshi GPS Parser\r
3  *\r
4  *      This software is distributed under a zlib-style license.\r
5  *      See license.txt for more information.\r
6  */\r
7 \r
8 using System;\r
9 using System.Collections.Generic;\r
10 using System.Text;\r
11 \r
12 namespace Yubeshi\r
13 {\r
14     public class GeodeticCoordinate\r
15     {\r
16         #region fields\r
17         private const double a = Constants.SemiMajorAxisA;\r
18         private const double b = Constants.SemiMajorAxisB;\r
19         private const double e1sq = Constants.FirstEccentricitySquared;\r
20         private const double pi = Constants.Pi;\r
21 \r
22         #endregion\r
23         #region constructors\r
24 \r
25         public GeodeticCoordinate(double latitude, double longitude)\r
26             : this(latitude, longitude, Double.NaN)\r
27         {\r
28         }\r
29 \r
30         public GeodeticCoordinate(double latitude, double longitude, double altitude)\r
31         {\r
32             Latitude = latitude;\r
33             Longitude = longitude;\r
34             Altitude = altitude;\r
35         }\r
36 \r
37         public GeodeticCoordinate(string latitude, string ns,\r
38                                     string longitude, string ew)\r
39         {\r
40             decimal latRaw = Decimal.Parse(latitude);\r
41             decimal lngRaw = Decimal.Parse(longitude);\r
42             decimal lat = Math.Floor(latRaw / 100m) + (latRaw % 100m) / 60m;\r
43             decimal lng = Math.Floor(lngRaw / 100m) + (lngRaw % 100m) / 60m;\r
44             Latitude = (double)((ns == "S") ? -lat : lat);\r
45             Longitude = (double)((ns == "W") ? -lng : lng);\r
46             Altitude = Double.NaN;\r
47         }\r
48         #endregion\r
49 \r
50         #region properties\r
51 \r
52         /// <summary>\r
53         ///    in Degree\r
54         /// </summary>\r
55         public double Latitude\r
56         {\r
57             get;\r
58             set;\r
59         }\r
60         /// <summary>\r
61         ///     in Degree\r
62         /// </summary>\r
63         public double Longitude\r
64         {\r
65             get;\r
66             set;\r
67         }\r
68 \r
69         /// <summary>\r
70         ///     in metre\r
71         /// </summary>\r
72         public double Altitude\r
73         {\r
74             get;\r
75             set;\r
76         }\r
77         #endregion\r
78 \r
79         #region public method\r
80         public EcefCoordinate ToEcefCoordinate()\r
81         {\r
82             double h = Double.IsNaN(Altitude) ? 0 : Altitude;\r
83             double sinPhi = Math.Sin(Latitude * pi / 180.0);\r
84             double cosPhi = Math.Cos(Latitude * pi / 180.0);\r
85             double khi = Math.Sqrt(1.0 - e1sq * sinPhi * sinPhi);\r
86             double n = a / khi;\r
87             double r = (n + h) * cosPhi;\r
88             double x = r * Math.Cos(Longitude * pi / 180);\r
89             double y = r * Math.Sin(Longitude * pi / 180);\r
90             double z = (n * (1 - e1sq) + h) * sinPhi;\r
91             return new EcefCoordinate(x, y, z);\r
92         }\r
93 \r
94 \r
95         #endregion\r
96 \r
97         #region private methods\r
98         #endregion\r
99     }\r
100 }\r