Fixes #53 - Suggest depicted country based on EXIF latitude/longitude (#4756)

* Fix - Suggest depicted country based on EXIF latitude/longitude

* Added null check and moved country suggestion to the end of the list
This commit is contained in:
Pratham Pahariya 2022-01-19 16:58:38 +05:30 committed by GitHub
parent d7474fc799
commit 7bcaab2442
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 14 deletions

View file

@ -148,6 +148,8 @@ dependencies {
//Glide //Glide
implementation 'com.github.bumptech.glide:glide:4.12.0' implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
implementation("io.github.coordinates2country:coordinates2country-android:1.2") { exclude group: 'com.google.android', module: 'android' }
} }
android { android {

View file

@ -255,7 +255,7 @@ public class UploadRepository {
*/ */
public Flowable<List<DepictedItem>> searchAllEntities(String query) { public Flowable<List<DepictedItem>> searchAllEntities(String query) {
return depictModel.searchAllEntities(query); return depictModel.searchAllEntities(query, this);
} }
/** /**
@ -265,14 +265,14 @@ public class UploadRepository {
* @return a single that provides the depictions * @return a single that provides the depictions
*/ */
public Single<List<DepictedItem>> getPlaceDepictions() { public Single<List<DepictedItem>> getPlaceDepictions() {
final Set<Place> places = new HashSet<>(); final Set<String> qids = new HashSet<>();
for (final UploadItem item : getUploads()) { for (final UploadItem item : getUploads()) {
final Place place = item.getPlace(); final Place place = item.getPlace();
if (place != null) { if (place != null) {
places.add(place); qids.add(place.getWikiDataEntityId());
} }
} }
return depictModel.getPlaceDepictions(new ArrayList<>(places)); return depictModel.getPlaceDepictions(new ArrayList<>(qids));
} }
/** /**

View file

@ -2,6 +2,8 @@ package fr.free.nrw.commons.upload.structure.depictions
import fr.free.nrw.commons.explore.depictions.DepictsClient import fr.free.nrw.commons.explore.depictions.DepictsClient
import fr.free.nrw.commons.nearby.Place import fr.free.nrw.commons.nearby.Place
import fr.free.nrw.commons.repository.UploadRepository
import io.github.coordinates2country.Coordinates2Country
import io.reactivex.Flowable import io.reactivex.Flowable
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.Single import io.reactivex.Single
@ -25,27 +27,37 @@ class DepictModel @Inject constructor(private val depictsClient: DepictsClient)
/** /**
* Search for depictions * Search for depictions
*/ */
fun searchAllEntities(query: String): Flowable<List<DepictedItem>> { fun searchAllEntities(query: String, repository: UploadRepository): Flowable<List<DepictedItem>> {
return if (query.isBlank()) return if (query.isBlank()) {
nearbyPlaces.switchMap { places: List<Place> -> nearbyPlaces.switchMap { places: List<Place> ->
getPlaceDepictions(places).toFlowable() val qids = mutableSetOf<String>()
for(place in places) {
place.wikiDataEntityId?.let { qids.add(it) }
}
repository.uploads.forEach { item ->
if(item.gpsCoords != null && item.gpsCoords.imageCoordsExists) {
Coordinates2Country.countryQID(item.gpsCoords.decLatitude,
item.gpsCoords.decLongitude)?.let { qids.add("Q$it") }
}
}
getPlaceDepictions(ArrayList(qids)).toFlowable()
} }
else } else
networkItems(query) networkItems(query)
} }
/** /**
* Provides [DepictedItem] instances via a [Single] for a given list of [Place], providing an * Provides [DepictedItem] instances via a [Single] for a given list of ids, providing an
* empty list if no places are provided or if there is an error * empty list if no places/country are provided or if there is an error
*/ */
fun getPlaceDepictions(places: List<Place>): Single<List<DepictedItem>> = fun getPlaceDepictions(qids: List<String>): Single<List<DepictedItem>> =
places.toIds().let { ids -> qids.toIds().let { ids ->
if (ids.isNotEmpty()) if (ids.isNotEmpty())
depictsClient.getEntities(ids) depictsClient.getEntities(ids)
.map{ .map{
it.entities() it.entities()
.values .values
.mapIndexed { index, entity -> DepictedItem(entity, places[index])} .mapIndexed { index, entity -> DepictedItem(entity)}
} }
.onErrorResumeWithEmptyList() .onErrorResumeWithEmptyList()
else Single.just(emptyList()) else Single.just(emptyList())
@ -75,7 +87,7 @@ class DepictModel @Inject constructor(private val depictsClient: DepictsClient)
} }
private fun List<Place>.toIds() = mapNotNull { it.wikiDataEntityId }.joinToString("|") private fun List<String>.toIds() = mapNotNull { it }.joinToString("|")
private fun <T> Single<List<T>>.onErrorResumeWithEmptyList() = onErrorResumeNext { t: Throwable -> private fun <T> Single<List<T>>.onErrorResumeWithEmptyList() = onErrorResumeNext { t: Throwable ->
Single.just(emptyList<T>()).also { Timber.e(t) } Single.just(emptyList<T>()).also { Timber.e(t) }