1 package tim.prune.load;
3 import java.io.BufferedReader;
5 import java.io.FileReader;
6 import java.io.IOException;
7 import java.util.ArrayList;
10 import tim.prune.data.Altitude;
11 import tim.prune.data.Field;
12 import tim.prune.data.SourceInfo;
15 * Class to handle the loading of Nmea files
17 public class NmeaFileLoader
19 /** App for callback of file loading */
20 private App _app = null;
24 * @param inApp App object
26 public NmeaFileLoader(App inApp)
32 * Open the selected file
33 * @param inFile File to open
35 public void openFile(File inFile)
37 BufferedReader reader = null;
38 ArrayList<NmeaMessage> messages = new ArrayList<NmeaMessage>();
39 String lastDate = null;
42 reader = new BufferedReader(new FileReader(inFile));
43 String currLine = reader.readLine();
44 boolean newSegment = true;
45 while (currLine != null)
47 // Try to make an NmeaMessage object for each line of file
48 if (currLine.trim().length() > 0)
50 NmeaMessage message = processGGA(currLine);
55 message.setSegment(newSegment);
56 message.setDate(lastDate);
57 // add message to list
58 messages.add(message);
60 // Start a new segment if fix lost
61 newSegment = !message.hasFix();
64 String date = getDateFromRMC(currLine);
67 if (lastDate == null && !messages.isEmpty()) {
68 // Backfill first few messages received before the first date
69 for (int m=0; m<messages.size(); m++) {
70 messages.get(m).setDate(date);
77 // Read next line, if any
78 currLine = reader.readLine();
81 catch (IOException ioe) {
82 _app.showErrorMessage("error.load.dialogtitle", "error.load.noread");
86 // close file ignoring errors
88 if (reader != null) reader.close();
90 catch (Exception e) {}
92 if (messages.size() > 0)
94 _app.informDataLoaded(getFieldArray(), makeDataArray(messages),
95 Altitude.Format.METRES, new SourceInfo(inFile, SourceInfo.FILE_TYPE.NMEA),
101 * Process the given GGA sentence and return the message
102 * @param inLine line to process
103 * @return message object
105 private static NmeaMessage processGGA(String inLine)
107 // Only consider lines which are long enough and begin with the GPS position sentence
108 if (inLine == null || inLine.length() < 20 || !inLine.startsWith("$GPGGA")) {
111 // Assume comma delimiter, split into array
112 String[] splitLine = inLine.split(",");
113 if (splitLine != null && splitLine.length >= 10)
115 return new NmeaMessage(splitLine[2] + splitLine[3], // latitude
116 splitLine[4] + splitLine[5], // longitude
117 splitLine[9], // altitude
118 splitLine[1], // timestamp
119 splitLine[6]); // fix
121 // Couldn't parse it, return null
126 * Process the given MRC sentence and return the date
127 * @param inLine line to process
128 * @return date, if any
130 private static String getDateFromRMC(String inLine)
132 // Only consider lines which are long enough and begin with the RMC sentence
133 if (inLine == null || inLine.length() < 20 || !inLine.startsWith("$GPRMC")) {
136 // Assume comma delimiter, split into array
137 String[] splitLine = inLine.split(",");
138 if (splitLine != null && splitLine.length >= 10)
140 return splitLine[9]; // date in position 9
142 // Couldn't parse it, return null
147 * Make an object array from the data list
148 * @param inList list of messages
149 * @return object array for loading
151 private static Object[][] makeDataArray(ArrayList<NmeaMessage> inList)
153 Object[][] result = new Object[inList.size()][];
154 for (int i=0; i<inList.size(); i++) {
155 result[i] = inList.get(i).getStrings();
161 * @see tim.prune.load.xml.XmlHandler#getFieldArray()
163 public Field[] getFieldArray()
165 final Field[] fields = {Field.LATITUDE, Field.LONGITUDE, Field.ALTITUDE,
166 Field.TIMESTAMP, Field.NEW_SEGMENT};