]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/SourceInfo.java
d71280db8a5e45c28d594b0306b3b82c10797b16
[GpsPrune.git] / tim / prune / data / SourceInfo.java
1 package tim.prune.data;
2
3 import java.io.File;
4
5 /**
6  * Class to hold the source of the point data, including the original file
7  * and file type, and references to each of the point objects
8  */
9 public class SourceInfo
10 {
11         /** File type of source file */
12         public enum FILE_TYPE {TEXT, GPX, KML, NMEA, GPSBABEL, GPSIES, JSON};
13
14         /** Source file */
15         private File _sourceFile = null;
16         /** Name of source */
17         private String _sourceName = null;
18         /** File type */
19         private FILE_TYPE _fileType = null;
20         /** File title, if any */
21         private String _fileTitle = null;
22
23         /** Array of datapoints */
24         private DataPoint[] _points = null;
25         /** Number of points */
26         private int _numPoints = 0;
27         /** Array of point indices (if necessary) */
28         private int[] _pointIndices = null;
29
30
31         /**
32          * Constructor
33          * @param inFile source file
34          * @param inType type of file
35          */
36         public SourceInfo(File inFile, FILE_TYPE inType)
37         {
38                 _sourceFile = inFile;
39                 _sourceName = inFile.getName();
40                 _fileType = inType;
41         }
42
43         /**
44          * Constructor
45          * @param inName name of source (without file)
46          * @param inType type of file
47          */
48         public SourceInfo(String inName, FILE_TYPE inType)
49         {
50                 _sourceFile = null;
51                 _sourceName = inName;
52                 _fileType = inType;
53         }
54
55         /**
56          * @param inTitle title of file, eg from <name> tag in gpx
57          */
58         public void setFileTitle(String inTitle)
59         {
60                 _fileTitle = inTitle;
61         }
62
63         /**
64          * @return source file
65          */
66         public File getFile()
67         {
68                 return _sourceFile;
69         }
70
71         /**
72          * @return source name
73          */
74         public String getName()
75         {
76                 return _sourceName;
77         }
78
79         /**
80          * @return file type of source
81          */
82         public FILE_TYPE getFileType()
83         {
84                 return _fileType;
85         }
86
87         /**
88          * @return title of file
89          */
90         public String getFileTitle()
91         {
92                 return _fileTitle;
93         }
94
95         /**
96          * @return number of points from this source
97          */
98         public int getNumPoints()
99         {
100                 return _numPoints;
101         }
102
103         /**
104          * Set the indices of the points selected out of a loaded track
105          * @param inSelectedFlags array of booleans showing whether each point in the original data was loaded or not
106          */
107         public void setPointIndices(boolean[] inSelectedFlags)
108         {
109                 _numPoints = inSelectedFlags.length;
110                 _pointIndices = new int[_numPoints];
111                 int p=0;
112                 for (int i=0; i<_numPoints; i++) {
113                         if (inSelectedFlags[i]) {_pointIndices[p++] = i;}
114                 }
115                 // Now the point indices array holds the index of each of the selected points
116         }
117
118         /**
119          * Take the points from the given track and store
120          * @param inTrack track object containing points
121          * @param inNumPoints number of points loaded
122          */
123         public void populatePointObjects(Track inTrack, int inNumPoints)
124         {
125                 if (_numPoints == 0) {_numPoints = inNumPoints;}
126                 if (inNumPoints > 0)
127                 {
128                         _points = new DataPoint[inNumPoints];
129                         int trackLen = inTrack.getNumPoints();
130                         System.arraycopy(inTrack.cloneContents(), trackLen-inNumPoints, _points, 0, inNumPoints);
131                         // Note data copied twice here but still more efficient than looping
132                 }
133         }
134
135         /**
136          * Look for the given point in the array
137          * @param inPoint point to look for
138          * @return index, or -1 if not found
139          */
140         public int getIndex(DataPoint inPoint)
141         {
142                 int idx = -1;
143                 for (int i=0; i<_points.length; i++)
144                 {
145                         if (_points[i] == inPoint) {
146                                 idx = i;
147                                 break;
148                         }
149                 }
150                 if (idx == -1) {return idx;}             // point not found
151                 if (_pointIndices == null) {return idx;} // All points loaded
152                 return _pointIndices[idx]; // use point index mapping
153         }
154 }