Merge pull request #10302 from osmandapp/wikivoyage_obf
Wikivoyage to OBF migration: Show detailed articles.
This commit is contained in:
commit
6c71f50e2d
2 changed files with 98 additions and 13 deletions
|
@ -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;
|
||||
|
|
|
@ -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<File> existingTravelBooks = new ArrayList<>();
|
||||
private List<TravelArticle> popularArticles = new ArrayList<TravelArticle>();
|
||||
|
||||
private BinaryMapIndexReader index = null;
|
||||
private TLongObjectMap<TravelArticle> 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,18 +198,18 @@ 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
LOG.error(e.getMessage());
|
||||
}
|
||||
|
||||
return popularArticles;
|
||||
}
|
||||
|
||||
|
||||
private TravelArticle readArticle(Amenity amenity, String lang) {
|
||||
TravelArticle res = new TravelArticle();
|
||||
|
||||
|
@ -215,13 +219,13 @@ 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(); //?
|
||||
res.tripId = getTripId(amenity);
|
||||
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")));
|
||||
|
@ -232,6 +236,19 @@ public class TravelObfHelper implements TravelHelper{
|
|||
return res;
|
||||
}
|
||||
|
||||
private long getTripId(Amenity amenity) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
return tripId;
|
||||
}
|
||||
|
||||
private BinaryMapIndexReader getBookBinaryIndex() throws IOException {
|
||||
application.getSettings().SELECTED_TRAVEL_BOOK.set(selectedTravelBook.getName());
|
||||
try {
|
||||
|
@ -254,14 +271,71 @@ 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<Amenity> 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 = application.getMapViewTrackingUtilities().getMapLocation();
|
||||
BinaryMapIndexReader.SearchRequest<Amenity> req = BinaryMapIndexReader.buildSearchPoiRequest(
|
||||
MapUtils.get31TileNumberX(ll.getLongitude()),
|
||||
MapUtils.get31TileNumberY(ll.getLatitude()), title,
|
||||
left, top, right, bottom,
|
||||
new ResultMatcher<Amenity>() {
|
||||
@Override
|
||||
public boolean publish(Amenity object) {
|
||||
if (object.getName(lang).equals(title)) {
|
||||
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 +343,19 @@ public class TravelObfHelper implements TravelHelper{
|
|||
return 0;
|
||||
}
|
||||
|
||||
|
||||
//TODO finish stub
|
||||
@Override
|
||||
public ArrayList<String> getArticleLangs(long cityId) {
|
||||
return null;
|
||||
ArrayList<String> res = new ArrayList<>();
|
||||
res.add("en");
|
||||
|
||||
for (TravelArticle article : popularArticles) {
|
||||
if (article.getTripId() == cityId) {
|
||||
res.add(article.getLang());
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in a new issue