Add NearbyQueryParams and refactor

This commit is contained in:
savsch 2024-12-23 21:23:14 +05:30
parent 8f108d269b
commit 24f4d90892
6 changed files with 74 additions and 50 deletions

View file

@ -11,6 +11,7 @@ import fr.free.nrw.commons.fileusages.GlobalFileUsagesResponse
import fr.free.nrw.commons.location.LatLng
import fr.free.nrw.commons.nearby.Place
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.PlaceBindings
import fr.free.nrw.commons.profile.achievements.FeaturedImages
@ -333,56 +334,32 @@ class OkHttpJsonApiClient @Inject constructor(
@Throws(Exception::class)
fun getNearbyItemCount(
screenTopRight: LatLng, screenBottomLeft: LatLng
queryParams: NearbyQueryParams
): Int {
val wikidataQuery: String =
FileUtils.readFromResource("/queries/rectangle_query_for_item_count.rq")
val wikidataQuery: String = when (queryParams) {
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
val westCornerLong = screenTopRight.longitude
val eastCornerLat = screenBottomLeft.latitude
val eastCornerLong = screenBottomLeft.longitude
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
is NearbyQueryParams.Radial -> {
FileUtils.readFromResource("/queries/radius_query_for_item_count.rq")
.replace("\${LAT}", String.format(Locale.ROOT, "%.4f", queryParams.center.latitude))
.replace("\${LONG}", String.format(Locale.ROOT, "%.4f", queryParams.center.longitude))
.replace("\${RAD}", String.format(Locale.ROOT, "%.2f", queryParams.radius))
}
}
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()!!
.newBuilder()
.addQueryParameter("query", query)
.addQueryParameter("query", wikidataQuery)
.addQueryParameter("format", "json")
val request: Request = Request.Builder()

View file

@ -2,6 +2,7 @@ package fr.free.nrw.commons.nearby;
import android.location.Location;
import androidx.annotation.Nullable;
import fr.free.nrw.commons.nearby.model.NearbyQueryParams;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
@ -135,8 +136,8 @@ public class NearbyPlaces {
final float latGap = results[0]/1000f;
if (Math.max(longGap,latGap)<100f){
final int itemCount = okHttpJsonApiClient.getNearbyItemCount(screenTopRight,
screenBottomLeft);
final int itemCount = okHttpJsonApiClient.getNearbyItemCount(
new NearbyQueryParams.Rectangular(screenTopRight, screenBottomLeft));
if(itemCount<upperLimit) {
return okHttpJsonApiClient.getNearbyPlaces(screenTopRight, screenBottomLeft, lang,
shouldQueryForMonuments, null);
@ -147,8 +148,8 @@ public class NearbyPlaces {
int targetRadius = maxRadius/2;
while (minRadius<maxRadius) {
targetRadius = minRadius + (maxRadius - minRadius + 1) / 2;
final int itemCount = okHttpJsonApiClient.getNearbyItemCount(centerPoint,
targetRadius / 100f);
final int itemCount = okHttpJsonApiClient.getNearbyItemCount(
new NearbyQueryParams.Radial(centerPoint, targetRadius / 100f));
if (itemCount >= lowerLimit && itemCount < upperLimit){
break;
}
@ -162,7 +163,7 @@ public class NearbyPlaces {
maxRadius = targetRadius - 1;
}
}
return new java.util.ArrayList<Place>(); // TODO make actual query
return new java.util.ArrayList<Place>();
}
/**

View file

@ -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()
}

View file

@ -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

View file

@ -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

View file

@ -8,6 +8,5 @@ WHERE {
bd:serviceParam wikibase:cornerWest "Point(${LONG_WEST} ${LAT_WEST})"^^geo:wktLiteral.
bd:serviceParam wikibase:cornerEast "Point(${LONG_EAST} ${LAT_EAST})"^^geo:wktLiteral.
}
}
GROUP BY ?item