mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
Move coordinate logic to LatLng class
This commit is contained in:
parent
6b30e5e297
commit
94b19f27b9
2 changed files with 54 additions and 15 deletions
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue