OSDN Git Service

green:
[importpicture/importpicture.git] / importPicture / src / osm / jp / gpx / ElementMapTRKSEG.java
1 package osm.jp.gpx;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.text.ParseException;
6 import java.util.Date;
7 import java.util.TreeMap;
8
9 import javax.xml.parsers.DocumentBuilder;
10 import javax.xml.parsers.DocumentBuilderFactory;
11 import javax.xml.parsers.ParserConfigurationException;
12
13 import org.w3c.dom.*;
14 import org.xml.sax.SAXException;
15
16 @SuppressWarnings("serial")
17 public class ElementMapTRKSEG extends TreeMap<Date, ElementMapTRKPT> {
18         /**
19          * TESTing
20          * @throws ParseException 
21          * @throws ParserConfigurationException 
22          * @throws IOException 
23          * @throws SAXException 
24          * @throws DOMException 
25          */
26         public static void main(String[] argv) throws DOMException, SAXException, IOException, ParserConfigurationException, ParseException {
27                 ElementMapTRKSEG mapTRKSEG = null;
28                 mapTRKSEG = new ElementMapTRKSEG();
29         mapTRKSEG.parse(new File("testdata/cameradata/separate.gpx"));
30         mapTRKSEG.printinfo();
31         }
32         
33         public ElementMapTRKSEG() {
34                 super(new TimeComparator());
35         }
36         
37     /**
38      * GPXファイルをパースする
39      * 
40      * <gpx>
41      *   <trk>
42      *     <trkseg>
43      *       <trkpt lat="35.32123832" lon="139.56965631">
44      *         <ele>47.20000076293945</ele>
45      *         <time>2012-06-15T03:00:29Z</time>
46      *         <hdop>0.5</hdop>
47      *       </trkpt>
48      *     </trkseg>
49      *   </trk>
50      * </gpx>
51          * 
52          * @param gpxFile
53          * @return Document
54          * @throws SAXException
55          * @throws IOException
56          * @throws ParserConfigurationException
57          * @throws DOMException
58          * @throws ParseException
59          */
60         public Document parse(File gpxFile) throws SAXException, IOException, ParserConfigurationException, DOMException, ParseException {
61         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
62         DocumentBuilder        builder = factory.newDocumentBuilder();
63         factory.setIgnoringElementContentWhitespace(true);
64         factory.setIgnoringComments(true);
65         factory.setValidating(true);
66         
67         Node gpx    = builder.parse(gpxFile).getFirstChild();
68         Document document = gpx.getOwnerDocument();
69         NodeList nodes = gpx.getChildNodes();
70         for (int i=0; i < nodes.getLength(); i++) {
71             Node node2 = nodes.item(i);
72             if (node2.getNodeName().equals("trk")) {
73                 Element trk = (Element) node2;
74                 
75                 NodeList nodes1 = trk.getChildNodes();
76                 for (int i1=0; i1 < nodes1.getLength(); i1++) {
77                     Node nodeTRKSEG = nodes1.item(i1);
78                     if (nodeTRKSEG.getNodeName().equals("trkseg")) {
79                         this.put(nodeTRKSEG);
80                     }
81                 }
82             }
83         }
84         return document;
85         }
86         
87         /**
88          * 拡張put value:Node<TRKSEG>をputするとNode<TRKSEG>内のNode<TRKSPT>を put(key,value)する。
89          * @param nodeTRKSEG
90          * @return      keyとして登録したtime:Date
91          * @throws ParseException 
92          * @throws DOMException 
93          */
94         public void put(Node nodeTRKSEG) throws DOMException, ParseException {
95         if (nodeTRKSEG.getNodeName().equals("trkseg")) {
96             NodeList nodes2 = nodeTRKSEG.getChildNodes();
97             
98             ElementMapTRKPT mapTRKPT = new ElementMapTRKPT();
99             for (int i2 = 0; i2 < nodes2.getLength(); i2++) {
100                 Node nodeTRKPT = nodes2.item(i2);
101                 if (nodeTRKPT.getNodeName().equals("trkpt")) {
102                         if (ImportPicture.param_GpxNoFirstNode && (i2 == 0)) {
103                                 continue;
104                         }
105                     mapTRKPT.put(new TagTrkpt((Element)nodeTRKPT));
106                 }
107             }
108             this.put(mapTRKPT);
109         }
110         }
111         
112         /**
113          * 拡張put value:ElementMapTRKPTをputするとElementMapTRKPT内の最初のエントリのtimeを読み取ってkeyとしてthis.put(key,value)する。
114          * @param value
115          * @return      keyとして登録したtime:Date
116          * @throws ParseException 
117          * @throws DOMException 
118          */
119         public void put(ElementMapTRKPT value) {
120                 for (Date key : value.keySet()) {
121                         this.put(key, value);
122                         return;
123                 }
124         }
125         
126         public void printinfo() {
127                 System.out.println("                                 +--------------------+--------------------|");
128                 System.out.println("  GPS logging time               | First Time         | Last Time          |");
129                 System.out.println("|--------------------------------+--------------------+--------------------|");
130
131                 for (java.util.Map.Entry<Date, ElementMapTRKPT> map : this.entrySet()) {
132                 ElementMapTRKPT mapTRKPT = map.getValue();
133                 mapTRKPT.printinfo();
134         }
135                 
136                 System.out.println("|--------------------------------+--------------------+--------------------|");
137                 System.out.println();
138         }
139
140 }