Skip to content

Commit f6a7759

Browse files
authored
Merge pull request commons-app#600 from whym/nearby-file
Read once and reuse the query file's content
2 parents 6232e35 + 22f5f51 commit f6a7759

File tree

4 files changed

+42
-42
lines changed

4 files changed

+42
-42
lines changed

app/src/main/assets/queries/nearby_query.rq

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ SELECT
1313
SERVICE wikibase:around {
1414
?item wdt:P625 ?location.
1515
bd:serviceParam wikibase:center "Point(${LONG} ${LAT})"^^geo:wktLiteral.
16-
bd:serviceParam wikibase:radius "${RADIUS}" . # Radius in kilometers.
16+
bd:serviceParam wikibase:radius "${RAD}" . # Radius in kilometers.
1717
}
1818

1919
# ... and without an image.

app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ public static List<Place> loadAttractionsFromLocation(LatLng curLatLng, Context
4141
NearbyPlaces nearbyPlaces = CommonsApplication.getInstance().getNearbyPlaces();
4242
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
4343
List<Place> places = prefs.getBoolean("useWikidata", true)
44-
? nearbyPlaces.getFromWikidataQuery(
45-
context,
46-
curLatLng,
47-
Locale.getDefault().getLanguage())
44+
? nearbyPlaces.getFromWikidataQuery(curLatLng, Locale.getDefault().getLanguage())
4845
: nearbyPlaces.getFromWikiNeedsPictures();
4946
if (curLatLng != null) {
5047
Timber.d("Sorting places by distance...");

app/src/main/java/fr/free/nrw/commons/nearby/NearbyPlaces.java

+24-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package fr.free.nrw.commons.nearby;
22

3-
import android.content.Context;
43
import android.net.Uri;
54
import android.os.StrictMode;
65

@@ -9,7 +8,6 @@
98
import java.io.InputStreamReader;
109
import java.net.URL;
1110
import java.net.URLConnection;
12-
import java.net.URLEncoder;
1311
import java.util.ArrayList;
1412
import java.util.Collections;
1513
import java.util.List;
@@ -25,22 +23,32 @@
2523
public class NearbyPlaces {
2624

2725
private static final int MIN_RESULTS = 40;
28-
private static final double INITIAL_RADIUS = 1.0; // in kilometer
29-
private static final double MAX_RADIUS = 300.0; // in kilometer
26+
private static final double INITIAL_RADIUS = 1.0; // in kilometers
27+
private static final double MAX_RADIUS = 300.0; // in kilometers
3028
private static final double RADIUS_MULTIPLIER = 1.618;
31-
private static final String WIKIDATA_QUERY_URL = "https://query.wikidata.org/sparql?query=${QUERY}";
29+
private static final Uri WIKIDATA_QUERY_URL = Uri.parse("https://query.wikidata.org/sparql");
30+
private static final Uri WIKIDATA_QUERY_UI_URL = Uri.parse("https://query.wikidata.org/");
31+
private final String wikidataQuery;
3232
private double radius = INITIAL_RADIUS;
3333
private List<Place> places;
3434

35-
List<Place> getFromWikidataQuery(Context context,
36-
LatLng curLatLng,
37-
String lang) {
35+
public NearbyPlaces() {
36+
try {
37+
String query = FileUtils.readFromResource("/assets/queries/nearby_query.rq");
38+
wikidataQuery = query;
39+
Timber.v(wikidataQuery);
40+
} catch (IOException e) {
41+
throw new RuntimeException(e);
42+
}
43+
}
44+
45+
List<Place> getFromWikidataQuery(LatLng curLatLng, String lang) {
3846
List<Place> places = Collections.emptyList();
3947

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

61-
private List<Place> getFromWikidataQuery(Context context,
62-
LatLng cur,
69+
private List<Place> getFromWikidataQuery(LatLng cur,
6370
String lang,
6471
double radius)
6572
throws IOException {
6673
List<Place> places = new ArrayList<>();
6774

68-
String query = FileUtils.readFromFile(context, "queries/nearby_query.rq")
69-
.replace("${RADIUS}", String.format(Locale.ROOT, "%.2f", radius))
75+
String query = wikidataQuery
76+
.replace("${RAD}", String.format(Locale.ROOT, "%.2f", radius))
7077
.replace("${LAT}", String.format(Locale.ROOT, "%.4f", cur.latitude))
7178
.replace("${LONG}", String.format(Locale.ROOT, "%.4f", cur.longitude))
7279
.replace("${LANG}", lang);
7380

74-
Timber.d("Wikidata query "+ query);
81+
Timber.v("# Wikidata query: \n" + query);
7582

7683
// format as a URL
77-
String url = WIKIDATA_QUERY_URL.replace(
78-
"${QUERY}",
79-
URLEncoder.encode(query, "utf-8").replace("+", "%20")
80-
);
81-
82-
Timber.d(url);
84+
Timber.d(WIKIDATA_QUERY_UI_URL.buildUpon().fragment(query).build().toString());
85+
String url = WIKIDATA_QUERY_URL.buildUpon()
86+
.appendQueryParameter("query", query).build().toString();
8387
URLConnection conn = new URL(url).openConnection();
8488
conn.setRequestProperty("Accept", "text/tab-separated-values");
8589
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

app/src/main/java/fr/free/nrw/commons/utils/FileUtils.java

+16-17
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
package fr.free.nrw.commons.utils;
22

3-
import android.content.Context;
4-
53
import java.io.BufferedReader;
64
import java.io.File;
75
import java.io.IOException;
86
import java.io.InputStreamReader;
97

10-
import timber.log.Timber;
8+
import fr.free.nrw.commons.CommonsApplication;
119

1210
public class FileUtils {
13-
public static String readFromFile(Context context, String fileName) {
14-
String stringBuilder = "";
11+
/**
12+
* Read and return the content of a resource file as string.
13+
*
14+
* @param fileName asset file's path (e.g. "/assets/queries/nearby_query.rq")
15+
* @return the content of the file
16+
*/
17+
public static String readFromResource(String fileName) throws IOException {
18+
StringBuffer buffer = new StringBuffer();
1519
BufferedReader reader = null;
1620
try {
1721
reader = new BufferedReader(
18-
new InputStreamReader(context.getAssets().open(fileName)));
19-
String mLine;
20-
while ((mLine = reader.readLine()) != null) {
21-
stringBuilder += mLine + "\n";
22+
new InputStreamReader(
23+
CommonsApplication.class.getResourceAsStream(fileName), "UTF-8"));
24+
String line;
25+
while ((line = reader.readLine()) != null) {
26+
buffer.append(line + "\n");
2227
}
23-
} catch (IOException e) {
24-
Timber.e("File not found exception", e);
2528
} finally {
2629
if (reader != null) {
27-
try {
28-
reader.close();
29-
} catch (IOException e) {
30-
//log the exception
31-
}
30+
reader.close();
3231
}
3332
}
34-
return stringBuilder;
33+
return buffer.toString();
3534
}
3635

3736
/**

0 commit comments

Comments
 (0)