Merge remote-tracking branch 'refs/remotes/origin/caching' into device-bugfix

This commit is contained in:
misaochan 2016-01-11 19:12:02 +13:00
commit 2bc09d16d2
3 changed files with 19 additions and 26 deletions

View file

@ -147,8 +147,6 @@ public class CommonsApplication extends Application {
return bitmapSize / 1024; return bitmapSize / 1024;
} }
}; };
} }
//For caching area -> categories //For caching area -> categories

View file

@ -18,6 +18,7 @@ public class CacheController {
private double xMinus, xPlus, yMinus, yPlus; private double xMinus, xPlus, yMinus, yPlus;
private static final String TAG = CacheController.class.getName(); private static final String TAG = CacheController.class.getName();
private static final int EARTH_RADIUS = 6378137;
public CacheController() { public CacheController() {
quadTree = new QuadTree(-180, -90, +180, +90); quadTree = new QuadTree(-180, -90, +180, +90);
@ -45,44 +46,37 @@ public class CacheController {
//Convert decLatitude and decLongitude to a coordinate offset range //Convert decLatitude and decLongitude to a coordinate offset range
convertCoordRange(); convertCoordRange();
pointsFound = quadTree.searchWithin(xMinus, yMinus, xPlus, yPlus); pointsFound = quadTree.searchWithin(xMinus, yMinus, xPlus, yPlus);
List displayCatList = new ArrayList(); List<String> displayCatList = new ArrayList<String>();
Log.d(TAG, "Points found in quadtree: " + pointsFound); Log.d(TAG, "Points found in quadtree: " + pointsFound);
List<String> flatCatList = new ArrayList<String>();
if (pointsFound.length != 0) { if (pointsFound.length != 0) {
Log.d(TAG, "Entering for loop"); Log.d(TAG, "Entering for loop");
int index = 0;
for (Point point : pointsFound) { for (Point point : pointsFound) {
Log.d(TAG, "Nearby point: " + point.toString()); Log.d(TAG, "Nearby point: " + point.toString());
Object cat = point.getValue(); displayCatList = (List<String>)point.getValue();
Log.d(TAG, "Nearby cat: " + cat); Log.d(TAG, "Nearby cat: " + point.getValue());
displayCatList.add(index, cat);
index++;
} }
//FIXME: temporary, can't figure out why for loop always only accesses 1 point
flatCatList = ((ArrayList<String>)displayCatList.get(0));
Log.d(TAG, "Categories found in cache: " + flatCatList.toString()); Log.d(TAG, "Categories found in cache: " + displayCatList.toString());
} else { } else {
Log.d(TAG, "No categories found in cache"); Log.d(TAG, "No categories found in cache");
} }
return flatCatList; return displayCatList;
} }
//Based on algorithm at http://gis.stackexchange.com/questions/2951/algorithm-for-offsetting-a-latitude-longitude-by-some-amount-of-meters
public void convertCoordRange() { public void convertCoordRange() {
//Position, decimal degrees //Position, decimal degrees
double lat = y; double lat = y;
double lon = x; double lon = x;
//Earths radius, sphere
double radius=6378137;
//offsets in meters //offsets in meters
double offset = 100; double offset = 100;
//Coordinate offsets in radians //Coordinate offsets in radians
double dLat = offset/radius; double dLat = offset/EARTH_RADIUS;
double dLon = offset/(radius*Math.cos(Math.PI*lat/180)); double dLon = offset/(EARTH_RADIUS*Math.cos(Math.PI*lat/180));
//OffsetPosition, decimal degrees //OffsetPosition, decimal degrees
yPlus = lat + dLat * 180/Math.PI; yPlus = lat + dLat * 180/Math.PI;

View file

@ -66,7 +66,7 @@ public class ShareActivity
if (cacheFound == false) { if (cacheFound == false) {
//Has to be called after apiCall.request() //Has to be called after apiCall.request()
cacheObj.cacheData.cacheCategory(); app.cacheData.cacheCategory();
Log.d(TAG, "Cache the categories found"); Log.d(TAG, "Cache the categories found");
} }
@ -221,27 +221,28 @@ public class ShareActivity
String filePath = getRealPathFromURI(mediaUri); String filePath = getRealPathFromURI(mediaUri);
Log.d(TAG, "Filepath: " + filePath); Log.d(TAG, "Filepath: " + filePath);
//Using global singleton to get CacheController to last longer than the activity lifecycle
cacheObj = ((CommonsApplication)this.getApplication());
if (filePath != null) { if (filePath != null) {
//extract the coordinates of image in decimal degrees //extract the coordinates of image in decimal degrees
Log.d(TAG, "Calling GPSExtractor"); Log.d(TAG, "Calling GPSExtractor");
GPSExtractor imageObj = new GPSExtractor(filePath); GPSExtractor imageObj = new GPSExtractor(filePath);
String decimalCoords = imageObj.getCoords(); String decimalCoords = imageObj.getCoords();
double decLongitude = imageObj.getDecLongitude();
double decLatitude = imageObj.getDecLatitude();
if (decimalCoords != null) { if (decimalCoords != null) {
double decLongitude = imageObj.getDecLongitude();
double decLatitude = imageObj.getDecLatitude();
Log.d(TAG, "Decimal coords of image: " + decimalCoords); Log.d(TAG, "Decimal coords of image: " + decimalCoords);
cacheObj.cacheData.setQtPoint(decLongitude, decLatitude); app.cacheData.setQtPoint(decLongitude, decLatitude);
MwVolleyApi apiCall = new MwVolleyApi(this); MwVolleyApi apiCall = new MwVolleyApi(this);
List displayCatList = cacheObj.cacheData.findCategory(); List displayCatList = app.cacheData.findCategory();
boolean catListEmpty = displayCatList.isEmpty();
//if no categories found in cache, call MW API to match image coords with nearby Commons categories //if no categories found in cache, call MW API to match image coords with nearby Commons categories
if (displayCatList.size() == 0) { if (catListEmpty) {
cacheFound = false; cacheFound = false;
apiCall.request(decimalCoords); apiCall.request(decimalCoords);
Log.d(TAG, "displayCatList size 0, calling MWAPI" + displayCatList.toString()); Log.d(TAG, "displayCatList size 0, calling MWAPI" + displayCatList.toString());