Fix obf helper

This commit is contained in:
Dima-1 2020-12-15 22:53:27 +02:00
parent 275ef9f622
commit 6cfc51fedf
5 changed files with 98 additions and 145 deletions

View file

@ -1539,6 +1539,17 @@ public class BinaryMapIndexReader {
return request;
}
public static SearchRequest<Amenity> buildSearchPoiRequest(LatLon latLon, int radius, int zoom,
SearchPoiTypeFilter poiTypeFilter,
ResultMatcher<Amenity> matcher) {
SearchRequest<Amenity> request = new SearchRequest<>();
request.setBBoxRadius(latLon.getLatitude(), latLon.getLongitude(), radius);
request.zoom = zoom;
request.poiTypeFilter = poiTypeFilter;
request.resultMatcher = matcher;
return request;
}
public static SearchRequest<RouteDataObject> buildSearchRouteRequest(int sleft, int sright, int stop, int sbottom,
ResultMatcher<RouteDataObject> matcher) {
SearchRequest<RouteDataObject> request = new SearchRequest<RouteDataObject>();

View file

@ -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);

View file

@ -1161,7 +1161,7 @@ public class ResourceManager {
return maps != null && maps.length > 0;
}
public BinaryMapIndexReader[] getTravelFiles() {
public List<BinaryMapIndexReader> getTravelRepositories() {
Collection<BinaryMapReaderResource> fileReaders = getFileReaders();
List<BinaryMapIndexReader> 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<String, String> getBackupIndexes(Map<String, String> map) {

View file

@ -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<WikivoyageSearchResult, List<WikivoyageSearchResult>> getNavigationMap(
final TravelArticle article);
@Nullable
TravelArticle getArticleById(String routeId, String lang);
TravelArticle getArticleByTitle(String title, String lang);

View file

@ -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<File> existingTravelBooks = new ArrayList<>();
private List<TravelArticle> popularArticles = new ArrayList<>();
private final Map<String, TravelArticle> cachedArticles;
private final TravelLocalDataHelper localDataHelper;
private List<BinaryMapIndexReader> 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,21 +65,21 @@ public class TravelObfHelper implements TravelHelper {
public List<TravelArticle> loadPopularArticles() {
String language = app.getLanguage();
final List<Amenity> articles = new ArrayList<>();
for (BinaryMapIndexReader travelBookReader : travelBookReaders) {
try {
BinaryMapIndexReader bookIndexReader = getBookBinaryIndex();
if (bookIndexReader == null) {
if (travelBookReader == null) {
popularArticles = new ArrayList<>();
return popularArticles;
}
LatLon ll = app.getMapViewTrackingUtilities().getMapLocation();
float coeff = 2;
Location myLocation = app.getLocationProvider().getLastKnownLocation();
LatLon ll;
if (myLocation != null) {
ll = new LatLon(myLocation.getLatitude(), myLocation.getLongitude());
} else {
ll = app.getMapViewTrackingUtilities().getMapLocation();
}
BinaryMapIndexReader.SearchRequest<Amenity> 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.buildSearchPoiRequest(ll, SEARCH_RADIUS,-1,
BinaryMapIndexReader.ACCEPT_ALL_POI_TYPE_FILTER,
new ResultMatcher<Amenity>() {
int count = 0;
@ -116,14 +98,12 @@ public class TravelObfHelper implements TravelHelper {
return false;
}
});
bookIndexReader.searchPoi(req);
bookIndexReader.close();
req.setBBoxRadius(ll.getLatitude(),ll.getLongitude(),100000);
travelBookReader.searchPoi(req);
travelBookReader.close();
if (articles.size() > 0) {
Iterator<Amenity> it = articles.iterator();
while (it.hasNext()) {
Amenity a = it.next();
for (Amenity a : articles) {
if (!a.getName(language).equals("")) {
TravelArticle article = readArticle(a, language);
popularArticles.add(article);
@ -134,13 +114,12 @@ public class TravelObfHelper implements TravelHelper {
} 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,21 +162,16 @@ 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<Amenity> amenities = Collections.emptyList();
for (BinaryMapIndexReader travelBookReader : travelBookReaders) {
try {
BinaryMapIndexReader indexReader = getBookBinaryIndex();
if (indexReader != null) {
if (travelBookReader != null) {
int left = 0;
int top = 0;
int right = Integer.MAX_VALUE;
@ -249,7 +196,7 @@ public class TravelObfHelper implements TravelHelper {
}
});
amenities = indexReader.searchPoiByName(req);
amenities = travelBookReader.searchPoiByName(req);
}
} catch (IOException e) {
//todo
@ -259,6 +206,7 @@ public class TravelObfHelper implements TravelHelper {
LOG.debug("searched article: " + a);
}
}
}
return res;
}