Wikivoyage to OBF migration: Work in progress 3. Add missing fields.

This commit is contained in:
MadWasp79 2020-11-27 10:15:03 +02:00
parent df3c7f5ab5
commit 1f2b383dad
2 changed files with 45 additions and 33 deletions

View file

@ -44,6 +44,11 @@ public class Amenity extends MapObject {
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";
public static final String OSM_DELETE_TAG = "osmand_change"; public static final String OSM_DELETE_TAG = "osmand_change";
public static final String IMAGE_TITLE = "image_title";
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";
private String subType; private String subType;
private PoiCategory type; private PoiCategory type;

View file

@ -6,7 +6,6 @@ import androidx.annotation.Nullable;
import net.osmand.Collator; import net.osmand.Collator;
import net.osmand.GPXUtilities; import net.osmand.GPXUtilities;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.Location;
import net.osmand.OsmAndCollator; import net.osmand.OsmAndCollator;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.ResultMatcher; import net.osmand.ResultMatcher;
@ -15,35 +14,30 @@ import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.binary.BinaryMapPoiReaderAdapter; import net.osmand.binary.BinaryMapPoiReaderAdapter;
import net.osmand.data.Amenity; import net.osmand.data.Amenity;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.osm.MapPoiTypes;
import net.osmand.osm.PoiCategory;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.activities.actions.OsmAndAction; import net.osmand.plus.R;
import net.osmand.plus.api.SQLiteAPI;
import net.osmand.plus.resources.AmenityIndexRepository;
import net.osmand.search.core.SearchResult;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils; import net.osmand.util.MapUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class TravelObfHelper implements TravelHelper{ public class TravelObfHelper implements TravelHelper{
private static final Log LOG = PlatformUtil.getLog(TravelObfHelper.class); private static final Log LOG = PlatformUtil.getLog(TravelObfHelper.class);
private static final String WIKIVOYAGE_OBF = "Wikivoyage.obf";
public static final String ROUTE_ARTICLE = "route_article";
private final OsmandApplication application; private final OsmandApplication application;
private Collator collator; private Collator collator;
private TravelLocalDataHelper localDataHelper; private TravelLocalDataHelper localDataHelper;
@ -58,14 +52,14 @@ public class TravelObfHelper implements TravelHelper{
public TravelObfHelper(OsmandApplication application) { public TravelObfHelper(OsmandApplication application) {
this.application = application; this.application = application;
collator = OsmAndCollator.primaryCollator(); collator = OsmAndCollator.primaryCollator();
localDataHelper = new TravelLocalDataHelper(application); //will I need it? localDataHelper = new TravelLocalDataHelper(application);
} }
public static boolean checkIfObfFileExists(OsmandApplication app) { public static boolean checkIfObfFileExists(OsmandApplication app) {
File[] files = app.getAppPath(IndexConstants.WIKIVOYAGE_INDEX_DIR).listFiles(); File[] files = app.getAppPath(IndexConstants.WIKIVOYAGE_INDEX_DIR).listFiles();
if (files != null) { if (files != null) {
for (File f : files) { for (File f : files) {
if (f.getName().equals("Wikivoyage.obf")) { if (f.getName().equals(WIKIVOYAGE_OBF)) {
return true; return true;
} }
} }
@ -79,7 +73,7 @@ public class TravelObfHelper implements TravelHelper{
} }
/** TODO /** TODO
* 1.implement regional travelbooks * 1. implement regional travelbooks
* 2. check settings for default? * 2. check settings for default?
*/ */
public void initTravelBooks() { public void initTravelBooks() {
@ -89,11 +83,11 @@ public class TravelObfHelper implements TravelHelper{
if (files != null && !files.isEmpty()) { if (files != null && !files.isEmpty()) {
for (File f : files) { for (File f : files) {
existingTravelBooks.add(f); existingTravelBooks.add(f);
// if (selectedTravelBook == null) { if (selectedTravelBook == null) {
// selectedTravelBook = f; selectedTravelBook = f;
// } else if (Algorithms.objectEquals(travelBook, f.getName())) { } else if (Algorithms.objectEquals(travelBook, f.getName())) {
// selectedTravelBook = f; selectedTravelBook = f;
// } }
} }
selectedTravelBook = files.get(0); selectedTravelBook = files.get(0);
} else { } else {
@ -134,12 +128,12 @@ public class TravelObfHelper implements TravelHelper{
@Override @Override
public List<File> getExistingTravelBooks() { public List<File> getExistingTravelBooks() {
return null; return existingTravelBooks;
} }
@Override @Override
public void selectTravelBook(File f) { public void selectTravelBook(File f) {
//todo
} }
@NonNull @NonNull
@ -179,7 +173,8 @@ public class TravelObfHelper implements TravelHelper{
@Override @Override
public boolean publish(Amenity object) { public boolean publish(Amenity object) {
if (object.getSubType().equals("route_article")) { //TODO need more logical way to filter results
if (object.getSubType().equals(ROUTE_ARTICLE)) {
articles.add(object); articles.add(object);
} }
return false; return false;
@ -216,18 +211,20 @@ public class TravelObfHelper implements TravelHelper{
res.title = amenity.getName(lang).equals("") ? amenity.getName() : amenity.getName(lang); res.title = amenity.getName(lang).equals("") ? amenity.getName() : amenity.getName(lang);
res.content = amenity.getDescription(lang); res.content = amenity.getDescription(lang);
res.isPartOf = ""; //add to file res.isPartOf = amenity.getTagContent(Amenity.IS_PART, lang) == null ? "" : amenity.getTagContent(Amenity.IS_PART, lang);
res.lat = amenity.getLocation().getLatitude(); res.lat = amenity.getLocation().getLatitude();
res.lon = amenity.getLocation().getLongitude(); res.lon = amenity.getLocation().getLongitude();
res.imageTitle = "";//add to file res.imageTitle = amenity.getTagContent(Amenity.IMAGE_TITLE, lang) == null ? "" : amenity.getTagContent(Amenity.IMAGE_TITLE, lang);
res.tripId = amenity.getId(); res.tripId = amenity.getId(); //?
res.originalId = amenity.getId()>>6; res.originalId = 0; //?
res.lang = lang; res.lang = lang;
res.contentsJson = ""; //add to file res.contentsJson = amenity.getTagContent(Amenity.CONTENT_JSON, lang) == null ? "" : amenity.getTagContent(Amenity.CONTENT_JSON, lang);
res.aggregatedPartOf = ""; //add to file 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
// try { // try {
// String gpxContent = Algorithms.gzipToString(); // String gpxContent = amenity.getAdditionalInfo("gpx_info");
res.gpxFile = null; //GPXUtilities.loadGPXFile(new ByteArrayInputStream(gpxContent.getBytes("UTF-8"))); // res.gpxFile = GPXUtilities.loadGPXFile(new ByteArrayInputStream(gpxContent.getBytes("UTF-8")));
// } catch (IOException e) { // } catch (IOException e) {
// LOG.error(e.getMessage(), e); // LOG.error(e.getMessage(), e);
// } // }
@ -279,16 +276,26 @@ public class TravelObfHelper implements TravelHelper{
@Override @Override
public String formatTravelBookName(File tb) { public String formatTravelBookName(File tb) {
return null; if (tb == null) {
return application.getString(R.string.shared_string_none);
}
String nm = tb.getName();
return nm.substring(0, nm.indexOf('.')).replace('_', ' ');
} }
@Override @Override
public String getGPXName(TravelArticle article) { public String getGPXName(TravelArticle article) {
return null; return article.getTitle().replace('/', '_').replace('\'', '_')
.replace('\"', '_') + IndexConstants.GPX_FILE_EXT;
} }
@Override @Override
public File createGpxFile(TravelArticle article) { public File createGpxFile(TravelArticle article) {
return null; final GPXUtilities.GPXFile gpx = article.getGpxFile();
File file = application.getAppPath(IndexConstants.GPX_TRAVEL_DIR + getGPXName(article));
if (!file.exists()) {
GPXUtilities.writeGpxFile(file, gpx);
}
return file;
} }
} }