Improvised query

This commit is contained in:
Kanahia 2024-05-16 15:16:58 +05:30
parent 3e9c3a6dae
commit 8e343f25a5
7 changed files with 142 additions and 17 deletions

View file

@ -27,6 +27,7 @@ import io.reactivex.Observable;
import io.reactivex.Single; import io.reactivex.Single;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -35,6 +36,7 @@ import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
@ -411,7 +413,7 @@ public class OkHttpJsonApiClient {
throws Exception { throws Exception {
final String wikidataQuery = FileUtils.readFromResource("/queries/query_for_item.rq"); final String wikidataQuery = FileUtils.readFromResource("/queries/query_for_item.rq");
final String query = wikidataQuery final String query = wikidataQuery
.replace("${ENTITY}", entityId) .replace("${ENTITY}", "wd:"+entityId)
.replace("${LANG}", language); .replace("${LANG}", language);
final HttpUrl.Builder urlBuilder = HttpUrl final HttpUrl.Builder urlBuilder = HttpUrl
.parse(sparqlQueryUrl) .parse(sparqlQueryUrl)
@ -435,6 +437,44 @@ public class OkHttpJsonApiClient {
throw new Exception(response.message()); throw new Exception(response.message());
} }
@Nullable
public List<Place> getNearbyPlacesFromQID(
final List<Place> placeList, final String language) throws IOException {
final String wikidataQuery = FileUtils.readFromResource("/queries/query_for_item.rq");
String qids = "";
for (final Place place : placeList) {
qids += "\n" + ("wd:" + place.getWikiDataEntityId());
}
final String query = wikidataQuery
.replace("${ENTITY}", qids)
.replace("${LANG}", language);
final HttpUrl.Builder urlBuilder = HttpUrl
.parse(sparqlQueryUrl)
.newBuilder()
.addQueryParameter("query", query)
.addQueryParameter("format", "json");
final Request request = new Request.Builder()
.url(urlBuilder.build())
.build();
try (Response response = okHttpClient.newCall(request).execute()) {
if (response.isSuccessful()) {
final String json = response.body().string();
final NearbyResponse nearbyResponse = gson.fromJson(json, NearbyResponse.class);
final List<NearbyResultItem> bindings = nearbyResponse.getResults().getBindings();
final List<Place> places = new ArrayList<>();
for (final NearbyResultItem item : bindings) {
final Place placeFromNearbyItem = Place.from(item);
places.add(placeFromNearbyItem);
}
return places;
} else {
throw new IOException("Unexpected response code: " + response.code());
}
}
}
/** /**
* Make API Call to get Places * Make API Call to get Places
* *

View file

@ -131,6 +131,10 @@ public class NearbyController extends MapController {
); );
} }
public List<Place> getPlacesFromQID(List<Place> placeList) throws Exception {
return nearbyPlaces.getPlacesFromQID(placeList, Locale.getDefault().getLanguage());
}
public Place getNearbyPlace(String entity) throws Exception { public Place getNearbyPlace(String entity) throws Exception {
return nearbyPlaces.getPlaceFromWikidataQuery( return nearbyPlaces.getPlaceFromWikidataQuery(
entity, entity,

View file

@ -134,6 +134,12 @@ public class NearbyPlaces {
.getNearbyPlace(entityId, lang); .getNearbyPlace(entityId, lang);
} }
public List<Place> getPlacesFromQID(final List<Place> placeList,
final String lang) throws Exception {
return okHttpJsonApiClient
.getNearbyPlacesFromQID(placeList, lang);
}
/** /**
* Runs the Wikidata query to retrieve the KML String * Runs the Wikidata query to retrieve the KML String
* *

View file

@ -66,6 +66,7 @@ import fr.free.nrw.commons.contributions.MainActivity.ActiveFragment;
import fr.free.nrw.commons.databinding.FragmentNearbyParentBinding; import fr.free.nrw.commons.databinding.FragmentNearbyParentBinding;
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment; import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
import fr.free.nrw.commons.kvstore.JsonKvStore; import fr.free.nrw.commons.kvstore.JsonKvStore;
import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.location.LocationPermissionsHelper; import fr.free.nrw.commons.location.LocationPermissionsHelper;
import fr.free.nrw.commons.location.LocationPermissionsHelper.LocationPermissionCallback; import fr.free.nrw.commons.location.LocationPermissionsHelper.LocationPermissionCallback;
import fr.free.nrw.commons.location.LocationServiceManager; import fr.free.nrw.commons.location.LocationServiceManager;
@ -103,6 +104,8 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -1326,10 +1329,12 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
if (nearbyPlacesInfo.placeList == null || nearbyPlacesInfo.placeList.isEmpty()) { if (nearbyPlacesInfo.placeList == null || nearbyPlacesInfo.placeList.isEmpty()) {
showErrorMessage(getString(R.string.no_nearby_places_around)); showErrorMessage(getString(R.string.no_nearby_places_around));
} else { } else {
updateMapMarkers(nearbyPlacesInfo, true); updateMapMarkers(nearbyPlacesInfo.placeList, nearbyPlacesInfo.currentLatLng,
true);
lastFocusLocation = searchLatLng; lastFocusLocation = searchLatLng;
lastMapFocus = new GeoPoint(searchLatLng.getLatitude(), lastMapFocus = new GeoPoint(searchLatLng.getLatitude(),
searchLatLng.getLongitude()); searchLatLng.getLongitude());
loadPlacesDataAsync(nearbyPlacesInfo.placeList, nearbyPlacesInfo.currentLatLng);
} }
}, },
throwable -> { throwable -> {
@ -1364,9 +1369,11 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
// Updating last searched location // Updating last searched location
applicationKvStore.putString("LastLocation", applicationKvStore.putString("LastLocation",
searchLatLng.getLatitude() + "," + searchLatLng.getLongitude()); searchLatLng.getLatitude() + "," + searchLatLng.getLongitude());
updateMapMarkers(nearbyPlacesInfo, false); updateMapMarkers(nearbyPlacesInfo.placeList, nearbyPlacesInfo.currentLatLng,
false);
lastMapFocus = new GeoPoint(searchLatLng.getLatitude(), lastMapFocus = new GeoPoint(searchLatLng.getLatitude(),
searchLatLng.getLongitude()); searchLatLng.getLongitude());
loadPlacesDataAsync(nearbyPlacesInfo.placeList, nearbyPlacesInfo.currentLatLng);
} }
}, },
throwable -> { throwable -> {
@ -1379,15 +1386,48 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
})); }));
} }
public void loadPlacesDataAsync(List<Place> placeList, LatLng curLatLng) {
final Observable<List<Place>> nearbyPlacesInfoObservable = Observable
.fromCallable(() -> nearbyController
.getPlacesFromQID(placeList));
compositeDisposable.add(nearbyPlacesInfoObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(p -> {
List<Place> places = p;
if (places == null || places.isEmpty()) {
showErrorMessage(getString(R.string.no_nearby_places_around));
} else {
for (Place place : places) {
for (Place foundPlace : placeList) {
if (place.siteLinks.getWikidataLink().equals(foundPlace.siteLinks.getWikidataLink())) {
place.location = foundPlace.location;
place.distance = foundPlace.distance;
place.setMonument(foundPlace.isMonument());
break;
}
}
}
updateMapMarkers(places, curLatLng, false);
}
},
throwable -> {
Timber.e(throwable);
showErrorMessage(getString(R.string.error_fetching_nearby_places)
+ throwable.getLocalizedMessage());
setFilterState();
}));
}
/** /**
* Populates places for your location, should be used for finding nearby places around a * Populates places for your location, should be used for finding nearby places around a
* location where you are. * location where you are.
* *
* @param nearbyPlacesInfo This variable has place list information and distances. * @param nearbyPlaces This variable has place list information and distances.
*/ */
private void updateMapMarkers(final NearbyController.NearbyPlacesInfo nearbyPlacesInfo, private void updateMapMarkers(final List<Place> nearbyPlaces, final LatLng curLatLng,
final boolean shouldUpdateSelectedMarker) { final boolean shouldUpdateSelectedMarker) {
presenter.updateMapMarkers(nearbyPlacesInfo, shouldUpdateSelectedMarker); presenter.updateMapMarkers(nearbyPlaces, curLatLng, shouldUpdateSelectedMarker);
setFilterState(); setFilterState();
} }
@ -1774,7 +1814,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
return (isBookmarked ? return (isBookmarked ?
R.drawable.ic_custom_map_marker_green_bookmarked : R.drawable.ic_custom_map_marker_green_bookmarked :
R.drawable.ic_custom_map_marker_green); R.drawable.ic_custom_map_marker_green);
} else if (!place.exists) { // Means that the topic of the Wikidata item does not exist in the real world anymore, for instance it is a past event, or a place that was destroyed } else if (!place.exists || (place.name == "")) { // Means that the topic of the Wikidata item does not exist in the real world anymore, for instance it is a past event, or a place that was destroyed
return (isBookmarked ? return (isBookmarked ?
R.drawable.ic_custom_map_marker_grey_bookmarked : R.drawable.ic_custom_map_marker_grey_bookmarked :
R.drawable.ic_custom_map_marker_grey); R.drawable.ic_custom_map_marker_grey);
@ -1797,6 +1837,13 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
Marker marker = new Marker(binding.map); Marker marker = new Marker(binding.map);
marker.setPosition(point); marker.setPosition(point);
marker.setIcon(icon); marker.setIcon(icon);
if (!Objects.equals(place.name, "")){
marker.setTitle(place.name);
marker.setSnippet(
containsParentheses(place.getLongDescription())
? getTextBetweenParentheses(
place.getLongDescription()) : place.getLongDescription());
}
marker.setTextLabelFontSize(40); marker.setTextLabelFontSize(40);
marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_TOP); marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_TOP);
marker.setOnMarkerClickListener((marker1, mapView) -> { marker.setOnMarkerClickListener((marker1, mapView) -> {
@ -1807,7 +1854,16 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
binding.bottomSheetDetails.dataCircularProgress.setVisibility(View.VISIBLE); binding.bottomSheetDetails.dataCircularProgress.setVisibility(View.VISIBLE);
binding.bottomSheetDetails.icon.setVisibility(View.GONE); binding.bottomSheetDetails.icon.setVisibility(View.GONE);
binding.bottomSheetDetails.wikiDataLl.setVisibility(View.GONE); binding.bottomSheetDetails.wikiDataLl.setVisibility(View.GONE);
getPlaceData(place.getWikiDataEntityId(), place, marker1); if (Objects.equals(place.name, "")){
getPlaceData(place.getWikiDataEntityId(), place, marker1);
}else {
marker.showInfoWindow();
binding.bottomSheetDetails.dataCircularProgress.setVisibility(View.GONE);
binding.bottomSheetDetails.icon.setVisibility(View.VISIBLE);
binding.bottomSheetDetails.wikiDataLl.setVisibility(View.VISIBLE);
passInfoToSheet(place);
hideBottomSheet();
}
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
return true; return true;
}); });
@ -1830,6 +1886,14 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
Marker marker = new Marker(binding.map); Marker marker = new Marker(binding.map);
marker.setPosition(point); marker.setPosition(point);
marker.setIcon(icon); marker.setIcon(icon);
Place place = nearbyBaseMarkers.get(i).getPlace();
if (!Objects.equals(place.name, "")){
marker.setTitle(place.name);
marker.setSnippet(
containsParentheses(place.getLongDescription())
? getTextBetweenParentheses(
place.getLongDescription()) : place.getLongDescription());
}
marker.setTextLabelFontSize(40); marker.setTextLabelFontSize(40);
marker.setId(String.valueOf(i)); marker.setId(String.valueOf(i));
marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_TOP); marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_TOP);
@ -1840,11 +1904,20 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
} }
clickedMarker = marker1; clickedMarker = marker1;
int index = Integer.parseInt(marker1.getId()); int index = Integer.parseInt(marker1.getId());
Place place = nearbyBaseMarkers.get(index).getPlace(); Place updatedPlace = nearbyBaseMarkers.get(index).getPlace();
binding.bottomSheetDetails.dataCircularProgress.setVisibility(View.VISIBLE); binding.bottomSheetDetails.dataCircularProgress.setVisibility(View.VISIBLE);
binding.bottomSheetDetails.icon.setVisibility(View.GONE); binding.bottomSheetDetails.icon.setVisibility(View.GONE);
binding.bottomSheetDetails.wikiDataLl.setVisibility(View.GONE); binding.bottomSheetDetails.wikiDataLl.setVisibility(View.GONE);
getPlaceData(place.getWikiDataEntityId(), place, marker1); if (Objects.equals(updatedPlace.name, "")){
getPlaceData(updatedPlace.getWikiDataEntityId(), updatedPlace, marker1);
}else {
marker.showInfoWindow();
binding.bottomSheetDetails.dataCircularProgress.setVisibility(View.GONE);
binding.bottomSheetDetails.icon.setVisibility(View.VISIBLE);
binding.bottomSheetDetails.wikiDataLl.setVisibility(View.VISIBLE);
passInfoToSheet(place);
hideBottomSheet();
}
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
return true; return true;
}); });

View file

@ -6,7 +6,7 @@ class ResultTuple {
@SerializedName("xml:lang") @SerializedName("xml:lang")
val language: String val language: String
val type: String val type: String
val value: String var value: String
constructor(lang: String, type: String, value: String) { constructor(lang: String, type: String, value: String) {
this.language = lang this.language = lang

View file

@ -25,6 +25,7 @@ import fr.free.nrw.commons.nearby.Label;
import fr.free.nrw.commons.nearby.MarkerPlaceGroup; import fr.free.nrw.commons.nearby.MarkerPlaceGroup;
import fr.free.nrw.commons.nearby.NearbyController; import fr.free.nrw.commons.nearby.NearbyController;
import fr.free.nrw.commons.nearby.NearbyFilterState; import fr.free.nrw.commons.nearby.NearbyFilterState;
import fr.free.nrw.commons.nearby.Place;
import fr.free.nrw.commons.nearby.contract.NearbyParentFragmentContract; import fr.free.nrw.commons.nearby.contract.NearbyParentFragmentContract;
import fr.free.nrw.commons.utils.LocationUtils; import fr.free.nrw.commons.utils.LocationUtils;
import fr.free.nrw.commons.wikidata.WikidataEditListener; import fr.free.nrw.commons.wikidata.WikidataEditListener;
@ -213,19 +214,20 @@ public class NearbyParentFragmentPresenter
* Populates places for custom location, should be used for finding nearby places around a * Populates places for custom location, should be used for finding nearby places around a
* location where you are not at. * location where you are not at.
* *
* @param nearbyPlacesInfo This variable has placeToCenter list information and distances. * @param nearbyPlaces This variable has placeToCenter list information and distances.
*/ */
public void updateMapMarkers(NearbyController.NearbyPlacesInfo nearbyPlacesInfo, boolean shouldTrackPosition) { public void updateMapMarkers(List<Place> nearbyPlaces, LatLng currentLatLng,
boolean shouldTrackPosition) {
if (null != nearbyParentFragmentView) { if (null != nearbyParentFragmentView) {
nearbyParentFragmentView.clearAllMarkers(); nearbyParentFragmentView.clearAllMarkers();
List<BaseMarker> baseMarkers = NearbyController List<BaseMarker> baseMarkers = NearbyController
.loadAttractionsFromLocationToBaseMarkerOptions(nearbyPlacesInfo.currentLatLng, .loadAttractionsFromLocationToBaseMarkerOptions(currentLatLng,
// Curlatlang will be used to calculate distances // Curlatlang will be used to calculate distances
nearbyPlacesInfo.placeList); nearbyPlaces);
nearbyParentFragmentView.updateMapMarkers(baseMarkers); nearbyParentFragmentView.updateMapMarkers(baseMarkers);
lockUnlockNearby(false); // So that new location updates wont come lockUnlockNearby(false); // So that new location updates wont come
nearbyParentFragmentView.setProgressBarVisibility(false); nearbyParentFragmentView.setProgressBarVisibility(false);
nearbyParentFragmentView.updateListFragment(nearbyPlacesInfo.placeList); nearbyParentFragmentView.updateListFragment(nearbyPlaces);
} }
} }

View file

@ -13,7 +13,7 @@ SELECT
WHERE { WHERE {
SERVICE <https://query.wikidata.org/sparql> { SERVICE <https://query.wikidata.org/sparql> {
values ?item { values ?item {
wd:${ENTITY} ${ENTITY}
} }
} }