diff --git a/commons/src/main/java/fr/free/nrw/commons/CommonsApplication.java b/commons/src/main/java/fr/free/nrw/commons/CommonsApplication.java index c2d4eb525..d6e1de079 100644 --- a/commons/src/main/java/fr/free/nrw/commons/CommonsApplication.java +++ b/commons/src/main/java/fr/free/nrw/commons/CommonsApplication.java @@ -147,8 +147,6 @@ public class CommonsApplication extends Application { return bitmapSize / 1024; } }; - - } //For caching area -> categories diff --git a/commons/src/main/java/fr/free/nrw/commons/caching/CacheController.java b/commons/src/main/java/fr/free/nrw/commons/caching/CacheController.java index 41d01c3c2..2681fb606 100644 --- a/commons/src/main/java/fr/free/nrw/commons/caching/CacheController.java +++ b/commons/src/main/java/fr/free/nrw/commons/caching/CacheController.java @@ -18,6 +18,7 @@ public class CacheController { private double xMinus, xPlus, yMinus, yPlus; private static final String TAG = CacheController.class.getName(); + private static final int EARTH_RADIUS = 6378137; public CacheController() { quadTree = new QuadTree(-180, -90, +180, +90); @@ -45,44 +46,37 @@ public class CacheController { //Convert decLatitude and decLongitude to a coordinate offset range convertCoordRange(); pointsFound = quadTree.searchWithin(xMinus, yMinus, xPlus, yPlus); - List displayCatList = new ArrayList(); + List displayCatList = new ArrayList(); Log.d(TAG, "Points found in quadtree: " + pointsFound); - List flatCatList = new ArrayList(); - if (pointsFound.length != 0) { Log.d(TAG, "Entering for loop"); - int index = 0; + for (Point point : pointsFound) { Log.d(TAG, "Nearby point: " + point.toString()); - Object cat = point.getValue(); - Log.d(TAG, "Nearby cat: " + cat); - displayCatList.add(index, cat); - index++; + displayCatList = (List)point.getValue(); + Log.d(TAG, "Nearby cat: " + point.getValue()); } - //FIXME: temporary, can't figure out why for loop always only accesses 1 point - flatCatList = ((ArrayList)displayCatList.get(0)); - Log.d(TAG, "Categories found in cache: " + flatCatList.toString()); + Log.d(TAG, "Categories found in cache: " + displayCatList.toString()); } else { 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() { //Position, decimal degrees double lat = y; double lon = x; - //Earth’s radius, sphere - double radius=6378137; //offsets in meters double offset = 100; //Coordinate offsets in radians - double dLat = offset/radius; - double dLon = offset/(radius*Math.cos(Math.PI*lat/180)); + double dLat = offset/EARTH_RADIUS; + double dLon = offset/(EARTH_RADIUS*Math.cos(Math.PI*lat/180)); //OffsetPosition, decimal degrees yPlus = lat + dLat * 180/Math.PI; diff --git a/commons/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java b/commons/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java index 7954ff5cf..4e5498e2d 100644 --- a/commons/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java +++ b/commons/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java @@ -66,7 +66,7 @@ public class ShareActivity if (cacheFound == false) { //Has to be called after apiCall.request() - cacheObj.cacheData.cacheCategory(); + app.cacheData.cacheCategory(); Log.d(TAG, "Cache the categories found"); } @@ -221,27 +221,28 @@ public class ShareActivity String filePath = getRealPathFromURI(mediaUri); 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) { //extract the coordinates of image in decimal degrees Log.d(TAG, "Calling GPSExtractor"); GPSExtractor imageObj = new GPSExtractor(filePath); String decimalCoords = imageObj.getCoords(); - double decLongitude = imageObj.getDecLongitude(); - double decLatitude = imageObj.getDecLatitude(); + if (decimalCoords != null) { + double decLongitude = imageObj.getDecLongitude(); + double decLatitude = imageObj.getDecLatitude(); + Log.d(TAG, "Decimal coords of image: " + decimalCoords); - cacheObj.cacheData.setQtPoint(decLongitude, decLatitude); + app.cacheData.setQtPoint(decLongitude, decLatitude); 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 (displayCatList.size() == 0) { + if (catListEmpty) { cacheFound = false; apiCall.request(decimalCoords); Log.d(TAG, "displayCatList size 0, calling MWAPI" + displayCatList.toString());