OSDN Git Service

refactering
[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 = new TagTrkpt(imaE.trkpt);
31         if (maeE != null) {
32                 this.maeTag = new TagTrkpt(maeE.trkpt);
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.removeElement("speed");
48                 imaTag.speedStr = null;
49             }
50         }
51         
52         if (imaTag.speedStr == null)  {
53             double d = GeoDistance.calcDistHubeny(imaTag.lat, imaTag.lon, maeTag.lat, maeTag.lon);
54             String str = Double.toString((d * 3600) / (imaTag.time.getTime() - maeTag.time.getTime()));
55             int iDot = str.indexOf('.');
56             if (iDot > 0) {
57                 str = str.substring(0, iDot+2);
58             }
59             imaTag.appendElement("speed", str);
60             imaTag.speedStr = str;
61         }
62     }
63
64     /**
65      *  経度(longitude)と経度から進行方向を求める
66      * @throws ParseException
67      */
68     public void complementationMagvar() throws ParseException {
69         if (imaTag.magvarStr != null) {
70             try {
71                 Double.parseDouble(imaTag.magvarStr);
72             }
73             catch (NumberFormatException e) {
74                 // 数字以外なら<magvar>エレメントを削除する
75                 imaTag.removeElement("magvar");
76                 imaTag.magvarStr = null;
77             }
78         }
79         
80         if (imaTag.magvarStr == null) {
81             Double r = Math.cos(Math.toRadians((imaTag.lat + maeTag.lat) / 2)) * R;
82             Double x = Math.toRadians(imaTag.lon - maeTag.lon) * r;
83             Double y = Math.toRadians(imaTag.lat - maeTag.lat) * R;
84             double rad = Math.toDegrees(Math.atan2(y, x));
85             
86             if (y >= 0) {
87                 if (x >= 0) {
88                     rad = 0 - (rad - 90);
89                 }
90                 else {
91                     rad = 360 - (rad - 90);
92                 }
93             }
94             else {
95                 if (x >= 0) {
96                     rad = 90 - rad;
97                 }
98                 else {
99                     rad = 90 - rad;
100                 }
101             }
102
103             String str = Double.toString(rad);
104             int iDot = str.indexOf('.');
105             if (iDot > 0) {
106                 str = str.substring(0, iDot);
107             }
108             imaTag.appendElement("magvar", str);
109             imaTag.magvarStr = str;
110         }
111     }
112 }