Search in sources :

Example 11 with LatLng

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"));
}
Also used : LatLng(fr.free.nrw.commons.location.LatLng) Test(org.junit.Test)

Example 12 with LatLng

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) URLConnection(java.net.URLConnection) URL(java.net.URL) BufferedReader(java.io.BufferedReader) LatLng(fr.free.nrw.commons.location.LatLng)

Example 13 with LatLng

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"));
}
Also used : LatLng(fr.free.nrw.commons.location.LatLng) Test(org.junit.Test)

Example 14 with LatLng

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"));
}
Also used : LatLng(fr.free.nrw.commons.location.LatLng) Test(org.junit.Test)

Example 15 with LatLng

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"));
}
Also used : LatLng(fr.free.nrw.commons.location.LatLng) Test(org.junit.Test)

Aggregations

LatLng (fr.free.nrw.commons.location.LatLng)22 Test (org.junit.Test)16 Bundle (android.os.Bundle)2 NearbyBaseMarker (fr.free.nrw.commons.nearby.NearbyBaseMarker)2 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 Uri (android.net.Uri)1 StrictMode (android.os.StrictMode)1 OnItemClick (butterknife.OnItemClick)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 Place (fr.free.nrw.commons.nearby.Place)1 UriDeserializer (fr.free.nrw.commons.utils.UriDeserializer)1 IOException (java.io.IOException)1 Type (java.lang.reflect.Type)1 URLConnection (java.net.URLConnection)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1