3
3
import android .content .Context ;
4
4
import android .net .Uri ;
5
5
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 ;
7
9
import java .io .BufferedReader ;
8
10
import java .io .IOException ;
9
11
import java .io .InputStreamReader ;
16
18
import java .util .Locale ;
17
19
import java .util .regex .Matcher ;
18
20
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 ;
23
21
import timber .log .Timber ;
24
22
25
23
public class NearbyPlaces {
26
24
27
25
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
30
28
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/" );
32
31
private static NearbyPlaces singleton ;
33
32
private double radius = INITIAL_RADIUS ;
34
33
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
+ }
35
41
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 ;
37
57
}
38
58
39
- List <Place > getFromWikidataQuery (Context context ,
40
- LatLng curLatLng ,
41
- String lang ) {
59
+ List <Place > getFromWikidataQuery (LatLng curLatLng , String lang ) {
42
60
List <Place > places = Collections .emptyList ();
43
61
44
62
try {
45
63
// increase the radius gradually to find a satisfactory number of nearby places
46
64
while (radius < MAX_RADIUS ) {
47
- places = getFromWikidataQuery (context , curLatLng , lang , radius );
65
+ places = getFromWikidataQuery (curLatLng , lang , radius );
48
66
Timber .d ("%d results at radius: %f" , places .size (), radius );
49
67
if (places .size () >= MIN_RESULTS ) {
50
68
break ;
@@ -62,28 +80,24 @@ List<Place> getFromWikidataQuery(Context context,
62
80
return places ;
63
81
}
64
82
65
- private List <Place > getFromWikidataQuery (Context context ,
66
- LatLng cur ,
83
+ private List <Place > getFromWikidataQuery (LatLng cur ,
67
84
String lang ,
68
85
double radius )
69
86
throws IOException {
70
87
List <Place > places = new ArrayList <>();
71
88
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 ))
74
91
.replace ("${LAT}" , String .format (Locale .ROOT , "%.4f" , cur .latitude ))
75
92
.replace ("${LONG}" , String .format (Locale .ROOT , "%.4f" , cur .longitude ))
76
93
.replace ("${LANG}" , lang );
77
94
78
- Timber .d ( " Wikidata query " + query );
95
+ Timber .v ( "# Wikidata query: \n " + query );
79
96
80
97
// 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 ();
87
101
URLConnection conn = new URL (url ).openConnection ();
88
102
conn .setRequestProperty ("Accept" , "text/tab-separated-values" );
89
103
BufferedReader in = new BufferedReader (new InputStreamReader (conn .getInputStream ()));
@@ -199,17 +213,4 @@ List<Place> getFromWikiNeedsPictures() {
199
213
}
200
214
return places ;
201
215
}
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
- }
215
216
}
0 commit comments