Skip to content

Commit 9a0b3bf

Browse files
[WIP]Fixes commons-app#545
- Add query for monuments - Make API call to fetch monuments - Attach the list of monuments with nearby places response and render the monumens on the Map along with other nearby items
1 parent b6ffe9f commit 9a0b3bf

File tree

8 files changed

+247
-51
lines changed

8 files changed

+247
-51
lines changed

app/src/main/java/fr/free/nrw/commons/mwapi/OkHttpJsonApiClient.java

+53
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,59 @@ public Observable<List<Place>> getNearbyPlaces(LatLng cur, String language, doub
298298
});
299299
}
300300

301+
/**
302+
* Wikidata query to fetch monuments
303+
*
304+
* @param cur : The current location coordinates
305+
* @param language : The language
306+
* @param radius : The radius around the current location within which we expect the results
307+
* @return
308+
* @throws IOException
309+
*/
310+
public Observable<List<Place>> getNearbyMonuments(LatLng cur, String language, double radius)
311+
throws IOException {
312+
String wikidataQuery = FileUtils.readFromResource("/queries/monuments_query.rq");
313+
if (radius < 10) {
314+
radius = 10d;//This is temporary, I will figure something out
315+
}
316+
String query = wikidataQuery
317+
.replace("${RAD}", String.format(Locale.ROOT, "%.2f", radius))
318+
.replace("${LAT}", String.format(Locale.ROOT, "%.4f", cur.getLatitude()))
319+
.replace("${LONG}", String.format(Locale.ROOT, "%.4f", cur.getLongitude()))
320+
.replace("${LANG}", language);
321+
322+
HttpUrl.Builder urlBuilder = HttpUrl
323+
.parse(sparqlQueryUrl)
324+
.newBuilder()
325+
.addQueryParameter("query", query)
326+
.addQueryParameter("format", "json");
327+
328+
Request request = new Request.Builder()
329+
.url(urlBuilder.build())
330+
.build();
331+
332+
return Observable.fromCallable(() -> {
333+
Response response = okHttpClient.newCall(request).execute();
334+
if (response != null && response.body() != null && response.isSuccessful()) {
335+
String json = response.body().string();
336+
if (json == null) {
337+
return new ArrayList<>();
338+
}
339+
340+
NearbyResponse nearbyResponse = gson.fromJson(json, NearbyResponse.class);
341+
List<NearbyResultItem> bindings = nearbyResponse.getResults().getBindings();
342+
List<Place> places = new ArrayList<>();
343+
for (NearbyResultItem item : bindings) {
344+
Place place = Place.from(item);
345+
place.setMonument(true);
346+
places.add(place);
347+
}
348+
return places;
349+
}
350+
return new ArrayList<>();
351+
});
352+
}
353+
301354
/**
302355
* Get the QIDs of all Wikidata items that are subclasses of the given Wikidata item. Example:
303356
* bridge -> suspended bridge, aqueduct, etc

app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java

+25-9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.mapbox.mapboxsdk.annotations.IconFactory;
1111
import com.mapbox.mapboxsdk.annotations.Marker;
1212

13+
import io.reactivex.Observable;
1314
import java.io.IOException;
1415
import java.util.ArrayList;
1516
import java.util.Collections;
@@ -127,6 +128,12 @@ public NearbyPlacesInfo loadAttractionsFromLocation(LatLng curLatLng, LatLng sea
127128
}
128129
}
129130

131+
public Observable<List<Place>> queryWikiDataForMonuments(
132+
LatLng latLng, String language)
133+
throws IOException {
134+
return nearbyPlaces.queryWikiDataForMonuments(latLng, language);
135+
}
136+
130137
/**
131138
* Loads attractions from location for list view, we need to return Place data type.
132139
*
@@ -168,6 +175,7 @@ public static List<NearbyBaseMarker> loadAttractionsFromLocationToBaseMarkerOpti
168175
VectorDrawableCompat vectorDrawable = null;
169176
VectorDrawableCompat vectorDrawableGreen = null;
170177
VectorDrawableCompat vectorDrawableGrey = null;
178+
VectorDrawableCompat vectorDrawableMonuments = null;
171179
vectorDrawable = null;
172180
try {
173181
vectorDrawable = VectorDrawableCompat.create(
@@ -176,41 +184,49 @@ public static List<NearbyBaseMarker> loadAttractionsFromLocationToBaseMarkerOpti
176184
context.getResources(), R.drawable.ic_custom_map_marker_green, context.getTheme());
177185
vectorDrawableGrey = VectorDrawableCompat.create(
178186
context.getResources(), R.drawable.ic_custom_map_marker_grey, context.getTheme());
187+
vectorDrawableMonuments = VectorDrawableCompat
188+
.create(context.getResources(), R.drawable.ic_custom_map_marker_monuments,
189+
context.getTheme());
179190
} catch (Resources.NotFoundException e) {
180191
// ignore when running tests.
181192
}
182193
if (vectorDrawable != null) {
183194
Bitmap icon = UiUtils.getBitmap(vectorDrawable);
184195
Bitmap iconGreen = UiUtils.getBitmap(vectorDrawableGreen);
185196
Bitmap iconGrey = UiUtils.getBitmap(vectorDrawableGrey);
197+
Bitmap iconMonuments = UiUtils.getBitmap(vectorDrawableMonuments);
186198

187199
for (Place place : placeList) {
200+
NearbyBaseMarker nearbyBaseMarker = new NearbyBaseMarker();
188201
String distance = formatDistanceBetween(curLatLng, place.location);
189202
place.setDistance(distance);
190203

191-
NearbyBaseMarker nearbyBaseMarker = new NearbyBaseMarker();
192204
nearbyBaseMarker.title(place.name);
193205
nearbyBaseMarker.position(
194-
new com.mapbox.mapboxsdk.geometry.LatLng(
195-
place.location.getLatitude(),
196-
place.location.getLongitude()));
206+
new com.mapbox.mapboxsdk.geometry.LatLng(
207+
place.location.getLatitude(),
208+
place.location.getLongitude()));
197209
nearbyBaseMarker.place(place);
198210
// Check if string is only spaces or empty, if so place doesn't have any picture
199-
if (!place.pic.trim().isEmpty()) {
211+
212+
if (place.isMonument()) {
213+
nearbyBaseMarker.icon(IconFactory.getInstance(context)
214+
.fromBitmap(iconMonuments));
215+
}
216+
else if (!place.pic.trim().isEmpty()) {
200217
if (iconGreen != null) {
201218
nearbyBaseMarker.icon(IconFactory.getInstance(context)
202-
.fromBitmap(iconGreen));
219+
.fromBitmap(iconGreen));
203220
}
204221
} 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
205222
if (iconGrey != null) {
206223
nearbyBaseMarker.icon(IconFactory.getInstance(context)
207-
.fromBitmap(iconGrey));
224+
.fromBitmap(iconGrey));
208225
}
209226
} else {
210227
nearbyBaseMarker.icon(IconFactory.getInstance(context)
211-
.fromBitmap(icon));
228+
.fromBitmap(icon));
212229
}
213-
214230
baseMarkerOptions.add(nearbyBaseMarker);
215231
}
216232
}

app/src/main/java/fr/free/nrw/commons/nearby/NearbyPlaces.java

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fr.free.nrw.commons.nearby;
22

3+
import io.reactivex.Observable;
34
import java.io.IOException;
45
import java.io.InterruptedIOException;
56
import java.util.Collections;
@@ -94,4 +95,10 @@ List<Place> radiusExpander(LatLng curLatLng, String lang, boolean returnClosestR
9495
public List<Place> getFromWikidataQuery(LatLng cur, String lang, double radius) throws IOException {
9596
return okHttpJsonApiClient.getNearbyPlaces(cur, lang, radius).blockingSingle();
9697
}
98+
99+
public Observable<List<Place>> queryWikiDataForMonuments(
100+
LatLng latLng, String language)
101+
throws IOException {
102+
return okHttpJsonApiClient.getNearbyMonuments(latLng, language, radius);
103+
}
97104
}

app/src/main/java/fr/free/nrw/commons/nearby/Place.java

+18
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import androidx.annotation.Nullable;
88

9+
import fr.free.nrw.commons.nearby.NearbyController.NearbyPlacesInfo;
910
import org.apache.commons.lang3.StringUtils;
1011

1112
import fr.free.nrw.commons.location.LatLng;
@@ -31,6 +32,7 @@ public class Place implements Parcelable {
3132

3233
public String distance;
3334
public final Sitelinks siteLinks;
35+
private boolean isMonument;
3436

3537

3638
public Place(String language,String name, Label label, String longDescription, LatLng location, String category, Sitelinks siteLinks, String pic, Boolean exists) {
@@ -165,6 +167,22 @@ public boolean hasCommonsLink() {
165167
return !(siteLinks == null || Uri.EMPTY.equals(siteLinks.getCommonsLink()));
166168
}
167169

170+
/**
171+
* Sets that this place in nearby is a WikiData monument
172+
* @param monument
173+
*/
174+
public void setMonument(final boolean monument) {
175+
isMonument = monument;
176+
}
177+
178+
/**
179+
* Returns if this place is a WikiData monument
180+
* @return
181+
*/
182+
public boolean isMonument() {
183+
return isMonument;
184+
}
185+
168186
/**
169187
* Check if we already have the exact same Place
170188
* @param o Place being tested

app/src/main/java/fr/free/nrw/commons/nearby/contract/NearbyParentFragmentContract.java

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.content.Context;
44

55
import com.mapbox.mapboxsdk.annotations.Marker;
6-
import com.mapbox.mapboxsdk.maps.MapboxMap;
76

87
import java.util.List;
98

@@ -13,7 +12,6 @@
1312
import fr.free.nrw.commons.nearby.Label;
1413
import fr.free.nrw.commons.nearby.NearbyBaseMarker;
1514
import fr.free.nrw.commons.nearby.Place;
16-
import fr.free.nrw.commons.nearby.presenter.NearbyParentFragmentPresenter;
1715

1816
public interface NearbyParentFragmentContract {
1917

0 commit comments

Comments
 (0)