OSDN Git Service

ab256154f7e4ca5d027fed5a06c9719a3ace1dcd
[importpicture/importpicture.git] / importPicture / src / osm / jp / gpx / ElementMapTRKPT.java
1 package osm.jp.gpx;
2
3 import java.text.ParseException;
4 import java.util.Date;
5 import java.util.TreeMap;
6
7 import org.w3c.dom.DOMException;
8
9 @SuppressWarnings("serial")
10 public class ElementMapTRKPT extends TreeMap<Date, TagTrkpt> {
11         public static final long DIFF_MAE_TIME = 3000L; // before 3 secound
12
13         public ElementMapTRKPT() {
14                 super(new TimeComparator());
15         }
16
17         /**
18          * 拡張put value:ElementをputするとElement内のtimeを読み取ってkeyとしてthis.put(key,value)する。
19          * <trkpt lat="36.4260153752" lon="138.0117778201">
20      *   <ele>614.90</ele>
21      *   <time>2017-05-21T23:02:16Z</time>
22      *   <hdop>0.5</hdop>
23      * </trkpt>
24          * @param value
25          * @return      keyとして登録したtime:Date
26          * @throws ParseException 
27          * @throws DOMException 
28          */
29         public Date put(TagTrkpt tag) throws DOMException, ParseException {
30         this.put(tag.time, tag);
31         return tag.time;
32         }
33         
34         /**
35      * 指定時刻(jptime)のTRKPTエレメントを取り出す。
36      * 
37      * @param jptime    指定する日時
38      * @return  エレメントTRKPT。指定時刻に対応するノードがないときはnullを返す。
39      * @throws ParseException
40      */
41     public TagTrkpt getValue(Date jptime) throws ParseException {
42         TagTrkpt imaE = getTrkpt(jptime);
43         if (imaE != null) {
44                 TagTrkpt maeE = getMaeTrkpt(imaE.time);
45             if (maeE != null) {
46                 Complementation comp = new Complementation(imaE, maeE);
47
48                 // <MAGVAR>がなければ、
49                 // 直前の位置と、現在地から進行方向を求める
50                 // 経度(longitude)と経度から進行方向を求める
51                 if (Complementation.param_GpxOverwriteMagvar) {
52                         comp.complementationMagvar();
53                 }
54
55                 // 緯度・経度と時間差から速度(km/h)を求める
56                 if (Complementation.param_GpxOutputSpeed) {
57                         comp.complementationSpeed();
58                 }
59                 //return (TagTrkpt)(comp.imaTag.trkpt.cloneNode(true));
60                 return (TagTrkpt)(comp.imaTag);
61             }
62             return imaE;
63         }
64         return null;
65     }
66     
67     /**
68      * [map]から指定した時刻の<trkpt>エレメントを取り出す。
69      * 取り出すエレメントは、指定した時刻と同一時刻、もしくは、直近・直前の時刻のエレメントとする。
70      * 指定した時刻以前のエレメントが存在しない場合は null を返す。
71      * 指定した時刻と直近・直前のエレメントの時刻との乖離が プロパティ[OVER_TIME_LIMIT=3000(ミリ秒)]より大きい場合には null を返す。
72      * 
73      * @param jptime
74      * @return  <trkpt>エレメント。対象のエレメントが存在しなかった場合には null。
75      * @throws ParseException
76      */
77     private TagTrkpt getTrkpt(Date jptime) throws ParseException {
78         Date keyTime = null;
79         for (Date key : this.keySet()) {
80                         int flag = jptime.compareTo(key);
81                         if (flag < 0) {
82                                 if (keyTime != null) {
83                                         return this.get(keyTime);
84                                 }
85                                 return null;
86                         }
87                         else if (flag == 0) {
88                                 return this.get(key);
89                         }
90                         else if (flag > 0) {
91                                 keyTime = new Date(key.getTime());
92                         }
93                 }
94                 if (keyTime != null) {
95                         if (Math.abs(keyTime.getTime() - jptime.getTime()) <= OVER_TIME_LIMIT) {
96                                 return this.get(keyTime);
97                         }
98                 }
99                 return null;
100     }
101     
102     /**
103      * ロガーの最終取得時刻を超えた場合、どこまでを有効とするかを設定する。
104      * この設定がないと、最終取得時刻を超えたものは全て有効になってしまう。
105      * OVER_TIME_LIMITは、GPSロガーの位置取得間隔()よりも長くする必要がある。長すぎても良くない。
106      */
107     public static long OVER_TIME_LIMIT = 3000;  // ミリ秒(msec)
108     
109     private TagTrkpt getMaeTrkpt(Date time) throws ParseException {
110         Date maeTime = null;
111                 for (Date key : this.keySet()) {
112                         int flag = time.compareTo(key);
113                         if (flag > 0) {
114                                 maeTime = new Date(key.getTime());
115                         }
116                         else if (flag == 0) {
117                                 if (maeTime == null) {
118                                         return null;
119                                 }
120                                 return this.get(maeTime);
121                         }
122                         else {
123                                 // time は key より古い
124                                 if (maeTime == null) {
125                                         return null;
126                                 }
127                                 if (Math.abs(maeTime.getTime() - time.getTime()) > OVER_TIME_LIMIT) {
128                                         return null;
129                                 }
130                                 return this.get(maeTime);
131                         }
132                 }
133         return null;
134     }
135     
136     public void printinfo() {
137         Date firstTime = null;
138         Date lastTime = null;
139                 for (Date key : this.keySet()) {
140                         if (firstTime == null) {
141                                 firstTime = new Date(key.getTime());
142                         }
143                         lastTime = new Date(key.getTime());
144                 }
145                 System.out.println(String.format("|                      <trkseg/> |%20s|%20s|", ImportPicture.toUTCString(firstTime), ImportPicture.toUTCString(lastTime)));
146     }
147 }