aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/Waypoint.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/Waypoint.java')
-rw-r--r--main/src/cgeo/geocaching/Waypoint.java62
1 files changed, 62 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/Waypoint.java b/main/src/cgeo/geocaching/Waypoint.java
index 47977bc..efedff5 100644
--- a/main/src/cgeo/geocaching/Waypoint.java
+++ b/main/src/cgeo/geocaching/Waypoint.java
@@ -3,14 +3,19 @@ package cgeo.geocaching;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.enumerations.WaypointType;
import cgeo.geocaching.geopoint.Geopoint;
+import cgeo.geocaching.utils.MatcherWrapper;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jdt.annotation.NonNull;
+import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
+import java.util.LinkedList;
import java.util.List;
+import java.util.Locale;
import java.util.Map;
+import java.util.regex.Pattern;
public class Waypoint implements IWaypoint {
@@ -285,4 +290,61 @@ public class Waypoint implements IWaypoint {
return gpxId;
}
+
+ /**
+ * Detect coordinates in the personal note and convert them to user defined waypoints. Works by rule of thumb.
+ *
+ * @param initialNote Note content
+ * @return a collection of found waypoints
+ */
+ public static Collection<Waypoint> parseWaypointsFromNote(@NonNull final String initialNote) {
+ final List<Waypoint> waypoints = new LinkedList<Waypoint>();
+ final Pattern COORDPATTERN = Pattern.compile("\\b[nNsS]{1}\\s*\\d"); // begin of coordinates
+
+ String note = initialNote;
+ MatcherWrapper matcher = new MatcherWrapper(COORDPATTERN, note);
+ int count = 1;
+ while (matcher.find()) {
+ try {
+ final Geopoint point = new Geopoint(note.substring(matcher.start()));
+ // Coords must have non zero latitude and longitude and at least one part shall have fractional degrees.
+ if (point.getLatitudeE6() != 0 && point.getLongitudeE6() != 0 &&
+ ((point.getLatitudeE6() % 1000) != 0 || (point.getLongitudeE6() % 1000) != 0)) {
+ final String name = CgeoApplication.getInstance().getString(R.string.cache_personal_note) + " " + count;
+ final String potentialWaypointType = note.substring(Math.max(0, matcher.start() - 15));
+ final Waypoint waypoint = new Waypoint(name, parseWaypointType(potentialWaypointType), false);
+ waypoint.setCoords(point);
+ waypoints.add(waypoint);
+ count++;
+ }
+ } catch (final Geopoint.ParseException e) {
+ // ignore
+ }
+
+ note = note.substring(matcher.start() + 1);
+ matcher = new MatcherWrapper(COORDPATTERN, note);
+ }
+ return waypoints;
+ }
+
+ /**
+ * Detect waypoint types in the personal note text. It works by rule of thumb only.
+ */
+ private static WaypointType parseWaypointType(final String input) {
+ final String lowerInput = StringUtils.substring(input, 0, 20).toLowerCase(Locale.getDefault());
+ for (final WaypointType wpType : WaypointType.values()) {
+ if (lowerInput.contains(wpType.getL10n().toLowerCase(Locale.getDefault()))) {
+ return wpType;
+ }
+ if (lowerInput.contains(wpType.id)) {
+ return wpType;
+ }
+ if (lowerInput.contains(wpType.name().toLowerCase(Locale.US))) {
+ return wpType;
+ }
+ }
+ return WaypointType.WAYPOINT;
+ }
+
+
}