Skip to content

Commit 7739085

Browse files
committed
Read once and reuse the query file's content
1 parent 1a1fc14 commit 7739085

File tree

4 files changed

+63
-68
lines changed

4 files changed

+63
-68
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

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
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;
9-
11+
import fr.free.nrw.commons.R;
12+
import fr.free.nrw.commons.location.LatLng;
1013
import java.util.ArrayList;
1114
import java.util.Collections;
1215
import java.util.Comparator;
1316
import java.util.HashMap;
1417
import java.util.List;
1518
import java.util.Locale;
1619
import java.util.Map;
17-
18-
import fr.free.nrw.commons.R;
19-
import fr.free.nrw.commons.location.LatLng;
2020
import timber.log.Timber;
2121

22-
import static fr.free.nrw.commons.utils.LengthUtils.computeDistanceBetween;
23-
import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween;
24-
2522

2623
public class NearbyController {
2724
private static final int MAX_RESULTS = 1000;
@@ -38,12 +35,10 @@ public static List<Place> loadAttractionsFromLocation(LatLng curLatLng, Context
3835
return Collections.emptyList();
3936
}
4037
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
38+
NearbyPlaces nearbyPlaces = NearbyPlaces.getInstance();
4139
List<Place> places = prefs.getBoolean("useWikidata", true)
42-
? NearbyPlaces.getInstance().getFromWikidataQuery(
43-
context,
44-
curLatLng,
45-
Locale.getDefault().getLanguage())
46-
: NearbyPlaces.getInstance().getFromWikiNeedsPictures();
40+
? nearbyPlaces.getFromWikidataQuery(curLatLng, Locale.getDefault().getLanguage())
41+
: nearbyPlaces.getFromWikiNeedsPictures();
4742
if (curLatLng != null) {
4843
Timber.d("Sorting places by distance...");
4944
final Map<Place, Double> distances = new HashMap<>();

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

+38-37
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import android.content.Context;
44
import android.net.Uri;
55
import android.os.StrictMode;
6-
6+
import fr.free.nrw.commons.Utils;
7+
import fr.free.nrw.commons.location.LatLng;
8+
import fr.free.nrw.commons.utils.FileUtils;
79
import java.io.BufferedReader;
810
import java.io.IOException;
911
import java.io.InputStreamReader;
@@ -16,35 +18,51 @@
1618
import java.util.Locale;
1719
import java.util.regex.Matcher;
1820
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;
2321
import timber.log.Timber;
2422

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/");
3231
private static NearbyPlaces singleton;
3332
private double radius = INITIAL_RADIUS;
3433
private List<Place> places;
34+
private final String wikidataQuery;
35+
36+
private NearbyPlaces() throws IOException {
37+
String query = FileUtils.readFromResource("/assets/queries/nearby_query.rq");
38+
wikidataQuery = query;
39+
Timber.v(wikidataQuery);
40+
}
3541

36-
private NearbyPlaces(){
42+
/**
43+
* Get the singleton instance of this class.
44+
* The instance is created upon the first invocation of this method, and then reused.
45+
*
46+
* @return The singleton instance
47+
*/
48+
public static synchronized NearbyPlaces getInstance() {
49+
if (singleton == null) {
50+
try {
51+
singleton = new NearbyPlaces();
52+
} catch (IOException e) {
53+
throw new RuntimeException(e);
54+
}
55+
}
56+
return singleton;
3757
}
3858

39-
List<Place> getFromWikidataQuery(Context context,
40-
LatLng curLatLng,
41-
String lang) {
59+
List<Place> getFromWikidataQuery(LatLng curLatLng, String lang) {
4260
List<Place> places = Collections.emptyList();
4361

4462
try {
4563
// increase the radius gradually to find a satisfactory number of nearby places
4664
while (radius < MAX_RADIUS) {
47-
places = getFromWikidataQuery(context, curLatLng, lang, radius);
65+
places = getFromWikidataQuery(curLatLng, lang, radius);
4866
Timber.d("%d results at radius: %f", places.size(), radius);
4967
if (places.size() >= MIN_RESULTS) {
5068
break;
@@ -62,28 +80,24 @@ List<Place> getFromWikidataQuery(Context context,
6280
return places;
6381
}
6482

65-
private List<Place> getFromWikidataQuery(Context context,
66-
LatLng cur,
83+
private List<Place> getFromWikidataQuery(LatLng cur,
6784
String lang,
6885
double radius)
6986
throws IOException {
7087
List<Place> places = new ArrayList<>();
7188

72-
String query = FileUtils.readFromFile(context, "queries/nearby_query.rq")
73-
.replace("${RADIUS}", String.format(Locale.ROOT, "%.2f", radius))
89+
String query = wikidataQuery
90+
.replace("${RAD}", String.format(Locale.ROOT, "%.2f", radius))
7491
.replace("${LAT}", String.format(Locale.ROOT, "%.4f", cur.latitude))
7592
.replace("${LONG}", String.format(Locale.ROOT, "%.4f", cur.longitude))
7693
.replace("${LANG}", lang);
7794

78-
Timber.d("Wikidata query "+ query);
95+
Timber.v("# Wikidata query: \n"+ query);
7996

8097
// format as a URL
81-
String url = WIKIDATA_QUERY_URL.replace(
82-
"${QUERY}",
83-
URLEncoder.encode(query, "utf-8").replace("+", "%20")
84-
);
85-
86-
Timber.d(url);
98+
Timber.d(WIKIDATA_QUERY_UI_URL.buildUpon().fragment(query).build().toString());
99+
String url = WIKIDATA_QUERY_URL.buildUpon()
100+
.appendQueryParameter("query", query).build().toString();
87101
URLConnection conn = new URL(url).openConnection();
88102
conn.setRequestProperty("Accept", "text/tab-separated-values");
89103
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
@@ -199,17 +213,4 @@ List<Place> getFromWikiNeedsPictures() {
199213
}
200214
return places;
201215
}
202-
203-
/**
204-
* Get the singleton instance of this class.
205-
* The instance is created upon the first invocation of this method, and then reused.
206-
*
207-
* @return The singleton instance
208-
*/
209-
public static synchronized NearbyPlaces getInstance() {
210-
if (singleton == null) {
211-
singleton = new NearbyPlaces();
212-
}
213-
return singleton;
214-
}
215216
}
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
11
package fr.free.nrw.commons.utils;
22

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

9-
import timber.log.Timber;
7+
import fr.free.nrw.commons.CommonsApplication;
108

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

0 commit comments

Comments
 (0)