Merge pull request #564 from tobias47n9e/master

Get coordinate template when parsing image page
This commit is contained in:
Josephine Lim 2017-05-14 10:20:17 +02:00 committed by GitHub
commit 3a92de0c21
5 changed files with 131 additions and 0 deletions

View file

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

View file

@ -1,5 +1,7 @@
package fr.free.nrw.commons; package fr.free.nrw.commons;
import fr.free.nrw.commons.location.LatLng;
import org.mediawiki.api.ApiResult; import org.mediawiki.api.ApiResult;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
@ -38,6 +40,7 @@ public class MediaDataExtractor {
private String author; private String author;
private Date date; private Date date;
private String license; private String license;
private String coordinates;
private LicenseList licenseList; private LicenseList licenseList;
/** /**
@ -122,6 +125,14 @@ public class MediaDataExtractor {
author = getFlatText(authorNode); 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... Pull up the license data list...
look for the templates in two ways: look for the templates in two ways:
@ -242,6 +253,25 @@ public class MediaDataExtractor {
return parentNode.getTextContent(); return parentNode.getTextContent();
} }
/**
* 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 Parsing failed.
*/
private String getCoordinates(Node parentNode) throws IOException {
NodeList childNodes = parentNode.getChildNodes();
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. // 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}}. // 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. // Text outside those wrappers is stuffed into a 'default' faux language key if present.
@ -287,6 +317,7 @@ public class MediaDataExtractor {
media.setCategories(categories); media.setCategories(categories);
media.setDescriptions(descriptions); media.setDescriptions(descriptions);
media.setCoordinates(coordinates);
if (license != null) { if (license != null) {
media.setLicense(license); media.setLicense(license);
} }

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();
}
} }

View file

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

View file

@ -159,6 +159,35 @@
/> />
</LinearLayout> </LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/subBackground"
android:padding="16dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:text="Coordinates"
android:textSize="16sp"
android:textStyle="bold"
android:paddingBottom="6dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="License link"
android:id="@+id/mediaDetailCoordinates"
android:layout_gravity="left|start"
android:background="?attr/subBackground"
android:textColor="@android:color/white"
android:textSize="14sp"
android:padding="12dp"
/>
</LinearLayout>
<fr.free.nrw.commons.media.MediaDetailSpacer <fr.free.nrw.commons.media.MediaDetailSpacer
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="8dp" android:layout_height="8dp"