@@ -2,6 +2,7 @@ package fr.free.nrw.commons.mwapi
2
2
3
3
import android.text.TextUtils
4
4
import com.google.gson.Gson
5
+ import com.google.gson.JsonParser
5
6
import fr.free.nrw.commons.BuildConfig
6
7
import fr.free.nrw.commons.campaigns.CampaignResponseDTO
7
8
import fr.free.nrw.commons.explore.depictions.DepictsClient
@@ -10,6 +11,7 @@ import fr.free.nrw.commons.fileusages.GlobalFileUsagesResponse
10
11
import fr.free.nrw.commons.location.LatLng
11
12
import fr.free.nrw.commons.nearby.Place
12
13
import fr.free.nrw.commons.nearby.model.ItemsClass
14
+ import fr.free.nrw.commons.nearby.model.NearbyQueryParams
13
15
import fr.free.nrw.commons.nearby.model.NearbyResponse
14
16
import fr.free.nrw.commons.nearby.model.PlaceBindings
15
17
import fr.free.nrw.commons.profile.achievements.FeaturedImages
@@ -330,36 +332,130 @@ class OkHttpJsonApiClient @Inject constructor(
330
332
throw Exception (response.message)
331
333
}
332
334
335
+ /* *
336
+ * Returns the count of items in the specified area by querying Wikidata.
337
+ *
338
+ * @param queryParams: a `NearbyQueryParam` specifying the geographical area.
339
+ * @return The count of items in the specified area.
340
+ */
341
+ @Throws(Exception ::class )
342
+ fun getNearbyItemCount (
343
+ queryParams : NearbyQueryParams
344
+ ): Int {
345
+ val wikidataQuery: String = when (queryParams) {
346
+ is NearbyQueryParams .Rectangular -> {
347
+ val westCornerLat = queryParams.screenTopRight.latitude
348
+ val westCornerLong = queryParams.screenTopRight.longitude
349
+ val eastCornerLat = queryParams.screenBottomLeft.latitude
350
+ val eastCornerLong = queryParams.screenBottomLeft.longitude
351
+ FileUtils .readFromResource(" /queries/rectangle_query_for_item_count.rq" )
352
+ .replace(" \$ {LAT_WEST}" , String .format(Locale .ROOT , " %.4f" , westCornerLat))
353
+ .replace(" \$ {LONG_WEST}" , String .format(Locale .ROOT , " %.4f" , westCornerLong))
354
+ .replace(" \$ {LAT_EAST}" , String .format(Locale .ROOT , " %.4f" , eastCornerLat))
355
+ .replace(" \$ {LONG_EAST}" , String .format(Locale .ROOT , " %.4f" , eastCornerLong))
356
+ }
357
+
358
+ is NearbyQueryParams .Radial -> {
359
+ FileUtils .readFromResource(" /queries/radius_query_for_item_count.rq" )
360
+ .replace(
361
+ " \$ {LAT}" ,
362
+ String .format(Locale .ROOT , " %.4f" , queryParams.center.latitude)
363
+ )
364
+ .replace(
365
+ " \$ {LONG}" ,
366
+ String .format(Locale .ROOT , " %.4f" , queryParams.center.longitude)
367
+ )
368
+ .replace(" \$ {RAD}" , String .format(Locale .ROOT , " %.2f" , queryParams.radiusInKm))
369
+ }
370
+ }
371
+
372
+ val urlBuilder: HttpUrl .Builder = sparqlQueryUrl.toHttpUrlOrNull()!!
373
+ .newBuilder()
374
+ .addQueryParameter(" query" , wikidataQuery)
375
+ .addQueryParameter(" format" , " json" )
376
+
377
+ val request: Request = Request .Builder ()
378
+ .url(urlBuilder.build())
379
+ .build()
380
+
381
+ val response = okHttpClient.newCall(request).execute()
382
+ if (response.body != null && response.isSuccessful) {
383
+ val json = response.body!! .string()
384
+ return JsonParser .parseString(json).getAsJsonObject().getAsJsonObject(" results" )
385
+ .getAsJsonArray(" bindings" ).get(0 ).getAsJsonObject().getAsJsonObject(" itemCount" )
386
+ .get(" value" ).asInt
387
+ }
388
+ throw Exception (response.message)
389
+ }
390
+
333
391
@Throws(Exception ::class )
334
392
fun getNearbyPlaces (
335
- screenTopRight : LatLng ,
336
- screenBottomLeft : LatLng , language : String ,
393
+ queryParams : NearbyQueryParams , language : String ,
337
394
shouldQueryForMonuments : Boolean , customQuery : String?
338
395
): List <Place >? {
339
396
Timber .d(" CUSTOM_SPARQL: %s" , (customQuery != null ).toString())
340
397
398
+ val locale = Locale .ROOT ;
341
399
val wikidataQuery: String = if (customQuery != null ) {
342
- customQuery
343
- } else if (! shouldQueryForMonuments) {
344
- FileUtils .readFromResource(" /queries/rectangle_query_for_nearby.rq" )
345
- } else {
346
- FileUtils .readFromResource(" /queries/rectangle_query_for_nearby_monuments.rq" )
347
- }
400
+ when (queryParams) {
401
+ is NearbyQueryParams .Rectangular -> {
402
+ val westCornerLat = queryParams.screenTopRight.latitude
403
+ val westCornerLong = queryParams.screenTopRight.longitude
404
+ val eastCornerLat = queryParams.screenBottomLeft.latitude
405
+ val eastCornerLong = queryParams.screenBottomLeft.longitude
406
+ customQuery
407
+ .replace(" \$ {LAT_WEST}" , String .format(locale, " %.4f" , westCornerLat))
408
+ .replace(" \$ {LONG_WEST}" , String .format(locale, " %.4f" , westCornerLong))
409
+ .replace(" \$ {LAT_EAST}" , String .format(locale, " %.4f" , eastCornerLat))
410
+ .replace(" \$ {LONG_EAST}" , String .format(locale, " %.4f" , eastCornerLong))
411
+ .replace(" \$ {LANG}" , language)
412
+ }
413
+ is NearbyQueryParams .Radial -> {
414
+ Timber .e(
415
+ " %s%s" ,
416
+ " okHttpJsonApiClient.getNearbyPlaces invoked with custom query" ,
417
+ " and radial coordinates. This is currently not supported."
418
+ )
419
+ " "
420
+ }
421
+ }
422
+ } else when (queryParams) {
423
+ is NearbyQueryParams .Radial -> {
424
+ val placeHolderQuery: String = if (! shouldQueryForMonuments) {
425
+ FileUtils .readFromResource(" /queries/radius_query_for_nearby.rq" )
426
+ } else {
427
+ FileUtils .readFromResource(" /queries/radius_query_for_nearby_monuments.rq" )
428
+ }
429
+ placeHolderQuery.replace(
430
+ " \$ {LAT}" , String .format(locale, " %.4f" , queryParams.center.latitude)
431
+ ).replace(
432
+ " \$ {LONG}" , String .format(locale, " %.4f" , queryParams.center.longitude)
433
+ )
434
+ .replace(" \$ {RAD}" , String .format(locale, " %.2f" , queryParams.radiusInKm))
435
+ }
348
436
349
- val westCornerLat = screenTopRight.latitude
350
- val westCornerLong = screenTopRight.longitude
351
- val eastCornerLat = screenBottomLeft.latitude
352
- val eastCornerLong = screenBottomLeft.longitude
437
+ is NearbyQueryParams .Rectangular -> {
438
+ val placeHolderQuery: String = if (! shouldQueryForMonuments) {
439
+ FileUtils .readFromResource(" /queries/rectangle_query_for_nearby.rq" )
440
+ } else {
441
+ FileUtils .readFromResource(" /queries/rectangle_query_for_nearby_monuments.rq" )
442
+ }
443
+ val westCornerLat = queryParams.screenTopRight.latitude
444
+ val westCornerLong = queryParams.screenTopRight.longitude
445
+ val eastCornerLat = queryParams.screenBottomLeft.latitude
446
+ val eastCornerLong = queryParams.screenBottomLeft.longitude
447
+ placeHolderQuery
448
+ .replace(" \$ {LAT_WEST}" , String .format(locale, " %.4f" , westCornerLat))
449
+ .replace(" \$ {LONG_WEST}" , String .format(locale, " %.4f" , westCornerLong))
450
+ .replace(" \$ {LAT_EAST}" , String .format(locale, " %.4f" , eastCornerLat))
451
+ .replace(" \$ {LONG_EAST}" , String .format(locale, " %.4f" , eastCornerLong))
452
+ .replace(" \$ {LANG}" , language)
453
+ }
454
+ }
353
455
354
- val query = wikidataQuery
355
- .replace(" \$ {LAT_WEST}" , String .format(Locale .ROOT , " %.4f" , westCornerLat))
356
- .replace(" \$ {LONG_WEST}" , String .format(Locale .ROOT , " %.4f" , westCornerLong))
357
- .replace(" \$ {LAT_EAST}" , String .format(Locale .ROOT , " %.4f" , eastCornerLat))
358
- .replace(" \$ {LONG_EAST}" , String .format(Locale .ROOT , " %.4f" , eastCornerLong))
359
- .replace(" \$ {LANG}" , language)
360
456
val urlBuilder: HttpUrl .Builder = sparqlQueryUrl.toHttpUrlOrNull()!!
361
457
.newBuilder()
362
- .addQueryParameter(" query" , query )
458
+ .addQueryParameter(" query" , wikidataQuery )
363
459
.addQueryParameter(" format" , " json" )
364
460
365
461
val request: Request = Request .Builder ()
0 commit comments