OSDN Git Service

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