Get coordinate template when parsing image page

Currently the image detail view does not display the
coordinates.
This commit adds the coordinate template to the
parser and displays the results rounded to 4 digits.
This commit is contained in:
Tobias Schönberg 2017-05-13 16:09:01 +02:00
parent 81d23a3b94
commit c6a2f2c7ea
4 changed files with 91 additions and 0 deletions

View file

@ -144,6 +144,14 @@ public class Media implements Parcelable {
this.license = license;
}
public String getCoordinates() {
return coordinates;
}
public void setCoordinates(String coordinates) {
this.coordinates = coordinates;
}
// Primary metadata fields
protected Uri localUri;
protected String imageUrl;
@ -155,6 +163,7 @@ public class Media implements Parcelable {
protected int width;
protected int height;
protected String license;
protected String coordinates;
protected String creator;
protected ArrayList<String> categories; // as loaded at runtime?
protected Map<String, String> descriptions; // multilingual descriptions as loaded

View file

@ -38,6 +38,7 @@ public class MediaDataExtractor {
private String author;
private Date date;
private String license;
private String coordinates;
private LicenseList licenseList;
/**
@ -122,6 +123,14 @@ public class MediaDataExtractor {
author = getFlatText(authorNode);
}
Node coordinateTemplateNode = findTemplate(doc.getDocumentElement(), "location");
if (coordinateTemplateNode != null) {
coordinates = getCoordinates(coordinateTemplateNode);
} else {
coordinates = "No coordinates found";
}
/*
Pull up the license data list...
look for the templates in two ways:
@ -242,6 +251,35 @@ 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:
* {{Location|47.50111007666667|19.055700301944444}}
* and extracts the latitude and longitude as a pretty string.
*
* @param parentNode The node of the coordinates template.
* @return Pretty formatted coordinates.
* @throws IOException
*/
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";
}
// Extract a dictionary of multilingual texts from a subset of the parse tree.
// Texts are wrapped in things like {{en|foo} or {{en|1=foo bar}}.
// Text outside those wrappers is stuffed into a 'default' faux language key if present.
@ -287,6 +325,7 @@ public class MediaDataExtractor {
media.setCategories(categories);
media.setDescriptions(descriptions);
media.setCoordinates(coordinates);
if (license != null) {
media.setLicense(license);
}

View file

@ -70,6 +70,7 @@ public class MediaDetailFragment extends Fragment {
private TextView title;
private TextView desc;
private TextView license;
private TextView coordinates;
private LinearLayout categoryContainer;
private ScrollView scrollView;
private ArrayList<String> categoryNames;
@ -123,6 +124,7 @@ public class MediaDetailFragment extends Fragment {
title = (TextView) view.findViewById(R.id.mediaDetailTitle);
desc = (TextView) view.findViewById(R.id.mediaDetailDesc);
license = (TextView) view.findViewById(R.id.mediaDetailLicense);
coordinates = (TextView) view.findViewById(R.id.mediaDetailCoordinates);
categoryContainer = (LinearLayout) view.findViewById(R.id.mediaDetailCategoryContainer);
licenseList = new LicenseList(getActivity());
@ -226,6 +228,7 @@ public class MediaDetailFragment extends Fragment {
// Set text of desc, license, and categories
desc.setText(prettyDescription(media));
license.setText(prettyLicense(media));
coordinates.setText(prettyCoordinates(media));
categoryNames.removeAll(categoryNames);
categoryNames.addAll(media.getCategories());
@ -388,4 +391,15 @@ public class MediaDetailFragment extends Fragment {
return licenseObj.getName();
}
}
/**
* Returns the coordinates nicely formatted.
*
* @return Coordinates as text.
*/
private String prettyCoordinates(Media media) {
String coordinates = media.getCoordinates();
return coordinates;
}
}