/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package routegui; import java.io.IOException; import java.util.Calendar; import org.jdesktop.swingx.mapviewer.GeoPosition; /** * * @author rick * http://www.codepedia.com/1/The+GPRMC+Sentence */ public class GPRMC { /* 0: The Command Word */ /** * 1: Satellite-Derived Time in UTC, in a compressed form "HHMMSS.XXX," * 9: UTC Date, to be put into calendar format DDMMYY */ public Calendar calendar; /** * 2: Satelite fix status */ public Boolean sateliteFixStatus; /** * 3: latitudeDecimalDegrees HHMM.M * 4: latitudeHemisphere [N|S] * 5: DoubleitudeDecimalDegrees HHHMM.M * 6: DoubleitudeHemisphere [E|W] * converted to Geoposition */ public GeoPosition position; /** * 7: speed in knobs */ public Double speed; /** * 8: bearing, measured as an "azimuth.", where 0 represents north, * 90 represents east, 180 represents south and 270 represents west. */ public Double bearing; /* 10: Empty */ /* 11: Empty */ /* 12: Empty */ /* 13: Checksum field */ private String getChecksum(String strLine) { int checksum = strLine.charAt(1); for (int i = 2; i < strLine.length() - 3; i++ ){ checksum = checksum ^ strLine.charAt(i); } return String.format("%1x", checksum); } GPRMC() { } GPRMC(String strLine) { boolean result = process(strLine); } public boolean process(String strLine) { String[] strSub = strLine.split("[,*]"); /* Checking sentence is valid using checksum */ if (! getChecksum(strLine).equals(strSub[13])) { return false; } /* Set calendar */ calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(strSub[9].substring(0,2))); calendar.set(Calendar.MONTH, Integer.parseInt(strSub[9].substring(2,4))); calendar.set(Calendar.YEAR, Integer.parseInt(strSub[9].substring(4,6)) + 2000); calendar.set(Calendar.HOUR, Integer.parseInt(strSub[1].substring(0,2))); calendar.set(Calendar.MINUTE, Integer.parseInt(strSub[1].substring(2,4))); calendar.set(Calendar.SECOND, Integer.parseInt(strSub[1].substring(4,6))); calendar.set(Calendar.MILLISECOND, Integer.parseInt(strSub[1].substring(7,9))); /* Set fix status */ sateliteFixStatus = strSub[2].equalsIgnoreCase("A"); if (sateliteFixStatus) { /** * Set position, * see: http://en.wikipedia.org/wiki/Geographic_coordinate_conversion * @todo: Milliseconds are ignored in calculation */ Double latitude = Double.parseDouble(strSub[3].substring(0, 2)); latitude += Double.parseDouble(strSub[3].substring(2)) / 60; if (strSub[4].equalsIgnoreCase("S")) { latitude = latitude * -1; } Double longitude = Double.parseDouble(strSub[5].substring(0, 3)); longitude += Double.parseDouble(strSub[5].substring(3)) / 60; if (strSub[6].equalsIgnoreCase("W")) { longitude = longitude * -1; } position = new GeoPosition(latitude, longitude); /* Set speed in knobs */ speed = Double.parseDouble(strSub[7]); /* Set bearing if available */ if (strSub[8].isEmpty()) { bearing = null; } else { bearing = Double.parseDouble(strSub[8]); } } return true; } }