From 75e61375e76872e9a046b914587e5d7035856060 Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Thu, 27 Sep 2018 18:56:06 +0300 Subject: [PATCH 1/5] Improve coordinates search --- .../java/net/osmand/util/LocationParser.java | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/util/LocationParser.java b/OsmAnd-java/src/main/java/net/osmand/util/LocationParser.java index 7b14967eb1..893f3f7d4b 100644 --- a/OsmAnd-java/src/main/java/net/osmand/util/LocationParser.java +++ b/OsmAnd-java/src/main/java/net/osmand/util/LocationParser.java @@ -24,10 +24,15 @@ public class LocationParser { return new LatLon(codeArea.getCenterLatitude(), codeArea.getCenterLongitude()); } } - if (locPhrase.length() == 0 || !(locPhrase.charAt(0) == '-' || Character.isDigit(locPhrase.charAt(0)) - || locPhrase.charAt(0) == 'S' || locPhrase.charAt(0) == 's' - || locPhrase.charAt(0) == 'N' || locPhrase.charAt(0) == 'n' - || locPhrase.contains("://"))) { + boolean valid = validate(locPhrase); + if (!valid) { + String[] split = locPhrase.split(" "); + if (split.length == 4) { + locPhrase = split[1] + " " + split[3]; + valid = validate(locPhrase); + } + } + if (!valid) { return null; } List d = new ArrayList<>(); @@ -172,6 +177,14 @@ public class LocationParser { return null; } + private static boolean validate(String locPhrase) { + if (!locPhrase.isEmpty()) { + char ch = Character.toLowerCase(locPhrase.charAt(0)); + return ch == '-' || Character.isDigit(ch) || ch == 's' || ch == 'n' || locPhrase.contains("://"); + } + return false; + } + public static double parse1Coordinate(List all, int begin, int end) { boolean neg = false; double d = 0; From 35794ea77eeaf2b7b46fa7909d50a3378a617b23 Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Thu, 27 Sep 2018 20:44:30 +0300 Subject: [PATCH 2/5] Add validation for coordinates --- .../java/net/osmand/util/LocationParser.java | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/util/LocationParser.java b/OsmAnd-java/src/main/java/net/osmand/util/LocationParser.java index 893f3f7d4b..732fa7ef7b 100644 --- a/OsmAnd-java/src/main/java/net/osmand/util/LocationParser.java +++ b/OsmAnd-java/src/main/java/net/osmand/util/LocationParser.java @@ -24,12 +24,12 @@ public class LocationParser { return new LatLon(codeArea.getCenterLatitude(), codeArea.getCenterLongitude()); } } - boolean valid = validate(locPhrase); + boolean valid = isValidLocPhrase(locPhrase); if (!valid) { String[] split = locPhrase.split(" "); - if (split.length == 4) { + if (split.length == 4 && split[1].contains(".") && split[3].contains(".")) { locPhrase = split[1] + " " + split[3]; - valid = validate(locPhrase); + valid = isValidLocPhrase(locPhrase); } } if (!valid) { @@ -48,7 +48,7 @@ public class LocationParser { if (Character.isLetter(ch)) { UTMPoint upoint = new UTMPoint(d.get(2), d.get(1), d.get(0).intValue(), ch); LatLonPoint ll = upoint.toLatLonPoint(); - return new LatLon(ll.getLatitude(), ll.getLongitude()); + return validOrNull(new LatLon(ll.getLatitude(), ll.getLongitude())); } } @@ -62,7 +62,7 @@ public class LocationParser { UTMPoint upoint = new UTMPoint(Double.parseDouble(north), Double.parseDouble(east), d.get(0) .intValue(), ch); LatLonPoint ll = upoint.toLatLonPoint(); - return new LatLon(ll.getLatitude(), ll.getLongitude()); + return validOrNull(new LatLon(ll.getLatitude(), ll.getLongitude())); } catch (NumberFormatException e) { } } @@ -130,10 +130,10 @@ public class LocationParser { if (split != -1) { double lat = parse1Coordinate(all, 0, split); double lon = parse1Coordinate(all, split, all.size()); - return new LatLon(lat, lon); + return validOrNull(new LatLon(lat, lon)); } if (d.size() == 2) { - return new LatLon(d.get(0), d.get(1)); + return validOrNull(new LatLon(d.get(0), d.get(1))); } // simple url case if (locPhrase.contains("://")) { @@ -152,7 +152,7 @@ public class LocationParser { } } if (lat != 0 && lon != 0 && only2decimals) { - return new LatLon(lat, lon); + return validOrNull(new LatLon(lat, lon)); } } // split by equal number of digits @@ -171,13 +171,25 @@ public class LocationParser { if (splitEq != -1) { double lat = parse1Coordinate(all, 0, splitEq); double lon = parse1Coordinate(all, splitEq, all.size()); - return new LatLon(lat, lon); + return validOrNull(new LatLon(lat, lon)); } } return null; } - private static boolean validate(String locPhrase) { + private static LatLon validOrNull(LatLon latLon) { + if (isValidCoordinate(latLon.getLatitude()) && isValidCoordinate(latLon.getLongitude())) { + return latLon; + } + return null; + } + + private static boolean isValidCoordinate(double coordinate) { + double coord = Math.abs(coordinate); + return 0 <= coord && coord <= 180; + } + + private static boolean isValidLocPhrase(String locPhrase) { if (!locPhrase.isEmpty()) { char ch = Character.toLowerCase(locPhrase.charAt(0)); return ch == '-' || Character.isDigit(ch) || ch == 's' || ch == 'n' || locPhrase.contains("://"); From 3365cab02d9cce8d07c6cff5b3dbb1aeae4f7f42 Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Thu, 27 Sep 2018 20:47:09 +0300 Subject: [PATCH 3/5] Refactor code --- .../java/net/osmand/util/LocationParser.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/util/LocationParser.java b/OsmAnd-java/src/main/java/net/osmand/util/LocationParser.java index 732fa7ef7b..ac3588879a 100644 --- a/OsmAnd-java/src/main/java/net/osmand/util/LocationParser.java +++ b/OsmAnd-java/src/main/java/net/osmand/util/LocationParser.java @@ -48,7 +48,7 @@ public class LocationParser { if (Character.isLetter(ch)) { UTMPoint upoint = new UTMPoint(d.get(2), d.get(1), d.get(0).intValue(), ch); LatLonPoint ll = upoint.toLatLonPoint(); - return validOrNull(new LatLon(ll.getLatitude(), ll.getLongitude())); + return createLatLon(ll.getLatitude(), ll.getLongitude()); } } @@ -62,7 +62,7 @@ public class LocationParser { UTMPoint upoint = new UTMPoint(Double.parseDouble(north), Double.parseDouble(east), d.get(0) .intValue(), ch); LatLonPoint ll = upoint.toLatLonPoint(); - return validOrNull(new LatLon(ll.getLatitude(), ll.getLongitude())); + return createLatLon(ll.getLatitude(), ll.getLongitude()); } catch (NumberFormatException e) { } } @@ -130,10 +130,10 @@ public class LocationParser { if (split != -1) { double lat = parse1Coordinate(all, 0, split); double lon = parse1Coordinate(all, split, all.size()); - return validOrNull(new LatLon(lat, lon)); + return createLatLon(lat, lon); } if (d.size() == 2) { - return validOrNull(new LatLon(d.get(0), d.get(1))); + return createLatLon(d.get(0), d.get(1)); } // simple url case if (locPhrase.contains("://")) { @@ -152,7 +152,7 @@ public class LocationParser { } } if (lat != 0 && lon != 0 && only2decimals) { - return validOrNull(new LatLon(lat, lon)); + return createLatLon(lat, lon); } } // split by equal number of digits @@ -171,15 +171,15 @@ public class LocationParser { if (splitEq != -1) { double lat = parse1Coordinate(all, 0, splitEq); double lon = parse1Coordinate(all, splitEq, all.size()); - return validOrNull(new LatLon(lat, lon)); + return createLatLon(lat, lon); } } return null; } - private static LatLon validOrNull(LatLon latLon) { - if (isValidCoordinate(latLon.getLatitude()) && isValidCoordinate(latLon.getLongitude())) { - return latLon; + private static LatLon createLatLon(double lat, double lon) { + if (isValidCoordinate(lat) && isValidCoordinate(lon)) { + return new LatLon(lat, lon); } return null; } From 92bcb61faca57383f88a6d23a6e2fbd93def3ee6 Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Tue, 2 Oct 2018 10:51:51 +0300 Subject: [PATCH 4/5] Remove redundant check; rename method --- .../java/net/osmand/util/LocationParser.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/util/LocationParser.java b/OsmAnd-java/src/main/java/net/osmand/util/LocationParser.java index ac3588879a..cfb15befb4 100644 --- a/OsmAnd-java/src/main/java/net/osmand/util/LocationParser.java +++ b/OsmAnd-java/src/main/java/net/osmand/util/LocationParser.java @@ -48,7 +48,7 @@ public class LocationParser { if (Character.isLetter(ch)) { UTMPoint upoint = new UTMPoint(d.get(2), d.get(1), d.get(0).intValue(), ch); LatLonPoint ll = upoint.toLatLonPoint(); - return createLatLon(ll.getLatitude(), ll.getLongitude()); + return validateAndCreateLatLon(ll.getLatitude(), ll.getLongitude()); } } @@ -62,7 +62,7 @@ public class LocationParser { UTMPoint upoint = new UTMPoint(Double.parseDouble(north), Double.parseDouble(east), d.get(0) .intValue(), ch); LatLonPoint ll = upoint.toLatLonPoint(); - return createLatLon(ll.getLatitude(), ll.getLongitude()); + return validateAndCreateLatLon(ll.getLatitude(), ll.getLongitude()); } catch (NumberFormatException e) { } } @@ -130,10 +130,10 @@ public class LocationParser { if (split != -1) { double lat = parse1Coordinate(all, 0, split); double lon = parse1Coordinate(all, split, all.size()); - return createLatLon(lat, lon); + return validateAndCreateLatLon(lat, lon); } if (d.size() == 2) { - return createLatLon(d.get(0), d.get(1)); + return validateAndCreateLatLon(d.get(0), d.get(1)); } // simple url case if (locPhrase.contains("://")) { @@ -152,7 +152,7 @@ public class LocationParser { } } if (lat != 0 && lon != 0 && only2decimals) { - return createLatLon(lat, lon); + return validateAndCreateLatLon(lat, lon); } } // split by equal number of digits @@ -171,13 +171,13 @@ public class LocationParser { if (splitEq != -1) { double lat = parse1Coordinate(all, 0, splitEq); double lon = parse1Coordinate(all, splitEq, all.size()); - return createLatLon(lat, lon); + return validateAndCreateLatLon(lat, lon); } } return null; } - private static LatLon createLatLon(double lat, double lon) { + private static LatLon validateAndCreateLatLon(double lat, double lon) { if (isValidCoordinate(lat) && isValidCoordinate(lon)) { return new LatLon(lat, lon); } @@ -185,8 +185,7 @@ public class LocationParser { } private static boolean isValidCoordinate(double coordinate) { - double coord = Math.abs(coordinate); - return 0 <= coord && coord <= 180; + return Math.abs(coordinate) <= 180; } private static boolean isValidLocPhrase(String locPhrase) { From 61e25617c1e0c9ec750a8ccd0bcb65f7419a97d1 Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Tue, 2 Oct 2018 11:04:06 +0300 Subject: [PATCH 5/5] Fix coordinates validation --- .../src/main/java/net/osmand/util/LocationParser.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/util/LocationParser.java b/OsmAnd-java/src/main/java/net/osmand/util/LocationParser.java index cfb15befb4..183c306456 100644 --- a/OsmAnd-java/src/main/java/net/osmand/util/LocationParser.java +++ b/OsmAnd-java/src/main/java/net/osmand/util/LocationParser.java @@ -178,16 +178,12 @@ public class LocationParser { } private static LatLon validateAndCreateLatLon(double lat, double lon) { - if (isValidCoordinate(lat) && isValidCoordinate(lon)) { + if (Math.abs(lat) <= 90 && Math.abs(lon) <= 180) { return new LatLon(lat, lon); } return null; } - private static boolean isValidCoordinate(double coordinate) { - return Math.abs(coordinate) <= 180; - } - private static boolean isValidLocPhrase(String locPhrase) { if (!locPhrase.isEmpty()) { char ch = Character.toLowerCase(locPhrase.charAt(0));