Merge pull request #5124 from osmandapp/wapoint_categories

Add wptCategoryNames to GPXTrackAnalysis
This commit is contained in:
Alexey 2018-03-14 15:23:31 +03:00 committed by GitHub
commit bbcf3869ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 73 additions and 20 deletions

View file

@ -2,7 +2,6 @@ package net.osmand.util;
import net.osmand.IProgress; import net.osmand.IProgress;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.binary.BinaryMapIndexReader;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -21,10 +20,12 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set;
/** /**
@ -177,6 +178,24 @@ public class Algorithms {
return ""; return "";
} }
public static Set<String> decodeStringSet(String s) {
if (isEmpty(s)) {
return Collections.emptySet();
}
return new HashSet<>(Arrays.asList(s.split(CHAR_TOSPLIT + "")));
}
public static String encodeStringSet(Set<String> set) {
if (set != null) {
StringBuilder sb = new StringBuilder();
for (String s : set) {
sb.append(s).append(CHAR_TOSPLIT);
}
return sb.toString();
}
return "";
}
public static int findFirstNumberEndIndex(String value) { public static int findFirstNumberEndIndex(String value) {
int i = 0; int i = 0;
boolean valid = false; boolean valid = false;

View file

@ -15,7 +15,7 @@ import java.util.List;
public class GPXDatabase { public class GPXDatabase {
private static final String DB_NAME = "gpx_database"; private static final String DB_NAME = "gpx_database";
private static final int DB_VERSION = 6; private static final int DB_VERSION = 7;
private static final String GPX_TABLE_NAME = "gpxTable"; private static final String GPX_TABLE_NAME = "gpxTable";
private static final String GPX_COL_NAME = "fileName"; private static final String GPX_COL_NAME = "fileName";
private static final String GPX_COL_DIR = "fileDir"; private static final String GPX_COL_DIR = "fileDir";
@ -47,6 +47,8 @@ public class GPXDatabase {
private static final String GPX_COL_API_IMPORTED = "apiImported"; private static final String GPX_COL_API_IMPORTED = "apiImported";
private static final String GPX_COL_WPT_CATEGORY_NAMES = "wptCategoryNames";
public static final int GPX_SPLIT_TYPE_NO_SPLIT = -1; public static final int GPX_SPLIT_TYPE_NO_SPLIT = -1;
public static final int GPX_SPLIT_TYPE_DISTANCE = 1; public static final int GPX_SPLIT_TYPE_DISTANCE = 1;
public static final int GPX_SPLIT_TYPE_TIME = 2; public static final int GPX_SPLIT_TYPE_TIME = 2;
@ -77,7 +79,8 @@ public class GPXDatabase {
GPX_COL_FILE_LAST_MODIFIED_TIME + " long, " + GPX_COL_FILE_LAST_MODIFIED_TIME + " long, " +
GPX_COL_SPLIT_TYPE + " int, " + GPX_COL_SPLIT_TYPE + " int, " +
GPX_COL_SPLIT_INTERVAL + " double, " + GPX_COL_SPLIT_INTERVAL + " double, " +
GPX_COL_API_IMPORTED + " int);"; // 1 = true, 0 = false GPX_COL_API_IMPORTED + " int, " + // 1 = true, 0 = false
GPX_COL_WPT_CATEGORY_NAMES + " TEXT);";
private static final String GPX_TABLE_SELECT = "SELECT " + private static final String GPX_TABLE_SELECT = "SELECT " +
GPX_COL_NAME + ", " + GPX_COL_NAME + ", " +
@ -102,7 +105,8 @@ public class GPXDatabase {
GPX_COL_FILE_LAST_MODIFIED_TIME + ", " + GPX_COL_FILE_LAST_MODIFIED_TIME + ", " +
GPX_COL_SPLIT_TYPE + ", " + GPX_COL_SPLIT_TYPE + ", " +
GPX_COL_SPLIT_INTERVAL + ", " + GPX_COL_SPLIT_INTERVAL + ", " +
GPX_COL_API_IMPORTED + GPX_COL_API_IMPORTED + ", " +
GPX_COL_WPT_CATEGORY_NAMES +
" FROM " + GPX_TABLE_NAME; " FROM " + GPX_TABLE_NAME;
private OsmandApplication context; private OsmandApplication context;
@ -244,6 +248,10 @@ public class GPXDatabase {
" SET " + GPX_COL_API_IMPORTED + " = ? " + " SET " + GPX_COL_API_IMPORTED + " = ? " +
"WHERE " + GPX_COL_API_IMPORTED + " IS NULL", new Object[]{0}); "WHERE " + GPX_COL_API_IMPORTED + " IS NULL", new Object[]{0});
} }
if (oldVersion < 7) {
db.execSQL("ALTER TABLE " + GPX_TABLE_NAME + " ADD " + GPX_COL_WPT_CATEGORY_NAMES + " TEXT");
}
} }
private boolean updateLastModifiedTime(GpxDataItem item) { private boolean updateLastModifiedTime(GpxDataItem item) {
@ -382,11 +390,12 @@ public class GPXDatabase {
} }
if (a != null) { if (a != null) {
db.execSQL( db.execSQL(
"INSERT INTO " + GPX_TABLE_NAME + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", "INSERT INTO " + GPX_TABLE_NAME + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
new Object[]{ fileName, fileDir, a.totalDistance, a.totalTracks, a.startTime, a.endTime, new Object[]{ fileName, fileDir, a.totalDistance, a.totalTracks, a.startTime, a.endTime,
a.timeSpan, a.timeMoving, a.totalDistanceMoving, a.diffElevationUp, a.diffElevationDown, a.timeSpan, a.timeMoving, a.totalDistanceMoving, a.diffElevationUp, a.diffElevationDown,
a.avgElevation, a.minElevation, a.maxElevation, a.maxSpeed, a.avgSpeed, a.points, a.wptPoints, a.avgElevation, a.minElevation, a.maxElevation, a.maxSpeed, a.avgSpeed, a.points, a.wptPoints,
color, item.file.lastModified(), item.splitType, item.splitInterval, item.apiImported ? 1 : 0}); color, item.file.lastModified(), item.splitType, item.splitInterval, item.apiImported ? 1 : 0,
Algorithms.encodeStringSet(item.analysis.wptCategoryNames)});
} else { } else {
db.execSQL("INSERT INTO " + GPX_TABLE_NAME + "(" + db.execSQL("INSERT INTO " + GPX_TABLE_NAME + "(" +
GPX_COL_NAME + ", " + GPX_COL_NAME + ", " +
@ -424,13 +433,14 @@ public class GPXDatabase {
GPX_COL_AVG_SPEED + " = ?, " + GPX_COL_AVG_SPEED + " = ?, " +
GPX_COL_POINTS + " = ?, " + GPX_COL_POINTS + " = ?, " +
GPX_COL_WPT_POINTS + " = ?, " + GPX_COL_WPT_POINTS + " = ?, " +
GPX_COL_FILE_LAST_MODIFIED_TIME + " = ? " + GPX_COL_FILE_LAST_MODIFIED_TIME + " = ?, " +
GPX_COL_WPT_CATEGORY_NAMES + " = ? " +
" WHERE " + GPX_COL_NAME + " = ? AND " + GPX_COL_DIR + " = ?", " WHERE " + GPX_COL_NAME + " = ? AND " + GPX_COL_DIR + " = ?",
new Object[]{ a.totalDistance, a.totalTracks, a.startTime, a.endTime, new Object[]{ a.totalDistance, a.totalTracks, a.startTime, a.endTime,
a.timeSpan, a.timeMoving, a.totalDistanceMoving, a.diffElevationUp, a.timeSpan, a.timeMoving, a.totalDistanceMoving, a.diffElevationUp,
a.diffElevationDown, a.avgElevation, a.minElevation, a.maxElevation, a.diffElevationDown, a.avgElevation, a.minElevation, a.maxElevation,
a.maxSpeed, a.avgSpeed, a.points, a.wptPoints, item.file.lastModified(), a.maxSpeed, a.avgSpeed, a.points, a.wptPoints, item.file.lastModified(),
fileName, fileDir }); Algorithms.encodeStringSet(a.wptCategoryNames), fileName, fileDir });
} finally { } finally {
db.close(); db.close();
} }
@ -463,6 +473,7 @@ public class GPXDatabase {
int splitType = (int)query.getInt(20); int splitType = (int)query.getInt(20);
double splitInterval = query.getDouble(21); double splitInterval = query.getDouble(21);
boolean apiImported = query.getInt(22) == 1; boolean apiImported = query.getInt(22) == 1;
String wptCategoryNames = query.getString(23);
GPXTrackAnalysis a = new GPXTrackAnalysis(); GPXTrackAnalysis a = new GPXTrackAnalysis();
a.totalDistance = totalDistance; a.totalDistance = totalDistance;
@ -482,6 +493,9 @@ public class GPXDatabase {
a.avgSpeed = avgSpeed; a.avgSpeed = avgSpeed;
a.points = points; a.points = points;
a.wptPoints = wptPoints; a.wptPoints = wptPoints;
if (wptCategoryNames != null) {
a.wptCategoryNames = Algorithms.decodeStringSet(wptCategoryNames);
}
File dir; File dir;
if (!Algorithms.isEmpty(fileDir)) { if (!Algorithms.isEmpty(fileDir)) {

View file

@ -42,10 +42,13 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.Stack; import java.util.Stack;
import java.util.TimeZone; import java.util.TimeZone;
@ -287,6 +290,8 @@ public class GPXUtilities {
public int points; public int points;
public int wptPoints = 0; public int wptPoints = 0;
public Set<String> wptCategoryNames;
public double metricEnd; public double metricEnd;
public double secondaryMetricEnd; public double secondaryMetricEnd;
public WptPt locationStart; public WptPt locationStart;
@ -787,6 +792,22 @@ public class GPXUtilities {
return Collections.unmodifiableList(points); return Collections.unmodifiableList(points);
} }
public Map<String, List<WptPt>> getPointsByCategories() {
Map<String, List<WptPt>> res = new HashMap<>();
for (WptPt pt : points) {
String category = pt.category == null ? "" : pt.category;
List<WptPt> list = res.get(category);
if (list != null) {
list.add(pt);
} else {
list = new ArrayList<>();
list.add(pt);
res.put(category, list);
}
}
return res;
}
public boolean isPointsEmpty() { public boolean isPointsEmpty() {
return points.isEmpty(); return points.isEmpty();
} }
@ -868,6 +889,7 @@ public class GPXUtilities {
public GPXTrackAnalysis getAnalysis(long fileTimestamp) { public GPXTrackAnalysis getAnalysis(long fileTimestamp) {
GPXTrackAnalysis g = new GPXTrackAnalysis(); GPXTrackAnalysis g = new GPXTrackAnalysis();
g.wptPoints = points.size(); g.wptPoints = points.size();
g.wptCategoryNames = getWaypointCategories(true);
List<SplitSegment> splitSegments = new ArrayList<GPXUtilities.SplitSegment>(); List<SplitSegment> splitSegments = new ArrayList<GPXUtilities.SplitSegment>();
for (int i = 0; i < tracks.size(); i++) { for (int i = 0; i < tracks.size(); i++) {
Track subtrack = tracks.get(i); Track subtrack = tracks.get(i);
@ -1167,14 +1189,12 @@ public class GPXUtilities {
return count; return count;
} }
public List<String> getWaypointCategories() { public Set<String> getWaypointCategories(boolean withDefaultCategory) {
List<String> categories = new ArrayList<>(); Set<String> categories = new HashSet<>();
for (WptPt pt : points) { for (WptPt pt : points) {
String category = pt.category; String category = pt.category == null ? "" : pt.category;
if (!TextUtils.isEmpty(category)) { if (withDefaultCategory || !TextUtils.isEmpty(category)) {
if (!categories.contains(category)) { categories.add(category);
categories.add(category);
}
} }
} }
return categories; return categories;

View file

@ -20,11 +20,9 @@ import net.osmand.plus.IconsCache;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.quickaction.QuickAction;
import net.osmand.plus.quickaction.QuickActionFactory;
import net.osmand.plus.quickaction.QuickActionRegistry;
import java.util.List; import java.util.List;
import java.util.Set;
import static android.util.TypedValue.COMPLEX_UNIT_DIP; import static android.util.TypedValue.COMPLEX_UNIT_DIP;
@ -69,7 +67,7 @@ public class SelectCategoryDialogFragment extends DialogFragment {
final FavouritesDbHelper helper = ((OsmandApplication) getActivity().getApplication()).getFavorites(); final FavouritesDbHelper helper = ((OsmandApplication) getActivity().getApplication()).getFavorites();
if (gpxFile != null) { if (gpxFile != null) {
List<String> categories = gpxFile.getWaypointCategories(); Set<String> categories = gpxFile.getWaypointCategories(false);
for (final String category : categories) { for (final String category : categories) {
addCategory(ll, category, 0); addCategory(ll, category, 0);
} }

View file

@ -124,7 +124,9 @@ public class AddTracksGroupBottomSheetDialogFragment extends AddGroupBottomSheet
processGPXFolder(gpxFile, sub); processGPXFolder(gpxFile, sub);
} else if (gpxFile.isFile() && gpxFile.getName().toLowerCase().endsWith(".gpx")) { } else if (gpxFile.isFile() && gpxFile.getName().toLowerCase().endsWith(".gpx")) {
GpxDataItem item = processedDataFiles.get(gpxFile); GpxDataItem item = processedDataFiles.get(gpxFile);
if (item == null || item.getFileLastModifiedTime() != gpxFile.lastModified()) { if (item == null
|| item.getFileLastModifiedTime() != gpxFile.lastModified()
|| item.getAnalysis().wptCategoryNames == null) {
GPXFile f = GPXUtilities.loadGPXFile(app, gpxFile); GPXFile f = GPXUtilities.loadGPXFile(app, gpxFile);
GPXTrackAnalysis analysis = f.getAnalysis(gpxFile.lastModified()); GPXTrackAnalysis analysis = f.getAnalysis(gpxFile.lastModified());
if (item == null) { if (item == null) {