use of fr.free.nrw.commons.location.LatLng in project apps-android-commons by commons-app.
the class LengthUtilsTest method testOneDegreeOnEquator.
@Test
public void testOneDegreeOnEquator() {
LatLng pointA = new LatLng(0, 0, 0);
LatLng pointB = new LatLng(0, 1, 0);
String distance = LengthUtils.formatDistanceBetween(pointA, pointB);
Assert.assertThat(distance, is("111.2km"));
}
use of fr.free.nrw.commons.location.LatLng in project apps-android-commons by commons-app.
the class NearbyPlaces method getFromWikidataQuery.
private List<Place> getFromWikidataQuery(LatLng cur, String lang, double radius) throws IOException {
List<Place> places = new ArrayList<>();
String query = wikidataQuery.replace("${RAD}", String.format(Locale.ROOT, "%.2f", radius)).replace("${LAT}", String.format(Locale.ROOT, "%.4f", cur.getLatitude())).replace("${LONG}", String.format(Locale.ROOT, "%.4f", cur.getLongitude())).replace("${LANG}", lang);
Timber.v("# Wikidata query: \n" + query);
// format as a URL
Timber.d(WIKIDATA_QUERY_UI_URL.buildUpon().fragment(query).build().toString());
String url = WIKIDATA_QUERY_URL.buildUpon().appendQueryParameter("query", query).build().toString();
URLConnection conn = new URL(url).openConnection();
conn.setRequestProperty("Accept", "text/tab-separated-values");
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
Timber.d("Reading from query result...");
while ((line = in.readLine()) != null) {
Timber.v(line);
// to pad columns and make fields a fixed size
line = line + "\n";
if (!line.startsWith("\"Point")) {
continue;
}
String[] fields = line.split("\t");
String point = fields[0];
String name = Utils.stripLocalizedString(fields[2]);
String type = Utils.stripLocalizedString(fields[4]);
String wikipediaSitelink = Utils.stripLocalizedString(fields[7]);
String commonsSitelink = Utils.stripLocalizedString(fields[8]);
String wikiDataLink = Utils.stripLocalizedString(fields[1]);
String icon = fields[5];
double latitude = 0;
double longitude = 0;
Matcher matcher = Pattern.compile("Point\\(([^ ]+) ([^ ]+)\\)").matcher(point);
if (!matcher.find()) {
continue;
}
try {
longitude = Double.parseDouble(matcher.group(1));
latitude = Double.parseDouble(matcher.group(2));
} catch (NumberFormatException e) {
throw new RuntimeException("LatLng parse error: " + point);
}
places.add(new Place(name, // list
type, // details
type, Uri.parse(icon), new LatLng(latitude, longitude, 0), new Sitelinks.Builder().setWikipediaLink(wikipediaSitelink).setCommonsLink(commonsSitelink).setWikidataLink(wikiDataLink).build()));
}
in.close();
return places;
}
use of fr.free.nrw.commons.location.LatLng in project apps-android-commons by commons-app.
the class LengthUtilsTest method testPoleToPole.
@Test
public void testPoleToPole() {
LatLng pointA = new LatLng(90, 0, 0);
LatLng pointB = new LatLng(-90, 0, 0);
String distance = LengthUtils.formatDistanceBetween(pointA, pointB);
Assert.assertThat(distance, is("20,015.1km"));
}
use of fr.free.nrw.commons.location.LatLng in project apps-android-commons by commons-app.
the class LengthUtilsTest method testZeroDistance.
@Test
public void testZeroDistance() {
LatLng pointA = new LatLng(0, 0, 0);
LatLng pointB = new LatLng(0, 0, 0);
String distance = LengthUtils.formatDistanceBetween(pointA, pointB);
Assert.assertThat(distance, is("0m"));
}
use of fr.free.nrw.commons.location.LatLng in project apps-android-commons by commons-app.
the class LengthUtilsTest method testOneDegreeSouthPole.
@Test
public void testOneDegreeSouthPole() {
LatLng pointA = new LatLng(-90, 0, 0);
LatLng pointB = new LatLng(-90, 1, 0);
String distance = LengthUtils.formatDistanceBetween(pointA, pointB);
Assert.assertThat(distance, is("0m"));
}
Aggregations