-
-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathTrack.java
More file actions
331 lines (264 loc) · 8.79 KB
/
Copy pathTrack.java
File metadata and controls
331 lines (264 loc) · 8.79 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package me.guillaumin.android.osmtracker.db.model;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import me.guillaumin.android.osmtracker.R;
import me.guillaumin.android.osmtracker.db.TrackContentProvider;
import me.guillaumin.android.osmtracker.db.TrackContentProvider.Schema;
import me.guillaumin.android.osmtracker.util.DistanceUtil;
import android.content.ContentResolver;
import android.database.Cursor;
/**
* Represents a Track
* @author Nicolas Guillaumin
*
*/
public class Track {
private static final DateFormat DATE_FORMAT = DateFormat.getDateTimeInstance();
public enum OSMVisibility {
Private(0, R.string.osm_visibility_private),
Public(1, R.string.osm_visibility_public),
Trackable(2, R.string.osm_visibility_trackable),
Identifiable(3, R.string.osm_visibility_identifiable);
public final int position;
public final int resId;
private OSMVisibility(int position, int resId) {
this.position = position;
this.resId = resId;
}
public static OSMVisibility fromPosition(int position) {
for (OSMVisibility v: values()) {
if (v.position == position) {
return v;
}
}
throw new IllegalArgumentException();
}
}
private String name;
private String description;
private OSMVisibility visibility;
private List<String> tags = new ArrayList<String>();
private int tpCount, wpCount;
private long trackDate;
private long trackId;
private Long startDate=null, endDate=null;
private Float startLat=null, startLong=null, endLat=null, endLong=null;
private Float distance=0.0f;
private Float elevationMax=0.0f,elevationMin=0.0f;
private Float speedMax=0.0f, speedMin=0.0f;
private boolean extraInformationRead = false;
private boolean extraInformationRead_stats = false;
private ContentResolver cr;
/**
* build a track object with the given cursor
*
* @param trackId id of the track that will be built
* @param tc cursor that is used to build the track
* @param cr the content resolver to use
* @param withExtraInformation if additional informations (startDate, endDate, first and last track point will be loaded from the database
* @return Track
*/
public static Track build(final long trackId, Cursor tc, ContentResolver cr, boolean withExtraInformation) {
Track out = new Track();
out.trackId = trackId;
out.cr = cr;
out.trackDate = tc.getLong(tc.getColumnIndex(Schema.COL_START_DATE));
out.name = tc.getString(tc.getColumnIndex(Schema.COL_NAME));
out.description = tc.getString(tc.getColumnIndex(Schema.COL_DESCRIPTION));
String tags = tc.getString(tc.getColumnIndex(Schema.COL_TAGS));
if (tags != null && ! "".equals(tags)) {
out.tags.addAll(Arrays.asList(tags.split(",")));
}
out.visibility = OSMVisibility.valueOf(tc.getString(tc.getColumnIndex(Schema.COL_OSM_VISIBILITY)));
out.tpCount = tc.getInt(tc.getColumnIndex(Schema.COL_TRACKPOINT_COUNT));
out.wpCount = tc.getInt(tc.getColumnIndex(Schema.COL_WAYPOINT_COUNT));
if(withExtraInformation){
out.readExtraInformation();
}
return out;
}
private void readExtraInformation(){
if(!extraInformationRead){
Cursor startCursor = cr.query(TrackContentProvider.trackStartUri(trackId), null, null, null, null);
if(startCursor.moveToFirst()){
startDate = startCursor.getLong(startCursor.getColumnIndex(Schema.COL_TIMESTAMP));
startLat = startCursor.getFloat(startCursor.getColumnIndex(Schema.COL_LATITUDE));
startLong = startCursor.getFloat(startCursor.getColumnIndex(Schema.COL_LONGITUDE));
}
startCursor.close();
Cursor endCursor = cr.query(TrackContentProvider.trackEndUri(trackId), null, null, null, null);
if(endCursor.moveToFirst()){
endDate = endCursor.getLong(endCursor.getColumnIndex(Schema.COL_TIMESTAMP));
endLat = endCursor.getFloat(endCursor.getColumnIndex(Schema.COL_LATITUDE));
endLong = endCursor.getFloat(endCursor.getColumnIndex(Schema.COL_LONGITUDE));
}
endCursor.close();
extraInformationRead = true;
}
}
/**
*
* @todo: allow to exclude zero speed values
*/
private void readExtraInformation_stats() {
if(!extraInformationRead_stats){
Cursor cursor = cr.query(TrackContentProvider.trackPointsUri(trackId), null, null, null, null);
Float latitudeCurrent, longitudeCurrent, latitudePrev, longitudePrev;
Float elevationCurr, speedCurr;
boolean avoidZeroSpeed = true;
if(cursor != null && cursor.moveToFirst()) {
// Initialize the min/max values
elevationMin = elevationMax = cursor.getFloat(cursor.getColumnIndex(Schema.COL_ELEVATION));
speedMax = cursor.getFloat(cursor.getColumnIndex(Schema.COL_SPEED));
speedMin = (avoidZeroSpeed && speedMax == 0) ? Float.MAX_VALUE : speedMax;
latitudePrev = cursor.getFloat(cursor.getColumnIndex(Schema.COL_LATITUDE));
longitudePrev = cursor.getFloat(cursor.getColumnIndex(Schema.COL_LONGITUDE));
// Iterate over all points
while (cursor.moveToNext()) {
latitudeCurrent = cursor.getFloat(cursor.getColumnIndex(Schema.COL_LATITUDE));
longitudeCurrent = cursor.getFloat(cursor.getColumnIndex(Schema.COL_LONGITUDE));
distance += DistanceUtil.getDistance(latitudePrev, longitudePrev, latitudeCurrent, longitudeCurrent);
latitudePrev = latitudeCurrent;
longitudePrev = longitudeCurrent;
// Compute the Elevation
elevationCurr = cursor.getFloat(cursor.getColumnIndex(Schema.COL_ELEVATION));
elevationMin = Math.min(elevationMin, elevationCurr);
elevationMax = Math.max(elevationMax, elevationCurr);
// Compute the Speed
speedCurr = cursor.getFloat(cursor.getColumnIndex(Schema.COL_SPEED));
if (!avoidZeroSpeed || speedCurr > 0) {
speedMin = Math.min(speedMin, speedCurr);
speedMax = Math.max(speedMax, speedCurr);
}
}
cursor.close();
extraInformationRead_stats = true;
}
}
}
public void setName(String name) {
this.name = name;
}
public void setDescription(String description) {
this.description = description;
}
public void setTpCount(int tpCount) {
this.tpCount = tpCount;
}
public void setWpCount(int wpCount) {
this.wpCount = wpCount;
}
public void setTracktDate(long tracktDate) {
this.trackDate = tracktDate;
}
public void setEndDate(long endDate) {
this.endDate = endDate;
}
public void setStartLat(float startLat) {
this.startLat = startLat;
}
public void setTrackDate(long trackDate) {
this.trackDate = trackDate;
}
public void setStartDate(long startDate) {
this.startDate = startDate;
}
public void setStartLong(float startLong) {
this.startLong = startLong;
}
public void setEndLat(float endLat) {
this.endLat = endLat;
}
public void setEndLong(float endLong) {
this.endLong = endLong;
}
public Integer getWpCount() {
return wpCount;
}
public Integer getTpCount() {
return tpCount;
}
public Float getDistance() {
readExtraInformation_stats();
return distance;
}
public Float getElevationMin() {
readExtraInformation_stats();
return elevationMin;
}
public Float getElevationMax() {
readExtraInformation_stats();
return elevationMax;
}
public Float getSpeedMax() {
readExtraInformation_stats();
return speedMax;
}
public Float getSpeedMin() {
readExtraInformation_stats();
// Avoid returning inconsistent min-speed if "avoidZeroSpeed" is set to true
return (speedMin > speedMax) ? 0.0f : speedMin;
}
public String getName() {
if (name != null && name.length() > 0) {
return name;
} else {
// Use start date as name
return DATE_FORMAT.format(new Date(trackDate));
}
}
public String getDescription() {
return description;
}
public List<String> getTags() {
return tags;
}
public OSMVisibility getVisibility() {
return visibility;
}
public String getCommaSeparatedTags() {
StringBuilder sb = new StringBuilder();
for (int i=0; i<tags.size(); i++) {
sb.append(tags.get(i));
if (i+1 < tags.size()) {
sb.append(",");
}
}
return sb.toString();
}
public String getStartDateAsString() {
readExtraInformation();
if (startDate != null) {
return DATE_FORMAT.format(new Date(startDate));
} else {
return "";
}
}
public String getEndDateAsString() {
readExtraInformation();
if (endDate != null) {
return DATE_FORMAT.format(new Date(endDate));
} else {
return "";
}
}
public Float getStartLat() {
readExtraInformation();
return startLat;
}
public Float getStartLong() {
readExtraInformation();
return startLong;
}
public Float getEndLat() {
readExtraInformation();
return endLat;
}
public Float getEndLong() {
readExtraInformation();
return endLong;
}
}