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>();
41 reader = new BufferedReader(new FileReader(inFile));
42 String currLine = reader.readLine();
43 boolean newSegment = true;
44 while (currLine != null)
46 // Try to make an NmeaMessage object for each line of file
47 if (currLine.trim().length() > 0)
49 NmeaMessage message = processLine(currLine);
52 if (message.hasFix()) {
53 message.setSegment(newSegment);
54 // add message to list
55 messages.add(message);
57 // Start a new segment if fix lost
58 newSegment = !message.hasFix();
61 // Read next line, if any
62 currLine = reader.readLine();
65 catch (IOException ioe) {
66 _app.showErrorMessage("error.load.dialogtitle", "error.load.noread");
70 // close file ignoring errors
73 if (reader != null) reader.close();
75 catch (Exception e) {}
77 if (messages.size() > 0)
79 _app.informDataLoaded(getFieldArray(), makeDataArray(messages),
80 Altitude.Format.METRES, new SourceInfo(inFile, SourceInfo.FILE_TYPE.NMEA));
85 * Process the given NMEA line and return the message
86 * @param inLine line to process
87 * @return message object
89 private static NmeaMessage processLine(String inLine)
91 // Only consider lines which are long enough and begin with the GPS position sentence
92 if (inLine == null || inLine.length() < 20 || !inLine.startsWith("$GPGGA")) {
95 // TODO: May be possible to pull date out of GPRMC messages, but then need to back-populate
96 // Assume comma delimiter, split into array
97 String[] splitLine = inLine.split(",");
98 if (splitLine != null && splitLine.length >= 10)
100 return new NmeaMessage(splitLine[2] + splitLine[3], // latitude
101 splitLine[4] + splitLine[5], // longitude
102 splitLine[9], // altitude
103 splitLine[1], // timestamp
104 splitLine[6]); // fix
106 // Couldn't parse it, return null
111 * Make an object array from the data list
112 * @param inList list of messages
113 * @return object array for loading
115 private static Object[][] makeDataArray(ArrayList<NmeaMessage> inList)
117 Object[][] result = new Object[inList.size()][];
118 for (int i=0; i<inList.size(); i++) {
119 result[i] = inList.get(i).getStrings();
125 * @see tim.prune.load.xml.XmlHandler#getFieldArray()
127 public Field[] getFieldArray()
129 final Field[] fields = {Field.LATITUDE, Field.LONGITUDE, Field.ALTITUDE,
130 Field.TIMESTAMP, Field.NEW_SEGMENT};