OSDN Git Service

red: osm.jp.gpx.ElementMapTRKPTTest$Keyとvalueのセット.get_最後の1秒後(ElementMapTRKPTTest...
[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.Iterator;
6 import java.util.Set;
7 import java.util.TreeMap;
8
9 import org.w3c.dom.DOMException;
10 import org.w3c.dom.Element;
11 import org.w3c.dom.Node;
12 import org.w3c.dom.NodeList;
13
14 @SuppressWarnings("serial")
15 public class ElementMapTRKPT extends TreeMap<Date, Element> {
16         public static final long DIFF_MAE_TIME = 3000L; // before 3 secound
17
18         public ElementMapTRKPT() {
19                 super(new TimeComparator());
20         }
21
22         /**
23          * 拡張put value:ElementをputするとElement内のtimeを読み取ってkeyとしてsuper.put(key,value)する。
24          * @param value
25          * @return      keyとして登録したtime:Date
26          * @throws ParseException 
27          * @throws DOMException 
28          */
29         public Date put(Element value) throws DOMException, ParseException {
30         NodeList nodes3 = value.getChildNodes();
31         for (int i3=0; i3 < nodes3.getLength(); i3++) {
32             Node node4 = nodes3.item(i3);
33             if (node4.getNodeName().equals("time")) {
34                 NodeList nodes4 = node4.getChildNodes();      // 子ノードを取得
35                 for (int i4=0; i4< nodes4.getLength(); i4++) {
36                     Node node5 = nodes4.item(i4);
37                     if (node5 != null) {
38                         if (node5.getNodeType() == Node.TEXT_NODE) {
39                             Date time = ImportPicture.dfuk.parse(node5.getNodeValue());
40                             this.put(time, value);
41                                 return time;
42                         }
43                     }
44                 }
45             }
46         }
47         return null;
48         }
49         
50         /**
51      * 指定時刻(jptime)のTRKPTエレメントを取り出す。
52      * 
53      * @param jptime    指定する日時
54      * @return  エレメントTRKPT。指定時刻に対応するノードがないときはnullを返す。
55      * @throws ParseException
56      */
57     public Element getValue(Date jptime) throws ParseException {
58         Element imaE = getTrkpt(jptime);
59         if (imaE != null) {
60                 Element maeE = getMaeTrkpt(new TagTrkpt(imaE));
61             if (maeE != null) {
62                 Complementation comp = new Complementation(imaE, maeE);
63
64                 // <MAGVAR>がなければ、
65                 // 直前の位置と、現在地から進行方向を求める
66                 // 経度(longitude)と経度から進行方向を求める
67                 if (Complementation.param_GpxOverwriteMagvar) {
68                         comp.complementationMagvar();
69                 }
70
71                 // 緯度・経度と時間差から速度(km/h)を求める
72                 if (Complementation.param_GpxOutputSpeed) {
73                         comp.complementationSpeed();
74                 }
75                 
76                 return (Element)(comp.imaTag.trkpt.cloneNode(true));
77             }
78         }
79         return null;
80     }
81     
82     /**
83      * [map]から指定した時刻の<trkpt>エレメントを取り出す。
84      * GPX時刻との差が10分以上は無効
85      * 
86      * @param mapTRKPT
87      * @param jptime
88      * @return  <trkpt>エレメント
89      * @throws ParseException
90      */
91     private Element getTrkpt(Date jptime) throws ParseException {
92         long sa = 2L * 3600000L;
93         long jpt = jptime.getTime();
94         
95         Element ret = null;
96
97         Set<Date> keySet = this.keySet();  //すべてのキー値を取得
98         Iterator<Date> keyIte = keySet.iterator();
99         while (keyIte.hasNext()) {
100             Date time = keyIte.next();
101             long t = time.getTime();
102
103             if (Math.abs(jpt - t) < sa) {
104                 sa = Math.abs(jpt - t);
105                 ret = this.get(time);
106             }
107         }
108
109         // GPX時刻との差が10分以内なら有効
110         if (sa < (60000L * 10L)) {
111             return ret;
112         }
113         return null;
114     }
115     
116     private Element getMaeTrkpt(TagTrkpt imaTrkpt) throws ParseException {
117         Element ret = null;
118         long diffTime = 2L * 3600000L;          // 2時間
119         long jpt = imaTrkpt.time.getTime() - DIFF_MAE_TIME;
120
121         Set<Date> keySet = this.keySet();  //すべてのキー値を取得
122         Iterator<Date> keyIte = keySet.iterator();
123         while (keyIte.hasNext()) {    //ループ。反復子iteratorによる キー 取得
124             Date time = keyIte.next();
125             long t = time.getTime();
126
127             if (Math.abs(jpt - t) < diffTime) {
128                 diffTime = Math.abs(jpt - t);
129                 ret = this.get(time);
130             }
131         }
132
133         // GPX時刻との差が10分以内なら有効
134         if (diffTime < (60000L * 10L)) {
135                 // 元の時刻との差が1秒以上あること
136                 if (diffTime < (imaTrkpt.time.getTime() - 1000)) {
137                 return ret;
138                 }
139         }
140         return null;
141     }
142 }