OSDN Git Service

AdjustTime
[importpicture/importpicture.git] / src / main / java / osm / jp / gpx / Complementation.java
1 package osm.jp.gpx;
2
3 import java.text.ParseException;
4
5 public class Complementation {
6     public static final Double R = (6378137D + 6356752.314D)/2D;        // 6367444.657m
7     
8     public TagTrkpt imaTag = null;
9     public TagTrkpt maeTag = null;
10     //public static boolean param_GpxOutputSpeed = false;
11     //public static boolean param_GpxOverwriteMagvar = false;
12     
13     /**
14      * @param imaE
15      * @param maeE
16      * @throws java.text.ParseException
17      * @code{
18      *  <trkpt lat="34.976635" lon="138.466228">
19      *          <ele>267.291</ele>
20      *          <magvar>359</magvar>
21      *          <speed></speed>
22      *          <time>2016-07-02T08:25:18Z</time>
23      *  </trkpt>
24      * }
25      *
26      *
27      * @throws ParseException
28      */
29     public Complementation(TagTrkpt imaE, TagTrkpt maeE) throws ParseException {
30         this.imaTag = imaE.clone();
31         if (maeE != null) {
32                 this.maeTag = maeE.clone();
33         }
34     }
35     
36     /**
37      * 緯度・経度と時間差から速度(km/h)を求める
38      * 
39      */
40     public void complementationSpeed() {
41         if (imaTag.speedStr != null)  {
42             try {
43                 Double.parseDouble(imaTag.speedStr);
44             }
45             catch (NumberFormatException e) {
46                 // 数字以外なら<speed>エレメントを削除する
47                 imaTag.speedStr = null;
48             }
49         }
50         
51         if (imaTag.speedStr == null)  {
52             double d = GeoDistance.calcDistHubeny(imaTag.lat, imaTag.lon, maeTag.lat, maeTag.lon);
53             String str = Double.toString((d * 3600) / (imaTag.time.getTime() - maeTag.time.getTime()));
54             int iDot = str.indexOf('.');
55             if (iDot > 0) {
56                 str = str.substring(0, iDot+2);
57             }
58             imaTag.speedStr = str;
59         }
60     }
61
62     /**
63      *  経度(longitude)と経度から進行方向を求める
64      * @throws ParseException
65      */
66     public void complementationMagvar() throws ParseException {
67         if (imaTag.magvarStr != null) {
68             try {
69                 Double.parseDouble(imaTag.magvarStr);
70             }
71             catch (NumberFormatException e) {
72                 // 数字以外なら<magvar>エレメントを削除する
73                 imaTag.magvarStr = null;
74             }
75         }
76         
77         if (imaTag.magvarStr == null) {
78             Double r = Math.cos(Math.toRadians((imaTag.lat + maeTag.lat) / 2)) * R;
79             Double x = Math.toRadians(imaTag.lon - maeTag.lon) * r;
80             Double y = Math.toRadians(imaTag.lat - maeTag.lat) * R;
81             double rad = Math.toDegrees(Math.atan2(y, x));
82             
83             if (y >= 0) {
84                 if (x >= 0) {
85                     rad = 0 - (rad - 90);
86                 }
87                 else {
88                     rad = 360 - (rad - 90);
89                 }
90             }
91             else {
92                 if (x >= 0) {
93                     rad = 90 - rad;
94                 }
95                 else {
96                     rad = 90 - rad;
97                 }
98             }
99
100             String str = Double.toString(rad);
101             int iDot = str.indexOf('.');
102             if (iDot > 0) {
103                 str = str.substring(0, iDot);
104             }
105             imaTag.magvarStr = str;
106         }
107     }
108 }