OSDN Git Service

SpeedとMagverを表示
[importpicture/importpicture.git] / importPicture / src / osm / jp / gpx / Complementation.java
1 package osm.jp.gpx;
2
3 import java.text.ParseException;
4 import org.w3c.dom.Element;
5
6 public class Complementation {
7     public static final Double R = (6378137D + 6356752.314D)/2D;        // 6367444.657m
8     //public static final double dLat = 0.00453D;                       // 1km距離を表す緯度(差分)
9     //public static final double dLon = 0.005588D;              // 1km距離を表す経度(差分)
10     
11     public TagTrkpt imaTag = null;
12     public TagTrkpt maeTag = null;
13     public static boolean param_GpxOutputSpeed = false;
14     public static boolean param_GpxOverwriteMagvar = false;
15     
16
17     /**
18      * 
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      * @param trkseg
27      * @param map
28      * @throws ParseException
29      */
30         public Complementation(Element imaE, Element maeE) throws ParseException {
31                 this.imaTag = new TagTrkpt(imaE);
32         if (maeE != null) {
33                 this.maeTag = new TagTrkpt(maeE);
34         }
35         }
36
37     
38     /**
39      * 緯度・経度と時間差から速度(km/h)を求める
40      * 
41      */
42     public void complementationSpeed() {
43         if (imaTag.speedStr != null)  {
44                 try {
45                 Double.parseDouble(imaTag.speedStr);
46                 }
47                 catch (NumberFormatException e) {
48                         // 数字以外なら<speed>エレメントを削除する
49                         imaTag.removeElement("speed");
50                         imaTag.speedStr = null;
51                 }
52         }
53         
54         if (imaTag.speedStr == null)  {
55                 double d = Coords.calcDistHubeny(imaTag.lat, imaTag.lon, maeTag.lat, maeTag.lon);
56             String str = Double.toString((d * 3600) / (imaTag.time.getTime() - maeTag.time.getTime()));
57             int iDot = str.indexOf('.');
58             if (iDot > 0) {
59                 str = str.substring(0, iDot+2);
60             }
61             imaTag.appendElement("speed", str);
62             imaTag.speedStr = new String(str);
63         }
64     }
65
66     /**
67      *  経度(longitude)と経度から進行方向を求める
68      * @param imaE
69      * @param imaLON
70      * @param imaLAT
71      * @param imaTIME
72      * @param maeLON
73      * @param maeLAT
74      * @param maeTIME
75      * @throws ParseException
76      */
77     public void complementationMagvar() throws ParseException {
78         if (imaTag.magvarStr != null) {
79                 try {
80                 Double.parseDouble(imaTag.magvarStr);
81                 }
82                 catch (NumberFormatException e) {
83                         // 数字以外なら<magvar>エレメントを削除する
84                         imaTag.removeElement("magvar");
85                         imaTag.magvarStr = null;
86                 }
87         }
88         
89         if (imaTag.magvarStr == null) {
90             Double r = Math.cos(Math.toRadians((imaTag.lat + maeTag.lat) / 2)) * R;
91             Double x = Math.toRadians(imaTag.lon - maeTag.lon) * r;
92             Double y = Math.toRadians(imaTag.lat - maeTag.lat) * R;
93             double rad = Math.toDegrees(Math.atan2(y, x));
94             
95             if (y >= 0) {
96                 if (x >= 0) {
97                     rad = 0 - (rad - 90);
98                 }
99                 else {
100                     rad = 360 - (rad - 90);
101                 }
102             }
103             else {
104                 if (x >= 0) {
105                     rad = 90 - rad;
106                 }
107                 else {
108                     rad = 90 - rad;
109                 }
110             }
111
112             String str = Double.toString(rad);
113             int iDot = str.indexOf('.');
114             if (iDot > 0) {
115                 str = str.substring(0, iDot);
116             }
117             imaTag.appendElement("magvar", str);
118             imaTag.magvarStr = new String(str);
119         }
120     }
121 }