mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-29 13:53:54 +01:00
Add NearbyQueryParams and refactor
This commit is contained in:
parent
8f108d269b
commit
24f4d90892
6 changed files with 74 additions and 50 deletions
|
|
@ -11,6 +11,7 @@ import fr.free.nrw.commons.fileusages.GlobalFileUsagesResponse
|
||||||
import fr.free.nrw.commons.location.LatLng
|
import fr.free.nrw.commons.location.LatLng
|
||||||
import fr.free.nrw.commons.nearby.Place
|
import fr.free.nrw.commons.nearby.Place
|
||||||
import fr.free.nrw.commons.nearby.model.ItemsClass
|
import fr.free.nrw.commons.nearby.model.ItemsClass
|
||||||
|
import fr.free.nrw.commons.nearby.model.NearbyQueryParams
|
||||||
import fr.free.nrw.commons.nearby.model.NearbyResponse
|
import fr.free.nrw.commons.nearby.model.NearbyResponse
|
||||||
import fr.free.nrw.commons.nearby.model.PlaceBindings
|
import fr.free.nrw.commons.nearby.model.PlaceBindings
|
||||||
import fr.free.nrw.commons.profile.achievements.FeaturedImages
|
import fr.free.nrw.commons.profile.achievements.FeaturedImages
|
||||||
|
|
@ -333,56 +334,32 @@ class OkHttpJsonApiClient @Inject constructor(
|
||||||
|
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
fun getNearbyItemCount(
|
fun getNearbyItemCount(
|
||||||
screenTopRight: LatLng, screenBottomLeft: LatLng
|
queryParams: NearbyQueryParams
|
||||||
): Int {
|
): Int {
|
||||||
val wikidataQuery: String =
|
val wikidataQuery: String = when (queryParams) {
|
||||||
FileUtils.readFromResource("/queries/rectangle_query_for_item_count.rq")
|
is NearbyQueryParams.Rectangular -> {
|
||||||
|
val westCornerLat = queryParams.screenTopRight.latitude
|
||||||
|
val westCornerLong = queryParams.screenTopRight.longitude
|
||||||
|
val eastCornerLat = queryParams.screenBottomLeft.latitude
|
||||||
|
val eastCornerLong = queryParams.screenBottomLeft.longitude
|
||||||
|
FileUtils.readFromResource("/queries/rectangle_query_for_item_count.rq")
|
||||||
|
.replace("\${LAT_WEST}", String.format(Locale.ROOT, "%.4f", westCornerLat))
|
||||||
|
.replace("\${LONG_WEST}", String.format(Locale.ROOT, "%.4f", westCornerLong))
|
||||||
|
.replace("\${LAT_EAST}", String.format(Locale.ROOT, "%.4f", eastCornerLat))
|
||||||
|
.replace("\${LONG_EAST}", String.format(Locale.ROOT, "%.4f", eastCornerLong))
|
||||||
|
}
|
||||||
|
|
||||||
val westCornerLat = screenTopRight.latitude
|
is NearbyQueryParams.Radial -> {
|
||||||
val westCornerLong = screenTopRight.longitude
|
FileUtils.readFromResource("/queries/radius_query_for_item_count.rq")
|
||||||
val eastCornerLat = screenBottomLeft.latitude
|
.replace("\${LAT}", String.format(Locale.ROOT, "%.4f", queryParams.center.latitude))
|
||||||
val eastCornerLong = screenBottomLeft.longitude
|
.replace("\${LONG}", String.format(Locale.ROOT, "%.4f", queryParams.center.longitude))
|
||||||
|
.replace("\${RAD}", String.format(Locale.ROOT, "%.2f", queryParams.radius))
|
||||||
val query = wikidataQuery
|
}
|
||||||
.replace("\${LAT_WEST}", String.format(Locale.ROOT, "%.4f", westCornerLat))
|
|
||||||
.replace("\${LONG_WEST}", String.format(Locale.ROOT, "%.4f", westCornerLong))
|
|
||||||
.replace("\${LAT_EAST}", String.format(Locale.ROOT, "%.4f", eastCornerLat))
|
|
||||||
.replace("\${LONG_EAST}", String.format(Locale.ROOT, "%.4f", eastCornerLong))
|
|
||||||
|
|
||||||
val urlBuilder: HttpUrl.Builder = sparqlQueryUrl.toHttpUrlOrNull()!!
|
|
||||||
.newBuilder()
|
|
||||||
.addQueryParameter("query", query)
|
|
||||||
.addQueryParameter("format", "json")
|
|
||||||
|
|
||||||
val request: Request = Request.Builder()
|
|
||||||
.url(urlBuilder.build())
|
|
||||||
.build()
|
|
||||||
|
|
||||||
val response = okHttpClient.newCall(request).execute()
|
|
||||||
if (response.body != null && response.isSuccessful) {
|
|
||||||
val json = response.body!!.string()
|
|
||||||
return JsonParser.parseString(json).getAsJsonObject().getAsJsonObject("results")
|
|
||||||
.getAsJsonArray("bindings").get(0).getAsJsonObject().getAsJsonObject("itemCount")
|
|
||||||
.get("value").asInt
|
|
||||||
}
|
}
|
||||||
throw Exception(response.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Throws(Exception::class)
|
|
||||||
fun getNearbyItemCount(
|
|
||||||
center: LatLng, radius: Float
|
|
||||||
): Int {
|
|
||||||
val wikidataQuery: String =
|
|
||||||
FileUtils.readFromResource("/queries/radius_query_for_item_count.rq")
|
|
||||||
|
|
||||||
val query = wikidataQuery
|
|
||||||
.replace("\${LAT}", String.format(Locale.ROOT, "%.4f", center.latitude))
|
|
||||||
.replace("\${LONG}", String.format(Locale.ROOT, "%.4f", center.longitude))
|
|
||||||
.replace("\${RAD}", String.format(Locale.ROOT, "%.2f", radius))
|
|
||||||
|
|
||||||
val urlBuilder: HttpUrl.Builder = sparqlQueryUrl.toHttpUrlOrNull()!!
|
val urlBuilder: HttpUrl.Builder = sparqlQueryUrl.toHttpUrlOrNull()!!
|
||||||
.newBuilder()
|
.newBuilder()
|
||||||
.addQueryParameter("query", query)
|
.addQueryParameter("query", wikidataQuery)
|
||||||
.addQueryParameter("format", "json")
|
.addQueryParameter("format", "json")
|
||||||
|
|
||||||
val request: Request = Request.Builder()
|
val request: Request = Request.Builder()
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package fr.free.nrw.commons.nearby;
|
||||||
|
|
||||||
import android.location.Location;
|
import android.location.Location;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
import fr.free.nrw.commons.nearby.model.NearbyQueryParams;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -135,8 +136,8 @@ public class NearbyPlaces {
|
||||||
final float latGap = results[0]/1000f;
|
final float latGap = results[0]/1000f;
|
||||||
|
|
||||||
if (Math.max(longGap,latGap)<100f){
|
if (Math.max(longGap,latGap)<100f){
|
||||||
final int itemCount = okHttpJsonApiClient.getNearbyItemCount(screenTopRight,
|
final int itemCount = okHttpJsonApiClient.getNearbyItemCount(
|
||||||
screenBottomLeft);
|
new NearbyQueryParams.Rectangular(screenTopRight, screenBottomLeft));
|
||||||
if(itemCount<upperLimit) {
|
if(itemCount<upperLimit) {
|
||||||
return okHttpJsonApiClient.getNearbyPlaces(screenTopRight, screenBottomLeft, lang,
|
return okHttpJsonApiClient.getNearbyPlaces(screenTopRight, screenBottomLeft, lang,
|
||||||
shouldQueryForMonuments, null);
|
shouldQueryForMonuments, null);
|
||||||
|
|
@ -147,8 +148,8 @@ public class NearbyPlaces {
|
||||||
int targetRadius = maxRadius/2;
|
int targetRadius = maxRadius/2;
|
||||||
while (minRadius<maxRadius) {
|
while (minRadius<maxRadius) {
|
||||||
targetRadius = minRadius + (maxRadius - minRadius + 1) / 2;
|
targetRadius = minRadius + (maxRadius - minRadius + 1) / 2;
|
||||||
final int itemCount = okHttpJsonApiClient.getNearbyItemCount(centerPoint,
|
final int itemCount = okHttpJsonApiClient.getNearbyItemCount(
|
||||||
targetRadius / 100f);
|
new NearbyQueryParams.Radial(centerPoint, targetRadius / 100f));
|
||||||
if (itemCount >= lowerLimit && itemCount < upperLimit){
|
if (itemCount >= lowerLimit && itemCount < upperLimit){
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -162,7 +163,7 @@ public class NearbyPlaces {
|
||||||
maxRadius = targetRadius - 1;
|
maxRadius = targetRadius - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new java.util.ArrayList<Place>(); // TODO make actual query
|
return new java.util.ArrayList<Place>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
package fr.free.nrw.commons.nearby.model
|
||||||
|
|
||||||
|
import fr.free.nrw.commons.location.LatLng
|
||||||
|
|
||||||
|
sealed class NearbyQueryParams {
|
||||||
|
class Rectangular(val screenTopRight: LatLng, val screenBottomLeft: LatLng) :
|
||||||
|
NearbyQueryParams()
|
||||||
|
|
||||||
|
class Radial(val center: LatLng, val radius: Float) : NearbyQueryParams()
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
SELECT
|
||||||
|
?item
|
||||||
|
(SAMPLE(?location) as ?location)
|
||||||
|
WHERE {
|
||||||
|
# Around given location.
|
||||||
|
SERVICE wikibase:around {
|
||||||
|
?item wdt:P625 ?location.
|
||||||
|
bd:serviceParam wikibase:center "Point(${LONG} ${LAT})"^^geo:wktLiteral.
|
||||||
|
bd:serviceParam wikibase:radius "${RAD}" . # Radius in kilometers.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GROUP BY ?item
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
SELECT
|
||||||
|
?item
|
||||||
|
(SAMPLE(?location) as ?location)
|
||||||
|
(SAMPLE(?monument) AS ?monument)
|
||||||
|
WHERE {
|
||||||
|
# Around given location.
|
||||||
|
SERVICE wikibase:around {
|
||||||
|
?item wdt:P625 ?location.
|
||||||
|
bd:serviceParam wikibase:center "Point(${LONG} ${LAT})"^^geo:wktLiteral.
|
||||||
|
bd:serviceParam wikibase:radius "${RAD}" . # Radius in kilometers.
|
||||||
|
}
|
||||||
|
|
||||||
|
# Wiki Loves Monuments
|
||||||
|
OPTIONAL {?item p:P1435 ?monument}
|
||||||
|
OPTIONAL {?item p:P2186 ?monument}
|
||||||
|
OPTIONAL {?item p:P1459 ?monument}
|
||||||
|
OPTIONAL {?item p:P1460 ?monument}
|
||||||
|
OPTIONAL {?item p:P1216 ?monument}
|
||||||
|
OPTIONAL {?item p:P709 ?monument}
|
||||||
|
OPTIONAL {?item p:P718 ?monument}
|
||||||
|
OPTIONAL {?item p:P5694 ?monument}
|
||||||
|
OPTIONAL {?item p:P3426 ?monument}
|
||||||
|
|
||||||
|
}
|
||||||
|
GROUP BY ?item
|
||||||
|
|
@ -8,6 +8,5 @@ WHERE {
|
||||||
bd:serviceParam wikibase:cornerWest "Point(${LONG_WEST} ${LAT_WEST})"^^geo:wktLiteral.
|
bd:serviceParam wikibase:cornerWest "Point(${LONG_WEST} ${LAT_WEST})"^^geo:wktLiteral.
|
||||||
bd:serviceParam wikibase:cornerEast "Point(${LONG_EAST} ${LAT_EAST})"^^geo:wktLiteral.
|
bd:serviceParam wikibase:cornerEast "Point(${LONG_EAST} ${LAT_EAST})"^^geo:wktLiteral.
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
GROUP BY ?item
|
GROUP BY ?item
|
||||||
Loading…
Add table
Add a link
Reference in a new issue