Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Merge pull request #344 from mapzen/chuck/dont-upload-traces-less-tha…
Browse files Browse the repository at this point in the history
…n-50-meters

Don't upload gpx traces with range < 50 meters
  • Loading branch information
baldur committed Nov 5, 2014
2 parents b7d5929 + 8037889 commit 449f805
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 4 deletions.
50 changes: 49 additions & 1 deletion src/main/java/com/mapzen/open/core/DataUploadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabaseLockedException;
import android.location.Location;
import android.os.AsyncTask;
import android.os.IBinder;

Expand Down Expand Up @@ -68,6 +69,18 @@

public class DataUploadService extends Service {
private static final int MIN_NUM_TRACKING_POINTS = 10;
private static final int MIN_RANGE_IN_METERS = 50;

private static final String RANGE_QUERY_EXT = " from "
+ TABLE_ROUTE_GROUP + " inner join " + TABLE_LOCATIONS + " on "
+ TABLE_LOCATIONS + "." + COLUMN_ROUTE_ID + " = " + TABLE_ROUTE_GROUP + "."
+ COLUMN_ROUTE_ID + " where " + COLUMN_GROUP_ID + " = ?";

private static final String MIN_LAT_QUERY = "select min(" + COLUMN_LAT + ")" + RANGE_QUERY_EXT;
private static final String MAX_LAT_QUERY = "select max(" + COLUMN_LAT + ")" + RANGE_QUERY_EXT;
private static final String MIN_LNG_QUERY = "select min(" + COLUMN_LNG + ")" + RANGE_QUERY_EXT;
private static final String MAX_LNG_QUERY = "select max(" + COLUMN_LNG + ")" + RANGE_QUERY_EXT;

private MapzenApplication app;

@Inject OAuthRequestFactory requestFactory;
Expand Down Expand Up @@ -172,7 +185,7 @@ public DOMSource getDocument(String groupId) {
DOMSource domSource = null;
try {
DateTimeFormatter isoDateParser = ISODateTimeFormat.dateTimeNoMillis();
String selectStatement = String.format(Locale.getDefault(),
String selectStatement = String.format(Locale.US,
"SELECT %s, %s, %s, %s, %s ", COLUMN_LAT, COLUMN_LNG, COLUMN_ALT, COLUMN_TIME,
COLUMN_SPEED);
String fullQuery = selectStatement
Expand Down Expand Up @@ -208,13 +221,48 @@ public DOMSource getDocument(String groupId) {
if (numberOfPoints < MIN_NUM_TRACKING_POINTS) {
return null;
}
if (calculateMaxRange(groupId) < MIN_RANGE_IN_METERS) {
return null;
}
domSource = new DOMSource(documentElement);
} catch (ParserConfigurationException e) {
Logger.e("Building xml failed: " + e.getMessage());
}
return domSource;
}

/**
* Calculates distance between the two theoretical farthest points in the route.
*
* @return theoretical max range in meters.
*/
private float calculateMaxRange(String groupId) {
final Cursor minLatCursor = app.getDb().rawQuery(MIN_LAT_QUERY, new String[] { groupId });
final Cursor maxLatCursor = app.getDb().rawQuery(MAX_LAT_QUERY, new String[] { groupId });
final Cursor minLngCursor = app.getDb().rawQuery(MIN_LNG_QUERY, new String[] { groupId });
final Cursor maxLngCursor = app.getDb().rawQuery(MAX_LNG_QUERY, new String[] { groupId });

minLatCursor.moveToFirst();
minLngCursor.moveToFirst();
maxLatCursor.moveToFirst();
maxLngCursor.moveToFirst();

final double minLat = minLatCursor.getDouble(0);
final double maxLat = maxLatCursor.getDouble(0);
final double minLng = minLngCursor.getDouble(0);
final double maxLng = maxLngCursor.getDouble(0);

final Location min = new Location("temp");
min.setLatitude(minLat);
min.setLongitude(minLng);

final Location max = new Location("temp");
max.setLatitude(maxLat);
max.setLongitude(maxLng);

return min.distanceTo(max);
}

private void setOutputFormat(Transformer transformer) {
Properties outFormat = new Properties();
outFormat.setProperty(INDENT, "yes");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,11 @@ public void add(SimpleFeature simpleFeature) {
}

public void update(SimpleFeature simpleFeature) {
TextView address = (TextView) pager.findViewById(R.id.address);
address.setText(String.format(Locale.getDefault(), "%s, %s",
simpleFeature.getCity(), simpleFeature.getAdmin()));
if (pager != null) {
TextView address = (TextView) pager.findViewById(R.id.address);
address.setText(String.format(Locale.getDefault(), "%s, %s",
simpleFeature.getCity(), simpleFeature.getAdmin()));
}
}

public void setSearchResults(List<Feature> features) {
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/com/mapzen/open/core/DataUploadServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,18 @@ public void shouldNotCrashWhenDatabaseIsNull() throws Exception {
service.onStartCommand(null, 0, 0);
}

@Test
public void shouldNotUploadWhenLessThan50MetersTraveled() throws Exception {
Token token = new Token("stuff", "fun");
app.setAccessToken(token);
String groupId = "test-group-id";
String routeId = "test-route-id";
fillLocationsTableAllSamePoint(groupId, routeId, 10);
DataUploadService spy = spy(service);
spy.onStartCommand(null, 0, 0);
verify(spy, never()).submitTrace(anyString(), anyString(), any(byte[].class));
}

private void makeGroupReady(String groupId) throws Exception {
ContentValues insertValues = new ContentValues();
insertValues.put(COLUMN_TABLE_ID, groupId);
Expand Down Expand Up @@ -281,6 +293,36 @@ private void fillLocationsTable(String groupId, String routeId, double numPoints
}
}

private void fillLocationsTableAllSamePoint(String groupId, String routeId, double numPoints)
throws Exception {
makeGroupReady(groupId);

ContentValues routeValues = new ContentValues();
routeValues.put(COLUMN_TABLE_ID, routeId);
routeValues.put(COLUMN_RAW, "does not matter");
long routeResults = app.getDb().insert(TABLE_ROUTES, null, routeValues);

ContentValues routeGroupValues = new ContentValues();
routeGroupValues.put(COLUMN_ROUTE_ID, routeId);
routeGroupValues.put(COLUMN_GROUP_ID, groupId);
long routeGroupResults = app.getDb().insert(TABLE_ROUTE_GROUP, null, routeGroupValues);

if (routeResults < 0 || routeGroupResults < 0) {
throw new Exception("database insertion failed");
}

final double testLat = 40.7484;
final double testLng = -73.9857;

ContentValues cv;
for (int i = 0; i < numPoints; i++) {
cv = valuesForLocationCorrection(getTestLocation(testLat, testLng),
getTestLocation(testLat, testLng), getTestInstruction(testLat, testLng),
routeId);
app.getDb().insert(TABLE_LOCATIONS, null, cv);
}
}

private void makeRouteUploaded(String routeId) {
ContentValues insertValues = new ContentValues();
insertValues.put(COLUMN_TABLE_ID, routeId);
Expand Down

0 comments on commit 449f805

Please sign in to comment.