1
1
package fr .free .nrw .commons .nearby ;
2
2
3
- import android .content .Context ;
4
3
import android .net .Uri ;
5
4
import android .os .StrictMode ;
6
5
9
8
import java .io .InputStreamReader ;
10
9
import java .net .URL ;
11
10
import java .net .URLConnection ;
12
- import java .net .URLEncoder ;
13
11
import java .util .ArrayList ;
14
12
import java .util .Collections ;
15
13
import java .util .List ;
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/" );
31
+ private final String wikidataQuery ;
32
32
private double radius = INITIAL_RADIUS ;
33
33
private List <Place > places ;
34
34
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 ) {
38
46
List <Place > places = Collections .emptyList ();
39
47
40
48
try {
41
49
// increase the radius gradually to find a satisfactory number of nearby places
42
50
while (radius < MAX_RADIUS ) {
43
- places = getFromWikidataQuery (context , curLatLng , lang , radius );
51
+ places = getFromWikidataQuery (curLatLng , lang , radius );
44
52
Timber .d ("%d results at radius: %f" , places .size (), radius );
45
53
if (places .size () >= MIN_RESULTS ) {
46
54
break ;
@@ -58,28 +66,24 @@ List<Place> getFromWikidataQuery(Context context,
58
66
return places ;
59
67
}
60
68
61
- private List <Place > getFromWikidataQuery (Context context ,
62
- LatLng cur ,
69
+ private List <Place > getFromWikidataQuery (LatLng cur ,
63
70
String lang ,
64
71
double radius )
65
72
throws IOException {
66
73
List <Place > places = new ArrayList <>();
67
74
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 ))
70
77
.replace ("${LAT}" , String .format (Locale .ROOT , "%.4f" , cur .latitude ))
71
78
.replace ("${LONG}" , String .format (Locale .ROOT , "%.4f" , cur .longitude ))
72
79
.replace ("${LANG}" , lang );
73
80
74
- Timber .d ( " Wikidata query " + query );
81
+ Timber .v ( "# Wikidata query: \n " + query );
75
82
76
83
// 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 ();
83
87
URLConnection conn = new URL (url ).openConnection ();
84
88
conn .setRequestProperty ("Accept" , "text/tab-separated-values" );
85
89
BufferedReader in = new BufferedReader (new InputStreamReader (conn .getInputStream ()));
0 commit comments