From 94b19f27b9508131c050a215bc4308677ef46117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Sch=C3=B6nberg?= Date: Sat, 13 May 2017 17:22:09 +0200 Subject: [PATCH] Move coordinate logic to LatLng class --- .../free/nrw/commons/MediaDataExtractor.java | 21 +++----- .../fr/free/nrw/commons/location/LatLng.java | 48 +++++++++++++++++++ 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/MediaDataExtractor.java b/app/src/main/java/fr/free/nrw/commons/MediaDataExtractor.java index cb22d9b3c..dd012926a 100644 --- a/app/src/main/java/fr/free/nrw/commons/MediaDataExtractor.java +++ b/app/src/main/java/fr/free/nrw/commons/MediaDataExtractor.java @@ -20,6 +20,7 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import fr.free.nrw.commons.location.LatLng; import timber.log.Timber; /** @@ -251,18 +252,6 @@ public class MediaDataExtractor { return parentNode.getTextContent(); } - /** - * Rounds the float to 4 digits. - * - * @param coordinate A coordinate value as string. - * @return String of the rounded number. - */ - private String formatCoordinate(String coordinate) { - Float floatNumber = Float.parseFloat(coordinate); - double roundedNumber = Math.round(floatNumber * 10000d) / 10000d; - return String.valueOf(roundedNumber); - } - /** * Extracts the coordinates from the template and returns them as pretty formatted string. * Loops over the children of the coordinate template: @@ -275,9 +264,11 @@ public class MediaDataExtractor { */ private String getCoordinates(Node parentNode) throws IOException { NodeList childNodes = parentNode.getChildNodes(); - String latitudeText = formatCoordinate(childNodes.item(1).getTextContent()); - String longitudeText = formatCoordinate(childNodes.item(2).getTextContent()); - return latitudeText + " N," + longitudeText + " E"; + double latitudeText = Double.parseDouble(childNodes.item(1).getTextContent()); + double longitudeText = Double.parseDouble(childNodes.item(2).getTextContent()); + LatLng coordinates = new LatLng(latitudeText, longitudeText); + + return coordinates.getPrettyCoordinateString(); } // Extract a dictionary of multilingual texts from a subset of the parse tree. diff --git a/app/src/main/java/fr/free/nrw/commons/location/LatLng.java b/app/src/main/java/fr/free/nrw/commons/location/LatLng.java index 839cba14e..c84095eb6 100644 --- a/app/src/main/java/fr/free/nrw/commons/location/LatLng.java +++ b/app/src/main/java/fr/free/nrw/commons/location/LatLng.java @@ -42,4 +42,52 @@ public class LatLng { public String toString() { return "lat/lng: (" + this.latitude + "," + this.longitude + ")"; } + + /** + * Rounds the float to 4 digits. + * + * @param coordinate A coordinate value as string. + * @return String of the rounded number. + */ + private String formatCoordinate(double coordinate) { + double roundedNumber = Math.round(coordinate * 10000d) / 10000d; + return String.valueOf(roundedNumber); + } + + /** + * Returns "N" or "S" depending on the latitude. + * + * @return "N" or "S". + */ + private String getNorthSouth() { + if (this.latitude < 0) { + return "S"; + } + + return "N"; + } + + /** + * Returns "E" or "W" depending on the longitude. + * + * @return "E" or "W". + */ + private String getEastWest() { + if (this.longitude < 180) { + return "E"; + } + + return "W"; + } + + /** + * Returns a nicely formatted coordinate string. Used e.g. in + * the detail view. + * + * @return The formatted string. + */ + public String getPrettyCoordinateString() { + return formatCoordinate(this.latitude) + " " + this.getNorthSouth() + ", " + + formatCoordinate(this.longitude) + " " + this.getEastWest(); + } }