aboutsummaryrefslogtreecommitdiff
path: root/engine/src/desktop/jme3tools/navigation/Position.java
blob: b55e852e21c117c69070d4836702381fc7c06b27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jme3tools.navigation;

/**
 * This class represents the position of an entity in the world.
 * 
 * @author Benjamin Jakobus (based on JMarine by Cormac Gebruers and Benjamin Jakobus)
 * @version 1.0
 * @since 1.0
 */
public class Position {

    /* the latitude (+ N/E) */
    private Coordinate lat;

    /* the longitude  (-W/S) */
    private Coordinate lng;

    /* An optional time to associate with this position - for historical tracking */
    private String utcTimeStamp;

    /* Degree position */
    private double degree;

    /**
     * A new position expressed in decimal format
     * @param dblLat
     * @param dblLng
     * @since 1.0
     */
    public Position(double dblLat, double dblLng) throws InvalidPositionException {
        lat = new Coordinate(dblLat, Coordinate.LAT);
        lng = new Coordinate(dblLng, Coordinate.LNG);
    }

    /**
     * A new position expressed in decimal format and degrees
     * @param dblLat
     * @param dblLng
     * @param degree
     * @since 1.0
     */
//    public Position(double dblLat, double dblLng, double degree) throws InvalidPositionException {
//        lat = new Coordinate(dblLat, Coordinate.LAT);
//        lng = new Coordinate(dblLng, Coordinate.LNG);
//        this.degree = degree;
//    }
    /**
     * A new position expressed in DegMin format
     * @param latDeg
     * @param latMin
     * @param lngDeg
     * @param lngMin
     * @since 1.0
     */
    public Position(int latDeg, float latMin, int latQuad, int lngDeg,
            float lngMin, int lngQuad) throws InvalidPositionException {
        lat = new Coordinate(latDeg, latMin, Coordinate.LAT, latQuad);
        lng = new Coordinate(lngDeg, lngMin, Coordinate.LNG, lngQuad);
    }

    /**
     * A new position expressed in ALRS format
     * @param lat
     * @param lng
     * @since 1.0
     */
    public Position(String lat, String lng) throws InvalidPositionException {
        this.lat = new Coordinate(lat);
        this.lng = new Coordinate(lng);
    }

    /**
     * A new position expressed in NMEA GPS message format:
     * 4807.038,N,01131.000,E
     * @param
     * @param
     * @param
     * @param
     * @since  12.0
     */
    public Position(String latNMEAGPS, String latQuad, String lngNMEAGPS, String lngQuad, String utcTimeStamp) {
        int quad;

        //LAT
        if (latQuad.compareTo("N") == 0) {
            quad = Coordinate.N;
        } else {
            quad = Coordinate.S;
        }
        try {
            this.lat = new Coordinate(Integer.valueOf(latNMEAGPS.substring(0, 2)), Float.valueOf(latNMEAGPS.substring(2)), Coordinate.LAT, quad);
        } catch (InvalidPositionException e) {
            e.printStackTrace();
        }

        //LNG
        if (lngQuad.compareTo("E") == 0) {
            quad = Coordinate.E;
        } else {
            quad = Coordinate.W;
        }
        try {
            this.lng = new Coordinate(Integer.valueOf(lngNMEAGPS.substring(0, 3)), Float.valueOf(lngNMEAGPS.substring(3)), Coordinate.LNG, quad);
        } catch (InvalidPositionException e) {
            e.printStackTrace();
        }

        //TIMESTAMP
        this.associateUTCTime(utcTimeStamp);
    }

    /**
     * Add a reference time for this position - useful for historical tracking
     * @param data
     * @since 1.0
     */
    public void associateUTCTime(String data) {
        utcTimeStamp = data;
    }

    /**
     * Returns the UTC time stamp
     * @return str the UTC timestamp
     * @since 1.0
     */
    public String utcTimeStamp() {
        return utcTimeStamp;
    }

    /**
     * Prints out position using decimal format
     * @return the position in decimal format
     */
    public String toStringDec() {
        return lat.toStringDec() + " " + lng.toStringDec();
    }

    /**
     * Return the position latitude in decimal format
     * @return the latitude in decimal format
     * @since 1.0
     */
    public double getLatitude() {
        return lat.decVal();
    }

    /**
     * Returns the degree of the entity
     * @return degree
     * @since 1.0
     */
//    public double getDegree() {
//        return degree;
//    }
    /**
     * Return the position longitude in decimal format
     * @return the longitude in decimal format
     * @since 1.0
     */
    public double getLongitude() {
        return lng.decVal();
    }

    /**
     * Prints out position using DegMin format
     * @return the position in DegMin Format
     * @since 1.0
     */
    public String toStringDegMin() {
        String output = "";
        output += lat.toStringDegMin();
        output += "   " + lng.toStringDegMin();
        return output;
    }

    /**
     * Prints out the position latitude
     * @return the latitude as a string for display purposes
     * @since 1.0
     */
    public String toStringDegMinLat() {
        return lat.toStringDegMin();
    }

    /**
     * Prints out the position longitude
     * @return the longitude as a string for display purposes
     * @since 1.0
     */
    public String toStringDegMinLng() {
        return lng.toStringDegMin();
    }

    /**
     * Prints out the position latitude
     * @return the latitude as a string for display purposes
     * @since 1.0
     */
    public String toStringDecLat() {
        return lat.toStringDec();
    }

    /**
     * Prints out the position longitude
     * @return the longitude as a string for display purposes
     * @since 1.0
     */
    public String toStringDecLng() {
        return lng.toStringDec();
    }

    //TEST HARNESS - DO NOT DELETE!
    public static void main(String[] argsc) {

        //NMEA GPS Position format:
        Position p = new Position("4807.038", "N", "01131.000", "W", "123519");
        System.out.println(p.toStringDegMinLat());
        System.out.println(p.getLatitude());
        System.out.println(p.getLongitude());
        System.out.println(p.toStringDegMinLng());
        System.out.println(p.utcTimeStamp());

    }//main
}