Improve GPX tracks. Write accuracy to GPX file

This commit is contained in:
Victor Shcherb 2011-08-28 13:23:39 +02:00
parent d4a3cc7fa2
commit e626425209
4 changed files with 84 additions and 42 deletions

View file

@ -34,24 +34,19 @@ public class GPXUtilities {
private final static NumberFormat latLonFormat = new DecimalFormat("0.00#####", new DecimalFormatSymbols(Locale.US));
public static class TrkPt {
public double lat;
public double lon;
public double ele;
public double speed;
public long time;
}
public static class WptPt {
public double lat;
public double lon;
public String name;
public String name = null;
// by default
public long time = 0;
public double ele = Double.NaN;
public double speed = 0;
public double hdop = Double.NaN;
}
public static class TrkSegment {
public List<TrkPt> points = new ArrayList<TrkPt>();
public List<WptPt> points = new ArrayList<WptPt>();
}
public static class Track {
@ -85,23 +80,9 @@ public class GPXUtilities {
serializer.startTag(null, "trk"); //$NON-NLS-1$
for (TrkSegment segment : track.segments) {
serializer.startTag(null, "trkseg"); //$NON-NLS-1$
for (TrkPt p : segment.points) {
for (WptPt p : segment.points) {
serializer.startTag(null, "trkpt"); //$NON-NLS-1$
serializer.attribute(null, "lat", latLonFormat.format(p.lat)); //$NON-NLS-1$ //$NON-NLS-2$
serializer.attribute(null, "lon", latLonFormat.format(p.lon)); //$NON-NLS-1$ //$NON-NLS-2$
serializer.startTag(null, "ele"); //$NON-NLS-1$
serializer.text(p.ele + ""); //$NON-NLS-1$
serializer.endTag(null, "ele"); //$NON-NLS-1$
serializer.startTag(null, "time"); //$NON-NLS-1$
serializer.text(format.format(new Date(p.time)));
serializer.endTag(null, "time"); //$NON-NLS-1$
if (p.speed > 0) {
serializer.startTag(null, "extensions");
serializer.startTag(null, "speed"); //$NON-NLS-1$
serializer.text(p.speed + ""); //$NON-NLS-1$
serializer.endTag(null, "speed"); //$NON-NLS-1$
serializer.endTag(null, "extensions");
}
writeWpt(format, serializer, p);
serializer.endTag(null, "trkpt"); //$NON-NLS-1$
}
@ -137,6 +118,39 @@ public class GPXUtilities {
}
return null;
}
private static void writeWpt(SimpleDateFormat format, XmlSerializer serializer, WptPt p) throws IOException {
serializer.attribute(null, "lat", latLonFormat.format(p.lat)); //$NON-NLS-1$ //$NON-NLS-2$
serializer.attribute(null, "lon", latLonFormat.format(p.lon)); //$NON-NLS-1$ //$NON-NLS-2$
if(!Double.isNaN(p.ele)){
serializer.startTag(null, "ele"); //$NON-NLS-1$
serializer.text(p.ele + ""); //$NON-NLS-1$
serializer.endTag(null, "ele"); //$NON-NLS-1$
}
if(p.name != null){
serializer.startTag(null, "name"); //$NON-NLS-1$
serializer.text(p.name);
serializer.endTag(null, "name"); //$NON-NLS-1$
}
if(!Double.isNaN(p.hdop)){
serializer.startTag(null, "hdop"); //$NON-NLS-1$
serializer.text(p.hdop +"");
serializer.endTag(null, "hdop"); //$NON-NLS-1$
}
if(p.time != 0){
serializer.startTag(null, "time"); //$NON-NLS-1$
serializer.text(format.format(new Date(p.time)));
serializer.endTag(null, "time"); //$NON-NLS-1$
}
if (p.speed > 0) {
serializer.startTag(null, "extensions");
serializer.startTag(null, "speed"); //$NON-NLS-1$
serializer.text(p.speed + ""); //$NON-NLS-1$
serializer.endTag(null, "speed"); //$NON-NLS-1$
serializer.endTag(null, "extensions");
}
}
public static class GPXFileResult {
@ -201,6 +215,13 @@ public class GPXUtilities {
} catch (ParseException e) {
}
}
} else if (current != null && parser.getName().equals("hdop")) { //$NON-NLS-1$
if (parser.next() == XmlPullParser.TEXT) {
try {
current.setAccuracy(Float.parseFloat(parser.getText()));
} catch (NumberFormatException e) {
}
}
} else if (current != null && parser.getName().equals("ele")) { //$NON-NLS-1$
if (parser.next() == XmlPullParser.TEXT) {
try {
@ -223,11 +244,7 @@ public class GPXUtilities {
parser.getName().equals("trkpt") || (!cloudMade && parser.getName().equals("rtept"))) { //$NON-NLS-1$ //$NON-NLS-2$
if (current != null) {
if (parser.getName().equals("wpt") && !cloudMade) { //$NON-NLS-1$
WptPt pt = new WptPt();
pt.lat = current.getLatitude();
pt.lon = current.getLongitude();
pt.name = currentName;
res.wayPoints.add(pt);
res.wayPoints.add(convertLocationToWayPoint(current, currentName));
} else {
if (res.locations.isEmpty()) {
res.locations.add(new ArrayList<Location>());
@ -248,5 +265,23 @@ public class GPXUtilities {
return res;
}
private static WptPt convertLocationToWayPoint(Location current, String name){
WptPt pt = new WptPt();
pt.lat = current.getLatitude();
pt.lon = current.getLongitude();
if(current.hasAltitude()) {
pt.ele = current.getAltitude();
}
if(current.hasSpeed()) {
pt.speed = current.getSpeed();
}
if(current.hasAccuracy()) {
pt.hdop = current.getAccuracy();
}
pt.time = current.getTime();
pt.name = name;
return pt;
}
}

View file

@ -163,8 +163,8 @@ public class NavigationService extends Service implements LocationListener {
locationManager.removeUpdates(this);
getLock(this).release();
}
savingTrackHelper.insertData(location.getLatitude(), location.getLongitude(), location.getAltitude(),
location.getSpeed(), location.getTime(), settings);
savingTrackHelper.insertData(location.getLatitude(), location.getLongitude(), location.getAltitude(),
location.getSpeed(), location.getAccuracy(), location.getTime(), settings);
if(routingHelper.isFollowingMode()){
routingHelper.setCurrentLocation(location);
}

View file

@ -524,7 +524,7 @@ public class MapActivity extends Activity implements IMapLocationListener, Senso
// write only with 50 meters accuracy
if (!location.hasAccuracy() || location.getAccuracy() < 50) {
savingTrackHelper.insertData(location.getLatitude(), location.getLongitude(), location.getAltitude(), location.getSpeed(),
location.getTime(), settings);
location.getAccuracy(), location.getTime(), settings);
}
}
registerUnregisterSensor(location);

View file

@ -10,7 +10,6 @@ import net.osmand.GPXUtilities;
import net.osmand.LogUtil;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.Track;
import net.osmand.GPXUtilities.TrkPt;
import net.osmand.GPXUtilities.TrkSegment;
import net.osmand.GPXUtilities.WptPt;
import net.osmand.plus.OsmandSettings;
@ -27,7 +26,7 @@ import android.text.format.DateFormat;
public class SavingTrackHelper extends SQLiteOpenHelper {
public final static String DATABASE_NAME = "tracks"; //$NON-NLS-1$
public final static int DATABASE_VERSION = 2;
public final static int DATABASE_VERSION = 3;
public final static String TRACK_NAME = "track"; //$NON-NLS-1$
public final static String TRACK_COL_DATE = "date"; //$NON-NLS-1$
@ -35,6 +34,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
public final static String TRACK_COL_LON = "lon"; //$NON-NLS-1$
public final static String TRACK_COL_ALTITUDE = "altitude"; //$NON-NLS-1$
public final static String TRACK_COL_SPEED = "speed"; //$NON-NLS-1$
public final static String TRACK_COL_HDOP = "hdop"; //$NON-NLS-1$
public final static String POINT_NAME = "point"; //$NON-NLS-1$
public final static String POINT_COL_DATE = "date"; //$NON-NLS-1$
@ -56,7 +56,10 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
public SavingTrackHelper(Context ctx){
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
this.ctx = ctx;
updateScript = "INSERT INTO " + TRACK_NAME + " VALUES (?, ?, ?, ?, ?)"; //$NON-NLS-1$ //$NON-NLS-2$
updateScript = "INSERT INTO " + TRACK_NAME +
" (" +TRACK_COL_LAT +", " +TRACK_COL_LON+", " +TRACK_COL_ALTITUDE+", " +TRACK_COL_SPEED
+", " +TRACK_COL_HDOP+", " +TRACK_COL_DATE+ ")" +
" VALUES (?, ?, ?, ?, ?, ?)"; //$NON-NLS-1$ //$NON-NLS-2$
updatePointsScript = "INSERT INTO " + POINT_NAME + " VALUES (?, ?, ?, ?)"; //$NON-NLS-1$ //$NON-NLS-2$
}
@ -82,6 +85,9 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
if(oldVersion < 2){
createTableForPoints(db);
}
if(oldVersion < 3){
db.execSQL("ALTER TABLE " + TRACK_NAME + " ADD " + TRACK_COL_HDOP + " double");
}
}
@ -179,19 +185,20 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
private void collectDBTracks(SQLiteDatabase db, Map<String, GPXFile> dataTracks) {
Cursor query = db.rawQuery("SELECT " + TRACK_COL_LAT + "," + TRACK_COL_LON + "," + TRACK_COL_ALTITUDE + "," //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ TRACK_COL_SPEED + "," + TRACK_COL_DATE + " FROM " + TRACK_NAME +" ORDER BY " + TRACK_COL_DATE +" ASC", null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ TRACK_COL_SPEED + "," + TRACK_COL_HDOP + "," + TRACK_COL_DATE + " FROM " + TRACK_NAME +" ORDER BY " + TRACK_COL_DATE +" ASC", null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
long previousTime = 0;
long previousInterval = 0;
TrkSegment segment = null;
Track track = null;
if (query.moveToFirst()) {
do {
TrkPt pt = new TrkPt();
WptPt pt = new WptPt();
pt.lat = query.getDouble(0);
pt.lon = query.getDouble(1);
pt.ele = query.getDouble(2);
pt.speed = query.getDouble(3);
long time = query.getLong(4);
pt.hdop = query.getDouble(4);
long time = query.getLong(5);
pt.time = time;
long currentInterval = Math.abs(time - previousTime);
@ -226,11 +233,11 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
query.close();
}
public void insertData(double lat, double lon, double alt, double speed, long time, OsmandSettings settings){
public void insertData(double lat, double lon, double alt, double speed, double hdop, long time, OsmandSettings settings){
if (time - lastTimeUpdated > settings.SAVE_TRACK_INTERVAL.get()*1000) {
SQLiteDatabase db = getWritableDatabase();
if (db != null) {
db.execSQL(updateScript, new Object[] { lat, lon, alt, speed, time });
db.execSQL(updateScript, new Object[] { lat, lon, alt, speed, hdop, time });
}
lastTimeUpdated = time;
}