Skip to content

Read once and reuse the query file's content #600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/src/main/assets/queries/nearby_query.rq
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ SELECT
SERVICE wikibase:around {
?item wdt:P625 ?location.
bd:serviceParam wikibase:center "Point(${LONG} ${LAT})"^^geo:wktLiteral.
bd:serviceParam wikibase:radius "${RADIUS}" . # Radius in kilometers.
bd:serviceParam wikibase:radius "${RAD}" . # Radius in kilometers.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you want to change the param to RAD

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I slightly prefer it because it will make code look more aligned.

                .replace("${RAD}", String.format(Locale.ROOT, "%.2f", radius))
                .replace("${LAT}", String.format(Locale.ROOT, "%.4f", cur.latitude))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In your previous PR I guess you forgot to make the change in the nearby_query.rb, so the nearby places stopped loading. Can you test it thoroughly once? I will be happy to merge it then. :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nearby list and map work okay for me with these devices.

  • Android 6.0.1, API 23 (real device)
  • Android 7.1.1, API 25 (emulator)
  • Android 5.1.1, API 22 (emulator)

Sorry about the oversight - the previous patch was incomplete.

}

# ... and without an image.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ public static List<Place> loadAttractionsFromLocation(LatLng curLatLng, Context
NearbyPlaces nearbyPlaces = CommonsApplication.getInstance().getNearbyPlaces();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
List<Place> places = prefs.getBoolean("useWikidata", true)
? nearbyPlaces.getFromWikidataQuery(
context,
curLatLng,
Locale.getDefault().getLanguage())
? nearbyPlaces.getFromWikidataQuery(curLatLng, Locale.getDefault().getLanguage())
: nearbyPlaces.getFromWikiNeedsPictures();
if (curLatLng != null) {
Timber.d("Sorting places by distance...");
Expand Down
44 changes: 24 additions & 20 deletions app/src/main/java/fr/free/nrw/commons/nearby/NearbyPlaces.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fr.free.nrw.commons.nearby;

import android.content.Context;
import android.net.Uri;
import android.os.StrictMode;

Expand All @@ -9,7 +8,6 @@
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -25,22 +23,32 @@
public class NearbyPlaces {

private static final int MIN_RESULTS = 40;
private static final double INITIAL_RADIUS = 1.0; // in kilometer
private static final double MAX_RADIUS = 300.0; // in kilometer
private static final double INITIAL_RADIUS = 1.0; // in kilometers
private static final double MAX_RADIUS = 300.0; // in kilometers
private static final double RADIUS_MULTIPLIER = 1.618;
private static final String WIKIDATA_QUERY_URL = "https://query.wikidata.org/sparql?query=${QUERY}";
private static final Uri WIKIDATA_QUERY_URL = Uri.parse("https://query.wikidata.org/sparql");
private static final Uri WIKIDATA_QUERY_UI_URL = Uri.parse("https://query.wikidata.org/");
private final String wikidataQuery;
private double radius = INITIAL_RADIUS;
private List<Place> places;

List<Place> getFromWikidataQuery(Context context,
LatLng curLatLng,
String lang) {
public NearbyPlaces() {
try {
String query = FileUtils.readFromResource("/assets/queries/nearby_query.rq");
wikidataQuery = query;
Timber.v(wikidataQuery);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

List<Place> getFromWikidataQuery(LatLng curLatLng, String lang) {
List<Place> places = Collections.emptyList();

try {
// increase the radius gradually to find a satisfactory number of nearby places
while (radius < MAX_RADIUS) {
places = getFromWikidataQuery(context, curLatLng, lang, radius);
places = getFromWikidataQuery(curLatLng, lang, radius);
Timber.d("%d results at radius: %f", places.size(), radius);
if (places.size() >= MIN_RESULTS) {
break;
Expand All @@ -58,28 +66,24 @@ List<Place> getFromWikidataQuery(Context context,
return places;
}

private List<Place> getFromWikidataQuery(Context context,
LatLng cur,
private List<Place> getFromWikidataQuery(LatLng cur,
String lang,
double radius)
throws IOException {
List<Place> places = new ArrayList<>();

String query = FileUtils.readFromFile(context, "queries/nearby_query.rq")
.replace("${RADIUS}", String.format(Locale.ROOT, "%.2f", radius))
String query = wikidataQuery
.replace("${RAD}", String.format(Locale.ROOT, "%.2f", radius))
.replace("${LAT}", String.format(Locale.ROOT, "%.4f", cur.latitude))
.replace("${LONG}", String.format(Locale.ROOT, "%.4f", cur.longitude))
.replace("${LANG}", lang);

Timber.d("Wikidata query "+ query);
Timber.v("# Wikidata query: \n" + query);

// format as a URL
String url = WIKIDATA_QUERY_URL.replace(
"${QUERY}",
URLEncoder.encode(query, "utf-8").replace("+", "%20")
);

Timber.d(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()));
Expand Down
33 changes: 16 additions & 17 deletions app/src/main/java/fr/free/nrw/commons/utils/FileUtils.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
package fr.free.nrw.commons.utils;

import android.content.Context;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import timber.log.Timber;
import fr.free.nrw.commons.CommonsApplication;

public class FileUtils {
public static String readFromFile(Context context, String fileName) {
String stringBuilder = "";
/**
* Read and return the content of a resource file as string.
*
* @param fileName asset file's path (e.g. "/assets/queries/nearby_query.rq")
* @return the content of the file
*/
public static String readFromResource(String fileName) throws IOException {
StringBuffer buffer = new StringBuffer();
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(context.getAssets().open(fileName)));
String mLine;
while ((mLine = reader.readLine()) != null) {
stringBuilder += mLine + "\n";
new InputStreamReader(
CommonsApplication.class.getResourceAsStream(fileName), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
} catch (IOException e) {
Timber.e("File not found exception", e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
reader.close();
}
}
return stringBuilder;
return buffer.toString();
}

/**
Expand Down