aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/Waypoint.java
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2014-02-28 21:36:54 +0100
committerSamuel Tardieu <sam@rfc1149.net>2014-02-28 22:14:48 +0100
commitce4c2d36b88e126aa245f7549a9a873e761e5c57 (patch)
tree0516c267e0b3fb710c1e1d121e0aef30492629ae /main/src/cgeo/geocaching/Waypoint.java
parent423da50116afcf1a6720f2a91fefece41562ef20 (diff)
downloadcgeo-ce4c2d36b88e126aa245f7549a9a873e761e5c57.zip
cgeo-ce4c2d36b88e126aa245f7549a9a873e761e5c57.tar.gz
cgeo-ce4c2d36b88e126aa245f7549a9a873e761e5c57.tar.bz2
refactoring: move waypoint parsing code in Waypoint
Diffstat (limited to 'main/src/cgeo/geocaching/Waypoint.java')
-rw-r--r--main/src/cgeo/geocaching/Waypoint.java63
1 files changed, 63 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/Waypoint.java b/main/src/cgeo/geocaching/Waypoint.java
index 47977bc..6626c4d 100644
--- a/main/src/cgeo/geocaching/Waypoint.java
+++ b/main/src/cgeo/geocaching/Waypoint.java
@@ -3,14 +3,20 @@ package cgeo.geocaching;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.enumerations.WaypointType;
import cgeo.geocaching.geopoint.Geopoint;
+import cgeo.geocaching.utils.Log;
+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 +291,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;
+ }
+
+
}