OSDN Git Service

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