Move coordinate logic to LatLng class

This commit is contained in:
Tobias Schönberg 2017-05-13 17:22:09 +02:00
parent 6b30e5e297
commit 94b19f27b9
2 changed files with 54 additions and 15 deletions

View file

@ -20,6 +20,7 @@ import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import fr.free.nrw.commons.location.LatLng;
import timber.log.Timber; import timber.log.Timber;
/** /**
@ -251,18 +252,6 @@ public class MediaDataExtractor {
return parentNode.getTextContent(); 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. * Extracts the coordinates from the template and returns them as pretty formatted string.
* Loops over the children of the coordinate template: * Loops over the children of the coordinate template:
@ -275,9 +264,11 @@ public class MediaDataExtractor {
*/ */
private String getCoordinates(Node parentNode) throws IOException { private String getCoordinates(Node parentNode) throws IOException {
NodeList childNodes = parentNode.getChildNodes(); NodeList childNodes = parentNode.getChildNodes();
String latitudeText = formatCoordinate(childNodes.item(1).getTextContent()); double latitudeText = Double.parseDouble(childNodes.item(1).getTextContent());
String longitudeText = formatCoordinate(childNodes.item(2).getTextContent()); double longitudeText = Double.parseDouble(childNodes.item(2).getTextContent());
return latitudeText + " N," + longitudeText + " E"; LatLng coordinates = new LatLng(latitudeText, longitudeText);
return coordinates.getPrettyCoordinateString();
} }
// Extract a dictionary of multilingual texts from a subset of the parse tree. // Extract a dictionary of multilingual texts from a subset of the parse tree.

View file

@ -42,4 +42,52 @@ public class LatLng {
public String toString() { public String toString() {
return "lat/lng: (" + this.latitude + "," + this.longitude + ")"; 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();
}
} }