From 2a50f7d1d82937c377086bd9585b954410a6433e Mon Sep 17 00:00:00 2001 From: MadWasp79 Date: Mon, 30 Nov 2020 19:41:20 +0200 Subject: [PATCH] Wikivoyage to OBF migration: Show detailed articles. --- .../main/java/net/osmand/data/Amenity.java | 1 + .../plus/wikivoyage/data/TravelObfHelper.java | 107 ++++++++++++++++-- 2 files changed, 97 insertions(+), 11 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java b/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java index f7be034cbe..1759e79034 100644 --- a/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java +++ b/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java @@ -43,6 +43,7 @@ public class Amenity extends MapObject { public static final String IS_PART = "is_part"; public static final String IS_AGGR_PART = "is_aggr_part"; public static final String CONTENT_JSON = "content_json"; + public static final String ROUTE_ID = "route_id"; private String subType; diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java index 3f6b27129e..298d0f3d9d 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java @@ -25,10 +25,13 @@ import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.util.ArrayList; +import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; +import gnu.trove.map.TLongObjectMap; +import gnu.trove.map.hash.TLongObjectHashMap; public class TravelObfHelper implements TravelHelper{ @@ -46,13 +49,14 @@ public class TravelObfHelper implements TravelHelper{ private List existingTravelBooks = new ArrayList<>(); private List popularArticles = new ArrayList(); - private BinaryMapIndexReader index = null; + private TLongObjectMap cachedArticles; public TravelObfHelper(OsmandApplication application) { this.application = application; collator = OsmAndCollator.primaryCollator(); localDataHelper = new TravelLocalDataHelper(application); + cachedArticles = new TLongObjectHashMap<>(); } public static boolean checkIfObfFileExists(OsmandApplication app) { @@ -194,7 +198,9 @@ public class TravelObfHelper implements TravelHelper{ while (it.hasNext()) { Amenity a = it.next(); if (!a.getName(language).equals("")) { - popularArticles.add(readArticle(a, language)); + TravelArticle article = readArticle(a, language); + popularArticles.add(article); + writeToCache(article); } } } @@ -205,7 +211,6 @@ public class TravelObfHelper implements TravelHelper{ return popularArticles; } - private TravelArticle readArticle(Amenity amenity, String lang) { TravelArticle res = new TravelArticle(); @@ -215,13 +220,22 @@ public class TravelObfHelper implements TravelHelper{ res.lat = amenity.getLocation().getLatitude(); res.lon = amenity.getLocation().getLongitude(); res.imageTitle = amenity.getTagContent(Amenity.IMAGE_TITLE, lang) == null ? "" : amenity.getTagContent(Amenity.IMAGE_TITLE, lang); - res.tripId = amenity.getId(); //? + long tripId = -1; + String val = amenity.getTagContent(Amenity.ROUTE_ID, null); + if (val != null && val.startsWith("Q")) { + try { + tripId = Long.parseLong(val.substring(1)); + } catch (NumberFormatException nfe) { + LOG.error(nfe.getMessage()); + } + } + res.tripId = tripId; res.originalId = 0; //? res.lang = lang; res.contentsJson = amenity.getTagContent(Amenity.CONTENT_JSON, lang) == null ? "" : amenity.getTagContent(Amenity.CONTENT_JSON, lang); res.aggregatedPartOf = amenity.getTagContent(Amenity.IS_AGGR_PART, lang) == null ? "" : amenity.getTagContent(Amenity.IS_AGGR_PART, lang); -// crash in some places, need to fix it +// occasional crashes // try { // String gpxContent = amenity.getAdditionalInfo("gpx_info"); // res.gpxFile = GPXUtilities.loadGPXFile(new ByteArrayInputStream(gpxContent.getBytes("UTF-8"))); @@ -254,14 +268,75 @@ public class TravelObfHelper implements TravelHelper{ return null; } - @Override - public TravelArticle getArticle(long cityId, String lang) { - return null; + private void writeToCache(TravelArticle article) { + cachedArticles.put(article.tripId, article); + } + + private TravelArticle getArticleFromCache(long tripId, String lang) { + + TravelArticle article = cachedArticles.get(tripId); +// if (aa != null) { +// article = readArticle(aa, lang); +// } + return article; } @Override - public TravelArticle getArticle(String title, String lang) { - return null; + public TravelArticle getArticle(long resId, String lang) { + TravelArticle article = getArticleFromCache(resId, lang); + if (article != null) { + return article; + } + String name = ""; //??? + return getArticle(name, lang); + } + + @Override + public TravelArticle getArticle(final String title, final String lang) { + TravelArticle res = null; + List amenities = Collections.emptyList(); + try { + BinaryMapIndexReader indexReader = getBookBinaryIndex(); + if (indexReader != null) { + int left = 0; + int top = 0; + int right = Integer.MAX_VALUE; + int bottom = Integer.MAX_VALUE; + final List results = new ArrayList<>(); + + LatLon ll = application.getMapViewTrackingUtilities().getMapLocation(); + + BinaryMapIndexReader.SearchRequest req = BinaryMapIndexReader.buildSearchPoiRequest( + MapUtils.get31TileNumberX(ll.getLongitude()), + MapUtils.get31TileNumberY(ll.getLatitude()), title, + left, top, right, bottom, + new ResultMatcher() { + @Override + public boolean publish(Amenity object) { + if (object.getName(lang).equals(title)) { + results.add(object); + return true; + } + return false; + } + + @Override + public boolean isCancelled() { + return false; + } + }); + + amenities = indexReader.searchPoiByName(req); + } + } catch (IOException e) { + //todo + } + if (!amenities.isEmpty()) { + for (Amenity a : amenities) { + LOG.debug("searched article: " + a); + } + } + return res; } @Override @@ -269,9 +344,19 @@ public class TravelObfHelper implements TravelHelper{ return 0; } + + //TODO finish stub @Override public ArrayList getArticleLangs(long cityId) { - return null; + ArrayList res = new ArrayList<>(); + res.add("en"); + + for (TravelArticle article : popularArticles) { + if (article.getTripId() == cityId) { + res.add(article.getLang()); + } + } + return res; } @Override