]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/NmeaFileLoader.java
Version 8, September 2009
[GpsPrune.git] / tim / prune / load / NmeaFileLoader.java
1 package tim.prune.load;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileReader;
6 import java.io.IOException;
7 import java.util.ArrayList;
8
9 import tim.prune.App;
10 import tim.prune.data.Altitude;
11 import tim.prune.data.Field;
12
13 /**
14  * Class to handle the loading of Nmea files
15  */
16 public class NmeaFileLoader
17 {
18         /** App for callback of file loading */
19         private App _app = null;
20
21         /**
22          * Constructor
23          * @param inApp App object
24          */
25         public NmeaFileLoader(App inApp)
26         {
27                 _app = inApp;
28         }
29
30         /**
31          * Open the selected file
32          * @param inFile File to open
33          */
34         public void openFile(File inFile)
35         {
36                 BufferedReader reader = null;
37                 ArrayList<NmeaMessage> messages = new ArrayList<NmeaMessage>();
38                 try
39                 {
40                         reader = new BufferedReader(new FileReader(inFile));
41                         String currLine = reader.readLine();
42                         boolean newSegment = true;
43                         while (currLine != null)
44                         {
45                                 // Try to make an NmeaMessage object for each line of file
46                                 if (currLine.trim().length() > 0)
47                                 {
48                                         NmeaMessage message = processLine(currLine);
49                                         if (message != null)
50                                         {
51                                                 if (message.hasFix()) {
52                                                         message.setSegment(newSegment);
53                                                         // add message to list
54                                                         messages.add(message);
55                                                 }
56                                                 // Start a new segment if fix lost
57                                                 newSegment = !message.hasFix();
58                                         }
59                                 }
60                                 // Read next line, if any
61                                 currLine = reader.readLine();
62                         }
63                 }
64                 catch (IOException ioe) {
65                         _app.showErrorMessage("error.load.dialogtitle", "error.load.noread");
66                 }
67                 finally
68                 {
69                         // close file ignoring errors
70                         try
71                         {
72                                 if (reader != null) reader.close();
73                         }
74                         catch (Exception e) {}
75                 }
76                 if (messages.size() > 0)
77                 {
78                         _app.informDataLoaded(getFieldArray(), makeDataArray(messages),
79                                 Altitude.Format.METRES, inFile.getName());
80                 }
81         }
82
83         /**
84          * Process the given NMEA line and return the message
85          * @param inLine line to process
86          * @return message object
87          */
88         private static NmeaMessage processLine(String inLine)
89         {
90                 // Only consider lines which are long enough and begin with the GPS position sentence
91                 if (inLine == null || inLine.length() < 20 || !inLine.startsWith("$GPGGA")) {
92                         return null;
93                 }
94                 // TODO: May be possible to pull date out of GPRMC messages, but then need to back-populate
95                 // Assume comma delimiter, split into array
96                 String[] splitLine = inLine.split(",");
97                 if (splitLine != null && splitLine.length >= 10)
98                 {
99                         return new NmeaMessage(splitLine[2] + splitLine[3], // latitude
100                                 splitLine[4] + splitLine[5], // longitude
101                                 splitLine[9], // altitude
102                                 splitLine[1], // timestamp
103                                 splitLine[6]); // fix
104                 }
105                 // Couldn't parse it, return null
106                 return null;
107         }
108
109         /**
110          * Make an object array from the data list
111          * @param inList list of messages
112          * @return object array for loading
113          */
114         private static Object[][] makeDataArray(ArrayList<NmeaMessage> inList)
115         {
116                 Object[][] result = new Object[inList.size()][];
117                 for (int i=0; i<inList.size(); i++) {
118                         result[i] = inList.get(i).getStrings();
119                 }
120                 return result;
121         }
122
123         /**
124          * @see tim.prune.load.xml.XmlHandler#getFieldArray()
125          */
126         public Field[] getFieldArray()
127         {
128                 final Field[] fields = {Field.LATITUDE, Field.LONGITUDE, Field.ALTITUDE,
129                         Field.TIMESTAMP, Field.NEW_SEGMENT};
130                 return fields;
131         }
132 }