mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 14:53:59 +01:00 
			
		
		
		
	Add query syntax and methods
This commit is contained in:
		
							parent
							
								
									a19ce3e208
								
							
						
					
					
						commit
						df8a973079
					
				
					 5 changed files with 103 additions and 5 deletions
				
			
		|  | @ -2,6 +2,7 @@ package fr.free.nrw.commons.mwapi | |||
| 
 | ||||
| import android.text.TextUtils | ||||
| import com.google.gson.Gson | ||||
| import com.google.gson.JsonParser | ||||
| import fr.free.nrw.commons.BuildConfig | ||||
| import fr.free.nrw.commons.campaigns.CampaignResponseDTO | ||||
| import fr.free.nrw.commons.explore.depictions.DepictsClient | ||||
|  | @ -330,6 +331,74 @@ class OkHttpJsonApiClient @Inject constructor( | |||
|         throw Exception(response.message) | ||||
|     } | ||||
| 
 | ||||
|     @Throws(Exception::class) | ||||
|     fun getNearbyItemCount( | ||||
|         screenTopRight: LatLng, screenBottomLeft: LatLng | ||||
|     ): Int { | ||||
|         val wikidataQuery: String = | ||||
|             FileUtils.readFromResource("/queries/rectangle_query_for_item_count.rq") | ||||
| 
 | ||||
|         val westCornerLat = screenTopRight.latitude | ||||
|         val westCornerLong = screenTopRight.longitude | ||||
|         val eastCornerLat = screenBottomLeft.latitude | ||||
|         val eastCornerLong = screenBottomLeft.longitude | ||||
| 
 | ||||
|         val query = wikidataQuery | ||||
|             .replace("\${LAT_WEST}", String.format(Locale.ROOT, "%.4f", westCornerLat)) | ||||
|             .replace("\${LONG_WEST}", String.format(Locale.ROOT, "%.4f", westCornerLong)) | ||||
|             .replace("\${LAT_EAST}", String.format(Locale.ROOT, "%.4f", eastCornerLat)) | ||||
|             .replace("\${LONG_EAST}", String.format(Locale.ROOT, "%.4f", eastCornerLong)) | ||||
| 
 | ||||
|         val urlBuilder: HttpUrl.Builder = sparqlQueryUrl.toHttpUrlOrNull()!! | ||||
|             .newBuilder() | ||||
|             .addQueryParameter("query", query) | ||||
|             .addQueryParameter("format", "json") | ||||
| 
 | ||||
|         val request: Request = Request.Builder() | ||||
|             .url(urlBuilder.build()) | ||||
|             .build() | ||||
| 
 | ||||
|         val response = okHttpClient.newCall(request).execute() | ||||
|         if (response.body != null && response.isSuccessful) { | ||||
|             val json = response.body!!.string() | ||||
|             return JsonParser.parseString(json).getAsJsonObject().getAsJsonObject("results") | ||||
|                 .getAsJsonArray("bindings").get(0).getAsJsonObject().getAsJsonObject("count") | ||||
|                 .get("value").asInt | ||||
|         } | ||||
|         throw Exception(response.message) | ||||
|     } | ||||
| 
 | ||||
|     @Throws(Exception::class) | ||||
|     fun getNearbyItemCount( | ||||
|         center: LatLng, radius: Double | ||||
|     ): Int { | ||||
|         val wikidataQuery: String = | ||||
|             FileUtils.readFromResource("/queries/radius_query_for_item_count.rq") | ||||
| 
 | ||||
|         val query = wikidataQuery | ||||
|             .replace("\${LAT}", String.format(Locale.ROOT, "%.4f", center.latitude)) | ||||
|             .replace("\${LONG}", String.format(Locale.ROOT, "%.4f", center.longitude)) | ||||
|             .replace("\${RAD}", String.format(Locale.ROOT, "%.2f", radius)) | ||||
| 
 | ||||
|         val urlBuilder: HttpUrl.Builder = sparqlQueryUrl.toHttpUrlOrNull()!! | ||||
|             .newBuilder() | ||||
|             .addQueryParameter("query", query) | ||||
|             .addQueryParameter("format", "json") | ||||
| 
 | ||||
|         val request: Request = Request.Builder() | ||||
|             .url(urlBuilder.build()) | ||||
|             .build() | ||||
| 
 | ||||
|         val response = okHttpClient.newCall(request).execute() | ||||
|         if (response.body != null && response.isSuccessful) { | ||||
|             val json = response.body!!.string() | ||||
|             return JsonParser.parseString(json).getAsJsonObject().getAsJsonObject("results") | ||||
|                 .getAsJsonArray("bindings").get(0).getAsJsonObject().getAsJsonObject("count") | ||||
|                 .get("value").asInt | ||||
|         } | ||||
|         throw Exception(response.message) | ||||
|     } | ||||
| 
 | ||||
|     @Throws(Exception::class) | ||||
|     fun getNearbyPlaces( | ||||
|         screenTopRight: LatLng, | ||||
|  |  | |||
|  | @ -196,8 +196,9 @@ public class NearbyController extends MapController { | |||
|             return null; | ||||
|         } | ||||
| 
 | ||||
|         List<Place> places = nearbyPlaces.getFromWikidataQuery(screenTopRight, screenBottomLeft, | ||||
|             Locale.getDefault().getLanguage(), shouldQueryForMonuments, customQuery); | ||||
|         List<Place> places = nearbyPlaces.getFromWikidataQuery(currentLatLng, screenTopRight, | ||||
|             screenBottomLeft, Locale.getDefault().getLanguage(), shouldQueryForMonuments, | ||||
|             customQuery); | ||||
| 
 | ||||
|         if (null != places && places.size() > 0) { | ||||
|             LatLng[] boundaryCoordinates = { | ||||
|  |  | |||
|  | @ -101,6 +101,7 @@ public class NearbyPlaces { | |||
|      * Retrieves a list of places from a Wikidata query based on screen coordinates and optional | ||||
|      * parameters. | ||||
|      * | ||||
|      * @param centerPoint             The center of the map, used for radius queries if required. | ||||
|      * @param screenTopRight          The top right corner of the screen (latitude, longitude). | ||||
|      * @param screenBottomLeft        The bottom left corner of the screen (latitude, longitude). | ||||
|      * @param lang                    The language for the query. | ||||
|  | @ -111,14 +112,23 @@ public class NearbyPlaces { | |||
|      * @throws Exception If an error occurs during the retrieval process. | ||||
|      */ | ||||
|     public List<Place> getFromWikidataQuery( | ||||
|         final fr.free.nrw.commons.location.LatLng centerPoint, | ||||
|         final fr.free.nrw.commons.location.LatLng screenTopRight, | ||||
|         final fr.free.nrw.commons.location.LatLng screenBottomLeft, final String lang, | ||||
|         final boolean shouldQueryForMonuments, | ||||
|         @Nullable final String customQuery) throws Exception { | ||||
|         if (customQuery != null){ | ||||
|             return okHttpJsonApiClient | ||||
|                 .getNearbyPlaces(screenTopRight, screenBottomLeft, lang, shouldQueryForMonuments, | ||||
|                     customQuery); | ||||
|         } | ||||
|         final double east = screenTopRight.getLongitude(); | ||||
|         final double west = screenBottomLeft.getLongitude(); | ||||
|         final double north = screenTopRight.getLatitude(); | ||||
|         final double south = screenBottomLeft.getLatitude(); | ||||
| 
 | ||||
|         return new java.util.ArrayList<Place>(); // TODO replace with actual method call | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Retrieves a list of places based on the provided list of places and language. | ||||
|  |  | |||
|  | @ -0,0 +1,9 @@ | |||
| SELECT (COUNT(?item) AS ?itemCount) | ||||
| WHERE { | ||||
|   # Around given location. | ||||
|   SERVICE wikibase:around { | ||||
|     ?item wdt:P625 ?location. | ||||
|     bd:serviceParam wikibase:center "Point(${LONG} ${LAT})"^^geo:wktLiteral. | ||||
|     bd:serviceParam wikibase:radius "${RAD}" . # Radius in kilometers. | ||||
|   } | ||||
| } | ||||
|  | @ -0,0 +1,9 @@ | |||
| SELECT (COUNT(DISTINCT ?item) AS ?count) | ||||
| WHERE { | ||||
|   SERVICE wikibase:box { | ||||
|     ?item wdt:P625 ?location. | ||||
|     bd:serviceParam wikibase:cornerWest "Point(${LONG_WEST} ${LAT_WEST})"^^geo:wktLiteral. | ||||
|     bd:serviceParam wikibase:cornerEast "Point(${LONG_EAST} ${LAT_EAST})"^^geo:wktLiteral. | ||||
|   } | ||||
| } | ||||
| &format=json | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 savsch
						savsch