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