]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/DataPoint.java
7acd4f9e364314985d0ea2abb85996901352037b
[GpsPrune.git] / tim / prune / data / DataPoint.java
1 package tim.prune.data;
2
3 import tim.prune.config.Config;
4
5 /**
6  * Class to represent a single data point in the series
7  * including all its fields
8  * Can be either a track point or a waypoint
9  */
10 public class DataPoint
11 {
12         /** Array of Strings holding raw values */
13         private String[] _fieldValues = null;
14         /** list of field definitions */
15         private FieldList _fieldList = null;
16         /** Special fields for coordinates */
17         private Coordinate _latitude = null, _longitude = null;
18         private Altitude _altitude;
19         private Timestamp _timestamp = null;
20         private Photo _photo = null;
21         private String _waypointName = null;
22         private boolean _startOfSegment = false;
23         private boolean _markedForDeletion = false;
24         private int _modifyCount = 0;
25
26         /**
27          * Constructor
28          * @param inValueArray array of String values
29          * @param inFieldList list of fields
30          * @param inAltFormat altitude format
31          */
32         public DataPoint(String[] inValueArray, FieldList inFieldList, Altitude.Format inAltFormat)
33         {
34                 // save data
35                 _fieldValues = inValueArray;
36                 // save list of fields
37                 _fieldList = inFieldList;
38                 // parse fields into objects
39                 parseFields(null, inAltFormat);
40         }
41
42
43         /**
44          * Parse the string values into objects eg Coordinates
45          * @param inField field which has changed, or null for all
46          * @param inAltFormat altitude format
47          */
48         private void parseFields(Field inField, Altitude.Format inAltFormat)
49         {
50                 if (inField == null || inField == Field.LATITUDE) {
51                         _latitude = new Latitude(getFieldValue(Field.LATITUDE));
52                 }
53                 if (inField == null || inField == Field.LONGITUDE) {
54                         _longitude = new Longitude(getFieldValue(Field.LONGITUDE));
55                 }
56                 if (inField == null || inField == Field.ALTITUDE) {
57                         _altitude = new Altitude(getFieldValue(Field.ALTITUDE), inAltFormat);
58                 }
59                 if (inField == null || inField == Field.TIMESTAMP) {
60                         _timestamp = new Timestamp(getFieldValue(Field.TIMESTAMP));
61                 }
62                 if (inField == null || inField == Field.WAYPT_NAME) {
63                         _waypointName = getFieldValue(Field.WAYPT_NAME);
64                 }
65                 if (inField == null || inField == Field.NEW_SEGMENT)
66                 {
67                         String segmentStr = getFieldValue(Field.NEW_SEGMENT);
68                         if (segmentStr != null) {segmentStr = segmentStr.trim();}
69                         _startOfSegment = (segmentStr != null && (segmentStr.equals("1") || segmentStr.toUpperCase().equals("Y")));
70                 }
71         }
72
73
74         /**
75          * Constructor for additional points (eg interpolated, photos)
76          * @param inLatitude latitude
77          * @param inLongitude longitude
78          * @param inAltitude altitude
79          */
80         public DataPoint(Coordinate inLatitude, Coordinate inLongitude, Altitude inAltitude)
81         {
82                 // Only these three fields are available
83                 _fieldValues = new String[3];
84                 Field[] fields = {Field.LATITUDE, Field.LONGITUDE, Field.ALTITUDE};
85                 _fieldList = new FieldList(fields);
86                 _latitude = inLatitude;
87                 _fieldValues[0] = inLatitude.output(Coordinate.FORMAT_NONE);
88                 _longitude = inLongitude;
89                 _fieldValues[1] = inLongitude.output(Coordinate.FORMAT_NONE);
90                 if (inAltitude == null) {
91                         _altitude = Altitude.NONE;
92                 }
93                 else {
94                         _altitude = inAltitude;
95                         _fieldValues[2] = "" + inAltitude.getValue();
96                 }
97                 _timestamp = new Timestamp(null);
98         }
99
100
101         /**
102          * Get the value for the given field
103          * @param inField field to interrogate
104          * @return value of field
105          */
106         public String getFieldValue(Field inField)
107         {
108                 return getFieldValue(_fieldList.getFieldIndex(inField));
109         }
110
111
112         /**
113          * Get the value at the given index
114          * @param inIndex index number starting at zero
115          * @return field value, or null if not found
116          */
117         public String getFieldValue(int inIndex)
118         {
119                 if (_fieldValues == null || inIndex < 0 || inIndex >= _fieldValues.length)
120                         return null;
121                 return _fieldValues[inIndex];
122         }
123
124
125         /**
126          * Set (or edit) the specified field value
127          * @param inField Field to set
128          * @param inValue value to set
129          * @param inUndo true if undo operation, false otherwise
130          */
131         public void setFieldValue(Field inField, String inValue, boolean inUndo)
132         {
133                 // See if this data point already has this field
134                 int fieldIndex = _fieldList.getFieldIndex(inField);
135                 // Add to field list if necessary
136                 if (fieldIndex < 0)
137                 {
138                         // If value is empty & field doesn't exist then do nothing
139                         if (inValue == null || inValue.equals(""))
140                         {
141                                 return;
142                         }
143                         // value isn't empty so extend field list
144                         fieldIndex = _fieldList.extendList(inField);
145                 }
146                 // Extend array of field values if necessary
147                 if (fieldIndex >= _fieldValues.length)
148                 {
149                         resizeValueArray(fieldIndex);
150                 }
151                 // Set field value in array
152                 _fieldValues[fieldIndex] = inValue;
153                 // Increment edit count on all field edits except segment
154                 if (inField != Field.NEW_SEGMENT) {
155                         if (!inUndo) {
156                                 _modifyCount++;
157                         }
158                         else {
159                                 _modifyCount--;
160                         }
161                 }
162                 // Change Coordinate, Altitude, Name or Timestamp fields after edit
163                 if (_altitude != null && _altitude.getFormat() != Altitude.Format.NO_FORMAT) {
164                         // Altitude already present so reuse format
165                         parseFields(inField, _altitude.getFormat());
166                 }
167                 else {
168                         // use default altitude format from config
169                         parseFields(inField, Config.getConfigBoolean(Config.KEY_METRIC_UNITS)?Altitude.Format.METRES:Altitude.Format.FEET);
170                 }
171         }
172
173         /**
174          * @return field list for this point
175          */
176         public FieldList getFieldList()
177         {
178                 return _fieldList;
179         }
180
181         /** @param inFlag true for start of track segment */
182         public void setSegmentStart(boolean inFlag)
183         {
184                 setFieldValue(Field.NEW_SEGMENT, inFlag?"1":null, false);
185         }
186
187         /**
188          * Mark the point for deletion
189          * @param inFlag true to delete, false to keep
190          */
191         public void setMarkedForDeletion(boolean inFlag) {
192                 _markedForDeletion = inFlag;
193         }
194
195         /** @return latitude */
196         public Coordinate getLatitude()
197         {
198                 return _latitude;
199         }
200         /** @return longitude */
201         public Coordinate getLongitude()
202         {
203                 return _longitude;
204         }
205         /** @return true if point has altitude */
206         public boolean hasAltitude()
207         {
208                 return _altitude.isValid();
209         }
210         /** @return altitude */
211         public Altitude getAltitude()
212         {
213                 return _altitude;
214         }
215         /** @return true if point has timestamp */
216         public boolean hasTimestamp()
217         {
218                 return _timestamp.isValid();
219         }
220         /** @return timestamp */
221         public Timestamp getTimestamp()
222         {
223                 return _timestamp;
224         }
225         /** @return waypoint name, if any */
226         public String getWaypointName()
227         {
228                 return _waypointName;
229         }
230
231         /** @return true if start of new track segment */
232         public boolean getSegmentStart()
233         {
234                 return _startOfSegment;
235         }
236
237         /** @return true if point marked for deletion */
238         public boolean getDeleteFlag()
239         {
240                 return _markedForDeletion;
241         }
242
243         /**
244          * @return true if point has a waypoint name
245          */
246         public boolean isWaypoint()
247         {
248                 return (_waypointName != null && !_waypointName.equals(""));
249         }
250
251         /**
252          * @return true if point has been modified since loading
253          */
254         public boolean isModified()
255         {
256                 return _modifyCount > 0;
257         }
258
259         /**
260          * Compare two DataPoint objects to see if they are duplicates
261          * @param inOther other object to compare
262          * @return true if the points are equivalent
263          */
264         public boolean isDuplicate(DataPoint inOther)
265         {
266                 if (inOther == null) return false;
267                 if (_longitude == null || _latitude == null
268                         || inOther._longitude == null || inOther._latitude == null)
269                 {
270                         return false;
271                 }
272                 // Make sure photo points aren't specified as duplicates
273                 if (_photo != null) return false;
274                 // Compare latitude and longitude
275                 if (!_longitude.equals(inOther._longitude) || !_latitude.equals(inOther._latitude))
276                 {
277                         return false;
278                 }
279                 // Note that conversion from decimal to dms can make non-identical points into duplicates
280                 // Compare waypoint name (if any)
281                 if (!isWaypoint())
282                 {
283                         return !inOther.isWaypoint();
284                 }
285                 return (inOther._waypointName != null && inOther._waypointName.equals(_waypointName));
286         }
287
288
289         /**
290          * Set the photo for this data point
291          * @param inPhoto Photo object
292          */
293         public void setPhoto(Photo inPhoto)
294         {
295                 _photo = inPhoto;
296         }
297
298
299         /**
300          * @return associated Photo object
301          */
302         public Photo getPhoto()
303         {
304                 return _photo;
305         }
306
307
308         /**
309          * @return true if the point is valid
310          */
311         public boolean isValid()
312         {
313                 return _latitude.isValid() && _longitude.isValid();
314         }
315
316         /**
317          * Interpolate a set of points between this one and the given one
318          * @param inEndPoint end point of interpolation
319          * @param inNumPoints number of points to generate
320          * @return the DataPoint array
321          */
322         public DataPoint[] interpolate(DataPoint inEndPoint, int inNumPoints)
323         {
324                 DataPoint[] range = new DataPoint[inNumPoints];
325                 // Loop over points
326                 for (int i=0; i<inNumPoints; i++)
327                 {
328                         Coordinate latitude = Coordinate.interpolate(_latitude, inEndPoint.getLatitude(), i, inNumPoints);
329                         Coordinate longitude = Coordinate.interpolate(_longitude, inEndPoint.getLongitude(), i, inNumPoints);
330                         Altitude altitude = Altitude.interpolate(_altitude, inEndPoint.getAltitude(), i, inNumPoints);
331                         range[i] = new DataPoint(latitude, longitude, altitude);
332                 }
333                 return range;
334         }
335
336         /**
337          * Interpolate between the two given points
338          * @param inStartPoint start point
339          * @param inEndPoint end point
340          * @param inFrac fractional distance from first point (0.0 to 1.0)
341          * @return new DataPoint object between two given ones
342          */
343         public static DataPoint interpolate(DataPoint inStartPoint, DataPoint inEndPoint, double inFrac)
344         {
345                 if (inStartPoint == null || inEndPoint == null) {return null;}
346                 return new DataPoint(
347                         Coordinate.interpolate(inStartPoint.getLatitude(), inEndPoint.getLatitude(), inFrac),
348                         Coordinate.interpolate(inStartPoint.getLongitude(), inEndPoint.getLongitude(), inFrac),
349                         Altitude.interpolate(inStartPoint.getAltitude(), inEndPoint.getAltitude(), inFrac)
350                 );
351         }
352
353         /**
354          * Calculate the number of radians between two points (for distance calculation)
355          * @param inPoint1 first point
356          * @param inPoint2 second point
357          * @return angular distance between points in radians
358          */
359         public static double calculateRadiansBetween(DataPoint inPoint1, DataPoint inPoint2)
360         {
361                 if (inPoint1 == null || inPoint2 == null)
362                         return 0.0;
363                 final double TO_RADIANS = Math.PI / 180.0;
364                 // Get lat and long from points
365                 double lat1 = inPoint1.getLatitude().getDouble() * TO_RADIANS;
366                 double lat2 = inPoint2.getLatitude().getDouble() * TO_RADIANS;
367                 double lon1 = inPoint1.getLongitude().getDouble() * TO_RADIANS;
368                 double lon2 = inPoint2.getLongitude().getDouble() * TO_RADIANS;
369                 // Formula given by Wikipedia:Great-circle_distance as follows:
370                 // angle = 2 arcsin( sqrt( (sin ((lat2-lat1)/2))^^2 + cos(lat1)cos(lat2)(sin((lon2-lon1)/2))^^2))
371                 double firstSine = Math.sin((lat2-lat1) / 2.0);
372                 double secondSine = Math.sin((lon2-lon1) / 2.0);
373                 double term2 = Math.cos(lat1) * Math.cos(lat2) * secondSine * secondSine;
374                 double answer = 2 * Math.asin(Math.sqrt(firstSine*firstSine + term2));
375                 // phew
376                 return answer;
377         }
378
379
380         /**
381          * Resize the value array
382          * @param inNewIndex new index to allow
383          */
384         private void resizeValueArray(int inNewIndex)
385         {
386                 int newSize = inNewIndex + 1;
387                 if (newSize > _fieldValues.length)
388                 {
389                         String[] newArray = new String[newSize];
390                         System.arraycopy(_fieldValues, 0, newArray, 0, _fieldValues.length);
391                         _fieldValues = newArray;
392                 }
393         }
394
395
396         /**
397          * @return a clone object with copied data
398          */
399         public DataPoint clonePoint()
400         {
401                 // Copy all values (note that photo not copied)
402                 String[] valuesCopy = new String[_fieldValues.length];
403                 System.arraycopy(_fieldValues, 0, valuesCopy, 0, _fieldValues.length);
404                 // Make new object to hold cloned data
405                 DataPoint point = new DataPoint(valuesCopy, _fieldList, _altitude.getFormat());
406                 return point;
407         }
408
409
410         /**
411          * Get string for debug
412          * @see java.lang.Object#toString()
413          */
414         public String toString()
415         {
416                 return "[Lat=" + getLatitude().toString() + ", Lon=" + getLongitude().toString() + "]";
417         }
418 }