Skip to content

Commit

Permalink
Integrate WLM
Browse files Browse the repository at this point in the history
- Show monuments in maps along with nearby
  • Loading branch information
ashishkumar468 committed Jul 8, 2021
1 parent 2233fc2 commit d87b663
Show file tree
Hide file tree
Showing 31 changed files with 2,718 additions and 108 deletions.
15 changes: 15 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.view.View;
Expand All @@ -17,6 +18,7 @@
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.core.content.ContextCompat;

import java.util.Date;
import org.wikipedia.dataclient.WikiSite;
import org.wikipedia.page.PageTitle;

Expand Down Expand Up @@ -206,4 +208,17 @@ public static void setUnderlinedText(TextView textView, int stringResourceName,
textView.setText(content);
}

/**
* For now we are enabling the monuments only when the date lies between 1 Sept & 31 OCt
* @param date
* @return
*/
public static boolean isMonumentsEnabled(Date date){
return true;
// if(date.getDay()>=1 && date.getMonth()>=9 && date.getDay()<=31 && date.getMonth()<=10){
// return true;
// }
// return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,59 @@ public Observable<List<Place>> getNearbyPlaces(LatLng cur, String language, doub
});
}

/**
* Wikidata query to fetch monuments
*
* @param cur : The current location coordinates
* @param language : The language
* @param radius : The radius around the current location within which we expect the results
* @return
* @throws IOException
*/
public Observable<List<Place>> getNearbyMonuments(LatLng cur, String language, final double radius)
throws IOException {
final String wikidataQuery = FileUtils.readFromResource("/queries/monuments_query.rq");
if (TextUtils.isEmpty(language)) {
language="en";
}
String query = wikidataQuery
.replace("${RAD}", String.format(Locale.ROOT, "%.2f", radius))
.replace("${LAT}", String.format(Locale.ROOT, "%.4f", cur.getLatitude()))
.replace("${LONG}", String.format(Locale.ROOT, "%.4f", cur.getLongitude()))
.replace("${LANG}", language);

HttpUrl.Builder urlBuilder = HttpUrl
.parse(sparqlQueryUrl)
.newBuilder()
.addQueryParameter("query", query)
.addQueryParameter("format", "json");

Request request = new Request.Builder()
.url(urlBuilder.build())
.build();

return Observable.fromCallable(() -> {
Response response = okHttpClient.newCall(request).execute();
if (response != null && response.body() != null && response.isSuccessful()) {
String json = response.body().string();
if (json == null) {
return new ArrayList<>();
}

NearbyResponse nearbyResponse = gson.fromJson(json, NearbyResponse.class);
List<NearbyResultItem> bindings = nearbyResponse.getResults().getBindings();
List<Place> places = new ArrayList<>();
for (NearbyResultItem item : bindings) {
Place place = Place.from(item);
place.setMonument(true);
places.add(place);
}
return places;
}
return new ArrayList<>();
});
}

/**
* Get the QIDs of all Wikidata items that are subclasses of the given Wikidata item. Example:
* bridge -> suspended bridge, aqueduct, etc
Expand Down
34 changes: 25 additions & 9 deletions app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.mapbox.mapboxsdk.annotations.IconFactory;
import com.mapbox.mapboxsdk.annotations.Marker;

import io.reactivex.Observable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -127,6 +128,12 @@ public NearbyPlacesInfo loadAttractionsFromLocation(LatLng curLatLng, LatLng sea
}
}

public Observable<List<Place>> queryWikiDataForMonuments(
LatLng latLng, String language)
throws IOException {
return nearbyPlaces.queryWikiDataForMonuments(latLng, language);
}

/**
* Loads attractions from location for list view, we need to return Place data type.
*
Expand Down Expand Up @@ -168,6 +175,7 @@ public static List<NearbyBaseMarker> loadAttractionsFromLocationToBaseMarkerOpti
VectorDrawableCompat vectorDrawable = null;
VectorDrawableCompat vectorDrawableGreen = null;
VectorDrawableCompat vectorDrawableGrey = null;
VectorDrawableCompat vectorDrawableMonuments = null;
vectorDrawable = null;
try {
vectorDrawable = VectorDrawableCompat.create(
Expand All @@ -176,41 +184,49 @@ public static List<NearbyBaseMarker> loadAttractionsFromLocationToBaseMarkerOpti
context.getResources(), R.drawable.ic_custom_map_marker_green, context.getTheme());
vectorDrawableGrey = VectorDrawableCompat.create(
context.getResources(), R.drawable.ic_custom_map_marker_grey, context.getTheme());
vectorDrawableMonuments = VectorDrawableCompat
.create(context.getResources(), R.drawable.ic_custom_map_marker_monuments,
context.getTheme());
} catch (Resources.NotFoundException e) {
// ignore when running tests.
}
if (vectorDrawable != null) {
Bitmap icon = UiUtils.getBitmap(vectorDrawable);
Bitmap iconGreen = UiUtils.getBitmap(vectorDrawableGreen);
Bitmap iconGrey = UiUtils.getBitmap(vectorDrawableGrey);
Bitmap iconMonuments = UiUtils.getBitmap(vectorDrawableMonuments);

for (Place place : placeList) {
NearbyBaseMarker nearbyBaseMarker = new NearbyBaseMarker();
String distance = formatDistanceBetween(curLatLng, place.location);
place.setDistance(distance);

NearbyBaseMarker nearbyBaseMarker = new NearbyBaseMarker();
nearbyBaseMarker.title(place.name);
nearbyBaseMarker.position(
new com.mapbox.mapboxsdk.geometry.LatLng(
place.location.getLatitude(),
place.location.getLongitude()));
new com.mapbox.mapboxsdk.geometry.LatLng(
place.location.getLatitude(),
place.location.getLongitude()));
nearbyBaseMarker.place(place);
// Check if string is only spaces or empty, if so place doesn't have any picture
if (!place.pic.trim().isEmpty()) {

if (place.isMonument()) {
nearbyBaseMarker.icon(IconFactory.getInstance(context)
.fromBitmap(iconMonuments));
}
else if (!place.pic.trim().isEmpty()) {
if (iconGreen != null) {
nearbyBaseMarker.icon(IconFactory.getInstance(context)
.fromBitmap(iconGreen));
.fromBitmap(iconGreen));
}
} 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
if (iconGrey != null) {
nearbyBaseMarker.icon(IconFactory.getInstance(context)
.fromBitmap(iconGrey));
.fromBitmap(iconGrey));
}
} else {
nearbyBaseMarker.icon(IconFactory.getInstance(context)
.fromBitmap(icon));
.fromBitmap(icon));
}

baseMarkerOptions.add(nearbyBaseMarker);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public class NearbyFilterState {
private boolean existsSelected;
private boolean needPhotoSelected;
private boolean wlmSelected;
private int checkBoxTriState;
private ArrayList<Label> selectedLabels;

Expand All @@ -16,6 +17,7 @@ public class NearbyFilterState {
private NearbyFilterState() {
existsSelected = false;
needPhotoSelected = true;
wlmSelected = true;
checkBoxTriState = -1; // Unknown
selectedLabels = new ArrayList<>(); // Initially empty
}
Expand All @@ -39,6 +41,14 @@ public static void setNeedPhotoSelected(boolean needPhotoSelected) {
getInstance().needPhotoSelected = needPhotoSelected;
}

public static void setWlmSelected(final boolean wlmSelected) {
getInstance().wlmSelected = wlmSelected;
}

public boolean isWlmSelected() {
return wlmSelected;
}

public boolean isExistsSelected() {
return existsSelected;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.free.nrw.commons.nearby;

import io.reactivex.Observable;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.Collections;
Expand Down Expand Up @@ -94,4 +95,11 @@ List<Place> radiusExpander(LatLng curLatLng, String lang, boolean returnClosestR
public List<Place> getFromWikidataQuery(LatLng cur, String lang, double radius) throws IOException {
return okHttpJsonApiClient.getNearbyPlaces(cur, lang, radius).blockingSingle();
}

public Observable<List<Place>> queryWikiDataForMonuments(
LatLng latLng, String language)
throws IOException {
return okHttpJsonApiClient
.getNearbyMonuments(latLng, language, NearbyController.latestSearchRadius);
}
}
69 changes: 53 additions & 16 deletions app/src/main/java/fr/free/nrw/commons/nearby/Place.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import android.os.Parcel;
import android.os.Parcelable;

import android.text.TextUtils;
import androidx.annotation.Nullable;

import fr.free.nrw.commons.nearby.NearbyController.NearbyPlacesInfo;
import org.apache.commons.lang3.StringUtils;

import fr.free.nrw.commons.location.LatLng;
Expand All @@ -31,9 +33,12 @@ public class Place implements Parcelable {

public String distance;
public final Sitelinks siteLinks;
private boolean isMonument;
@Nullable
private String address;


public Place(String language,String name, Label label, String longDescription, LatLng location, String category, Sitelinks siteLinks, String pic, Boolean exists) {
public Place(String language,String name, Label label, String longDescription, LatLng location, String category, Sitelinks siteLinks, String pic, Boolean exists, @Nullable String address) {
this.language = language;
this.name = name;
this.label = label;
Expand All @@ -43,6 +48,7 @@ public Place(String language,String name, Label label, String longDescription, L
this.siteLinks = siteLinks;
this.pic = (pic == null) ? "":pic;
this.exists = exists;
this.address = address;
}
public Place(Parcel in) {
this.language = in.readString();
Expand All @@ -56,6 +62,8 @@ public Place(Parcel in) {
this.pic = (picString == null) ? "":picString;
String existString = in.readString();
this.exists = Boolean.parseBoolean(existString);
this.isMonument = in.readInt() == 1;
this.address = in.readString();
}
public static Place from(NearbyResultItem item) {
String itemClass = item.getClassName().getValue();
Expand All @@ -80,20 +88,21 @@ public static Place from(NearbyResultItem item) {
? " (" + description + ")" : "")
: description);
return new Place(
item.getLabel().getLanguage(),
item.getLabel().getValue(),
Label.fromText(classEntityId), // list
description, // description and label of Wikidata item
PlaceUtils.latLngFromPointString(item.getLocation().getValue()),
item.getCommonsCategory().getValue(),
new Sitelinks.Builder()
.setWikipediaLink(item.getWikipediaArticle().getValue())
.setCommonsLink(item.getCommonsArticle().getValue())
.setWikidataLink(item.getItem().getValue())
.build(),
item.getPic().getValue(),
// Checking if the place exists or not
(item.getDestroyed().getValue() == "") && (item.getEndTime().getValue() == ""));
item.getLabel().getLanguage(),
item.getLabel().getValue(),
Label.fromText(classEntityId), // list
description, // description and label of Wikidata item
PlaceUtils.latLngFromPointString(item.getLocation().getValue()),
item.getCommonsCategory().getValue(),
new Sitelinks.Builder()
.setWikipediaLink(item.getWikipediaArticle().getValue())
.setCommonsLink(item.getCommonsArticle().getValue())
.setWikidataLink(item.getItem().getValue())
.build(),
item.getPic().getValue(),
// Checking if the place exists or not
(item.getDestroyed().getValue() == "") && (item.getEndTime().getValue() == ""),
item.getAddress());
}

/**
Expand Down Expand Up @@ -126,7 +135,12 @@ public LatLng getLocation() {
* Gets the long description of the place
* @return long description
*/
public String getLongDescription() { return longDescription; }
public String getLongDescription() {
if(isMonument){
return name+ (TextUtils.isEmpty(address)?"": String.format("(%s)", address));
}
return longDescription;
}

/**
* Gets the Commons category of the place
Expand Down Expand Up @@ -181,6 +195,27 @@ public boolean hasCommonsLink() {
return !(siteLinks == null || Uri.EMPTY.equals(siteLinks.getCommonsLink()));
}

/**
* Sets that this place in nearby is a WikiData monument
* @param monument
*/
public void setMonument(final boolean monument) {
isMonument = monument;
}

/**
* Returns if this place is a WikiData monument
* @return
*/
public boolean isMonument() {
return isMonument;
}

@Nullable
public String getAddress() {
return address;
}

/**
* Check if we already have the exact same Place
* @param o Place being tested
Expand Down Expand Up @@ -233,6 +268,8 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(siteLinks, 0);
dest.writeString(pic);
dest.writeString(exists.toString());
dest.writeInt(isMonument ? 1 : 0);
dest.writeString(TextUtils.isEmpty(address) ? "" : address);
}

public static final Creator<Place> CREATOR = new Creator<Place>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.content.Context;

import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.maps.MapboxMap;

import java.util.List;

Expand All @@ -13,7 +12,6 @@
import fr.free.nrw.commons.nearby.Label;
import fr.free.nrw.commons.nearby.NearbyBaseMarker;
import fr.free.nrw.commons.nearby.Place;
import fr.free.nrw.commons.nearby.presenter.NearbyParentFragmentPresenter;

public interface NearbyParentFragmentContract {

Expand Down Expand Up @@ -60,7 +58,7 @@ interface View {

void displayAllMarkers();

void filterMarkersByLabels(List<Label> selectedLabels, boolean existsSelected, boolean needPhotoSelected, boolean filterForPlaceState, boolean filterForAllNoneType);
void filterMarkersByLabels(List<Label> selectedLabels, boolean existsSelected, boolean needPhotoSelected, boolean wlmSelected, boolean filterForPlaceState, boolean filterForAllNoneType);

LatLng getCameraTarget();

Expand Down
Loading

0 comments on commit d87b663

Please sign in to comment.