Skip to content

Commit fead6a8

Browse files
committed
Read once and reuse the query file's content
1 parent 825cdaf commit fead6a8

File tree

4 files changed

+51
-54
lines changed

4 files changed

+51
-54
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

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package fr.free.nrw.commons.nearby;
22

3+
import static fr.free.nrw.commons.utils.LengthUtils.computeDistanceBetween;
4+
import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween;
5+
36
import android.content.Context;
47
import android.content.SharedPreferences;
58
import android.preference.PreferenceManager;
6-
79
import com.mapbox.mapboxsdk.annotations.Icon;
810
import com.mapbox.mapboxsdk.annotations.IconFactory;
911

12+
import fr.free.nrw.commons.CommonsApplication;
13+
import fr.free.nrw.commons.R;
14+
import fr.free.nrw.commons.location.LatLng;
1015
import java.util.ArrayList;
1116
import java.util.Collections;
1217
import java.util.Comparator;
@@ -15,14 +20,8 @@
1520
import java.util.Locale;
1621
import java.util.Map;
1722

18-
import fr.free.nrw.commons.CommonsApplication;
19-
import fr.free.nrw.commons.R;
20-
import fr.free.nrw.commons.location.LatLng;
2123
import timber.log.Timber;
2224

23-
import static fr.free.nrw.commons.utils.LengthUtils.computeDistanceBetween;
24-
import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween;
25-
2625

2726
public class NearbyController {
2827
private static final int MAX_RESULTS = 1000;
@@ -41,10 +40,7 @@ public static List<Place> loadAttractionsFromLocation(LatLng curLatLng, Context
4140
NearbyPlaces nearbyPlaces = CommonsApplication.getInstance().getNearbyPlaces();
4241
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
4342
List<Place> places = prefs.getBoolean("useWikidata", true)
44-
? nearbyPlaces.getFromWikidataQuery(
45-
context,
46-
curLatLng,
47-
Locale.getDefault().getLanguage())
43+
? nearbyPlaces.getFromWikidataQuery(curLatLng, Locale.getDefault().getLanguage())
4844
: nearbyPlaces.getFromWikiNeedsPictures();
4945
if (curLatLng != null) {
5046
Timber.d("Sorting places by distance...");

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

+27-25
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,52 @@
11
package fr.free.nrw.commons.nearby;
22

3-
import android.content.Context;
43
import android.net.Uri;
54
import android.os.StrictMode;
6-
5+
import fr.free.nrw.commons.Utils;
6+
import fr.free.nrw.commons.location.LatLng;
7+
import fr.free.nrw.commons.utils.FileUtils;
78
import java.io.BufferedReader;
89
import java.io.IOException;
910
import java.io.InputStreamReader;
1011
import java.net.URL;
1112
import java.net.URLConnection;
12-
import java.net.URLEncoder;
1313
import java.util.ArrayList;
1414
import java.util.Collections;
1515
import java.util.List;
1616
import java.util.Locale;
1717
import java.util.regex.Matcher;
1818
import java.util.regex.Pattern;
19-
20-
import fr.free.nrw.commons.Utils;
21-
import fr.free.nrw.commons.location.LatLng;
22-
import fr.free.nrw.commons.utils.FileUtils;
2319
import timber.log.Timber;
2420

2521
public class NearbyPlaces {
2622

2723
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
24+
private static final double INITIAL_RADIUS = 1.0; // in kilometers
25+
private static final double MAX_RADIUS = 300.0; // in kilometers
3026
private static final double RADIUS_MULTIPLIER = 1.618;
31-
private static final String WIKIDATA_QUERY_URL = "https://query.wikidata.org/sparql?query=${QUERY}";
27+
private static final Uri WIKIDATA_QUERY_URL = Uri.parse("https://query.wikidata.org/sparql");
28+
private static final Uri WIKIDATA_QUERY_UI_URL = Uri.parse("https://query.wikidata.org/");
29+
private final String wikidataQuery;
3230
private double radius = INITIAL_RADIUS;
3331
private List<Place> places;
3432

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

4046
try {
4147
// increase the radius gradually to find a satisfactory number of nearby places
4248
while (radius < MAX_RADIUS) {
43-
places = getFromWikidataQuery(context, curLatLng, lang, radius);
49+
places = getFromWikidataQuery(curLatLng, lang, radius);
4450
Timber.d("%d results at radius: %f", places.size(), radius);
4551
if (places.size() >= MIN_RESULTS) {
4652
break;
@@ -58,28 +64,24 @@ List<Place> getFromWikidataQuery(Context context,
5864
return places;
5965
}
6066

61-
private List<Place> getFromWikidataQuery(Context context,
62-
LatLng cur,
67+
private List<Place> getFromWikidataQuery(LatLng cur,
6368
String lang,
6469
double radius)
6570
throws IOException {
6671
List<Place> places = new ArrayList<>();
6772

68-
String query = FileUtils.readFromFile(context, "queries/nearby_query.rq")
69-
.replace("${RADIUS}", String.format(Locale.ROOT, "%.2f", radius))
73+
String query = wikidataQuery
74+
.replace("${RAD}", String.format(Locale.ROOT, "%.2f", radius))
7075
.replace("${LAT}", String.format(Locale.ROOT, "%.4f", cur.latitude))
7176
.replace("${LONG}", String.format(Locale.ROOT, "%.4f", cur.longitude))
7277
.replace("${LANG}", lang);
7378

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

7681
// 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);
82+
Timber.d(WIKIDATA_QUERY_UI_URL.buildUpon().fragment(query).build().toString());
83+
String url = WIKIDATA_QUERY_URL.buildUpon()
84+
.appendQueryParameter("query", query).build().toString();
8385
URLConnection conn = new URL(url).openConnection();
8486
conn.setRequestProperty("Accept", "text/tab-separated-values");
8587
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)