mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Read once and reuse the query file's content
This commit is contained in:
parent
825cdafe7b
commit
22f5f51082
4 changed files with 42 additions and 42 deletions
|
|
@ -13,7 +13,7 @@ SELECT
|
|||
SERVICE wikibase:around {
|
||||
?item wdt:P625 ?location.
|
||||
bd:serviceParam wikibase:center "Point(${LONG} ${LAT})"^^geo:wktLiteral.
|
||||
bd:serviceParam wikibase:radius "${RADIUS}" . # Radius in kilometers.
|
||||
bd:serviceParam wikibase:radius "${RAD}" . # Radius in kilometers.
|
||||
}
|
||||
|
||||
# ... and without an image.
|
||||
|
|
|
|||
|
|
@ -41,10 +41,7 @@ public class NearbyController {
|
|||
NearbyPlaces nearbyPlaces = CommonsApplication.getInstance().getNearbyPlaces();
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
List<Place> places = prefs.getBoolean("useWikidata", true)
|
||||
? nearbyPlaces.getFromWikidataQuery(
|
||||
context,
|
||||
curLatLng,
|
||||
Locale.getDefault().getLanguage())
|
||||
? nearbyPlaces.getFromWikidataQuery(curLatLng, Locale.getDefault().getLanguage())
|
||||
: nearbyPlaces.getFromWikiNeedsPictures();
|
||||
if (curLatLng != null) {
|
||||
Timber.d("Sorting places by distance...");
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package fr.free.nrw.commons.nearby;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.os.StrictMode;
|
||||
|
||||
|
|
@ -9,7 +8,6 @@ import java.io.IOException;
|
|||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
|
@ -25,22 +23,32 @@ import timber.log.Timber;
|
|||
public class NearbyPlaces {
|
||||
|
||||
private static final int MIN_RESULTS = 40;
|
||||
private static final double INITIAL_RADIUS = 1.0; // in kilometer
|
||||
private static final double MAX_RADIUS = 300.0; // in kilometer
|
||||
private static final double INITIAL_RADIUS = 1.0; // in kilometers
|
||||
private static final double MAX_RADIUS = 300.0; // in kilometers
|
||||
private static final double RADIUS_MULTIPLIER = 1.618;
|
||||
private static final String WIKIDATA_QUERY_URL = "https://query.wikidata.org/sparql?query=${QUERY}";
|
||||
private static final Uri WIKIDATA_QUERY_URL = Uri.parse("https://query.wikidata.org/sparql");
|
||||
private static final Uri WIKIDATA_QUERY_UI_URL = Uri.parse("https://query.wikidata.org/");
|
||||
private final String wikidataQuery;
|
||||
private double radius = INITIAL_RADIUS;
|
||||
private List<Place> places;
|
||||
|
||||
List<Place> getFromWikidataQuery(Context context,
|
||||
LatLng curLatLng,
|
||||
String lang) {
|
||||
public NearbyPlaces() {
|
||||
try {
|
||||
String query = FileUtils.readFromResource("/assets/queries/nearby_query.rq");
|
||||
wikidataQuery = query;
|
||||
Timber.v(wikidataQuery);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
List<Place> getFromWikidataQuery(LatLng curLatLng, String lang) {
|
||||
List<Place> places = Collections.emptyList();
|
||||
|
||||
try {
|
||||
// increase the radius gradually to find a satisfactory number of nearby places
|
||||
while (radius < MAX_RADIUS) {
|
||||
places = getFromWikidataQuery(context, curLatLng, lang, radius);
|
||||
places = getFromWikidataQuery(curLatLng, lang, radius);
|
||||
Timber.d("%d results at radius: %f", places.size(), radius);
|
||||
if (places.size() >= MIN_RESULTS) {
|
||||
break;
|
||||
|
|
@ -58,28 +66,24 @@ public class NearbyPlaces {
|
|||
return places;
|
||||
}
|
||||
|
||||
private List<Place> getFromWikidataQuery(Context context,
|
||||
LatLng cur,
|
||||
private List<Place> getFromWikidataQuery(LatLng cur,
|
||||
String lang,
|
||||
double radius)
|
||||
throws IOException {
|
||||
List<Place> places = new ArrayList<>();
|
||||
|
||||
String query = FileUtils.readFromFile(context, "queries/nearby_query.rq")
|
||||
.replace("${RADIUS}", String.format(Locale.ROOT, "%.2f", radius))
|
||||
String query = wikidataQuery
|
||||
.replace("${RAD}", String.format(Locale.ROOT, "%.2f", radius))
|
||||
.replace("${LAT}", String.format(Locale.ROOT, "%.4f", cur.latitude))
|
||||
.replace("${LONG}", String.format(Locale.ROOT, "%.4f", cur.longitude))
|
||||
.replace("${LANG}", lang);
|
||||
|
||||
Timber.d("Wikidata query "+ query);
|
||||
Timber.v("# Wikidata query: \n" + query);
|
||||
|
||||
// format as a URL
|
||||
String url = WIKIDATA_QUERY_URL.replace(
|
||||
"${QUERY}",
|
||||
URLEncoder.encode(query, "utf-8").replace("+", "%20")
|
||||
);
|
||||
|
||||
Timber.d(url);
|
||||
Timber.d(WIKIDATA_QUERY_UI_URL.buildUpon().fragment(query).build().toString());
|
||||
String url = WIKIDATA_QUERY_URL.buildUpon()
|
||||
.appendQueryParameter("query", query).build().toString();
|
||||
URLConnection conn = new URL(url).openConnection();
|
||||
conn.setRequestProperty("Accept", "text/tab-separated-values");
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||
|
|
|
|||
|
|
@ -1,37 +1,36 @@
|
|||
package fr.free.nrw.commons.utils;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import timber.log.Timber;
|
||||
import fr.free.nrw.commons.CommonsApplication;
|
||||
|
||||
public class FileUtils {
|
||||
public static String readFromFile(Context context, String fileName) {
|
||||
String stringBuilder = "";
|
||||
/**
|
||||
* Read and return the content of a resource file as string.
|
||||
*
|
||||
* @param fileName asset file's path (e.g. "/assets/queries/nearby_query.rq")
|
||||
* @return the content of the file
|
||||
*/
|
||||
public static String readFromResource(String fileName) throws IOException {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
reader = new BufferedReader(
|
||||
new InputStreamReader(context.getAssets().open(fileName)));
|
||||
String mLine;
|
||||
while ((mLine = reader.readLine()) != null) {
|
||||
stringBuilder += mLine + "\n";
|
||||
new InputStreamReader(
|
||||
CommonsApplication.class.getResourceAsStream(fileName), "UTF-8"));
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
buffer.append(line + "\n");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Timber.e("File not found exception", e);
|
||||
} finally {
|
||||
if (reader != null) {
|
||||
try {
|
||||
reader.close();
|
||||
} catch (IOException e) {
|
||||
//log the exception
|
||||
}
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
return stringBuilder;
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue