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;
14 * Class to handle the loading of Nmea files
16 public class NmeaFileLoader
18 /** App for callback of file loading */
19 private App _app = null;
23 * @param inApp App object
25 public NmeaFileLoader(App inApp)
31 * Open the selected file
32 * @param inFile File to open
34 public void openFile(File inFile)
36 BufferedReader reader = null;
37 ArrayList<NmeaMessage> messages = new ArrayList<NmeaMessage>();
40 reader = new BufferedReader(new FileReader(inFile));
41 String currLine = reader.readLine();
42 boolean newSegment = true;
43 while (currLine != null)
45 // Try to make an NmeaMessage object for each line of file
46 if (currLine.trim().length() > 0)
48 NmeaMessage message = processLine(currLine);
51 if (message.hasFix()) {
52 message.setSegment(newSegment);
53 // add message to list
54 messages.add(message);
56 // Start a new segment if fix lost
57 newSegment = !message.hasFix();
60 // Read next line, if any
61 currLine = reader.readLine();
64 catch (IOException ioe) {
65 _app.showErrorMessage("error.load.dialogtitle", "error.load.noread");
69 // close file ignoring errors
72 if (reader != null) reader.close();
74 catch (Exception e) {}
76 if (messages.size() > 0)
78 _app.informDataLoaded(getFieldArray(), makeDataArray(messages),
79 Altitude.Format.METRES, inFile.getName());
84 * Process the given NMEA line and return the message
85 * @param inLine line to process
86 * @return message object
88 private static NmeaMessage processLine(String inLine)
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")) {
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)
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
105 // Couldn't parse it, return null
110 * Make an object array from the data list
111 * @param inList list of messages
112 * @return object array for loading
114 private static Object[][] makeDataArray(ArrayList<NmeaMessage> inList)
116 Object[][] result = new Object[inList.size()][];
117 for (int i=0; i<inList.size(); i++) {
118 result[i] = inList.get(i).getStrings();
124 * @see tim.prune.load.xml.XmlHandler#getFieldArray()
126 public Field[] getFieldArray()
128 final Field[] fields = {Field.LATITUDE, Field.LONGITUDE, Field.ALTITUDE,
129 Field.TIMESTAMP, Field.NEW_SEGMENT};