]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/Checker.java
9d84fabacec3f20e4bbb2ecded9170715410d5bf
[GpsPrune.git] / tim / prune / data / Checker.java
1 package tim.prune.data;
2
3 /**
4  * Class to provide checking functions
5  */
6 public abstract class Checker
7 {
8
9         /**
10          * Check if a given track is doubled, so that each point is given twice,
11          * once as waypoint and again as track point
12          * @param inTrack track to check
13          * @return true if track is doubled, false otherwise
14          */
15         public static boolean isDoubledTrack(Track inTrack)
16         {
17                 // Check for empty track
18                 if (inTrack == null || inTrack.getNumPoints() < 2) {return false;}
19                 // Check for non-even number of points
20                 final int numPoints = inTrack.getNumPoints();
21                 if (numPoints % 2 != 0) {return false;}
22                 // Loop through first half of track
23                 final int halfNum = numPoints / 2;
24                 for (int i=0; i<halfNum; i++)
25                 {
26                         DataPoint firstPoint = inTrack.getPoint(i);
27                         DataPoint secondPoint = inTrack.getPoint(i + halfNum);
28                         if (!firstPoint.getLatitude().equals(secondPoint.getLatitude())
29                                 || !firstPoint.getLongitude().equals(secondPoint.getLongitude())) {
30                                 return false;
31                         }
32                 }
33                 // Passed the test, so contents must all be doubled
34                 return true;
35         }
36
37         /**
38          * Find the index of the next segment start after the given index
39          * @param inTrack track object
40          * @param inIndex current index
41          * @return index of next segment start
42          */
43         public static int getNextSegmentStart(Track inTrack, int inIndex)
44         {
45                 int i = inIndex + 1;
46                 DataPoint point = null;
47                 while ((point=inTrack.getPoint(i)) != null && (point.isWaypoint() || !point.getSegmentStart())) {
48                         i++;
49                 }
50                 return Math.min(i, inTrack.getNumPoints()-1);
51         }
52
53         /**
54          * Find the index of the previous segment start before the given index
55          * @param inTrack track object
56          * @param inIndex current index
57          * @return index of previous segment start
58          */
59         public static int getPreviousSegmentStart(Track inTrack, int inIndex)
60         {
61                 int i = inIndex - 1;
62                 DataPoint point = null;
63                 while ((point=inTrack.getPoint(i)) != null && (point.isWaypoint() || !point.getSegmentStart())) {
64                         i--;
65                 }
66                 // Have we gone past the beginning of the track?
67                 i = Math.max(i, 0);
68                 // count forwards past the waypoints if necessary
69                 while ((point=inTrack.getPoint(i)) != null && point.isWaypoint()) {
70                         i++;
71                 }
72                 return i;
73         }
74
75         /**
76          * Find the index of the last track point in the current segment
77          * @param inTrack track object
78          * @param inIndex current index
79          * @return index of next segment end
80          */
81         public static int getNextSegmentEnd(Track inTrack, int inIndex)
82         {
83                 // First, go to start of following segment, or the end of the track
84                 int i = getNextSegmentStart(inTrack, inIndex);
85                 // If it's the next segment, subtract one
86                 DataPoint point = inTrack.getPoint(i);
87                 if (point == null || point.getSegmentStart())
88                 {
89                         i--;
90                 }
91                 // Now we may be on a waypoint, so count back to get the last track point
92                 while ((point=inTrack.getPoint(i)) != null && point.isWaypoint()) {
93                         i--;
94                 }
95                 return Math.min(i, inTrack.getNumPoints()-1);
96         }
97
98
99         /**
100          * @param inTrack track object
101          * @return true if there is at least one waypoint with a timestamp
102          */
103         public static boolean haveWaypointsGotTimestamps(Track inTrack)
104         {
105                 if (inTrack != null)
106                 {
107                         for (int i=0; i<inTrack.getNumPoints(); i++)
108                         {
109                                 DataPoint p = inTrack.getPoint(i);
110                                 if (p != null && p.isWaypoint() && p.hasTimestamp())
111                                 {
112                                         return true;
113                                 }
114                         }
115                 }
116                 return false;
117         }
118 }