OSDN Git Service

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