Merge pull request #9406 from osmandapp/Fix_7131

Fix #7131
This commit is contained in:
Vitaliy 2020-07-09 11:45:10 +03:00 committed by GitHub
commit b42346f90a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 206 additions and 1 deletions

View file

@ -38,6 +38,7 @@ public class Amenity extends MapObject {
public static final String COLLECTION_TIMES = "collection_times"; public static final String COLLECTION_TIMES = "collection_times";
public static final String CONTENT = "content"; public static final String CONTENT = "content";
public static final String CUISINE = "cuisine"; public static final String CUISINE = "cuisine";
public static final String WIKIDATA = "wikidata";
public static final String DISH = "dish"; public static final String DISH = "dish";
public static final String REF = "ref"; public static final String REF = "ref";
public static final String OSM_DELETE_VALUE = "delete"; public static final String OSM_DELETE_VALUE = "delete";

View file

@ -471,7 +471,7 @@ public class AmenityMenuBuilder extends MenuBuilder {
} }
textPrefix = app.getString(R.string.poi_cuisine); textPrefix = app.getString(R.string.poi_cuisine);
vl = sb.toString(); vl = sb.toString();
} else if (key.contains(Amenity.ROUTE)) { } else if (key.contains(Amenity.ROUTE) || key.equals(Amenity.WIKIDATA)) {
continue; continue;
} else { } else {
if (key.contains(Amenity.DESCRIPTION)) { if (key.contains(Amenity.DESCRIPTION)) {
@ -784,12 +784,16 @@ public class AmenityMenuBuilder extends MenuBuilder {
Map<String, String> additionalInfo = amenity.getAdditionalInfo(); Map<String, String> additionalInfo = amenity.getAdditionalInfo();
String imageValue = additionalInfo.get("image"); String imageValue = additionalInfo.get("image");
String mapillaryValue = additionalInfo.get("mapillary"); String mapillaryValue = additionalInfo.get("mapillary");
String wikidataValue = additionalInfo.get("wikidata");
if (!Algorithms.isEmpty(imageValue)) { if (!Algorithms.isEmpty(imageValue)) {
params.put("osm_image", imageValue); params.put("osm_image", imageValue);
} }
if (!Algorithms.isEmpty(mapillaryValue)) { if (!Algorithms.isEmpty(mapillaryValue)) {
params.put("osm_mapillary_key", mapillaryValue); params.put("osm_mapillary_key", mapillaryValue);
} }
if (!Algorithms.isEmpty(wikidataValue)) {
params.put("wikidata_id", wikidataValue);
}
return params; return params;
} }

View file

@ -25,6 +25,7 @@ import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.MenuBuilder; import net.osmand.plus.mapcontextmenu.MenuBuilder;
import net.osmand.plus.mapillary.MapillaryContributeCard; import net.osmand.plus.mapillary.MapillaryContributeCard;
import net.osmand.plus.mapillary.MapillaryImageCard; import net.osmand.plus.mapillary.MapillaryImageCard;
import net.osmand.plus.wikimedia.WikiImageHelper;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import org.json.JSONArray; import org.json.JSONArray;
@ -437,6 +438,11 @@ public abstract class ImageCard extends AbstractCard {
pms.put("lang", preferredLang); pms.put("lang", preferredLang);
} }
if (this.params != null) { if (this.params != null) {
String wikidataId = this.params.get("wikidata_id");
if (wikidataId != null) {
this.params.remove("wikidata_id");
WikiImageHelper.fillWikiMediaCards(mapActivity, wikidataId, result);
}
pms.putAll(this.params); pms.putAll(this.params);
} }
String response = AndroidNetworkUtils.sendRequest(app, "https://osmand.net/api/cm_place", pms, String response = AndroidNetworkUtils.sendRequest(app, "https://osmand.net/api/cm_place", pms,

View file

@ -0,0 +1,28 @@
package net.osmand.plus.wikimedia;
public class WikiImage {
private String imageName;
private String imageStubUrl;
private String imageHiResUrl;
public WikiImage(String imageName, String imageStubUrl,
String imageHiResUrl) {
this.imageName = imageName;
this.imageStubUrl = imageStubUrl;
this.imageHiResUrl = imageHiResUrl;
}
public String getImageName() {
return imageName;
}
public String getImageStubUrl() {
return imageStubUrl;
}
public String getImageHiResUrl() {
return imageHiResUrl;
}
}

View file

@ -0,0 +1,40 @@
package net.osmand.plus.wikimedia;
import android.view.View;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard;
import net.osmand.util.Algorithms;
public class WikiImageCard extends ImageCard {
public WikiImageCard(final MapActivity mapActivity,
final WikiImage wikiImage) {
super(mapActivity, null);
if (topIconId == 0) {
topIconId = R.drawable.ic_logo_wikimedia;
}
this.imageUrl = wikiImage.getImageStubUrl();
this.title = wikiImage.getImageName();
this.url = this.imageUrl;
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
openUrl(getMapActivity(), getMyApplication(), getTitle(), wikiImage.getImageHiResUrl(),
isExternalLink() || Algorithms.isEmpty(getImageHiresUrl()),
!Algorithms.isEmpty(getImageHiresUrl()));
}
};
if (!Algorithms.isEmpty(buttonText)) {
this.onButtonClickListener = onClickListener;
} else {
this.onClickListener = onClickListener;
}
}
}

View file

@ -0,0 +1,126 @@
package net.osmand.plus.wikimedia;
import androidx.annotation.NonNull;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import net.osmand.PlatformUtil;
import net.osmand.osm.io.NetworkUtils;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.logging.Log;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
public class WikiImageHelper {
private static final String WIKIDATA_API_ENDPOINT = "https://www.wikidata.org/w/api.php";
private static final String ACTION = "?action=wbgetclaims&property=P18&entity=";
private static final String FORMAT_JSON = "&format=json";
private static final String IMAGE_BASE_URL = "https://upload.wikimedia.org/wikipedia/commons/";
private static final String WIKIDATA_PREFIX = "Q";
private static final int THUMB_SIZE = 500;
private static final Log LOG = PlatformUtil.getLog(WikiImageHelper.class);
public static void fillWikiMediaCards(@NonNull MapActivity mapActivity, @NonNull String wikidata,
List<ImageCard> images) {
if (wikidata.startsWith(WIKIDATA_PREFIX)) {
StringBuilder rawResponse = new StringBuilder();
String url = WIKIDATA_API_ENDPOINT + ACTION + wikidata + FORMAT_JSON;
String error = NetworkUtils.sendGetRequest(url, null, rawResponse);
if (error == null) {
try {
Gson gson = new Gson();
WikipediaResponse response = gson.fromJson(rawResponse.toString(), WikipediaResponse.class);
for (WikiImage img : getImageData(response)) {
images.add(new WikiImageCard(mapActivity, img));
}
return;
} catch (JsonSyntaxException e) {
error = e.getLocalizedMessage();
}
}
LOG.error(error);
} else {
LOG.error("Wrong WikiMedia ID");
}
}
private static List<WikiImage> getImageData(WikipediaResponse response) {
List<WikiImage> images = new ArrayList<>();
for (P18 p18 : response.claims.p18) {
String imageFileName = p18.mainsnak.datavalue.value;
if (imageFileName != null) {
try {
String imageName = URLDecoder.decode(imageFileName, "UTF-8");
imageFileName = imageName.replace(" ", "_");
imageName = imageName.substring(0, imageName.lastIndexOf("."));
String[] urlHashParts = getHash(imageFileName);
String imageHiResUrl = IMAGE_BASE_URL +
urlHashParts[0] + "/" + urlHashParts[1] + "/" +
imageFileName;
String imageStubUrl = IMAGE_BASE_URL + "thumb/" +
urlHashParts[0] + "/" + urlHashParts[1] + "/" +
imageFileName + "/" + THUMB_SIZE + "px-" +
imageFileName;
images.add(new WikiImage(imageName, imageStubUrl, imageHiResUrl));
} catch (UnsupportedEncodingException e) {
LOG.error(e.getLocalizedMessage());
}
}
}
return images;
}
@NonNull
private static String[] getHash(@NonNull String s) {
String md5 = new String(Hex.encodeHex(DigestUtils.md5(s)));
return new String[]{md5.substring(0, 1), md5.substring(0, 2)};
}
private static class Claims {
@SerializedName("P18")
@Expose
private List<P18> p18 = null;
}
private static class Datavalue {
@SerializedName("value")
@Expose
private String value;
@SerializedName("type")
@Expose
private String type;
}
private static class Mainsnak {
@SerializedName("datavalue")
@Expose
private Datavalue datavalue;
@SerializedName("datatype")
@Expose
private String datatype;
}
private static class P18 {
@SerializedName("mainsnak")
@Expose
private Mainsnak mainsnak;
}
private static class WikipediaResponse {
@SerializedName("claims")
@Expose
private Claims claims;
}
}