OSDN Git Service

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