From 7ce715c1da8f6b683b4134c5fd0ba7d7cd5a5de2 Mon Sep 17 00:00:00 2001 From: misaochan Date: Tue, 5 Jan 2016 18:10:10 +1300 Subject: [PATCH 01/44] Copying Quadtree src files manually --- .../fr/free/nrw/commons/caching/Func.java | 7 + .../fr/free/nrw/commons/caching/Node.java | 123 +++++ .../fr/free/nrw/commons/caching/NodeType.java | 11 + .../fr/free/nrw/commons/caching/Point.java | 69 +++ .../fr/free/nrw/commons/caching/QuadTree.java | 477 ++++++++++++++++++ .../commons/caching/QuadTreeException.java | 8 + 6 files changed, 695 insertions(+) create mode 100644 commons/src/main/java/fr/free/nrw/commons/caching/Func.java create mode 100644 commons/src/main/java/fr/free/nrw/commons/caching/Node.java create mode 100644 commons/src/main/java/fr/free/nrw/commons/caching/NodeType.java create mode 100644 commons/src/main/java/fr/free/nrw/commons/caching/Point.java create mode 100644 commons/src/main/java/fr/free/nrw/commons/caching/QuadTree.java create mode 100644 commons/src/main/java/fr/free/nrw/commons/caching/QuadTreeException.java diff --git a/commons/src/main/java/fr/free/nrw/commons/caching/Func.java b/commons/src/main/java/fr/free/nrw/commons/caching/Func.java new file mode 100644 index 000000000..9703fc98f --- /dev/null +++ b/commons/src/main/java/fr/free/nrw/commons/caching/Func.java @@ -0,0 +1,7 @@ +package fr.free.nrw.commons.caching; + +import fr.free.nrw.commons.caching.QuadTree; + +public interface Func { + public void call(QuadTree quadTree, Node node); +} diff --git a/commons/src/main/java/fr/free/nrw/commons/caching/Node.java b/commons/src/main/java/fr/free/nrw/commons/caching/Node.java new file mode 100644 index 000000000..1ab054a99 --- /dev/null +++ b/commons/src/main/java/fr/free/nrw/commons/caching/Node.java @@ -0,0 +1,123 @@ +package fr.free.nrw.commons.caching; + + class Node { + + private double x; + private double y; + private double w; + private double h; + private Node opt_parent; + private Point point; + private NodeType nodetype = NodeType.EMPTY; + private Node nw; + private Node ne; + private Node sw; + private Node se; + + /** + * Constructs a new quad tree node. + * + * @param {double} x X-coordiate of node. + * @param {double} y Y-coordinate of node. + * @param {double} w Width of node. + * @param {double} h Height of node. + * @param {Node} opt_parent Optional parent node. + * @constructor + */ + public Node(double x, double y, double w, double h, Node opt_parent) { + this.x = x; + this.y = y; + this.w = w; + this.h = h; + this.opt_parent = opt_parent; + } + + public double getX() { + return x; + } + + public void setX(double x) { + this.x = x; + } + + public double getY() { + return y; + } + + public void setY(double y) { + this.y = y; + } + + public double getW() { + return w; + } + + public void setW(double w) { + this.w = w; + } + + public double getH() { + return h; + } + + public void setH(double h) { + this.h = h; + } + + public Node getParent() { + return opt_parent; + } + + public void setParent(Node opt_parent) { + this.opt_parent = opt_parent; + } + + public void setPoint(Point point) { + this.point = point; + } + + public Point getPoint() { + return this.point; + } + + public void setNodeType(NodeType nodetype) { + this.nodetype = nodetype; + } + + public NodeType getNodeType() { + return this.nodetype; + } + + + public void setNw(Node nw) { + this.nw = nw; + } + + public void setNe(Node ne) { + this.ne = ne; + } + + public void setSw(Node sw) { + this.sw = sw; + } + + public void setSe(Node se) { + this.se = se; + } + + public Node getNe() { + return ne; + } + + public Node getNw() { + return nw; + } + + public Node getSw() { + return sw; + } + + public Node getSe() { + return se; + } +} diff --git a/commons/src/main/java/fr/free/nrw/commons/caching/NodeType.java b/commons/src/main/java/fr/free/nrw/commons/caching/NodeType.java new file mode 100644 index 000000000..9aac701fa --- /dev/null +++ b/commons/src/main/java/fr/free/nrw/commons/caching/NodeType.java @@ -0,0 +1,11 @@ +package fr.free.nrw.commons.caching; + +/** + * Enumeration of node types. + * @enum {number} + */ +public enum NodeType { + EMPTY, + LEAF, + POINTER +} diff --git a/commons/src/main/java/fr/free/nrw/commons/caching/Point.java b/commons/src/main/java/fr/free/nrw/commons/caching/Point.java new file mode 100644 index 000000000..6b2fdb8c1 --- /dev/null +++ b/commons/src/main/java/fr/free/nrw/commons/caching/Point.java @@ -0,0 +1,69 @@ +package fr.free.nrw.commons.caching; + +public class Point implements Comparable { + + private double x; + private double y; + private Object opt_value; + + /** + * Creates a new point object. + * + * @param {double} x The x-coordinate of the point. + * @param {double} y The y-coordinate of the point. + * @param {Object} opt_value Optional value associated with the point. + */ + public Point(double x, double y, Object opt_value) { + this.x = x; + this.y = y; + this.opt_value = opt_value; + } + + public double getX() { + return x; + } + + public void setX(double x) { + this.x = x; + } + + public double getY() { + return y; + } + + public void setY(double y) { + this.y = y; + } + + public Object getValue() { + return opt_value; + } + + public void setValue(Object opt_value) { + this.opt_value = opt_value; + } + + @Override + public String toString() { + return "(" + this.x + ", " + this.y + ")"; + } + + @Override + public int compareTo(Object o) { + Point tmp = (Point) o; + if (this.x < tmp.x) { + return -1; + } else if (this.x > tmp.x) { + return 1; + } else { + if (this.y < tmp.y) { + return -1; + } else if (this.y > tmp.y) { + return 1; + } + return 0; + } + + } + +} diff --git a/commons/src/main/java/fr/free/nrw/commons/caching/QuadTree.java b/commons/src/main/java/fr/free/nrw/commons/caching/QuadTree.java new file mode 100644 index 000000000..1f77a3491 --- /dev/null +++ b/commons/src/main/java/fr/free/nrw/commons/caching/QuadTree.java @@ -0,0 +1,477 @@ +package fr.free.nrw.commons.caching; + +import java.util.ArrayList; +import java.util.List; + + +/** + * Datastructure: A point Quad Tree for representing 2D data. Each + * region has the same ratio as the bounds for the tree. + *

+ * The implementation currently requires pre-determined bounds for data as it + * can not rebalance itself to that degree. + */ +public class QuadTree { + + + private Node root_; + private int count_ = 0; + + /** + * Constructs a new quad tree. + * + * @param {double} minX Minimum x-value that can be held in tree. + * @param {double} minY Minimum y-value that can be held in tree. + * @param {double} maxX Maximum x-value that can be held in tree. + * @param {double} maxY Maximum y-value that can be held in tree. + */ + public QuadTree(double minX, double minY, double maxX, double maxY) { + this.root_ = new Node(minX, minY, maxX - minX, maxY - minY, null); + } + + /** + * Returns a reference to the tree's root node. Callers shouldn't modify nodes, + * directly. This is a convenience for visualization and debugging purposes. + * + * @return {Node} The root node. + */ + public Node getRootNode() { + return this.root_; + } + + //TODO: Marked by Nicolas as: Set an object at particular coordinates + /** + * Sets the value of an (x, y) point within the quad-tree. + * + * @param {double} x The x-coordinate. + * @param {double} y The y-coordinate. + * @param {Object} value The value associated with the point. + */ + public void set(double x, double y, Object value) { + + Node root = this.root_; + if (x < root.getX() || y < root.getY() || x > root.getX() + root.getW() || y > root.getY() + root.getH()) { + throw new QuadTreeException("Out of bounds : (" + x + ", " + y + ")"); + } + if (this.insert(root, new Point(x, y, value))) { + this.count_++; + } + } + + /** + * Gets the value of the point at (x, y) or null if the point is empty. + * + * @param {double} x The x-coordinate. + * @param {double} y The y-coordinate. + * @param {Object} opt_default The default value to return if the node doesn't + * exist. + * @return {*} The value of the node, the default value if the node + * doesn't exist, or undefined if the node doesn't exist and no default + * has been provided. + */ + public Object get(double x, double y, Object opt_default) { + Node node = this.find(this.root_, x, y); + return node != null ? node.getPoint().getValue() : opt_default; + } + + /** + * Removes a point from (x, y) if it exists. + * + * @param {double} x The x-coordinate. + * @param {double} y The y-coordinate. + * @return {Object} The value of the node that was removed, or null if the + * node doesn't exist. + */ + public Object remove(double x, double y) { + Node node = this.find(this.root_, x, y); + if (node != null) { + Object value = node.getPoint().getValue(); + node.setPoint(null); + node.setNodeType(NodeType.EMPTY); + this.balance(node); + this.count_--; + return value; + } else { + return null; + } + } + + /** + * Returns true if the point at (x, y) exists in the tree. + * + * @param {double} x The x-coordinate. + * @param {double} y The y-coordinate. + * @return {boolean} Whether the tree contains a point at (x, y). + */ + public boolean contains(double x, double y) { + return this.get(x, y, null) != null; + } + + /** + * @return {boolean} Whether the tree is empty. + */ + public boolean isEmpty() { + return this.root_.getNodeType() == NodeType.EMPTY; + } + + /** + * @return {number} The number of items in the tree. + */ + public int getCount() { + return this.count_; + } + + /** + * Removes all items from the tree. + */ + public void clear() { + this.root_.setNw(null); + this.root_.setNe(null); + this.root_.setSw(null); + this.root_.setSe(null); + this.root_.setNodeType(NodeType.EMPTY); + this.root_.setPoint(null); + this.count_ = 0; + } + + /** + * Returns an array containing the coordinates of each point stored in the tree. + * @return {Array.} Array of coordinates. + */ + public Point[] getKeys() { + final List arr = new ArrayList(); + this.traverse(this.root_, new Func() { + @Override + public void call(QuadTree quadTree, Node node) { + arr.add(node.getPoint()); + } + }); + return arr.toArray(new Point[arr.size()]); + } + + /** + * Returns an array containing all values stored within the tree. + * @return {Array.} The values stored within the tree. + */ + public Object[] getValues() { + final List arr = new ArrayList(); + this.traverse(this.root_, new Func() { + @Override + public void call(QuadTree quadTree, Node node) { + arr.add(node.getPoint().getValue()); + } + }); + + return arr.toArray(new Object[arr.size()]); + } + + public Point[] searchIntersect(final double xmin, final double ymin, final double xmax, final double ymax) { + final List arr = new ArrayList(); + this.navigate(this.root_, new Func() { + @Override + public void call(QuadTree quadTree, Node node) { + Point pt = node.getPoint(); + if (pt.getX() < xmin || pt.getX() > xmax || pt.getY() < ymin || pt.getY() > ymax) { + // Definitely not within the polygon! + } else { + arr.add(node.getPoint()); + } + + } + }, xmin, ymin, xmax, ymax); + return arr.toArray(new Point[arr.size()]); + } + + //TODO: Marked by Nicolas as: Find objects within a rectangle + public Point[] searchWithin(final double xmin, final double ymin, final double xmax, final double ymax) { + final List arr = new ArrayList(); + this.navigate(this.root_, new Func() { + @Override + public void call(QuadTree quadTree, Node node) { + Point pt = node.getPoint(); + if (pt.getX() > xmin && pt.getX() < xmax && pt.getY() > ymin && pt.getY() < ymax) { + arr.add(node.getPoint()); + } + } + }, xmin, ymin, xmax, ymax); + return arr.toArray(new Point[arr.size()]); + } + + public void navigate(Node node, Func func, double xmin, double ymin, double xmax, double ymax) { + switch (node.getNodeType()) { + case LEAF: + func.call(this, node); + break; + + case POINTER: + if (intersects(xmin, ymax, xmax, ymin, node.getNe())) + this.navigate(node.getNe(), func, xmin, ymin, xmax, ymax); + if (intersects(xmin, ymax, xmax, ymin, node.getSe())) + this.navigate(node.getSe(), func, xmin, ymin, xmax, ymax); + if (intersects(xmin, ymax, xmax, ymin, node.getSw())) + this.navigate(node.getSw(), func, xmin, ymin, xmax, ymax); + if (intersects(xmin, ymax, xmax, ymin, node.getNw())) + this.navigate(node.getNw(), func, xmin, ymin, xmax, ymax); + break; + } + } + + private boolean intersects(double left, double bottom, double right, double top, Node node) { + return !(node.getX() > right || + (node.getX() + node.getW()) < left || + node.getY() > bottom || + (node.getY() + node.getH()) < top); + } + /** + * Clones the quad-tree and returns the new instance. + * @return {QuadTree} A clone of the tree. + */ + public QuadTree clone() { + double x1 = this.root_.getX(); + double y1 = this.root_.getY(); + double x2 = x1 + this.root_.getW(); + double y2 = y1 + this.root_.getH(); + final QuadTree clone = new QuadTree(x1, y1, x2, y2); + // This is inefficient as the clone needs to recalculate the structure of the + // tree, even though we know it already. But this is easier and can be + // optimized when/if needed. + this.traverse(this.root_, new Func() { + @Override + public void call(QuadTree quadTree, Node node) { + clone.set(node.getPoint().getX(), node.getPoint().getY(), node.getPoint().getValue()); + } + }); + + + return clone; + } + + /** + * Traverses the tree depth-first, with quadrants being traversed in clockwise + * order (NE, SE, SW, NW). The provided function will be called for each + * leaf node that is encountered. + * @param {QuadTree.Node} node The current node. + * @param {function(QuadTree.Node)} fn The function to call + * for each leaf node. This function takes the node as an argument, and its + * return value is irrelevant. + * @private + */ + public void traverse(Node node, Func func) { + switch (node.getNodeType()) { + case LEAF: + func.call(this, node); + break; + + case POINTER: + this.traverse(node.getNe(), func); + this.traverse(node.getSe(), func); + this.traverse(node.getSw(), func); + this.traverse(node.getNw(), func); + break; + } + } + + /** + * Finds a leaf node with the same (x, y) coordinates as the target point, or + * null if no point exists. + * @param {QuadTree.Node} node The node to search in. + * @param {number} x The x-coordinate of the point to search for. + * @param {number} y The y-coordinate of the point to search for. + * @return {QuadTree.Node} The leaf node that matches the target, + * or null if it doesn't exist. + * @private + */ + public Node find(Node node, double x, double y) { + Node resposne = null; + switch (node.getNodeType()) { + case EMPTY: + break; + + case LEAF: + resposne = node.getPoint().getX() == x && node.getPoint().getY() == y ? node : null; + break; + + case POINTER: + resposne = this.find(this.getQuadrantForPoint(node, x, y), x, y); + break; + + default: + throw new QuadTreeException("Invalid nodeType"); + } + return resposne; + } + + /** + * Inserts a point into the tree, updating the tree's structure if necessary. + * @param {.QuadTree.Node} parent The parent to insert the point + * into. + * @param {QuadTree.Point} point The point to insert. + * @return {boolean} True if a new node was added to the tree; False if a node + * already existed with the correpsonding coordinates and had its value + * reset. + * @private + */ + private boolean insert(Node parent, Point point) { + Boolean result = false; + switch (parent.getNodeType()) { + case EMPTY: + this.setPointForNode(parent, point); + result = true; + break; + case LEAF: + if (parent.getPoint().getX() == point.getX() && parent.getPoint().getY() == point.getY()) { + this.setPointForNode(parent, point); + result = false; + } else { + this.split(parent); + result = this.insert(parent, point); + } + break; + case POINTER: + result = this.insert( + this.getQuadrantForPoint(parent, point.getX(), point.getY()), point); + break; + + default: + throw new QuadTreeException("Invalid nodeType in parent"); + } + return result; + } + + /** + * Converts a leaf node to a pointer node and reinserts the node's point into + * the correct child. + * @param {QuadTree.Node} node The node to split. + * @private + */ + private void split(Node node) { + Point oldPoint = node.getPoint(); + node.setPoint(null); + + node.setNodeType(NodeType.POINTER); + + double x = node.getX(); + double y = node.getY(); + double hw = node.getW() / 2; + double hh = node.getH() / 2; + + node.setNw(new Node(x, y, hw, hh, node)); + node.setNe(new Node(x + hw, y, hw, hh, node)); + node.setSw(new Node(x, y + hh, hw, hh, node)); + node.setSe(new Node(x + hw, y + hh, hw, hh, node)); + + this.insert(node, oldPoint); + } + + /** + * Attempts to balance a node. A node will need balancing if all its children + * are empty or it contains just one leaf. + * @param {QuadTree.Node} node The node to balance. + * @private + */ + private void balance(Node node) { + switch (node.getNodeType()) { + case EMPTY: + case LEAF: + if (node.getParent() != null) { + this.balance(node.getParent()); + } + break; + + case POINTER: { + Node nw = node.getNw(); + Node ne = node.getNe(); + Node sw = node.getSw(); + Node se = node.getSe(); + Node firstLeaf = null; + + // Look for the first non-empty child, if there is more than one then we + // break as this node can't be balanced. + if (nw.getNodeType() != NodeType.EMPTY) { + firstLeaf = nw; + } + if (ne.getNodeType() != NodeType.EMPTY) { + if (firstLeaf != null) { + break; + } + firstLeaf = ne; + } + if (sw.getNodeType() != NodeType.EMPTY) { + if (firstLeaf != null) { + break; + } + firstLeaf = sw; + } + if (se.getNodeType() != NodeType.EMPTY) { + if (firstLeaf != null) { + break; + } + firstLeaf = se; + } + + if (firstLeaf == null) { + // All child nodes are empty: so make this node empty. + node.setNodeType(NodeType.EMPTY); + node.setNw(null); + node.setNe(null); + node.setSw(null); + node.setSe(null); + + } else if (firstLeaf.getNodeType() == NodeType.POINTER) { + // Only child was a pointer, therefore we can't rebalance. + break; + + } else { + // Only child was a leaf: so update node's point and make it a leaf. + node.setNodeType(NodeType.LEAF); + node.setNw(null); + node.setNe(null); + node.setSw(null); + node.setSe(null); + node.setPoint(firstLeaf.getPoint()); + } + + // Try and balance the parent as well. + if (node.getParent() != null) { + this.balance(node.getParent()); + } + } + break; + } + } + + /** + * Returns the child quadrant within a node that contains the given (x, y) + * coordinate. + * @param {QuadTree.Node} parent The node. + * @param {number} x The x-coordinate to look for. + * @param {number} y The y-coordinate to look for. + * @return {QuadTree.Node} The child quadrant that contains the + * point. + * @private + */ + //TODO: Is this the method we want to retrieve the 'area' given a coordinate? + private Node getQuadrantForPoint(Node parent, double x, double y) { + double mx = parent.getX() + parent.getW() / 2; + double my = parent.getY() + parent.getH() / 2; + if (x < mx) { + return y < my ? parent.getNw() : parent.getSw(); + } else { + return y < my ? parent.getNe() : parent.getSe(); + } + } + + /** + * Sets the point for a node, as long as the node is a leaf or empty. + * @param {QuadTree.Node} node The node to set the point for. + * @param {QuadTree.Point} point The point to set. + * @private + */ + private void setPointForNode(Node node, Point point) { + if (node.getNodeType() == NodeType.POINTER) { + throw new QuadTreeException("Can not set point for node of type POINTER"); + } + node.setNodeType(NodeType.LEAF); + node.setPoint(point); + } +} diff --git a/commons/src/main/java/fr/free/nrw/commons/caching/QuadTreeException.java b/commons/src/main/java/fr/free/nrw/commons/caching/QuadTreeException.java new file mode 100644 index 000000000..36b0022d9 --- /dev/null +++ b/commons/src/main/java/fr/free/nrw/commons/caching/QuadTreeException.java @@ -0,0 +1,8 @@ +package fr.free.nrw.commons.caching; + +public class QuadTreeException extends RuntimeException { + + public QuadTreeException(String s) { + super(s); + } +} From 09505c0c176724573125637fbd61241daa6910f7 Mon Sep 17 00:00:00 2001 From: misaochan Date: Tue, 5 Jan 2016 18:21:21 +1300 Subject: [PATCH 02/44] Preparing for caching --- commons/src/main/java/fr/free/nrw/commons/caching/Func.java | 1 - .../main/java/fr/free/nrw/commons/upload/ShareActivity.java | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/commons/src/main/java/fr/free/nrw/commons/caching/Func.java b/commons/src/main/java/fr/free/nrw/commons/caching/Func.java index 9703fc98f..622828ad2 100644 --- a/commons/src/main/java/fr/free/nrw/commons/caching/Func.java +++ b/commons/src/main/java/fr/free/nrw/commons/caching/Func.java @@ -1,6 +1,5 @@ package fr.free.nrw.commons.caching; -import fr.free.nrw.commons.caching.QuadTree; public interface Func { public void call(QuadTree quadTree, Node node); 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 369d9b990..18d1ebebf 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 @@ -174,7 +174,6 @@ public class ShareActivity mediaUriString = mediaUri.toString(); Log.d("Image", "Uri: " + mediaUriString); - //convert image Uri to file path FilePathConverter uriObj = new FilePathConverter(this, mediaUri); String filePath = uriObj.getFilePath(); @@ -187,8 +186,10 @@ public class ShareActivity if (coords != null) { Log.d("Image", "Coords of image: " + coords); - MwVolleyApi apiCall = new MwVolleyApi(this); + //TODO: Insert cache query here, only send API request if no cached categories + + MwVolleyApi apiCall = new MwVolleyApi(this); //asynchronous calls to MediaWiki Commons API to match image coords with nearby Commons categories apiCall.request(coords); } From dcd5d600027d6cbf7a43c4002b1b2557c42ee4c4 Mon Sep 17 00:00:00 2001 From: misaochan Date: Tue, 5 Jan 2016 18:30:10 +1300 Subject: [PATCH 03/44] CacheController backbone up For area category caching logic --- .../nrw/commons/caching/CacheController.java | 23 +++++++++++++++++++ .../free/nrw/commons/upload/GPSExtractor.java | 3 ++- .../nrw/commons/upload/ShareActivity.java | 6 ++++- 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 commons/src/main/java/fr/free/nrw/commons/caching/CacheController.java 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 new file mode 100644 index 000000000..1f2244bf8 --- /dev/null +++ b/commons/src/main/java/fr/free/nrw/commons/caching/CacheController.java @@ -0,0 +1,23 @@ +package fr.free.nrw.commons.caching; + +import android.content.Context; +import android.util.Log; + +/** + * Created by misao on 05-Jan-16. + */ +public class CacheController { + + private Context context; + private String coords; + + public CacheController(Context context, String coords) { + this.context = context; + this.coords = coords; + } + + public String getCoords() { + Log.d("Cache", "Coords passed to cache: " + coords); + return coords; + } +} diff --git a/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java b/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java index 88191b894..25815da5a 100644 --- a/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java +++ b/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java @@ -68,7 +68,8 @@ public class GPSExtractor { decLongitude = 0 - convertToDegree(longitude); } - return (String.valueOf(decLatitude) + "|" + String.valueOf(decLongitude)); + //Have to return Longitude before Latitude for X/Y conversion. Long = X; Lat = Y + return ("Long|Lat" + String.valueOf(decLongitude) + "|" + String.valueOf(decLatitude)); } private double convertToDegree(String stringDMS){ 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 18d1ebebf..200a0d660 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 @@ -11,6 +11,7 @@ import android.util.Log; import android.widget.*; import fr.free.nrw.commons.*; +import fr.free.nrw.commons.caching.CacheController; import fr.free.nrw.commons.modifications.CategoryModifier; import fr.free.nrw.commons.modifications.TemplateRemoveModifier; import fr.free.nrw.commons.CommonsApplication; @@ -188,9 +189,12 @@ public class ShareActivity Log.d("Image", "Coords of image: " + coords); //TODO: Insert cache query here, only send API request if no cached categories + CacheController cacheObj = new CacheController(this, coords); + cacheObj.getCoords(); + - MwVolleyApi apiCall = new MwVolleyApi(this); //asynchronous calls to MediaWiki Commons API to match image coords with nearby Commons categories + MwVolleyApi apiCall = new MwVolleyApi(this); apiCall.request(coords); } } From ffe967b6235576b63ce56d4170fd47cca5d353a6 Mon Sep 17 00:00:00 2001 From: misaochan Date: Tue, 5 Jan 2016 18:31:39 +1300 Subject: [PATCH 04/44] Minor coord fix --- .../src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java | 2 +- .../src/main/java/fr/free/nrw/commons/upload/ShareActivity.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java b/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java index 25815da5a..2a04382cf 100644 --- a/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java +++ b/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java @@ -69,7 +69,7 @@ public class GPSExtractor { } //Have to return Longitude before Latitude for X/Y conversion. Long = X; Lat = Y - return ("Long|Lat" + String.valueOf(decLongitude) + "|" + String.valueOf(decLatitude)); + return (String.valueOf(decLongitude) + "|" + String.valueOf(decLatitude)); } private double convertToDegree(String stringDMS){ 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 200a0d660..eb6afd648 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 @@ -186,7 +186,7 @@ public class ShareActivity String coords = imageObj.getCoords(); if (coords != null) { - Log.d("Image", "Coords of image: " + coords); + Log.d("Image", "Coords of image in Long/Lat: " + coords); //TODO: Insert cache query here, only send API request if no cached categories CacheController cacheObj = new CacheController(this, coords); From 3432ffeae22b35c5b1346790ff8ad9f39a18d7b5 Mon Sep 17 00:00:00 2001 From: misaochan Date: Tue, 5 Jan 2016 18:46:52 +1300 Subject: [PATCH 05/44] Flip lat long values around for XY coords --- .../free/nrw/commons/upload/GPSExtractor.java | 44 ++++++++++++++++--- .../nrw/commons/upload/ShareActivity.java | 2 +- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java b/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java index 2a04382cf..d05f3a168 100644 --- a/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java +++ b/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java @@ -15,7 +15,7 @@ public class GPSExtractor { } //Extract GPS coords of image - public String getCoords() { + public String getCoords(boolean xyCoordsReq) { ExifInterface exif; String latitude = ""; @@ -23,6 +23,7 @@ public class GPSExtractor { String latitude_ref = ""; String longitude_ref = ""; String decimalCoords = ""; + String xyCoords = ""; try { exif = new ExifInterface(filePath); @@ -45,11 +46,17 @@ public class GPSExtractor { Log.d("Image", "Longitude: " + longitude + " " + longitude_ref); decimalCoords = getDecimalCoords(latitude, latitude_ref, longitude, longitude_ref); - return decimalCoords; + xyCoords = getXyCoords(latitude, latitude_ref, longitude, longitude_ref); + + if (xyCoordsReq = true) { + return xyCoords; + } else { + return decimalCoords; + } } } - //Converts format of coords into decimal coords as required by API for next step + //Converts format of coords into decimal coords as required by MediaWiki API private String getDecimalCoords(String latitude, String latitude_ref, String longitude, String longitude_ref) { double decLatitude, decLongitude; @@ -68,10 +75,37 @@ public class GPSExtractor { decLongitude = 0 - convertToDegree(longitude); } - //Have to return Longitude before Latitude for X/Y conversion. Long = X; Lat = Y - return (String.valueOf(decLongitude) + "|" + String.valueOf(decLatitude)); + String decimalCoords = String.valueOf(decLatitude) + "|" + String.valueOf(decLongitude); + Log.d("Coords", "Latitude and Longitude are " + decimalCoords); + return decimalCoords; } + //Converts format of coords into XY values as required by Quadtree for caching + private String getXyCoords(String latitude, String latitude_ref, String longitude, String longitude_ref) { + + double decLatitude, decLongitude; + + if(latitude_ref.equals("N")){ + decLatitude = convertToDegree(latitude); + } + else{ + decLatitude = 0 - convertToDegree(latitude); + } + + if(longitude_ref.equals("E")){ + decLongitude = convertToDegree(longitude); + } + else{ + decLongitude = 0 - convertToDegree(longitude); + } + + //Have to return Longitude before Latitude for X/Y conversion. Long = X; Lat = Y + String xyCoords = String.valueOf(decLongitude) + "|" + String.valueOf(decLatitude); + Log.d("Coords", "X and Y are " + xyCoords); + return xyCoords; + } + + private double convertToDegree(String stringDMS){ double result; String[] DMS = stringDMS.split(",", 3); 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 eb6afd648..5774141f0 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 @@ -183,7 +183,7 @@ public class ShareActivity //extract the coordinates of image in decimal degrees Log.d("Image", "Calling GPSExtractor"); GPSExtractor imageObj = new GPSExtractor(filePath); - String coords = imageObj.getCoords(); + String coords = imageObj.getCoords(false); if (coords != null) { Log.d("Image", "Coords of image in Long/Lat: " + coords); From f12428814f4f47d7a12524c579570f9f499ce43b Mon Sep 17 00:00:00 2001 From: misaochan Date: Tue, 5 Jan 2016 18:55:28 +1300 Subject: [PATCH 06/44] Implemented xyCoords --- .../fr/free/nrw/commons/upload/GPSExtractor.java | 14 +++++++------- .../fr/free/nrw/commons/upload/ShareActivity.java | 13 ++++++++----- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java b/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java index d05f3a168..e699fe067 100644 --- a/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java +++ b/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java @@ -42,15 +42,15 @@ public class GPSExtractor { longitude = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE); longitude_ref = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF); - Log.d("Image", "Latitude: " + latitude + " " + latitude_ref); - Log.d("Image", "Longitude: " + longitude + " " + longitude_ref); - - decimalCoords = getDecimalCoords(latitude, latitude_ref, longitude, longitude_ref); - xyCoords = getXyCoords(latitude, latitude_ref, longitude, longitude_ref); - - if (xyCoordsReq = true) { + if (xyCoordsReq == true) { + Log.d("Image", "Latitude: " + latitude + " " + latitude_ref); + Log.d("Image", "Longitude: " + longitude + " " + longitude_ref); + xyCoords = getXyCoords(latitude, latitude_ref, longitude, longitude_ref); return xyCoords; } else { + Log.d("Image", "Latitude: " + latitude + " " + latitude_ref); + Log.d("Image", "Longitude: " + longitude + " " + longitude_ref); + decimalCoords = getDecimalCoords(latitude, latitude_ref, longitude, longitude_ref); return decimalCoords; } } 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 5774141f0..3ba4a8b1a 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 @@ -183,19 +183,22 @@ public class ShareActivity //extract the coordinates of image in decimal degrees Log.d("Image", "Calling GPSExtractor"); GPSExtractor imageObj = new GPSExtractor(filePath); - String coords = imageObj.getCoords(false); + //decimalCoords for MediaWiki API, xyCoords for Quadtree + String decimalCoords = imageObj.getCoords(false); + String xyCoords = imageObj.getCoords(true); - if (coords != null) { - Log.d("Image", "Coords of image in Long/Lat: " + coords); + if (decimalCoords != null) { + Log.d("Coords", "Decimal coords of image: " + decimalCoords); + Log.d("Coords", "XY coords of image: " + xyCoords); //TODO: Insert cache query here, only send API request if no cached categories - CacheController cacheObj = new CacheController(this, coords); + CacheController cacheObj = new CacheController(this, xyCoords); cacheObj.getCoords(); //asynchronous calls to MediaWiki Commons API to match image coords with nearby Commons categories MwVolleyApi apiCall = new MwVolleyApi(this); - apiCall.request(coords); + apiCall.request(decimalCoords); } } From 651fd61189cb5219a1c1f8026529bef03e49a534 Mon Sep 17 00:00:00 2001 From: misaochan Date: Tue, 5 Jan 2016 19:10:44 +1300 Subject: [PATCH 07/44] Fixed x and y, should be ints not string for quadtree --- .../nrw/commons/caching/CacheController.java | 20 +++++++++++++------ .../free/nrw/commons/upload/GPSExtractor.java | 11 +++++++++- .../nrw/commons/upload/ShareActivity.java | 10 +++++----- 3 files changed, 29 insertions(+), 12 deletions(-) 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 1f2244bf8..6a125e8c4 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 @@ -9,15 +9,23 @@ import android.util.Log; public class CacheController { private Context context; - private String coords; + private double decLongitude; + private double decLatitude; + private QuadTree quadTree; + private String category = "test"; - public CacheController(Context context, String coords) { + public CacheController(Context context, double decLongitude, double decLatitude) { this.context = context; - this.coords = coords; + this.decLongitude = decLongitude; + this.decLatitude = decLatitude; + } - public String getCoords() { - Log.d("Cache", "Coords passed to cache: " + coords); - return coords; + + public void callQuadTree() { + quadTree = new QuadTree(-180, -90, +180, +90); + Log.d("Cache", "New QuadTree created"); + Log.d("Cache", "X (longitude) value: " + decLongitude + ", Y (latitude) value: " + decLatitude); + quadTree.set(decLongitude, decLatitude, category); } } diff --git a/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java b/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java index e699fe067..28685353d 100644 --- a/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java +++ b/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java @@ -9,6 +9,7 @@ import java.io.IOException; public class GPSExtractor { private String filePath; + private double decLatitude, decLongitude; public GPSExtractor(String filePath){ this.filePath = filePath; @@ -56,10 +57,18 @@ public class GPSExtractor { } } + public double getDecLatitude() { + return decLatitude; + } + + public double getDecLongitude() { + return decLongitude; + } + //Converts format of coords into decimal coords as required by MediaWiki API private String getDecimalCoords(String latitude, String latitude_ref, String longitude, String longitude_ref) { - double decLatitude, decLongitude; + if(latitude_ref.equals("N")){ decLatitude = convertToDegree(latitude); 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 3ba4a8b1a..8e6b6ae75 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 @@ -185,16 +185,16 @@ public class ShareActivity GPSExtractor imageObj = new GPSExtractor(filePath); //decimalCoords for MediaWiki API, xyCoords for Quadtree String decimalCoords = imageObj.getCoords(false); - String xyCoords = imageObj.getCoords(true); + + double decLongitude = imageObj.getDecLongitude(); + double decLatitude = imageObj.getDecLatitude(); if (decimalCoords != null) { Log.d("Coords", "Decimal coords of image: " + decimalCoords); - Log.d("Coords", "XY coords of image: " + xyCoords); //TODO: Insert cache query here, only send API request if no cached categories - CacheController cacheObj = new CacheController(this, xyCoords); - cacheObj.getCoords(); - + CacheController cacheObj = new CacheController(this, decLongitude, decLatitude); + cacheObj.callQuadTree(); //asynchronous calls to MediaWiki Commons API to match image coords with nearby Commons categories MwVolleyApi apiCall = new MwVolleyApi(this); From 74dcd9df8f64e90182f568da3f34649692f240c7 Mon Sep 17 00:00:00 2001 From: misaochan Date: Tue, 5 Jan 2016 19:13:15 +1300 Subject: [PATCH 08/44] Cleaned up unnecessary code --- .../free/nrw/commons/upload/GPSExtractor.java | 45 +++---------------- .../nrw/commons/upload/ShareActivity.java | 2 +- 2 files changed, 8 insertions(+), 39 deletions(-) diff --git a/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java b/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java index 28685353d..4de87d42a 100644 --- a/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java +++ b/commons/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java @@ -16,7 +16,7 @@ public class GPSExtractor { } //Extract GPS coords of image - public String getCoords(boolean xyCoordsReq) { + public String getCoords() { ExifInterface exif; String latitude = ""; @@ -24,7 +24,6 @@ public class GPSExtractor { String latitude_ref = ""; String longitude_ref = ""; String decimalCoords = ""; - String xyCoords = ""; try { exif = new ExifInterface(filePath); @@ -43,17 +42,12 @@ public class GPSExtractor { longitude = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE); longitude_ref = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF); - if (xyCoordsReq == true) { - Log.d("Image", "Latitude: " + latitude + " " + latitude_ref); - Log.d("Image", "Longitude: " + longitude + " " + longitude_ref); - xyCoords = getXyCoords(latitude, latitude_ref, longitude, longitude_ref); - return xyCoords; - } else { - Log.d("Image", "Latitude: " + latitude + " " + latitude_ref); - Log.d("Image", "Longitude: " + longitude + " " + longitude_ref); - decimalCoords = getDecimalCoords(latitude, latitude_ref, longitude, longitude_ref); - return decimalCoords; - } + + Log.d("Image", "Latitude: " + latitude + " " + latitude_ref); + Log.d("Image", "Longitude: " + longitude + " " + longitude_ref); + decimalCoords = getDecimalCoords(latitude, latitude_ref, longitude, longitude_ref); + return decimalCoords; + } } @@ -89,31 +83,6 @@ public class GPSExtractor { return decimalCoords; } - //Converts format of coords into XY values as required by Quadtree for caching - private String getXyCoords(String latitude, String latitude_ref, String longitude, String longitude_ref) { - - double decLatitude, decLongitude; - - if(latitude_ref.equals("N")){ - decLatitude = convertToDegree(latitude); - } - else{ - decLatitude = 0 - convertToDegree(latitude); - } - - if(longitude_ref.equals("E")){ - decLongitude = convertToDegree(longitude); - } - else{ - decLongitude = 0 - convertToDegree(longitude); - } - - //Have to return Longitude before Latitude for X/Y conversion. Long = X; Lat = Y - String xyCoords = String.valueOf(decLongitude) + "|" + String.valueOf(decLatitude); - Log.d("Coords", "X and Y are " + xyCoords); - return xyCoords; - } - private double convertToDegree(String stringDMS){ double result; 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 8e6b6ae75..e06a70e28 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 @@ -184,7 +184,7 @@ public class ShareActivity Log.d("Image", "Calling GPSExtractor"); GPSExtractor imageObj = new GPSExtractor(filePath); //decimalCoords for MediaWiki API, xyCoords for Quadtree - String decimalCoords = imageObj.getCoords(false); + String decimalCoords = imageObj.getCoords(); double decLongitude = imageObj.getDecLongitude(); double decLatitude = imageObj.getDecLatitude(); From e311753988f529620ef8e544d6b04a3da121722d Mon Sep 17 00:00:00 2001 From: misaochan Date: Tue, 5 Jan 2016 19:24:43 +1300 Subject: [PATCH 09/44] Setting up cacheCategory() and findCategory() --- .../nrw/commons/caching/CacheController.java | 24 +++++++++++++++---- .../nrw/commons/upload/ShareActivity.java | 6 +++-- 2 files changed, 24 insertions(+), 6 deletions(-) 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 6a125e8c4..f7cd202cb 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 @@ -3,6 +3,9 @@ package fr.free.nrw.commons.caching; import android.content.Context; import android.util.Log; +import java.util.ArrayList; +import java.util.List; + /** * Created by misao on 05-Jan-16. */ @@ -12,20 +15,33 @@ public class CacheController { private double decLongitude; private double decLatitude; private QuadTree quadTree; - private String category = "test"; + private List categoryList; public CacheController(Context context, double decLongitude, double decLatitude) { this.context = context; this.decLongitude = decLongitude; this.decLatitude = decLatitude; - } - public void callQuadTree() { + public void initQuadTree() { quadTree = new QuadTree(-180, -90, +180, +90); Log.d("Cache", "New QuadTree created"); Log.d("Cache", "X (longitude) value: " + decLongitude + ", Y (latitude) value: " + decLatitude); - quadTree.set(decLongitude, decLatitude, category); + } + + + public void cacheCategory() { + + //TODO: Remove later, only setting categories manually for debugging + categoryList = new ArrayList(); + categoryList.add("UK"); + categoryList.add("US"); + quadTree.set(decLongitude, decLatitude, categoryList); + } + + public void findCategory() { + //TODO: Convert decLatitude and decLongitude to a range + quadTree.searchWithin(final double xmin, final double ymin, final double xmax, final double ymax); } } 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 e06a70e28..94e60627f 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 @@ -192,13 +192,15 @@ public class ShareActivity if (decimalCoords != null) { Log.d("Coords", "Decimal coords of image: " + decimalCoords); - //TODO: Insert cache query here, only send API request if no cached categories CacheController cacheObj = new CacheController(this, decLongitude, decLatitude); - cacheObj.callQuadTree(); + cacheObj.initQuadTree(); + //TODO: If no categories found from cache in that area, call MW API //asynchronous calls to MediaWiki Commons API to match image coords with nearby Commons categories MwVolleyApi apiCall = new MwVolleyApi(this); apiCall.request(decimalCoords); + + //TODO: Set Category object for this point } } From b0f8af7b37aa027a072cc2a87bf49179269ff8d2 Mon Sep 17 00:00:00 2001 From: misaochan Date: Tue, 5 Jan 2016 19:40:00 +1300 Subject: [PATCH 10/44] Dummy categories working --- .../nrw/commons/caching/CacheController.java | 27 +++++++++++++------ .../nrw/commons/upload/ShareActivity.java | 2 ++ 2 files changed, 21 insertions(+), 8 deletions(-) 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 f7cd202cb..205fb458a 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 @@ -12,22 +12,23 @@ import java.util.List; public class CacheController { private Context context; - private double decLongitude; - private double decLatitude; + private double x; + private double y; private QuadTree quadTree; private List categoryList; + private Point[] pointsFound; public CacheController(Context context, double decLongitude, double decLatitude) { this.context = context; - this.decLongitude = decLongitude; - this.decLatitude = decLatitude; + x = decLongitude; + y = decLatitude; } public void initQuadTree() { quadTree = new QuadTree(-180, -90, +180, +90); Log.d("Cache", "New QuadTree created"); - Log.d("Cache", "X (longitude) value: " + decLongitude + ", Y (latitude) value: " + decLatitude); + Log.d("Cache", "X (longitude) value: " + x + ", Y (latitude) value: " + y); } @@ -37,11 +38,21 @@ public class CacheController { categoryList = new ArrayList(); categoryList.add("UK"); categoryList.add("US"); - quadTree.set(decLongitude, decLatitude, categoryList); + quadTree.set(x, y, categoryList); } public void findCategory() { - //TODO: Convert decLatitude and decLongitude to a range - quadTree.searchWithin(final double xmin, final double ymin, final double xmax, final double ymax); + //TODO: Convert decLatitude and decLongitude to a range with proper formula, for testing just use 10 + pointsFound = quadTree.searchWithin(x-10, y-10, x+10, y+10); + Log.d("Cache", "Points found: " + pointsFound.toString()); + double x; + Object cat = null; + //TODO: This does not truly iterate, just for testing. In future probably need to store results in Array + for (Point point: pointsFound) { + x = point.getX(); + cat = point.getValue(); + } + Log.d("Cache", "Categories found: " + cat.toString()); + } } 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 94e60627f..303777834 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 @@ -194,6 +194,8 @@ public class ShareActivity CacheController cacheObj = new CacheController(this, decLongitude, decLatitude); cacheObj.initQuadTree(); + cacheObj.cacheCategory(); + cacheObj.findCategory(); //TODO: If no categories found from cache in that area, call MW API //asynchronous calls to MediaWiki Commons API to match image coords with nearby Commons categories From 5f44ba2975566aa4171f26981376e8142e0dd3ac Mon Sep 17 00:00:00 2001 From: misaochan Date: Wed, 6 Jan 2016 15:42:49 +1300 Subject: [PATCH 11/44] Minor changes --- .../main/java/fr/free/nrw/commons/caching/CacheController.java | 3 --- 1 file changed, 3 deletions(-) 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 205fb458a..5a600e7ec 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 @@ -6,9 +6,6 @@ import android.util.Log; import java.util.ArrayList; import java.util.List; -/** - * Created by misao on 05-Jan-16. - */ public class CacheController { private Context context; From a527425baa389cad9fd5da655917766bb5e7e8cc Mon Sep 17 00:00:00 2001 From: misaochan Date: Wed, 6 Jan 2016 16:11:57 +1300 Subject: [PATCH 12/44] Attempting real categories cache --- .../nrw/commons/caching/CacheController.java | 47 ++++++++++++------- .../free/nrw/commons/upload/MwVolleyApi.java | 2 + .../nrw/commons/upload/ShareActivity.java | 12 +++-- 3 files changed, 40 insertions(+), 21 deletions(-) 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 5a600e7ec..8bbefd960 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 @@ -6,6 +6,8 @@ import android.util.Log; import java.util.ArrayList; import java.util.List; +import fr.free.nrw.commons.upload.MwVolleyApi; + public class CacheController { private Context context; @@ -15,15 +17,15 @@ public class CacheController { private List categoryList; private Point[] pointsFound; - public CacheController(Context context, double decLongitude, double decLatitude) { - this.context = context; - x = decLongitude; - y = decLatitude; + public CacheController() { + } - public void initQuadTree() { + public void initQuadTree(double decLongitude, double decLatitude) { quadTree = new QuadTree(-180, -90, +180, +90); + x = decLongitude; + y = decLatitude; Log.d("Cache", "New QuadTree created"); Log.d("Cache", "X (longitude) value: " + x + ", Y (latitude) value: " + y); } @@ -31,25 +33,36 @@ public class CacheController { public void cacheCategory() { - //TODO: Remove later, only setting categories manually for debugging - categoryList = new ArrayList(); - categoryList.add("UK"); - categoryList.add("US"); + if (MwVolleyApi.GpsCatExists.getGpsCatExists() == true) { + categoryList = new ArrayList(MwVolleyApi.getGpsCat()); + Log.d("Cache", "Categories being cached: " + categoryList); + } else { + Log.d("Cache", "No categories found, so no categories cached"); + } + + //categoryList = new ArrayList(); + //categoryList.add("UK"); + //categoryList.add("US"); quadTree.set(x, y, categoryList); } public void findCategory() { //TODO: Convert decLatitude and decLongitude to a range with proper formula, for testing just use 10 pointsFound = quadTree.searchWithin(x-10, y-10, x+10, y+10); - Log.d("Cache", "Points found: " + pointsFound.toString()); - double x; - Object cat = null; - //TODO: This does not truly iterate, just for testing. In future probably need to store results in Array - for (Point point: pointsFound) { - x = point.getX(); - cat = point.getValue(); + Log.d("Cache", "Points found in quadtree: " + pointsFound); + + if (pointsFound != null) { + double x; + Object cat = null; + //TODO: This does not truly iterate, just for testing. In future probably need to store results in Array + for (Point point : pointsFound) { + x = point.getX(); + cat = point.getValue(); + } + Log.d("Cache", "Categories found: " + cat.toString()); + } else { + Log.d("Cache", "No categories found in cache"); } - Log.d("Cache", "Categories found: " + cat.toString()); } } diff --git a/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java b/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java index e3c69d2b4..dd74faf20 100644 --- a/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java +++ b/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java @@ -24,6 +24,8 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import fr.free.nrw.commons.caching.CacheController; + public class MwVolleyApi { private static RequestQueue REQUEST_QUEUE; 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 303777834..86ac2eca3 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 @@ -47,6 +47,8 @@ public class ShareActivity private UploadController uploadController; + private CacheController cacheObj; + public ShareActivity() { super(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE); } @@ -54,6 +56,8 @@ public class ShareActivity public void uploadActionInitiated(String title, String description) { Toast startingToast = Toast.makeText(getApplicationContext(), R.string.uploading_started, Toast.LENGTH_LONG); startingToast.show(); + //Has to be called after apiCall.request() + cacheObj.cacheCategory(); uploadController.startUpload(title, mediaUri, description, mimeType, source, new UploadController.ContributionUploadProgress() { public void onUploadStarted(Contribution contribution) { ShareActivity.this.contribution = contribution; @@ -179,6 +183,8 @@ public class ShareActivity FilePathConverter uriObj = new FilePathConverter(this, mediaUri); String filePath = uriObj.getFilePath(); + cacheObj = new CacheController(); + if (filePath != null) { //extract the coordinates of image in decimal degrees Log.d("Image", "Calling GPSExtractor"); @@ -191,10 +197,8 @@ public class ShareActivity if (decimalCoords != null) { Log.d("Coords", "Decimal coords of image: " + decimalCoords); + cacheObj.initQuadTree(decLongitude, decLatitude); - CacheController cacheObj = new CacheController(this, decLongitude, decLatitude); - cacheObj.initQuadTree(); - cacheObj.cacheCategory(); cacheObj.findCategory(); //TODO: If no categories found from cache in that area, call MW API @@ -202,7 +206,7 @@ public class ShareActivity MwVolleyApi apiCall = new MwVolleyApi(this); apiCall.request(decimalCoords); - //TODO: Set Category object for this point + } } From 920da3b5926fafec007e8a78aeda7241db9cdb2e Mon Sep 17 00:00:00 2001 From: misaochan Date: Wed, 6 Jan 2016 16:35:03 +1300 Subject: [PATCH 13/44] Worked on findCategory() loop --- .../nrw/commons/caching/CacheController.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) 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 8bbefd960..2152f780d 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 @@ -47,19 +47,25 @@ public class CacheController { } public void findCategory() { + //TODO: Convert decLatitude and decLongitude to a range with proper formula, for testing just use 10 pointsFound = quadTree.searchWithin(x-10, y-10, x+10, y+10); + ArrayList catList = new ArrayList(); Log.d("Cache", "Points found in quadtree: " + pointsFound); - if (pointsFound != null) { - double x; - Object cat = null; - //TODO: This does not truly iterate, just for testing. In future probably need to store results in Array + if (pointsFound.length != 0) { + + Log.d("Cache", "Entering for loop"); + int index = 0; for (Point point : pointsFound) { - x = point.getX(); - cat = point.getValue(); + Log.d("Cache", "Point: " + point.toString()); + Object cat = point.getValue(); + Log.d("Cache", "Cat: " + cat); + catList.add(index, cat); + index++; } - Log.d("Cache", "Categories found: " + cat.toString()); + + Log.d("Cache", "Categories found: " + catList.toString()); } else { Log.d("Cache", "No categories found in cache"); } From 3d19d6be3171f206f36dfca195cb1ecbd57b52ef Mon Sep 17 00:00:00 2001 From: misaochan Date: Wed, 6 Jan 2016 16:55:38 +1300 Subject: [PATCH 14/44] Added global singleton to keep Quadtree data across activity lifecycle --- .../java/fr/free/nrw/commons/CacheApplication.java | 11 +++++++++++ .../fr/free/nrw/commons/caching/CacheController.java | 5 ++--- .../fr/free/nrw/commons/upload/ShareActivity.java | 9 ++++++--- 3 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 commons/src/main/java/fr/free/nrw/commons/CacheApplication.java diff --git a/commons/src/main/java/fr/free/nrw/commons/CacheApplication.java b/commons/src/main/java/fr/free/nrw/commons/CacheApplication.java new file mode 100644 index 000000000..edd279d6f --- /dev/null +++ b/commons/src/main/java/fr/free/nrw/commons/CacheApplication.java @@ -0,0 +1,11 @@ +package fr.free.nrw.commons; + +import android.app.Application; + +import fr.free.nrw.commons.caching.CacheController; + + +public class CacheApplication extends Application +{ + public CacheController data = new CacheController(); +} \ No newline at end of file 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 2152f780d..453b8d66f 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,12 +18,11 @@ public class CacheController { private Point[] pointsFound; public CacheController() { - + quadTree = new QuadTree(-180, -90, +180, +90); } - public void initQuadTree(double decLongitude, double decLatitude) { - quadTree = new QuadTree(-180, -90, +180, +90); + public void setQtPoint(double decLongitude, double decLatitude) { x = decLongitude; y = decLatitude; Log.d("Cache", "New QuadTree created"); 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 86ac2eca3..651b3be36 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 @@ -183,7 +183,10 @@ public class ShareActivity FilePathConverter uriObj = new FilePathConverter(this, mediaUri); String filePath = uriObj.getFilePath(); - cacheObj = new CacheController(); + //Using global singleton to get CacheController to last longer than the activity lifecycle + CacheApplication cacheObj = ((CacheApplication)this.getApplication()); + + if (filePath != null) { //extract the coordinates of image in decimal degrees @@ -197,9 +200,9 @@ public class ShareActivity if (decimalCoords != null) { Log.d("Coords", "Decimal coords of image: " + decimalCoords); - cacheObj.initQuadTree(decLongitude, decLatitude); + cacheObj.data.setQtPoint(decLongitude, decLatitude); - cacheObj.findCategory(); + cacheObj.data.findCategory(); //TODO: If no categories found from cache in that area, call MW API //asynchronous calls to MediaWiki Commons API to match image coords with nearby Commons categories From 0c15a3515bb18646f353ebb297a9d5e3ebb083e5 Mon Sep 17 00:00:00 2001 From: misaochan Date: Wed, 6 Jan 2016 17:15:15 +1300 Subject: [PATCH 15/44] Used CommonsApplication singleton to persist cache data --- .../java/fr/free/nrw/commons/CacheApplication.java | 11 ----------- .../java/fr/free/nrw/commons/CommonsApplication.java | 7 +++++++ .../fr/free/nrw/commons/upload/ShareActivity.java | 10 +++++----- 3 files changed, 12 insertions(+), 16 deletions(-) delete mode 100644 commons/src/main/java/fr/free/nrw/commons/CacheApplication.java diff --git a/commons/src/main/java/fr/free/nrw/commons/CacheApplication.java b/commons/src/main/java/fr/free/nrw/commons/CacheApplication.java deleted file mode 100644 index edd279d6f..000000000 --- a/commons/src/main/java/fr/free/nrw/commons/CacheApplication.java +++ /dev/null @@ -1,11 +0,0 @@ -package fr.free.nrw.commons; - -import android.app.Application; - -import fr.free.nrw.commons.caching.CacheController; - - -public class CacheApplication extends Application -{ - public CacheController data = new CacheController(); -} \ No newline at end of file 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 ea8f0c210..a32eb98f0 100644 --- a/commons/src/main/java/fr/free/nrw/commons/CommonsApplication.java +++ b/commons/src/main/java/fr/free/nrw/commons/CommonsApplication.java @@ -28,6 +28,8 @@ import org.apache.http.params.BasicHttpParams; import org.mediawiki.api.*; import org.apache.http.impl.client.*; import org.apache.http.params.CoreProtocolPNames; + +import fr.free.nrw.commons.caching.CacheController; import fr.free.nrw.commons.data.*; import com.android.volley.toolbox.*; @@ -67,6 +69,8 @@ public class CommonsApplication extends Application { public RequestQueue volleyQueue; + public CacheController cacheData; + public static AbstractHttpClient createHttpClient() { BasicHttpParams params = new BasicHttpParams(); SchemeRegistry schemeRegistry = new SchemeRegistry(); @@ -143,8 +147,11 @@ public class CommonsApplication extends Application { return bitmapSize / 1024; } }; + + } + cacheData = new CacheController(); DiskBasedCache cache = new DiskBasedCache(getCacheDir(), 16 * 1024 * 1024); volleyQueue = new RequestQueue(cache, new BasicNetwork(new HurlStack())); volleyQueue.start(); 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 651b3be36..451eaa1dd 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 @@ -47,7 +47,7 @@ public class ShareActivity private UploadController uploadController; - private CacheController cacheObj; + private CommonsApplication cacheObj; public ShareActivity() { super(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE); @@ -57,7 +57,7 @@ public class ShareActivity Toast startingToast = Toast.makeText(getApplicationContext(), R.string.uploading_started, Toast.LENGTH_LONG); startingToast.show(); //Has to be called after apiCall.request() - cacheObj.cacheCategory(); + cacheObj.cacheData.cacheCategory(); uploadController.startUpload(title, mediaUri, description, mimeType, source, new UploadController.ContributionUploadProgress() { public void onUploadStarted(Contribution contribution) { ShareActivity.this.contribution = contribution; @@ -184,7 +184,7 @@ public class ShareActivity String filePath = uriObj.getFilePath(); //Using global singleton to get CacheController to last longer than the activity lifecycle - CacheApplication cacheObj = ((CacheApplication)this.getApplication()); + cacheObj = ((CommonsApplication)this.getApplication()); @@ -200,9 +200,9 @@ public class ShareActivity if (decimalCoords != null) { Log.d("Coords", "Decimal coords of image: " + decimalCoords); - cacheObj.data.setQtPoint(decLongitude, decLatitude); + cacheObj.cacheData.setQtPoint(decLongitude, decLatitude); - cacheObj.data.findCategory(); + cacheObj.cacheData.findCategory(); //TODO: If no categories found from cache in that area, call MW API //asynchronous calls to MediaWiki Commons API to match image coords with nearby Commons categories From c8773101d5d171ebca7940199f99efd19db24034 Mon Sep 17 00:00:00 2001 From: misaochan Date: Wed, 6 Jan 2016 17:30:18 +1300 Subject: [PATCH 16/44] Adding documentation --- .../java/fr/free/nrw/commons/caching/CacheController.java | 6 +++--- .../main/java/fr/free/nrw/commons/upload/ShareActivity.java | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) 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 453b8d66f..6e1f8e0bc 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 @@ -57,14 +57,14 @@ public class CacheController { Log.d("Cache", "Entering for loop"); int index = 0; for (Point point : pointsFound) { - Log.d("Cache", "Point: " + point.toString()); + Log.d("Cache", "Nearby point: " + point.toString()); Object cat = point.getValue(); - Log.d("Cache", "Cat: " + cat); + Log.d("Cache", "Nearby cat: " + cat); catList.add(index, cat); index++; } - Log.d("Cache", "Categories found: " + catList.toString()); + Log.d("Cache", "Categories found in cache: " + catList.toString()); } else { Log.d("Cache", "No categories found in cache"); } 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 451eaa1dd..d9b783bfb 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 @@ -204,7 +204,10 @@ public class ShareActivity cacheObj.cacheData.findCategory(); + //TODO: If categories found from cache in that area, skip API call and display those categories instead + //TODO: If no categories found from cache in that area, call MW API + //asynchronous calls to MediaWiki Commons API to match image coords with nearby Commons categories MwVolleyApi apiCall = new MwVolleyApi(this); apiCall.request(decimalCoords); From 3b1a8e2c97badfcaa349ac1d18c5d603340989da Mon Sep 17 00:00:00 2001 From: misaochan Date: Wed, 6 Jan 2016 18:08:05 +1300 Subject: [PATCH 17/44] Changed confusing names --- .../nrw/commons/caching/CacheController.java | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) 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 6e1f8e0bc..038ed1816 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 @@ -14,7 +14,6 @@ public class CacheController { private double x; private double y; private QuadTree quadTree; - private List categoryList; private Point[] pointsFound; public CacheController() { @@ -32,26 +31,24 @@ public class CacheController { public void cacheCategory() { + List pointCatList = new ArrayList(); if (MwVolleyApi.GpsCatExists.getGpsCatExists() == true) { - categoryList = new ArrayList(MwVolleyApi.getGpsCat()); - Log.d("Cache", "Categories being cached: " + categoryList); + pointCatList.addAll(MwVolleyApi.getGpsCat()); + Log.d("Cache", "Categories being cached: " + pointCatList); } else { Log.d("Cache", "No categories found, so no categories cached"); } - - //categoryList = new ArrayList(); - //categoryList.add("UK"); - //categoryList.add("US"); - quadTree.set(x, y, categoryList); + quadTree.set(x, y, pointCatList); } public void findCategory() { //TODO: Convert decLatitude and decLongitude to a range with proper formula, for testing just use 10 pointsFound = quadTree.searchWithin(x-10, y-10, x+10, y+10); - ArrayList catList = new ArrayList(); + ArrayList displayCatList = new ArrayList(); Log.d("Cache", "Points found in quadtree: " + pointsFound); + //TODO: Potentially flatten catList and iterate thru it to convert into unique Set? But currently just displays one point... if (pointsFound.length != 0) { Log.d("Cache", "Entering for loop"); @@ -60,7 +57,7 @@ public class CacheController { Log.d("Cache", "Nearby point: " + point.toString()); Object cat = point.getValue(); Log.d("Cache", "Nearby cat: " + cat); - catList.add(index, cat); + displayCatList.add(index, cat); index++; } From 4e5fb1b1ae2ca66db41a1d2d69d605b85362a226 Mon Sep 17 00:00:00 2001 From: misaochan Date: Wed, 6 Jan 2016 18:08:30 +1300 Subject: [PATCH 18/44] Minor fix --- .../main/java/fr/free/nrw/commons/caching/CacheController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 038ed1816..055d3773f 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 @@ -61,7 +61,7 @@ public class CacheController { index++; } - Log.d("Cache", "Categories found in cache: " + catList.toString()); + Log.d("Cache", "Categories found in cache: " + displayCatList.toString()); } else { Log.d("Cache", "No categories found in cache"); } From a13eb88cd3ce8ddc12a5b80d888dbf0de7f85efa Mon Sep 17 00:00:00 2001 From: misaochan Date: Wed, 6 Jan 2016 18:50:46 +1300 Subject: [PATCH 19/44] findCategory() returns List --- .../fr/free/nrw/commons/caching/CacheController.java | 9 +++++---- .../java/fr/free/nrw/commons/upload/ShareActivity.java | 3 ++- 2 files changed, 7 insertions(+), 5 deletions(-) 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 055d3773f..fe078ce04 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 @@ -41,11 +41,11 @@ public class CacheController { quadTree.set(x, y, pointCatList); } - public void findCategory() { + public List findCategory() { //TODO: Convert decLatitude and decLongitude to a range with proper formula, for testing just use 10 pointsFound = quadTree.searchWithin(x-10, y-10, x+10, y+10); - ArrayList displayCatList = new ArrayList(); + List displayCatList = new ArrayList(); Log.d("Cache", "Points found in quadtree: " + pointsFound); //TODO: Potentially flatten catList and iterate thru it to convert into unique Set? But currently just displays one point... @@ -60,11 +60,12 @@ public class CacheController { displayCatList.add(index, cat); index++; } - Log.d("Cache", "Categories found in cache: " + displayCatList.toString()); } else { Log.d("Cache", "No categories found in cache"); } - + return displayCatList; } + + public } 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 d9b783bfb..8414c47d6 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 @@ -23,6 +23,7 @@ import fr.free.nrw.commons.modifications.ModificationsContentProvider; import fr.free.nrw.commons.modifications.ModifierSequence; import java.util.ArrayList; +import java.util.List; public class ShareActivity @@ -202,7 +203,7 @@ public class ShareActivity Log.d("Coords", "Decimal coords of image: " + decimalCoords); cacheObj.cacheData.setQtPoint(decLongitude, decLatitude); - cacheObj.cacheData.findCategory(); + List displayCatList = cacheObj.cacheData.findCategory(); //TODO: If categories found from cache in that area, skip API call and display those categories instead From 16606a91215781702e373a9db55a061f1c6f5450 Mon Sep 17 00:00:00 2001 From: misaochan Date: Wed, 6 Jan 2016 19:01:46 +1300 Subject: [PATCH 20/44] Added coord offset conversion for searchWithin() --- .../nrw/commons/caching/CacheController.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) 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 fe078ce04..000db5125 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 @@ -15,6 +15,7 @@ public class CacheController { private double y; private QuadTree quadTree; private Point[] pointsFound; + private double xMinus, xPlus, yMinus, yPlus; public CacheController() { quadTree = new QuadTree(-180, -90, +180, +90); @@ -44,7 +45,8 @@ public class CacheController { public List findCategory() { //TODO: Convert decLatitude and decLongitude to a range with proper formula, for testing just use 10 - pointsFound = quadTree.searchWithin(x-10, y-10, x+10, y+10); + convertCoordRange(); + pointsFound = quadTree.searchWithin(xMinus, yMinus, xPlus, yPlus); List displayCatList = new ArrayList(); Log.d("Cache", "Points found in quadtree: " + pointsFound); @@ -67,5 +69,27 @@ public class CacheController { return displayCatList; } - public + 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)); + + //OffsetPosition, decimal degrees + yPlus = lat + dLat * 180/Math.PI; + yMinus = lat - dLat * 180/Math.PI; + xPlus = lon + dLon * 180/Math.PI; + xMinus = lon - dLon * 180/Math.PI; + Log.d("Cache", "Search within: xMinus=" + xMinus + ", yMinus=" + yMinus + ", xPlus=" + xPlus + ", yPlus=" + yPlus); + } } From 33ec4e50f362aae0413acab03c106edc9860ea54 Mon Sep 17 00:00:00 2001 From: misaochan Date: Wed, 6 Jan 2016 19:04:30 +1300 Subject: [PATCH 21/44] Added documentation --- .../main/java/fr/free/nrw/commons/caching/CacheController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 000db5125..30c03ec97 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 @@ -44,7 +44,7 @@ public class CacheController { public List findCategory() { - //TODO: Convert decLatitude and decLongitude to a range with proper formula, for testing just use 10 + //Convert decLatitude and decLongitude to a coordinate offset range convertCoordRange(); pointsFound = quadTree.searchWithin(xMinus, yMinus, xPlus, yPlus); List displayCatList = new ArrayList(); From 1efc6e38ed821435b2a46976c4d24a07d3811a63 Mon Sep 17 00:00:00 2001 From: misaochan Date: Wed, 6 Jan 2016 19:07:21 +1300 Subject: [PATCH 22/44] Tidied CacheController code --- .../fr/free/nrw/commons/caching/CacheController.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) 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 30c03ec97..acbc28106 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 @@ -10,9 +10,7 @@ import fr.free.nrw.commons.upload.MwVolleyApi; public class CacheController { - private Context context; - private double x; - private double y; + private double x, y; private QuadTree quadTree; private Point[] pointsFound; private double xMinus, xPlus, yMinus, yPlus; @@ -21,7 +19,6 @@ public class CacheController { quadTree = new QuadTree(-180, -90, +180, +90); } - public void setQtPoint(double decLongitude, double decLatitude) { x = decLongitude; y = decLatitude; @@ -29,7 +26,6 @@ public class CacheController { Log.d("Cache", "X (longitude) value: " + x + ", Y (latitude) value: " + y); } - public void cacheCategory() { List pointCatList = new ArrayList(); @@ -44,7 +40,7 @@ public class CacheController { public List findCategory() { - //Convert decLatitude and decLongitude to a coordinate offset range + //Convert decLatitude and decLongitude to a coordinate offset range convertCoordRange(); pointsFound = quadTree.searchWithin(xMinus, yMinus, xPlus, yPlus); List displayCatList = new ArrayList(); @@ -76,11 +72,9 @@ public class CacheController { //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)); From 0a33dc5daeb2de7bd466baf642f331fe0e5dcb72 Mon Sep 17 00:00:00 2001 From: misaochan Date: Thu, 7 Jan 2016 15:55:22 +1300 Subject: [PATCH 23/44] Documentation --- .../src/main/java/fr/free/nrw/commons/CommonsApplication.java | 2 ++ 1 file changed, 2 insertions(+) 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 a32eb98f0..c2d4eb525 100644 --- a/commons/src/main/java/fr/free/nrw/commons/CommonsApplication.java +++ b/commons/src/main/java/fr/free/nrw/commons/CommonsApplication.java @@ -151,7 +151,9 @@ public class CommonsApplication extends Application { } + //For caching area -> categories cacheData = new CacheController(); + DiskBasedCache cache = new DiskBasedCache(getCacheDir(), 16 * 1024 * 1024); volleyQueue = new RequestQueue(cache, new BasicNetwork(new HurlStack())); volleyQueue.start(); From 85f0ae2a154b271d5bc1b98e1415736498c119f8 Mon Sep 17 00:00:00 2001 From: misaochan Date: Thu, 7 Jan 2016 16:09:35 +1300 Subject: [PATCH 24/44] Added if cache found logic --- .../free/nrw/commons/upload/MwVolleyApi.java | 9 ++++++-- .../nrw/commons/upload/ShareActivity.java | 22 ++++++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java b/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java index dd74faf20..29e8c9d24 100644 --- a/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java +++ b/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java @@ -34,6 +34,7 @@ public class MwVolleyApi { private String coordsLog; protected static Set categorySet; + private static List categoryList; private static final String MWURL = "https://commons.wikimedia.org/"; @@ -44,8 +45,12 @@ public class MwVolleyApi { //To get the list of categories for display public static List getGpsCat() { - List list = new ArrayList(categorySet); - return list; + categoryList = new ArrayList(categorySet); + return categoryList; + } + + public static void setGpsCat(List cachedList) { + categoryList = new ArrayList(cachedList); } 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 8414c47d6..cdb780ab8 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 @@ -57,8 +57,11 @@ public class ShareActivity public void uploadActionInitiated(String title, String description) { Toast startingToast = Toast.makeText(getApplicationContext(), R.string.uploading_started, Toast.LENGTH_LONG); startingToast.show(); + //Has to be called after apiCall.request() cacheObj.cacheData.cacheCategory(); + Log.d("Cache", "Cache the categories found"); + uploadController.startUpload(title, mediaUri, description, mimeType, source, new UploadController.ContributionUploadProgress() { public void onUploadStarted(Contribution contribution) { ShareActivity.this.contribution = contribution; @@ -203,16 +206,19 @@ public class ShareActivity Log.d("Coords", "Decimal coords of image: " + decimalCoords); cacheObj.cacheData.setQtPoint(decLongitude, decLatitude); + MwVolleyApi apiCall = new MwVolleyApi(this); + List displayCatList = cacheObj.cacheData.findCategory(); - //TODO: If categories found from cache in that area, skip API call and display those categories instead - - //TODO: If no categories found from cache in that area, call MW API - - //asynchronous calls to MediaWiki Commons API to match image coords with nearby Commons categories - MwVolleyApi apiCall = new MwVolleyApi(this); - apiCall.request(decimalCoords); - + //if no categories found in cache, call MW API to match image coords with nearby Commons categories + if (displayCatList.size() == 0) { + apiCall.request(decimalCoords); + Log.d("Cache", "displayCatList size 0, calling MWAPI"); + } else { + //TODO: Set categoryList in MwVolleyApi + MwVolleyApi.setGpsCat(displayCatList); + Log.d("Cache", "Cache found, setting categoryList in MwVolleyApi to " + displayCatList.toString()); + } } } From 46af717e40c1cc2befe8fa938bb2bb91576b7ca4 Mon Sep 17 00:00:00 2001 From: misaochan Date: Thu, 7 Jan 2016 16:14:16 +1300 Subject: [PATCH 25/44] Docs --- .../main/java/fr/free/nrw/commons/caching/CacheController.java | 2 +- .../src/main/java/fr/free/nrw/commons/upload/ShareActivity.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 acbc28106..2d007b405 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 @@ -46,7 +46,7 @@ public class CacheController { List displayCatList = new ArrayList(); Log.d("Cache", "Points found in quadtree: " + pointsFound); - //TODO: Potentially flatten catList and iterate thru it to convert into unique Set? But currently just displays one point... + //TODO: Make this return a proper flat array if (pointsFound.length != 0) { Log.d("Cache", "Entering for loop"); 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 cdb780ab8..be0e360b8 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 @@ -215,7 +215,7 @@ public class ShareActivity apiCall.request(decimalCoords); Log.d("Cache", "displayCatList size 0, calling MWAPI"); } else { - //TODO: Set categoryList in MwVolleyApi + //TODO: Set categoryList in MwVolleyApi. Not filling up right. Maybe do global singleton for MwVolleyApi MwVolleyApi.setGpsCat(displayCatList); Log.d("Cache", "Cache found, setting categoryList in MwVolleyApi to " + displayCatList.toString()); } From d2f765daec7af0b12c21e04a1e9480d0e0d40365 Mon Sep 17 00:00:00 2001 From: misaochan Date: Thu, 7 Jan 2016 16:56:31 +1300 Subject: [PATCH 26/44] Flattened category list found in cache --- .../free/nrw/commons/caching/CacheController.java | 13 +++++++++---- .../fr/free/nrw/commons/upload/MwVolleyApi.java | 1 + .../fr/free/nrw/commons/upload/ShareActivity.java | 4 ++-- 3 files changed, 12 insertions(+), 6 deletions(-) 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 2d007b405..27259bb5f 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 @@ -38,14 +38,15 @@ public class CacheController { quadTree.set(x, y, pointCatList); } - public List findCategory() { + public ArrayList findCategory() { //Convert decLatitude and decLongitude to a coordinate offset range convertCoordRange(); pointsFound = quadTree.searchWithin(xMinus, yMinus, xPlus, yPlus); - List displayCatList = new ArrayList(); + ArrayList displayCatList = new ArrayList(); Log.d("Cache", "Points found in quadtree: " + pointsFound); + ArrayList flatCatList = null; //TODO: Make this return a proper flat array if (pointsFound.length != 0) { @@ -58,13 +59,17 @@ public class CacheController { displayCatList.add(index, cat); index++; } - Log.d("Cache", "Categories found in cache: " + displayCatList.toString()); + //FIXME: temporary, can't figure out why for loop always only accesses 1 point + flatCatList = ((ArrayList)displayCatList.get(0)); + Log.d("Cache", "Categories found in cache: " + flatCatList.toString()); } else { Log.d("Cache", "No categories found in cache"); } - return displayCatList; + return flatCatList; } + + public void convertCoordRange() { //Position, decimal degrees double lat = y; diff --git a/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java b/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java index 29e8c9d24..d8617ed7e 100644 --- a/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java +++ b/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java @@ -51,6 +51,7 @@ public class MwVolleyApi { public static void setGpsCat(List cachedList) { categoryList = new ArrayList(cachedList); + Log.d("Cache", "Setting GPS cats from cache: " + categoryList.toString()); } 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 be0e360b8..b0f0ebf79 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 @@ -213,9 +213,9 @@ public class ShareActivity //if no categories found in cache, call MW API to match image coords with nearby Commons categories if (displayCatList.size() == 0) { apiCall.request(decimalCoords); - Log.d("Cache", "displayCatList size 0, calling MWAPI"); + Log.d("Cache", "displayCatList size 0, calling MWAPI" + displayCatList.toString()); } else { - //TODO: Set categoryList in MwVolleyApi. Not filling up right. Maybe do global singleton for MwVolleyApi + //TODO: Set categoryList in MwVolleyApi. Not filling up right. Maybe do global singleton for MwVolleyApi? Can't do that, we want new cats for each upload, so new instance of mwapi MwVolleyApi.setGpsCat(displayCatList); Log.d("Cache", "Cache found, setting categoryList in MwVolleyApi to " + displayCatList.toString()); } From b99f847c70c7e6e8cbc2a2e5e8cf76ac3734cf4d Mon Sep 17 00:00:00 2001 From: misaochan Date: Thu, 7 Jan 2016 17:01:53 +1300 Subject: [PATCH 27/44] Returns flat list of categories --- .../java/fr/free/nrw/commons/caching/CacheController.java | 4 ++-- .../main/java/fr/free/nrw/commons/upload/ShareActivity.java | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) 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 27259bb5f..b178b1ba8 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 @@ -46,8 +46,8 @@ public class CacheController { ArrayList displayCatList = new ArrayList(); Log.d("Cache", "Points found in quadtree: " + pointsFound); - ArrayList flatCatList = null; - //TODO: Make this return a proper flat array + ArrayList flatCatList = new ArrayList(); + if (pointsFound.length != 0) { Log.d("Cache", "Entering for loop"); 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 b0f0ebf79..8186036bf 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 @@ -211,6 +211,7 @@ public class ShareActivity List displayCatList = cacheObj.cacheData.findCategory(); //if no categories found in cache, call MW API to match image coords with nearby Commons categories + if (displayCatList.size() == 0) { apiCall.request(decimalCoords); Log.d("Cache", "displayCatList size 0, calling MWAPI" + displayCatList.toString()); From 207ee2bfacc3f6ad9d1eaa1e7865127d05179d39 Mon Sep 17 00:00:00 2001 From: misaochan Date: Thu, 7 Jan 2016 17:06:55 +1300 Subject: [PATCH 28/44] Trying with 1000 radius --- .../main/java/fr/free/nrw/commons/upload/MwVolleyApi.java | 5 +++-- .../main/java/fr/free/nrw/commons/upload/ShareActivity.java | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java b/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java index d8617ed7e..a0886a154 100644 --- a/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java +++ b/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java @@ -50,7 +50,8 @@ public class MwVolleyApi { } public static void setGpsCat(List cachedList) { - categoryList = new ArrayList(cachedList); + categoryList = new ArrayList(); + categoryList.addAll(cachedList); Log.d("Cache", "Setting GPS cats from cache: " + categoryList.toString()); } @@ -85,7 +86,7 @@ public class MwVolleyApi { .appendQueryParameter("codistancefrompoint", coords) .appendQueryParameter("generator", "geosearch") .appendQueryParameter("ggscoord", coords) - .appendQueryParameter("ggsradius", "100") + .appendQueryParameter("ggsradius", "1000") .appendQueryParameter("ggslimit", "10") .appendQueryParameter("ggsnamespace", "6") .appendQueryParameter("ggsprop", "type|name|dim|country|region|globe") 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 8186036bf..601ea3c2f 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 @@ -217,8 +217,9 @@ public class ShareActivity Log.d("Cache", "displayCatList size 0, calling MWAPI" + displayCatList.toString()); } else { //TODO: Set categoryList in MwVolleyApi. Not filling up right. Maybe do global singleton for MwVolleyApi? Can't do that, we want new cats for each upload, so new instance of mwapi - MwVolleyApi.setGpsCat(displayCatList); + Log.d("Cache", "Cache found, setting categoryList in MwVolleyApi to " + displayCatList.toString()); + MwVolleyApi.setGpsCat(displayCatList); } } From 9c58e283717a7e68cd9ca8cc2c9412973e4d5b0c Mon Sep 17 00:00:00 2001 From: misaochan Date: Thu, 7 Jan 2016 17:33:35 +1300 Subject: [PATCH 29/44] Replaced TAGs in logs --- .../nrw/commons/caching/CacheController.java | 24 ++++++++++--------- .../free/nrw/commons/upload/MwVolleyApi.java | 3 ++- .../nrw/commons/upload/ShareActivity.java | 19 ++++++++++----- 3 files changed, 28 insertions(+), 18 deletions(-) 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 b178b1ba8..1bce5d4c0 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 @@ -15,6 +15,8 @@ public class CacheController { private Point[] pointsFound; private double xMinus, xPlus, yMinus, yPlus; + private static final String TAG = CacheController.class.getName(); + public CacheController() { quadTree = new QuadTree(-180, -90, +180, +90); } @@ -22,8 +24,8 @@ public class CacheController { public void setQtPoint(double decLongitude, double decLatitude) { x = decLongitude; y = decLatitude; - Log.d("Cache", "New QuadTree created"); - Log.d("Cache", "X (longitude) value: " + x + ", Y (latitude) value: " + y); + Log.d(TAG, "New QuadTree created"); + Log.d(TAG, "X (longitude) value: " + x + ", Y (latitude) value: " + y); } public void cacheCategory() { @@ -31,9 +33,9 @@ public class CacheController { List pointCatList = new ArrayList(); if (MwVolleyApi.GpsCatExists.getGpsCatExists() == true) { pointCatList.addAll(MwVolleyApi.getGpsCat()); - Log.d("Cache", "Categories being cached: " + pointCatList); + Log.d(TAG, "Categories being cached: " + pointCatList); } else { - Log.d("Cache", "No categories found, so no categories cached"); + Log.d(TAG, "No categories found, so no categories cached"); } quadTree.set(x, y, pointCatList); } @@ -44,26 +46,26 @@ public class CacheController { convertCoordRange(); pointsFound = quadTree.searchWithin(xMinus, yMinus, xPlus, yPlus); ArrayList displayCatList = new ArrayList(); - Log.d("Cache", "Points found in quadtree: " + pointsFound); + Log.d(TAG, "Points found in quadtree: " + pointsFound); ArrayList flatCatList = new ArrayList(); if (pointsFound.length != 0) { - Log.d("Cache", "Entering for loop"); + Log.d(TAG, "Entering for loop"); int index = 0; for (Point point : pointsFound) { - Log.d("Cache", "Nearby point: " + point.toString()); + Log.d(TAG, "Nearby point: " + point.toString()); Object cat = point.getValue(); - Log.d("Cache", "Nearby cat: " + cat); + Log.d(TAG, "Nearby cat: " + cat); displayCatList.add(index, cat); index++; } //FIXME: temporary, can't figure out why for loop always only accesses 1 point flatCatList = ((ArrayList)displayCatList.get(0)); - Log.d("Cache", "Categories found in cache: " + flatCatList.toString()); + Log.d(TAG, "Categories found in cache: " + flatCatList.toString()); } else { - Log.d("Cache", "No categories found in cache"); + Log.d(TAG, "No categories found in cache"); } return flatCatList; } @@ -89,6 +91,6 @@ public class CacheController { yMinus = lat - dLat * 180/Math.PI; xPlus = lon + dLon * 180/Math.PI; xMinus = lon - dLon * 180/Math.PI; - Log.d("Cache", "Search within: xMinus=" + xMinus + ", yMinus=" + yMinus + ", xPlus=" + xPlus + ", yPlus=" + yPlus); + Log.d(TAG, "Search within: xMinus=" + xMinus + ", yMinus=" + yMinus + ", xPlus=" + xPlus + ", yPlus=" + yPlus); } } diff --git a/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java b/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java index a0886a154..de0213dcb 100644 --- a/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java +++ b/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java @@ -37,6 +37,7 @@ public class MwVolleyApi { private static List categoryList; private static final String MWURL = "https://commons.wikimedia.org/"; + private static final String TAG = MwVolleyApi.class.getName(); public MwVolleyApi(Context context) { this.context = context; @@ -52,7 +53,7 @@ public class MwVolleyApi { public static void setGpsCat(List cachedList) { categoryList = new ArrayList(); categoryList.addAll(cachedList); - Log.d("Cache", "Setting GPS cats from cache: " + categoryList.toString()); + Log.d(TAG, "Setting GPS cats from cache: " + categoryList.toString()); } 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 601ea3c2f..d167f7bef 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 @@ -49,6 +49,9 @@ public class ShareActivity private UploadController uploadController; private CommonsApplication cacheObj; + private boolean cacheFound; + + private static final String TAG = ShareActivity.class.getName(); public ShareActivity() { super(WikiAccountAuthenticator.COMMONS_ACCOUNT_TYPE); @@ -58,9 +61,11 @@ public class ShareActivity Toast startingToast = Toast.makeText(getApplicationContext(), R.string.uploading_started, Toast.LENGTH_LONG); startingToast.show(); - //Has to be called after apiCall.request() - cacheObj.cacheData.cacheCategory(); - Log.d("Cache", "Cache the categories found"); + if (cacheFound == false) { + //Has to be called after apiCall.request() + cacheObj.cacheData.cacheCategory(); + Log.d("Cache", "Cache the categories found"); + } uploadController.startUpload(title, mediaUri, description, mimeType, source, new UploadController.ContributionUploadProgress() { public void onUploadStarted(Contribution contribution) { @@ -213,12 +218,14 @@ public class ShareActivity //if no categories found in cache, call MW API to match image coords with nearby Commons categories if (displayCatList.size() == 0) { + cacheFound = false; apiCall.request(decimalCoords); - Log.d("Cache", "displayCatList size 0, calling MWAPI" + displayCatList.toString()); + Log.d(TAG, "displayCatList size 0, calling MWAPI" + displayCatList.toString()); + } else { //TODO: Set categoryList in MwVolleyApi. Not filling up right. Maybe do global singleton for MwVolleyApi? Can't do that, we want new cats for each upload, so new instance of mwapi - - Log.d("Cache", "Cache found, setting categoryList in MwVolleyApi to " + displayCatList.toString()); + cacheFound = true; + Log.d(TAG, "Cache found, setting categoryList in MwVolleyApi to " + displayCatList.toString()); MwVolleyApi.setGpsCat(displayCatList); } From 2384733934d3ada9674df5688c939ceb6605a85c Mon Sep 17 00:00:00 2001 From: misaochan Date: Thu, 7 Jan 2016 17:35:05 +1300 Subject: [PATCH 30/44] Replaced TAGs --- .../free/nrw/commons/category/CategorizationFragment.java | 7 ++++--- .../main/java/fr/free/nrw/commons/upload/MwVolleyApi.java | 4 ++-- .../java/fr/free/nrw/commons/upload/ShareActivity.java | 8 ++++---- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/commons/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java b/commons/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java index e5491c27f..3a6d1d329 100644 --- a/commons/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java +++ b/commons/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java @@ -51,6 +51,7 @@ public class CategorizationFragment extends SherlockFragment{ private ContentProviderClient client; private final int SEARCH_CATS_LIMIT = 25; + private static final String TAG = CategorizationFragment.class.getName(); public static class CategoryItem implements Parcelable { public String name; @@ -152,9 +153,9 @@ public class CategorizationFragment extends SherlockFragment{ } if (MwVolleyApi.GpsCatExists.getGpsCatExists() == true){ - Log.d("Cat", "GPS cats found in CategorizationFragment.java" + MwVolleyApi.getGpsCat().toString()); + Log.d(TAG, "GPS cats found in CategorizationFragment.java" + MwVolleyApi.getGpsCat().toString()); List gpsItems = new ArrayList(MwVolleyApi.getGpsCat()); - Log.d("Cat", "GPS items: " + gpsItems.toString()); + Log.d(TAG, "GPS items: " + gpsItems.toString()); mergedItems.addAll(gpsItems); } @@ -165,7 +166,7 @@ public class CategorizationFragment extends SherlockFragment{ // faaaail throw new RuntimeException(e); } - Log.d("Cat", "Merged items: " + mergedItems.toString()); + Log.d(TAG, "Merged items: " + mergedItems.toString()); return mergedItems; } diff --git a/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java b/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java index de0213dcb..ac149f7a1 100644 --- a/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java +++ b/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java @@ -174,11 +174,11 @@ public class MwVolleyApi { private String printSet() { if (categorySet == null || categorySet.isEmpty()) { GpsCatExists.setGpsCatExists(false); - Log.d("Cat", "gpsCatExists=" + GpsCatExists.getGpsCatExists()); + Log.d(TAG, "gpsCatExists=" + GpsCatExists.getGpsCatExists()); return "No collection of categories"; } else { GpsCatExists.setGpsCatExists(true); - Log.d("Cat", "gpsCatExists=" + GpsCatExists.getGpsCatExists()); + Log.d(TAG, "gpsCatExists=" + GpsCatExists.getGpsCatExists()); return "CATEGORIES FOUND" + categorySet.toString(); } } 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 d167f7bef..6a6aec0be 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 @@ -64,7 +64,7 @@ public class ShareActivity if (cacheFound == false) { //Has to be called after apiCall.request() cacheObj.cacheData.cacheCategory(); - Log.d("Cache", "Cache the categories found"); + Log.d(TAG, "Cache the categories found"); } uploadController.startUpload(title, mediaUri, description, mimeType, source, new UploadController.ContributionUploadProgress() { @@ -187,7 +187,7 @@ public class ShareActivity } mediaUriString = mediaUri.toString(); - Log.d("Image", "Uri: " + mediaUriString); + Log.d(TAG, "Uri: " + mediaUriString); //convert image Uri to file path FilePathConverter uriObj = new FilePathConverter(this, mediaUri); String filePath = uriObj.getFilePath(); @@ -199,7 +199,7 @@ public class ShareActivity if (filePath != null) { //extract the coordinates of image in decimal degrees - Log.d("Image", "Calling GPSExtractor"); + Log.d(TAG, "Calling GPSExtractor"); GPSExtractor imageObj = new GPSExtractor(filePath); //decimalCoords for MediaWiki API, xyCoords for Quadtree String decimalCoords = imageObj.getCoords(); @@ -208,7 +208,7 @@ public class ShareActivity double decLatitude = imageObj.getDecLatitude(); if (decimalCoords != null) { - Log.d("Coords", "Decimal coords of image: " + decimalCoords); + Log.d(TAG, "Decimal coords of image: " + decimalCoords); cacheObj.cacheData.setQtPoint(decLongitude, decLatitude); MwVolleyApi apiCall = new MwVolleyApi(this); From ab7db68d6c2bef2a147f3f392ddb546852a7c54d Mon Sep 17 00:00:00 2001 From: misaochan Date: Thu, 7 Jan 2016 17:41:58 +1300 Subject: [PATCH 31/44] CategorizationFragment displays cached categories --- .../src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java b/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java index ac149f7a1..00a38ac02 100644 --- a/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java +++ b/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java @@ -46,7 +46,6 @@ public class MwVolleyApi { //To get the list of categories for display public static List getGpsCat() { - categoryList = new ArrayList(categorySet); return categoryList; } @@ -181,6 +180,7 @@ public class MwVolleyApi { Log.d(TAG, "gpsCatExists=" + GpsCatExists.getGpsCatExists()); return "CATEGORIES FOUND" + categorySet.toString(); } + } @Override @@ -235,6 +235,7 @@ public class MwVolleyApi { } } + categoryList = new ArrayList(categorySet); builder.replace(builder.length() - 1, builder.length(), ""); return builder.toString(); } From b5ee6e3c5e9fc51192260f7547d9de967a5339b3 Mon Sep 17 00:00:00 2001 From: misaochan Date: Thu, 7 Jan 2016 17:51:27 +1300 Subject: [PATCH 32/44] Minor code reformatting --- .../java/fr/free/nrw/commons/caching/CacheController.java | 7 +------ .../main/java/fr/free/nrw/commons/upload/MwVolleyApi.java | 3 --- .../java/fr/free/nrw/commons/upload/ShareActivity.java | 7 ------- 3 files changed, 1 insertion(+), 16 deletions(-) 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 1bce5d4c0..13db87430 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 @@ -1,6 +1,5 @@ package fr.free.nrw.commons.caching; -import android.content.Context; import android.util.Log; import java.util.ArrayList; @@ -29,7 +28,6 @@ public class CacheController { } public void cacheCategory() { - List pointCatList = new ArrayList(); if (MwVolleyApi.GpsCatExists.getGpsCatExists() == true) { pointCatList.addAll(MwVolleyApi.getGpsCat()); @@ -41,7 +39,6 @@ public class CacheController { } public ArrayList findCategory() { - //Convert decLatitude and decLongitude to a coordinate offset range convertCoordRange(); pointsFound = quadTree.searchWithin(xMinus, yMinus, xPlus, yPlus); @@ -51,7 +48,6 @@ public class CacheController { ArrayList flatCatList = new ArrayList(); if (pointsFound.length != 0) { - Log.d(TAG, "Entering for loop"); int index = 0; for (Point point : pointsFound) { @@ -63,6 +59,7 @@ public class CacheController { } //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()); } else { Log.d(TAG, "No categories found in cache"); @@ -70,8 +67,6 @@ public class CacheController { return flatCatList; } - - public void convertCoordRange() { //Position, decimal degrees double lat = y; diff --git a/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java b/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java index 00a38ac02..7b6c78bd6 100644 --- a/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java +++ b/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java @@ -44,7 +44,6 @@ public class MwVolleyApi { categorySet = new HashSet(); } - //To get the list of categories for display public static List getGpsCat() { return categoryList; } @@ -55,9 +54,7 @@ public class MwVolleyApi { Log.d(TAG, "Setting GPS cats from cache: " + categoryList.toString()); } - public void request(String coords) { - coordsLog = coords; String apiUrl = buildUrl(coords); Log.d("Image", "URL: " + apiUrl); 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 6a6aec0be..ea0d5b8b4 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 @@ -182,7 +182,6 @@ public class ShareActivity } else { source = Contribution.SOURCE_EXTERNAL; } - mimeType = intent.getType(); } @@ -195,15 +194,11 @@ public class ShareActivity //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); - //decimalCoords for MediaWiki API, xyCoords for Quadtree String decimalCoords = imageObj.getCoords(); - double decLongitude = imageObj.getDecLongitude(); double decLatitude = imageObj.getDecLatitude(); @@ -216,14 +211,12 @@ public class ShareActivity List displayCatList = cacheObj.cacheData.findCategory(); //if no categories found in cache, call MW API to match image coords with nearby Commons categories - if (displayCatList.size() == 0) { cacheFound = false; apiCall.request(decimalCoords); Log.d(TAG, "displayCatList size 0, calling MWAPI" + displayCatList.toString()); } else { - //TODO: Set categoryList in MwVolleyApi. Not filling up right. Maybe do global singleton for MwVolleyApi? Can't do that, we want new cats for each upload, so new instance of mwapi cacheFound = true; Log.d(TAG, "Cache found, setting categoryList in MwVolleyApi to " + displayCatList.toString()); MwVolleyApi.setGpsCat(displayCatList); From 49d890a0d7fb1f13ec7ab45dec74ed29fcb23dd6 Mon Sep 17 00:00:00 2001 From: misaochan Date: Thu, 7 Jan 2016 18:35:07 +1300 Subject: [PATCH 33/44] Minor List refactor --- .../java/fr/free/nrw/commons/caching/CacheController.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 13db87430..40a7b99e4 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 @@ -38,14 +38,14 @@ public class CacheController { quadTree.set(x, y, pointCatList); } - public ArrayList findCategory() { + public List findCategory() { //Convert decLatitude and decLongitude to a coordinate offset range convertCoordRange(); pointsFound = quadTree.searchWithin(xMinus, yMinus, xPlus, yPlus); - ArrayList displayCatList = new ArrayList(); + List displayCatList = new ArrayList(); Log.d(TAG, "Points found in quadtree: " + pointsFound); - ArrayList flatCatList = new ArrayList(); + List flatCatList = new ArrayList(); if (pointsFound.length != 0) { Log.d(TAG, "Entering for loop"); From ba9ed66e0f6d107fd3d61d3a13634af5a825d945 Mon Sep 17 00:00:00 2001 From: misaochan Date: Fri, 8 Jan 2016 01:21:33 +1300 Subject: [PATCH 34/44] Changed to max possible radius (10000m) --- .../src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java b/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java index 7b6c78bd6..fe55f3f7f 100644 --- a/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java +++ b/commons/src/main/java/fr/free/nrw/commons/upload/MwVolleyApi.java @@ -83,7 +83,7 @@ public class MwVolleyApi { .appendQueryParameter("codistancefrompoint", coords) .appendQueryParameter("generator", "geosearch") .appendQueryParameter("ggscoord", coords) - .appendQueryParameter("ggsradius", "1000") + .appendQueryParameter("ggsradius", "10000") .appendQueryParameter("ggslimit", "10") .appendQueryParameter("ggsnamespace", "6") .appendQueryParameter("ggsprop", "type|name|dim|country|region|globe") From ac933fc99315338d8e1112677a5ea8f07665dc76 Mon Sep 17 00:00:00 2001 From: misaochan Date: Fri, 8 Jan 2016 16:06:15 +1300 Subject: [PATCH 35/44] Prep for release --- commons/AndroidManifest.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commons/AndroidManifest.xml b/commons/AndroidManifest.xml index 7fed5543f..de1e407dc 100644 --- a/commons/AndroidManifest.xml +++ b/commons/AndroidManifest.xml @@ -1,7 +1,7 @@ + android:versionCode="21" + android:versionName="1.5" > Date: Fri, 8 Jan 2016 16:10:57 +1300 Subject: [PATCH 36/44] Prep for release, changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6255cd920..d8a8798a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Wikimedia Commons for Android ## v1.5 +- Caches area and associated categories +- Increases search radius for nearby categories + +## v1.4 - New feature: Suggests nearby Commons categories ## v1.3 From 7ac01e5a0172b1e9dd81f5f9b9020067c0d11e3e Mon Sep 17 00:00:00 2001 From: misaochan Date: Fri, 8 Jan 2016 17:19:41 +1300 Subject: [PATCH 37/44] Imported QuadTree as .jar instead --- .../nrw/commons/caching/CacheController.java | 3 + .../fr/free/nrw/commons/caching/Func.java | 6 - .../fr/free/nrw/commons/caching/Node.java | 123 ----- .../fr/free/nrw/commons/caching/NodeType.java | 11 - .../fr/free/nrw/commons/caching/Point.java | 69 --- .../fr/free/nrw/commons/caching/QuadTree.java | 477 ------------------ .../commons/caching/QuadTreeException.java | 8 - .../nrw/commons/upload/ShareActivity.java | 1 + 8 files changed, 4 insertions(+), 694 deletions(-) delete mode 100644 commons/src/main/java/fr/free/nrw/commons/caching/Func.java delete mode 100644 commons/src/main/java/fr/free/nrw/commons/caching/Node.java delete mode 100644 commons/src/main/java/fr/free/nrw/commons/caching/NodeType.java delete mode 100644 commons/src/main/java/fr/free/nrw/commons/caching/Point.java delete mode 100644 commons/src/main/java/fr/free/nrw/commons/caching/QuadTree.java delete mode 100644 commons/src/main/java/fr/free/nrw/commons/caching/QuadTreeException.java 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 40a7b99e4..41d01c3c2 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 @@ -2,6 +2,9 @@ package fr.free.nrw.commons.caching; import android.util.Log; +import com.github.varunpant.quadtree.Point; +import com.github.varunpant.quadtree.QuadTree; + import java.util.ArrayList; import java.util.List; diff --git a/commons/src/main/java/fr/free/nrw/commons/caching/Func.java b/commons/src/main/java/fr/free/nrw/commons/caching/Func.java deleted file mode 100644 index 622828ad2..000000000 --- a/commons/src/main/java/fr/free/nrw/commons/caching/Func.java +++ /dev/null @@ -1,6 +0,0 @@ -package fr.free.nrw.commons.caching; - - -public interface Func { - public void call(QuadTree quadTree, Node node); -} diff --git a/commons/src/main/java/fr/free/nrw/commons/caching/Node.java b/commons/src/main/java/fr/free/nrw/commons/caching/Node.java deleted file mode 100644 index 1ab054a99..000000000 --- a/commons/src/main/java/fr/free/nrw/commons/caching/Node.java +++ /dev/null @@ -1,123 +0,0 @@ -package fr.free.nrw.commons.caching; - - class Node { - - private double x; - private double y; - private double w; - private double h; - private Node opt_parent; - private Point point; - private NodeType nodetype = NodeType.EMPTY; - private Node nw; - private Node ne; - private Node sw; - private Node se; - - /** - * Constructs a new quad tree node. - * - * @param {double} x X-coordiate of node. - * @param {double} y Y-coordinate of node. - * @param {double} w Width of node. - * @param {double} h Height of node. - * @param {Node} opt_parent Optional parent node. - * @constructor - */ - public Node(double x, double y, double w, double h, Node opt_parent) { - this.x = x; - this.y = y; - this.w = w; - this.h = h; - this.opt_parent = opt_parent; - } - - public double getX() { - return x; - } - - public void setX(double x) { - this.x = x; - } - - public double getY() { - return y; - } - - public void setY(double y) { - this.y = y; - } - - public double getW() { - return w; - } - - public void setW(double w) { - this.w = w; - } - - public double getH() { - return h; - } - - public void setH(double h) { - this.h = h; - } - - public Node getParent() { - return opt_parent; - } - - public void setParent(Node opt_parent) { - this.opt_parent = opt_parent; - } - - public void setPoint(Point point) { - this.point = point; - } - - public Point getPoint() { - return this.point; - } - - public void setNodeType(NodeType nodetype) { - this.nodetype = nodetype; - } - - public NodeType getNodeType() { - return this.nodetype; - } - - - public void setNw(Node nw) { - this.nw = nw; - } - - public void setNe(Node ne) { - this.ne = ne; - } - - public void setSw(Node sw) { - this.sw = sw; - } - - public void setSe(Node se) { - this.se = se; - } - - public Node getNe() { - return ne; - } - - public Node getNw() { - return nw; - } - - public Node getSw() { - return sw; - } - - public Node getSe() { - return se; - } -} diff --git a/commons/src/main/java/fr/free/nrw/commons/caching/NodeType.java b/commons/src/main/java/fr/free/nrw/commons/caching/NodeType.java deleted file mode 100644 index 9aac701fa..000000000 --- a/commons/src/main/java/fr/free/nrw/commons/caching/NodeType.java +++ /dev/null @@ -1,11 +0,0 @@ -package fr.free.nrw.commons.caching; - -/** - * Enumeration of node types. - * @enum {number} - */ -public enum NodeType { - EMPTY, - LEAF, - POINTER -} diff --git a/commons/src/main/java/fr/free/nrw/commons/caching/Point.java b/commons/src/main/java/fr/free/nrw/commons/caching/Point.java deleted file mode 100644 index 6b2fdb8c1..000000000 --- a/commons/src/main/java/fr/free/nrw/commons/caching/Point.java +++ /dev/null @@ -1,69 +0,0 @@ -package fr.free.nrw.commons.caching; - -public class Point implements Comparable { - - private double x; - private double y; - private Object opt_value; - - /** - * Creates a new point object. - * - * @param {double} x The x-coordinate of the point. - * @param {double} y The y-coordinate of the point. - * @param {Object} opt_value Optional value associated with the point. - */ - public Point(double x, double y, Object opt_value) { - this.x = x; - this.y = y; - this.opt_value = opt_value; - } - - public double getX() { - return x; - } - - public void setX(double x) { - this.x = x; - } - - public double getY() { - return y; - } - - public void setY(double y) { - this.y = y; - } - - public Object getValue() { - return opt_value; - } - - public void setValue(Object opt_value) { - this.opt_value = opt_value; - } - - @Override - public String toString() { - return "(" + this.x + ", " + this.y + ")"; - } - - @Override - public int compareTo(Object o) { - Point tmp = (Point) o; - if (this.x < tmp.x) { - return -1; - } else if (this.x > tmp.x) { - return 1; - } else { - if (this.y < tmp.y) { - return -1; - } else if (this.y > tmp.y) { - return 1; - } - return 0; - } - - } - -} diff --git a/commons/src/main/java/fr/free/nrw/commons/caching/QuadTree.java b/commons/src/main/java/fr/free/nrw/commons/caching/QuadTree.java deleted file mode 100644 index 1f77a3491..000000000 --- a/commons/src/main/java/fr/free/nrw/commons/caching/QuadTree.java +++ /dev/null @@ -1,477 +0,0 @@ -package fr.free.nrw.commons.caching; - -import java.util.ArrayList; -import java.util.List; - - -/** - * Datastructure: A point Quad Tree for representing 2D data. Each - * region has the same ratio as the bounds for the tree. - *

- * The implementation currently requires pre-determined bounds for data as it - * can not rebalance itself to that degree. - */ -public class QuadTree { - - - private Node root_; - private int count_ = 0; - - /** - * Constructs a new quad tree. - * - * @param {double} minX Minimum x-value that can be held in tree. - * @param {double} minY Minimum y-value that can be held in tree. - * @param {double} maxX Maximum x-value that can be held in tree. - * @param {double} maxY Maximum y-value that can be held in tree. - */ - public QuadTree(double minX, double minY, double maxX, double maxY) { - this.root_ = new Node(minX, minY, maxX - minX, maxY - minY, null); - } - - /** - * Returns a reference to the tree's root node. Callers shouldn't modify nodes, - * directly. This is a convenience for visualization and debugging purposes. - * - * @return {Node} The root node. - */ - public Node getRootNode() { - return this.root_; - } - - //TODO: Marked by Nicolas as: Set an object at particular coordinates - /** - * Sets the value of an (x, y) point within the quad-tree. - * - * @param {double} x The x-coordinate. - * @param {double} y The y-coordinate. - * @param {Object} value The value associated with the point. - */ - public void set(double x, double y, Object value) { - - Node root = this.root_; - if (x < root.getX() || y < root.getY() || x > root.getX() + root.getW() || y > root.getY() + root.getH()) { - throw new QuadTreeException("Out of bounds : (" + x + ", " + y + ")"); - } - if (this.insert(root, new Point(x, y, value))) { - this.count_++; - } - } - - /** - * Gets the value of the point at (x, y) or null if the point is empty. - * - * @param {double} x The x-coordinate. - * @param {double} y The y-coordinate. - * @param {Object} opt_default The default value to return if the node doesn't - * exist. - * @return {*} The value of the node, the default value if the node - * doesn't exist, or undefined if the node doesn't exist and no default - * has been provided. - */ - public Object get(double x, double y, Object opt_default) { - Node node = this.find(this.root_, x, y); - return node != null ? node.getPoint().getValue() : opt_default; - } - - /** - * Removes a point from (x, y) if it exists. - * - * @param {double} x The x-coordinate. - * @param {double} y The y-coordinate. - * @return {Object} The value of the node that was removed, or null if the - * node doesn't exist. - */ - public Object remove(double x, double y) { - Node node = this.find(this.root_, x, y); - if (node != null) { - Object value = node.getPoint().getValue(); - node.setPoint(null); - node.setNodeType(NodeType.EMPTY); - this.balance(node); - this.count_--; - return value; - } else { - return null; - } - } - - /** - * Returns true if the point at (x, y) exists in the tree. - * - * @param {double} x The x-coordinate. - * @param {double} y The y-coordinate. - * @return {boolean} Whether the tree contains a point at (x, y). - */ - public boolean contains(double x, double y) { - return this.get(x, y, null) != null; - } - - /** - * @return {boolean} Whether the tree is empty. - */ - public boolean isEmpty() { - return this.root_.getNodeType() == NodeType.EMPTY; - } - - /** - * @return {number} The number of items in the tree. - */ - public int getCount() { - return this.count_; - } - - /** - * Removes all items from the tree. - */ - public void clear() { - this.root_.setNw(null); - this.root_.setNe(null); - this.root_.setSw(null); - this.root_.setSe(null); - this.root_.setNodeType(NodeType.EMPTY); - this.root_.setPoint(null); - this.count_ = 0; - } - - /** - * Returns an array containing the coordinates of each point stored in the tree. - * @return {Array.} Array of coordinates. - */ - public Point[] getKeys() { - final List arr = new ArrayList(); - this.traverse(this.root_, new Func() { - @Override - public void call(QuadTree quadTree, Node node) { - arr.add(node.getPoint()); - } - }); - return arr.toArray(new Point[arr.size()]); - } - - /** - * Returns an array containing all values stored within the tree. - * @return {Array.} The values stored within the tree. - */ - public Object[] getValues() { - final List arr = new ArrayList(); - this.traverse(this.root_, new Func() { - @Override - public void call(QuadTree quadTree, Node node) { - arr.add(node.getPoint().getValue()); - } - }); - - return arr.toArray(new Object[arr.size()]); - } - - public Point[] searchIntersect(final double xmin, final double ymin, final double xmax, final double ymax) { - final List arr = new ArrayList(); - this.navigate(this.root_, new Func() { - @Override - public void call(QuadTree quadTree, Node node) { - Point pt = node.getPoint(); - if (pt.getX() < xmin || pt.getX() > xmax || pt.getY() < ymin || pt.getY() > ymax) { - // Definitely not within the polygon! - } else { - arr.add(node.getPoint()); - } - - } - }, xmin, ymin, xmax, ymax); - return arr.toArray(new Point[arr.size()]); - } - - //TODO: Marked by Nicolas as: Find objects within a rectangle - public Point[] searchWithin(final double xmin, final double ymin, final double xmax, final double ymax) { - final List arr = new ArrayList(); - this.navigate(this.root_, new Func() { - @Override - public void call(QuadTree quadTree, Node node) { - Point pt = node.getPoint(); - if (pt.getX() > xmin && pt.getX() < xmax && pt.getY() > ymin && pt.getY() < ymax) { - arr.add(node.getPoint()); - } - } - }, xmin, ymin, xmax, ymax); - return arr.toArray(new Point[arr.size()]); - } - - public void navigate(Node node, Func func, double xmin, double ymin, double xmax, double ymax) { - switch (node.getNodeType()) { - case LEAF: - func.call(this, node); - break; - - case POINTER: - if (intersects(xmin, ymax, xmax, ymin, node.getNe())) - this.navigate(node.getNe(), func, xmin, ymin, xmax, ymax); - if (intersects(xmin, ymax, xmax, ymin, node.getSe())) - this.navigate(node.getSe(), func, xmin, ymin, xmax, ymax); - if (intersects(xmin, ymax, xmax, ymin, node.getSw())) - this.navigate(node.getSw(), func, xmin, ymin, xmax, ymax); - if (intersects(xmin, ymax, xmax, ymin, node.getNw())) - this.navigate(node.getNw(), func, xmin, ymin, xmax, ymax); - break; - } - } - - private boolean intersects(double left, double bottom, double right, double top, Node node) { - return !(node.getX() > right || - (node.getX() + node.getW()) < left || - node.getY() > bottom || - (node.getY() + node.getH()) < top); - } - /** - * Clones the quad-tree and returns the new instance. - * @return {QuadTree} A clone of the tree. - */ - public QuadTree clone() { - double x1 = this.root_.getX(); - double y1 = this.root_.getY(); - double x2 = x1 + this.root_.getW(); - double y2 = y1 + this.root_.getH(); - final QuadTree clone = new QuadTree(x1, y1, x2, y2); - // This is inefficient as the clone needs to recalculate the structure of the - // tree, even though we know it already. But this is easier and can be - // optimized when/if needed. - this.traverse(this.root_, new Func() { - @Override - public void call(QuadTree quadTree, Node node) { - clone.set(node.getPoint().getX(), node.getPoint().getY(), node.getPoint().getValue()); - } - }); - - - return clone; - } - - /** - * Traverses the tree depth-first, with quadrants being traversed in clockwise - * order (NE, SE, SW, NW). The provided function will be called for each - * leaf node that is encountered. - * @param {QuadTree.Node} node The current node. - * @param {function(QuadTree.Node)} fn The function to call - * for each leaf node. This function takes the node as an argument, and its - * return value is irrelevant. - * @private - */ - public void traverse(Node node, Func func) { - switch (node.getNodeType()) { - case LEAF: - func.call(this, node); - break; - - case POINTER: - this.traverse(node.getNe(), func); - this.traverse(node.getSe(), func); - this.traverse(node.getSw(), func); - this.traverse(node.getNw(), func); - break; - } - } - - /** - * Finds a leaf node with the same (x, y) coordinates as the target point, or - * null if no point exists. - * @param {QuadTree.Node} node The node to search in. - * @param {number} x The x-coordinate of the point to search for. - * @param {number} y The y-coordinate of the point to search for. - * @return {QuadTree.Node} The leaf node that matches the target, - * or null if it doesn't exist. - * @private - */ - public Node find(Node node, double x, double y) { - Node resposne = null; - switch (node.getNodeType()) { - case EMPTY: - break; - - case LEAF: - resposne = node.getPoint().getX() == x && node.getPoint().getY() == y ? node : null; - break; - - case POINTER: - resposne = this.find(this.getQuadrantForPoint(node, x, y), x, y); - break; - - default: - throw new QuadTreeException("Invalid nodeType"); - } - return resposne; - } - - /** - * Inserts a point into the tree, updating the tree's structure if necessary. - * @param {.QuadTree.Node} parent The parent to insert the point - * into. - * @param {QuadTree.Point} point The point to insert. - * @return {boolean} True if a new node was added to the tree; False if a node - * already existed with the correpsonding coordinates and had its value - * reset. - * @private - */ - private boolean insert(Node parent, Point point) { - Boolean result = false; - switch (parent.getNodeType()) { - case EMPTY: - this.setPointForNode(parent, point); - result = true; - break; - case LEAF: - if (parent.getPoint().getX() == point.getX() && parent.getPoint().getY() == point.getY()) { - this.setPointForNode(parent, point); - result = false; - } else { - this.split(parent); - result = this.insert(parent, point); - } - break; - case POINTER: - result = this.insert( - this.getQuadrantForPoint(parent, point.getX(), point.getY()), point); - break; - - default: - throw new QuadTreeException("Invalid nodeType in parent"); - } - return result; - } - - /** - * Converts a leaf node to a pointer node and reinserts the node's point into - * the correct child. - * @param {QuadTree.Node} node The node to split. - * @private - */ - private void split(Node node) { - Point oldPoint = node.getPoint(); - node.setPoint(null); - - node.setNodeType(NodeType.POINTER); - - double x = node.getX(); - double y = node.getY(); - double hw = node.getW() / 2; - double hh = node.getH() / 2; - - node.setNw(new Node(x, y, hw, hh, node)); - node.setNe(new Node(x + hw, y, hw, hh, node)); - node.setSw(new Node(x, y + hh, hw, hh, node)); - node.setSe(new Node(x + hw, y + hh, hw, hh, node)); - - this.insert(node, oldPoint); - } - - /** - * Attempts to balance a node. A node will need balancing if all its children - * are empty or it contains just one leaf. - * @param {QuadTree.Node} node The node to balance. - * @private - */ - private void balance(Node node) { - switch (node.getNodeType()) { - case EMPTY: - case LEAF: - if (node.getParent() != null) { - this.balance(node.getParent()); - } - break; - - case POINTER: { - Node nw = node.getNw(); - Node ne = node.getNe(); - Node sw = node.getSw(); - Node se = node.getSe(); - Node firstLeaf = null; - - // Look for the first non-empty child, if there is more than one then we - // break as this node can't be balanced. - if (nw.getNodeType() != NodeType.EMPTY) { - firstLeaf = nw; - } - if (ne.getNodeType() != NodeType.EMPTY) { - if (firstLeaf != null) { - break; - } - firstLeaf = ne; - } - if (sw.getNodeType() != NodeType.EMPTY) { - if (firstLeaf != null) { - break; - } - firstLeaf = sw; - } - if (se.getNodeType() != NodeType.EMPTY) { - if (firstLeaf != null) { - break; - } - firstLeaf = se; - } - - if (firstLeaf == null) { - // All child nodes are empty: so make this node empty. - node.setNodeType(NodeType.EMPTY); - node.setNw(null); - node.setNe(null); - node.setSw(null); - node.setSe(null); - - } else if (firstLeaf.getNodeType() == NodeType.POINTER) { - // Only child was a pointer, therefore we can't rebalance. - break; - - } else { - // Only child was a leaf: so update node's point and make it a leaf. - node.setNodeType(NodeType.LEAF); - node.setNw(null); - node.setNe(null); - node.setSw(null); - node.setSe(null); - node.setPoint(firstLeaf.getPoint()); - } - - // Try and balance the parent as well. - if (node.getParent() != null) { - this.balance(node.getParent()); - } - } - break; - } - } - - /** - * Returns the child quadrant within a node that contains the given (x, y) - * coordinate. - * @param {QuadTree.Node} parent The node. - * @param {number} x The x-coordinate to look for. - * @param {number} y The y-coordinate to look for. - * @return {QuadTree.Node} The child quadrant that contains the - * point. - * @private - */ - //TODO: Is this the method we want to retrieve the 'area' given a coordinate? - private Node getQuadrantForPoint(Node parent, double x, double y) { - double mx = parent.getX() + parent.getW() / 2; - double my = parent.getY() + parent.getH() / 2; - if (x < mx) { - return y < my ? parent.getNw() : parent.getSw(); - } else { - return y < my ? parent.getNe() : parent.getSe(); - } - } - - /** - * Sets the point for a node, as long as the node is a leaf or empty. - * @param {QuadTree.Node} node The node to set the point for. - * @param {QuadTree.Point} point The point to set. - * @private - */ - private void setPointForNode(Node node, Point point) { - if (node.getNodeType() == NodeType.POINTER) { - throw new QuadTreeException("Can not set point for node of type POINTER"); - } - node.setNodeType(NodeType.LEAF); - node.setPoint(point); - } -} diff --git a/commons/src/main/java/fr/free/nrw/commons/caching/QuadTreeException.java b/commons/src/main/java/fr/free/nrw/commons/caching/QuadTreeException.java deleted file mode 100644 index 36b0022d9..000000000 --- a/commons/src/main/java/fr/free/nrw/commons/caching/QuadTreeException.java +++ /dev/null @@ -1,8 +0,0 @@ -package fr.free.nrw.commons.caching; - -public class QuadTreeException extends RuntimeException { - - public QuadTreeException(String s) { - super(s); - } -} 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 ea0d5b8b4..bb40f4fe1 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 @@ -26,6 +26,7 @@ import java.util.ArrayList; import java.util.List; + public class ShareActivity extends AuthenticatedActivity implements SingleUploadFragment.OnUploadActionInitiated, From 881acda52d3b8597daf62adddb54cf8dd152b949 Mon Sep 17 00:00:00 2001 From: misaochan Date: Fri, 8 Jan 2016 17:31:30 +1300 Subject: [PATCH 38/44] Prep for release --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8a8798a1..526c48bab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## v1.5 - Caches area and associated categories -- Increases search radius for nearby categories +- Increased search radius for nearby categories ## v1.4 - New feature: Suggests nearby Commons categories From e62760ebc00744e70a5b24b82320c8fe8e6e181e Mon Sep 17 00:00:00 2001 From: misaochan Date: Mon, 11 Jan 2016 16:55:17 +1300 Subject: [PATCH 39/44] Added constant, renamed cacheObj to app --- .../main/java/fr/free/nrw/commons/CommonsApplication.java | 2 -- .../java/fr/free/nrw/commons/caching/CacheController.java | 7 +++---- .../java/fr/free/nrw/commons/upload/ShareActivity.java | 6 +++--- 3 files changed, 6 insertions(+), 9 deletions(-) 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..a5cc63242 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); @@ -75,14 +76,12 @@ public class CacheController { 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 bb40f4fe1..4ab76b949 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 @@ -64,7 +64,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"); } @@ -205,11 +205,11 @@ public class ShareActivity if (decimalCoords != null) { 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(); //if no categories found in cache, call MW API to match image coords with nearby Commons categories if (displayCatList.size() == 0) { From 9baafdf82bbca1a09ea821ed2feb71f55e5b214a Mon Sep 17 00:00:00 2001 From: misaochan Date: Mon, 11 Jan 2016 16:56:49 +1300 Subject: [PATCH 40/44] Remove cacheObj --- .../main/java/fr/free/nrw/commons/upload/ShareActivity.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) 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 4ab76b949..d66543c63 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 @@ -191,9 +191,7 @@ public class ShareActivity //convert image Uri to file path FilePathConverter uriObj = new FilePathConverter(this, mediaUri); String filePath = uriObj.getFilePath(); - - //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 From 5d4768e1ee0aec6af9ab24b127b78896bdbfe5b2 Mon Sep 17 00:00:00 2001 From: misaochan Date: Mon, 11 Jan 2016 17:02:02 +1300 Subject: [PATCH 41/44] Simplify catList is empty logic --- .../java/fr/free/nrw/commons/upload/ShareActivity.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 d66543c63..d81aaea6b 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 @@ -191,7 +191,7 @@ public class ShareActivity //convert image Uri to file path FilePathConverter uriObj = new FilePathConverter(this, mediaUri); String filePath = uriObj.getFilePath(); - + if (filePath != null) { //extract the coordinates of image in decimal degrees @@ -206,11 +206,11 @@ public class ShareActivity app.cacheData.setQtPoint(decLongitude, decLatitude); MwVolleyApi apiCall = new MwVolleyApi(this); - 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()); From 9cd0f8fb5ae23026611f004e4cc00e04b7eff48d Mon Sep 17 00:00:00 2001 From: misaochan Date: Mon, 11 Jan 2016 17:05:08 +1300 Subject: [PATCH 42/44] Moved variables inside if block --- .../java/fr/free/nrw/commons/upload/ShareActivity.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) 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 d81aaea6b..c6eb6698b 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 @@ -198,17 +198,20 @@ public class ShareActivity 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); app.cacheData.setQtPoint(decLongitude, decLatitude); MwVolleyApi apiCall = new MwVolleyApi(this); + 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 (catListEmpty) { cacheFound = false; From 6537ff217cf38d6b00f8d6d3a43b34e938fc24ea Mon Sep 17 00:00:00 2001 From: misaochan Date: Mon, 11 Jan 2016 17:48:39 +1300 Subject: [PATCH 43/44] Tidied up displayCatList with List cas --- .../nrw/commons/caching/CacheController.java | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) 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 a5cc63242..ba749df28 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 @@ -46,29 +46,23 @@ 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; } public void convertCoordRange() { From 0fe4bcc4c77c4aab32f509268d6caed8b01fafb4 Mon Sep 17 00:00:00 2001 From: misaochan Date: Mon, 11 Jan 2016 17:50:46 +1300 Subject: [PATCH 44/44] Added algorithm link for searchWithin() --- .../main/java/fr/free/nrw/commons/caching/CacheController.java | 1 + 1 file changed, 1 insertion(+) 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 ba749df28..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 @@ -65,6 +65,7 @@ public class CacheController { 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;