diff --git a/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapIndexReader.java b/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapIndexReader.java index 5e45a893ba..035b33d470 100644 --- a/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapIndexReader.java +++ b/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapIndexReader.java @@ -1538,9 +1538,20 @@ public class BinaryMapIndexReader { return request; } - - public static SearchRequest buildSearchRouteRequest(int sleft, int sright, int stop, int sbottom, - ResultMatcher matcher){ + + public static SearchRequest buildSearchPoiRequest(LatLon latLon, int radius, int zoom, + SearchPoiTypeFilter poiTypeFilter, + ResultMatcher matcher) { + SearchRequest request = new SearchRequest<>(); + request.setBBoxRadius(latLon.getLatitude(), latLon.getLongitude(), radius); + request.zoom = zoom; + request.poiTypeFilter = poiTypeFilter; + request.resultMatcher = matcher; + return request; + } + + public static SearchRequest buildSearchRouteRequest(int sleft, int sright, int stop, int sbottom, + ResultMatcher matcher) { SearchRequest request = new SearchRequest(); request.left = sleft; request.right = sright; diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index f8a5446efd..0a1f1d5c8e 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -459,7 +459,7 @@ public class AppInitializer implements IProgress { app.searchUICore = startupInit(new QuickSearchHelper(app), QuickSearchHelper.class); app.mapViewTrackingUtilities = startupInit(new MapViewTrackingUtilities(app), MapViewTrackingUtilities.class); - app.travelHelper = app.getResourceManager().hasTravelObfFile() + app.travelHelper = true ? new TravelObfHelper(app) : new TravelDbHelper(app); app.travelHelper = startupInit(app.travelHelper, TravelHelper.class); diff --git a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java index 609ea7910d..424186421b 100644 --- a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java +++ b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java @@ -1161,7 +1161,7 @@ public class ResourceManager { return maps != null && maps.length > 0; } - public BinaryMapIndexReader[] getTravelFiles() { + public List getTravelRepositories() { Collection fileReaders = getFileReaders(); List readers = new ArrayList<>(fileReaders.size()); for (BinaryMapReaderResource res : fileReaders) { @@ -1175,11 +1175,7 @@ public class ResourceManager { } } } - return readers.toArray(new BinaryMapIndexReader[0]); - } - - public boolean hasTravelObfFile() { - return isMapsPresentInDirectory(IndexConstants.WIKIVOYAGE_INDEX_DIR); + return readers; } public Map getBackupIndexes(Map map) { diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelHelper.java index 12976ed666..6ff6cf914b 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelHelper.java @@ -3,11 +3,8 @@ package net.osmand.plus.wikivoyage.data; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import net.osmand.plus.OsmandApplication; - import java.io.File; import java.util.ArrayList; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -30,6 +27,7 @@ public interface TravelHelper { Map> getNavigationMap( final TravelArticle article); + @Nullable TravelArticle getArticleById(String routeId, String lang); TravelArticle getArticleByTitle(String title, String lang); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java index 65fe1e88e6..2621315eaf 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java @@ -4,11 +4,10 @@ import androidx.annotation.NonNull; import net.osmand.GPXUtilities; import net.osmand.IndexConstants; +import net.osmand.Location; import net.osmand.PlatformUtil; import net.osmand.ResultMatcher; -import net.osmand.binary.BinaryIndexPart; import net.osmand.binary.BinaryMapIndexReader; -import net.osmand.binary.BinaryMapPoiReaderAdapter; import net.osmand.data.Amenity; import net.osmand.data.LatLon; import net.osmand.plus.OsmandApplication; @@ -19,11 +18,9 @@ import org.apache.commons.logging.Log; import java.io.File; import java.io.IOException; -import java.io.RandomAccessFile; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; @@ -31,14 +28,14 @@ public class TravelObfHelper implements TravelHelper { private static final Log LOG = PlatformUtil.getLog(TravelObfHelper.class); public static final String ROUTE_ARTICLE = "route_article"; + public static final int SEARCH_RADIUS = 100000; private final OsmandApplication app; - private File selectedTravelBook = null; - private final List existingTravelBooks = new ArrayList<>(); private List popularArticles = new ArrayList<>(); private final Map cachedArticles; private final TravelLocalDataHelper localDataHelper; + private List travelBookReaders; public TravelObfHelper(OsmandApplication app) { this.app = app; @@ -54,22 +51,7 @@ public class TravelObfHelper implements TravelHelper { @Override public void initializeDataOnAppStartup() { - - BinaryMapIndexReader[] readers = app.getResourceManager().getTravelFiles(); - String travelBook = app.getSettings().SELECTED_TRAVEL_BOOK.get(); - existingTravelBooks.clear(); - if (readers != null) { - for (BinaryMapIndexReader reader : readers) { - File f = reader.getFile(); - existingTravelBooks.add(f); - if (selectedTravelBook == null) { - selectedTravelBook = f; - } else if (Algorithms.objectEquals(travelBook, f.getName())) { - selectedTravelBook = f; - } - selectedTravelBook = reader.getFile(); - } - } + travelBookReaders = app.getResourceManager().getTravelRepositories(); } @Override @@ -83,64 +65,61 @@ public class TravelObfHelper implements TravelHelper { public List loadPopularArticles() { String language = app.getLanguage(); final List articles = new ArrayList<>(); - try { - BinaryMapIndexReader bookIndexReader = getBookBinaryIndex(); - if (bookIndexReader == null) { - popularArticles = new ArrayList<>(); - return popularArticles; - } - LatLon ll = app.getMapViewTrackingUtilities().getMapLocation(); - float coeff = 2; - BinaryMapIndexReader.SearchRequest req = - BinaryMapIndexReader.buildSearchPoiRequest( - MapUtils.get31TileNumberX(ll.getLongitude() - coeff), - MapUtils.get31TileNumberX(ll.getLongitude() + coeff), - MapUtils.get31TileNumberY(ll.getLatitude() + coeff), - MapUtils.get31TileNumberY(ll.getLatitude() - coeff), - -1, - BinaryMapIndexReader.ACCEPT_ALL_POI_TYPE_FILTER, - new ResultMatcher() { - int count = 0; + for (BinaryMapIndexReader travelBookReader : travelBookReaders) { + try { + if (travelBookReader == null) { + popularArticles = new ArrayList<>(); + return popularArticles; + } + Location myLocation = app.getLocationProvider().getLastKnownLocation(); + LatLon ll; + if (myLocation != null) { + ll = new LatLon(myLocation.getLatitude(), myLocation.getLongitude()); + } else { + ll = app.getMapViewTrackingUtilities().getMapLocation(); + } + BinaryMapIndexReader.SearchRequest req = + BinaryMapIndexReader.buildSearchPoiRequest(ll, SEARCH_RADIUS,-1, + BinaryMapIndexReader.ACCEPT_ALL_POI_TYPE_FILTER, + new ResultMatcher() { + int count = 0; - @Override - public boolean publish(Amenity object) { - //TODO need more logical way to filter results - if (object.getSubType().equals(ROUTE_ARTICLE)) { - articles.add(object); + @Override + public boolean publish(Amenity object) { + //TODO need more logical way to filter results + if (object.getSubType().equals(ROUTE_ARTICLE)) { + articles.add(object); + } + return false; } - return false; - } - @Override - public boolean isCancelled() { - return false; - } - }); + @Override + public boolean isCancelled() { + return false; + } + }); + req.setBBoxRadius(ll.getLatitude(),ll.getLongitude(),100000); + travelBookReader.searchPoi(req); + travelBookReader.close(); - bookIndexReader.searchPoi(req); - bookIndexReader.close(); - - if (articles.size() > 0) { - Iterator it = articles.iterator(); - while (it.hasNext()) { - Amenity a = it.next(); - if (!a.getName(language).equals("")) { - TravelArticle article = readArticle(a, language); - popularArticles.add(article); - cachedArticles.put(article.routeId, article); + if (articles.size() > 0) { + for (Amenity a : articles) { + if (!a.getName(language).equals("")) { + TravelArticle article = readArticle(a, language); + popularArticles.add(article); + cachedArticles.put(article.routeId, article); + } } } + } catch (Exception e) { + LOG.error(e.getMessage()); } - } catch (Exception e) { - LOG.error(e.getMessage()); } return popularArticles; } - private TravelArticle readArticle(Amenity amenity, String lang) { TravelArticle res = new TravelArticle(); - res.title = amenity.getName(lang).equals("") ? amenity.getName() : amenity.getName(lang); res.content = amenity.getDescription(lang); res.isPartOf = amenity.getTagContent(Amenity.IS_PART, lang) == null ? "" : amenity.getTagContent(Amenity.IS_PART, lang); @@ -152,15 +131,6 @@ public class TravelObfHelper implements TravelHelper { 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); - -// occasional crashes -// try { -// String gpxContent = amenity.getAdditionalInfo("gpx_info"); -// res.gpxFile = GPXUtilities.loadGPXFile(new ByteArrayInputStream(gpxContent.getBytes("UTF-8"))); -// } catch (IOException e) { -// LOG.error(e.getMessage(), e); -// } - return res; } @@ -168,27 +138,9 @@ public class TravelObfHelper implements TravelHelper { return amenity.getTagContent(Amenity.ROUTE_ID, null); } - - private BinaryMapIndexReader getBookBinaryIndex() throws IOException { - app.getSettings().SELECTED_TRAVEL_BOOK.set(selectedTravelBook.getName()); - try { - RandomAccessFile r = new RandomAccessFile(selectedTravelBook.getAbsolutePath(), "r"); - BinaryMapIndexReader index = new BinaryMapIndexReader(r, selectedTravelBook); - for (BinaryIndexPart p : index.getIndexes()) { - if (p instanceof BinaryMapPoiReaderAdapter.PoiRegion) { - return index; - } - } - } catch (IOException e) { - System.err.println("File doesn't have valid structure : " + selectedTravelBook.getName() + " " + e.getMessage()); - throw e; - } - return null; - } - @Override public boolean isAnyTravelBookPresent() { - return selectedTravelBook != null; + return !Algorithms.isEmpty(travelBookReaders); } @NonNull @@ -210,53 +162,49 @@ public class TravelObfHelper implements TravelHelper { @Override public TravelArticle getArticleById(String routeId, String lang) { - TravelArticle article = cachedArticles.get(routeId); - if (article != null) { - return article; - } - String name = ""; //??? - return getArticleByTitle(name, lang); + return cachedArticles.get(routeId); } @Override public TravelArticle getArticleByTitle(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; - LatLon ll = app.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)) { - return true; + for (BinaryMapIndexReader travelBookReader : travelBookReaders) { + try { + if (travelBookReader != null) { + int left = 0; + int top = 0; + int right = Integer.MAX_VALUE; + int bottom = Integer.MAX_VALUE; + LatLon ll = app.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)) { + return true; + } + return false; } - return false; - } - @Override - public boolean isCancelled() { - return false; - } - }); + @Override + public boolean isCancelled() { + return false; + } + }); - amenities = indexReader.searchPoiByName(req); + amenities = travelBookReader.searchPoiByName(req); + } + } catch (IOException e) { + //todo } - } catch (IOException e) { - //todo - } - if (!amenities.isEmpty()) { - for (Amenity a : amenities) { - LOG.debug("searched article: " + a); + if (!amenities.isEmpty()) { + for (Amenity a : amenities) { + LOG.debug("searched article: " + a); + } } } return res;