OSDN Git Service

ea4c71f7ca201ee6c8f24b55cbc5b191fa4970b1
[importpicture/importpicture.git] / src / main / java / osm / jp / gpx / GpxParser.java
1 package osm.jp.gpx;
2
3 import java.text.ParseException;
4 import org.xml.sax.Attributes;
5 import org.xml.sax.helpers.DefaultHandler;
6
7 /**
8  * GPXファイルをパースする
9  * @param gpxFile
10  * @code{
11  * <gpx>
12  *   <trk>
13  *     <trkseg>
14  *       <trkpt lat="35.32123832" lon="139.56965631">
15  *         <ele>47.20000076293945</ele>
16  *         <time>2012-06-15T03:00:29Z</time>
17  *         <hdop>0.5</hdop>
18  *       </trkpt>
19  *     </trkseg>
20  *   </trk>
21  * </gpx>
22  * }
23  * 
24  */
25 public class GpxParser extends DefaultHandler {
26         StringBuffer outSb;
27         int segCnt = 0;
28         int kptCnt = 0;
29         boolean kpt = false;
30         TagTrkpt tag = null;
31         public ElementMapTRKPT trkpt;
32         public ElementMapTRKSEG trkseg = new ElementMapTRKSEG();
33         
34     AppParameters params;
35
36     public GpxParser(AppParameters params) {
37         super();
38         this.params = params;
39         trkpt = new ElementMapTRKPT(params);
40     }
41     
42         /**
43      * ドキュメント開始
44      */
45     public void startDocument() {
46         outSb = new StringBuffer();
47     }
48  
49     /**
50      * ドキュメント終了
51      */
52     public void endDocument() {
53     }
54     
55     public void startElement(String uri,String localName, String qName, Attributes atts) {
56                 if(qName.equals("trkseg")){
57                         segCnt++;
58                         kptCnt = 0;
59                         if (trkpt.size() > 0) {
60                                 trkpt.printinfo();
61                                 trkseg.put((ElementMapTRKPT) trkpt.clone());
62                                 trkpt.clear();
63                         }
64                 }
65                 if(qName.equals("trkpt")){
66                         kpt = true;
67                         kptCnt++;
68                         if (tag != null) {
69                                 if (tag.getTime() != null) {
70                                         trkpt.put(tag.clone());
71                                 }
72                                 tag = null;
73                         }
74
75                         Double lat = null;
76                     Double lon = null;
77
78                         for (int i = 0; i < atts.getLength(); i++) {
79                                 String aname = atts.getQName(i);
80                                 if (aname.equals("lat")) {
81                                         lat = new Double(atts.getValue(i));
82                                 }
83                                 if (aname.equals("lon")) {
84                                         lon = new Double(atts.getValue(i));
85                                 }
86                         }
87                         
88                         if ((lat != null) && (lon != null)) {
89                                 tag = new TagTrkpt(lat, lon);
90                         }
91                 }
92                 if(qName.equals("ele")){
93                         outSb = new StringBuffer();
94                 }
95                 if(qName.equals("time")){
96                         outSb = new StringBuffer();
97                 }
98                 if(qName.equals("magvar")){
99                         outSb = new StringBuffer();
100                 }
101                 if(qName.equals("speed")){
102                         outSb = new StringBuffer();
103                 }
104         }
105
106     /**
107      * 要素の終了タグ読み込み時に毎回呼ばれる
108      */
109     public void endElement(String uri,String localName,String qName) {
110         if(qName.equals("trkseg")){
111                         if (trkpt.size() > 0) {
112                                 trkseg.put((ElementMapTRKPT) trkpt.clone());
113                                 trkpt.clear();
114                         }
115         }
116         if(qName.equals("trkpt")){
117                 kpt = false;
118                         if (tag != null) {
119                                 if (tag.getTime() != null) {
120                                         trkpt.put(tag);
121                                 }
122                                 tag = null;
123                         }
124         }
125                 if(qName.equals("ele")){
126                         tag.setEle(outSb.toString());
127                 }
128                 if(qName.equals("time")){
129                         try {
130                                 tag.setTime(ImportPicture.toUTCDate(outSb.toString()));
131                         } catch (ParseException e) {}
132                 }
133                 if(qName.equals("magvar")){
134                         tag.setMagvar(outSb.toString());
135                 }
136                 if(qName.equals("speed")){
137                         tag.setSpeed(outSb.toString());
138                 }
139                 outSb = new StringBuffer();
140     }
141
142     /**
143      * テキストデータ読み込み時に毎回呼ばれる
144      */
145     public void characters(char[] ch, int offset, int length) {
146         if (kpt) {
147             outSb.append(new String(ch, offset, length));
148         }
149     }
150     
151  }