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));
100 * Process the given GGA sentence and return the message
101 * @param inLine line to process
102 * @return message object
104 private static NmeaMessage processGGA(String inLine)
106 // Only consider lines which are long enough and begin with the GPS position sentence
107 if (inLine == null || inLine.length() < 20 || !inLine.startsWith("$GPGGA")) {
110 // Assume comma delimiter, split into array
111 String[] splitLine = inLine.split(",");
112 if (splitLine != null && splitLine.length >= 10)
114 return new NmeaMessage(splitLine[2] + splitLine[3], // latitude
115 splitLine[4] + splitLine[5], // longitude
116 splitLine[9], // altitude
117 splitLine[1], // timestamp
118 splitLine[6]); // fix
120 // Couldn't parse it, return null
125 * Process the given MRC sentence and return the date
126 * @param inLine line to process
127 * @return date, if any
129 private static String getDateFromRMC(String inLine)
131 // Only consider lines which are long enough and begin with the RMC sentence
132 if (inLine == null || inLine.length() < 20 || !inLine.startsWith("$GPRMC")) {
135 // Assume comma delimiter, split into array
136 String[] splitLine = inLine.split(",");
137 if (splitLine != null && splitLine.length >= 10)
139 return splitLine[9]; // date in position 9
141 // Couldn't parse it, return null
146 * Make an object array from the data list
147 * @param inList list of messages
148 * @return object array for loading
150 private static Object[][] makeDataArray(ArrayList<NmeaMessage> inList)
152 Object[][] result = new Object[inList.size()][];
153 for (int i=0; i<inList.size(); i++) {
154 result[i] = inList.get(i).getStrings();
160 * @see tim.prune.load.xml.XmlHandler#getFieldArray()
162 public Field[] getFieldArray()
164 final Field[] fields = {Field.LATITUDE, Field.LONGITUDE, Field.ALTITUDE,
165 Field.TIMESTAMP, Field.NEW_SEGMENT};