diff --git a/app/src/main/java/fr/free/nrw/commons/mwapi/OkHttpJsonApiClient.kt b/app/src/main/java/fr/free/nrw/commons/mwapi/OkHttpJsonApiClient.kt index 889e65c37..e64d8821f 100644 --- a/app/src/main/java/fr/free/nrw/commons/mwapi/OkHttpJsonApiClient.kt +++ b/app/src/main/java/fr/free/nrw/commons/mwapi/OkHttpJsonApiClient.kt @@ -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() diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyPlaces.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyPlaces.java index 4af541442..6f619cc85 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyPlaces.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyPlaces.java @@ -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= lowerLimit && itemCount < upperLimit){ break; } @@ -162,7 +163,7 @@ public class NearbyPlaces { maxRadius = targetRadius - 1; } } - return new java.util.ArrayList(); // TODO make actual query + return new java.util.ArrayList(); } /** diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/model/NearbyQueryParams.kt b/app/src/main/java/fr/free/nrw/commons/nearby/model/NearbyQueryParams.kt new file mode 100644 index 000000000..ea2957abe --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/nearby/model/NearbyQueryParams.kt @@ -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() +} \ No newline at end of file diff --git a/app/src/main/resources/queries/radius_query_for_nearby.rq b/app/src/main/resources/queries/radius_query_for_nearby.rq index e69de29bb..86c4ff602 100644 --- a/app/src/main/resources/queries/radius_query_for_nearby.rq +++ b/app/src/main/resources/queries/radius_query_for_nearby.rq @@ -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 \ No newline at end of file diff --git a/app/src/main/resources/queries/radius_query_for_nearby_monuments.rq b/app/src/main/resources/queries/radius_query_for_nearby_monuments.rq new file mode 100644 index 000000000..84b3095af --- /dev/null +++ b/app/src/main/resources/queries/radius_query_for_nearby_monuments.rq @@ -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 \ No newline at end of file diff --git a/app/src/main/resources/queries/rectangle_query_for_nearby.rq b/app/src/main/resources/queries/rectangle_query_for_nearby.rq index 336b22db7..f8c70c74a 100644 --- a/app/src/main/resources/queries/rectangle_query_for_nearby.rq +++ b/app/src/main/resources/queries/rectangle_query_for_nearby.rq @@ -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 \ No newline at end of file