1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5 package jme3tools.navigation;
6 
7 /**
8  * This class represents the position of an entity in the world.
9  *
10  * @author Benjamin Jakobus (based on JMarine by Cormac Gebruers and Benjamin Jakobus)
11  * @version 1.0
12  * @since 1.0
13  */
14 public class Position {
15 
16     /* the latitude (+ N/E) */
17     private Coordinate lat;
18 
19     /* the longitude  (-W/S) */
20     private Coordinate lng;
21 
22     /* An optional time to associate with this position - for historical tracking */
23     private String utcTimeStamp;
24 
25     /* Degree position */
26     private double degree;
27 
28     /**
29      * A new position expressed in decimal format
30      * @param dblLat
31      * @param dblLng
32      * @since 1.0
33      */
Position(double dblLat, double dblLng)34     public Position(double dblLat, double dblLng) throws InvalidPositionException {
35         lat = new Coordinate(dblLat, Coordinate.LAT);
36         lng = new Coordinate(dblLng, Coordinate.LNG);
37     }
38 
39     /**
40      * A new position expressed in decimal format and degrees
41      * @param dblLat
42      * @param dblLng
43      * @param degree
44      * @since 1.0
45      */
46 //    public Position(double dblLat, double dblLng, double degree) throws InvalidPositionException {
47 //        lat = new Coordinate(dblLat, Coordinate.LAT);
48 //        lng = new Coordinate(dblLng, Coordinate.LNG);
49 //        this.degree = degree;
50 //    }
51     /**
52      * A new position expressed in DegMin format
53      * @param latDeg
54      * @param latMin
55      * @param lngDeg
56      * @param lngMin
57      * @since 1.0
58      */
Position(int latDeg, float latMin, int latQuad, int lngDeg, float lngMin, int lngQuad)59     public Position(int latDeg, float latMin, int latQuad, int lngDeg,
60             float lngMin, int lngQuad) throws InvalidPositionException {
61         lat = new Coordinate(latDeg, latMin, Coordinate.LAT, latQuad);
62         lng = new Coordinate(lngDeg, lngMin, Coordinate.LNG, lngQuad);
63     }
64 
65     /**
66      * A new position expressed in ALRS format
67      * @param lat
68      * @param lng
69      * @since 1.0
70      */
Position(String lat, String lng)71     public Position(String lat, String lng) throws InvalidPositionException {
72         this.lat = new Coordinate(lat);
73         this.lng = new Coordinate(lng);
74     }
75 
76     /**
77      * A new position expressed in NMEA GPS message format:
78      * 4807.038,N,01131.000,E
79      * @param
80      * @param
81      * @param
82      * @param
83      * @since  12.0
84      */
Position(String latNMEAGPS, String latQuad, String lngNMEAGPS, String lngQuad, String utcTimeStamp)85     public Position(String latNMEAGPS, String latQuad, String lngNMEAGPS, String lngQuad, String utcTimeStamp) {
86         int quad;
87 
88         //LAT
89         if (latQuad.compareTo("N") == 0) {
90             quad = Coordinate.N;
91         } else {
92             quad = Coordinate.S;
93         }
94         try {
95             this.lat = new Coordinate(Integer.valueOf(latNMEAGPS.substring(0, 2)), Float.valueOf(latNMEAGPS.substring(2)), Coordinate.LAT, quad);
96         } catch (InvalidPositionException e) {
97             e.printStackTrace();
98         }
99 
100         //LNG
101         if (lngQuad.compareTo("E") == 0) {
102             quad = Coordinate.E;
103         } else {
104             quad = Coordinate.W;
105         }
106         try {
107             this.lng = new Coordinate(Integer.valueOf(lngNMEAGPS.substring(0, 3)), Float.valueOf(lngNMEAGPS.substring(3)), Coordinate.LNG, quad);
108         } catch (InvalidPositionException e) {
109             e.printStackTrace();
110         }
111 
112         //TIMESTAMP
113         this.associateUTCTime(utcTimeStamp);
114     }
115 
116     /**
117      * Add a reference time for this position - useful for historical tracking
118      * @param data
119      * @since 1.0
120      */
associateUTCTime(String data)121     public void associateUTCTime(String data) {
122         utcTimeStamp = data;
123     }
124 
125     /**
126      * Returns the UTC time stamp
127      * @return str the UTC timestamp
128      * @since 1.0
129      */
utcTimeStamp()130     public String utcTimeStamp() {
131         return utcTimeStamp;
132     }
133 
134     /**
135      * Prints out position using decimal format
136      * @return the position in decimal format
137      */
toStringDec()138     public String toStringDec() {
139         return lat.toStringDec() + " " + lng.toStringDec();
140     }
141 
142     /**
143      * Return the position latitude in decimal format
144      * @return the latitude in decimal format
145      * @since 1.0
146      */
getLatitude()147     public double getLatitude() {
148         return lat.decVal();
149     }
150 
151     /**
152      * Returns the degree of the entity
153      * @return degree
154      * @since 1.0
155      */
156 //    public double getDegree() {
157 //        return degree;
158 //    }
159     /**
160      * Return the position longitude in decimal format
161      * @return the longitude in decimal format
162      * @since 1.0
163      */
getLongitude()164     public double getLongitude() {
165         return lng.decVal();
166     }
167 
168     /**
169      * Prints out position using DegMin format
170      * @return the position in DegMin Format
171      * @since 1.0
172      */
toStringDegMin()173     public String toStringDegMin() {
174         String output = "";
175         output += lat.toStringDegMin();
176         output += "   " + lng.toStringDegMin();
177         return output;
178     }
179 
180     /**
181      * Prints out the position latitude
182      * @return the latitude as a string for display purposes
183      * @since 1.0
184      */
toStringDegMinLat()185     public String toStringDegMinLat() {
186         return lat.toStringDegMin();
187     }
188 
189     /**
190      * Prints out the position longitude
191      * @return the longitude as a string for display purposes
192      * @since 1.0
193      */
toStringDegMinLng()194     public String toStringDegMinLng() {
195         return lng.toStringDegMin();
196     }
197 
198     /**
199      * Prints out the position latitude
200      * @return the latitude as a string for display purposes
201      * @since 1.0
202      */
toStringDecLat()203     public String toStringDecLat() {
204         return lat.toStringDec();
205     }
206 
207     /**
208      * Prints out the position longitude
209      * @return the longitude as a string for display purposes
210      * @since 1.0
211      */
toStringDecLng()212     public String toStringDecLng() {
213         return lng.toStringDec();
214     }
215 
216     //TEST HARNESS - DO NOT DELETE!
main(String[] argsc)217     public static void main(String[] argsc) {
218 
219         //NMEA GPS Position format:
220         Position p = new Position("4807.038", "N", "01131.000", "W", "123519");
221         System.out.println(p.toStringDegMinLat());
222         System.out.println(p.getLatitude());
223         System.out.println(p.getLongitude());
224         System.out.println(p.toStringDegMinLng());
225         System.out.println(p.utcTimeStamp());
226 
227     }//main
228 }
229