diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2012-06-05 10:37:47 +0200 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2012-06-05 10:37:47 +0200 |
| commit | d70e5af6977c7920a1edaa9a83d06a765d12c6dc (patch) | |
| tree | b4089c4ddd6c20e7015678c55f01d5fadd7bc584 /main/src/cgeo/geocaching/geopoint | |
| parent | a69ef9900c468ad1b1947462ee5984429b92a12d (diff) | |
| parent | ed6478e3b56bb2d27c6274fba3bb84f8310f6440 (diff) | |
| download | cgeo-d70e5af6977c7920a1edaa9a83d06a765d12c6dc.zip cgeo-d70e5af6977c7920a1edaa9a83d06a765d12c6dc.tar.gz cgeo-d70e5af6977c7920a1edaa9a83d06a765d12c6dc.tar.bz2 | |
Merge branch 'issue-1722' into upstream
Diffstat (limited to 'main/src/cgeo/geocaching/geopoint')
6 files changed, 276 insertions, 264 deletions
diff --git a/main/src/cgeo/geocaching/geopoint/Geopoint.java b/main/src/cgeo/geocaching/geopoint/Geopoint.java index faec82e..612b6ad 100644 --- a/main/src/cgeo/geocaching/geopoint/Geopoint.java +++ b/main/src/cgeo/geocaching/geopoint/Geopoint.java @@ -3,10 +3,6 @@ package cgeo.geocaching.geopoint; import cgeo.geocaching.ICoordinates; import cgeo.geocaching.R; import cgeo.geocaching.geopoint.GeopointFormatter.Format; -import cgeo.geocaching.geopoint.direction.DDD; -import cgeo.geocaching.geopoint.direction.DMM; -import cgeo.geocaching.geopoint.direction.DMS; -import cgeo.geocaching.geopoint.direction.Direction; import cgeo.geocaching.network.Network; import cgeo.geocaching.network.Parameters; import cgeo.geocaching.utils.Log; @@ -30,11 +26,6 @@ public final class Geopoint implements ICoordinates, Parcelable { private final double latitude; private final double longitude; - private Direction direction; - private DDD ddd; - private DMM dmm; - private DMS dms; - /** * Creates new Geopoint with given latitude and longitude (both degree). * @@ -101,6 +92,66 @@ public final class Geopoint implements ICoordinates, Parcelable { } /** + * Create new Geopoint from individual textual components. + * + * @param latDir + * @param latDeg + * @param latDegFrac + * @param lonDir + * @param lonDeg + * @param lonDegFrac + */ + public Geopoint(final String latDir, final String latDeg, final String latDegFrac, + final String lonDir, final String lonDeg, final String lonDegFrac) { + latitude = Double.parseDouble(latDeg + "." + addZeros(Integer.parseInt(latDegFrac), 5)) * + getLatSign(latDir); + longitude = Double.parseDouble(lonDeg + "." + addZeros(Integer.parseInt(lonDegFrac), 5)) * + getLonSign(lonDir); + } + + /** + * Create new Geopoint from individual textual components. + * + * @param latDir + * @param latDeg + * @param latMin + * @param latMinFrac + * @param lonDir + * @param lonDeg + * @param lonMin + * @param lonMinFrac + */ + public Geopoint(final String latDir, final String latDeg, final String latMin, final String latMinFrac, + final String lonDir, final String lonDeg, final String lonMin, final String lonMinFrac) { + latitude = (Double.parseDouble(latDeg) + Double.parseDouble(latMin + "." + addZeros(Integer.parseInt(latMinFrac), 3)) / 60) * + (getLatSign(latDir)); + longitude = (Double.parseDouble(lonDeg) + Double.parseDouble(lonMin + "." + addZeros(Integer.parseInt(lonMinFrac), 3)) / 60) * + (getLonSign(lonDir)); + } + + /** + * Create new Geopoint from individual textual components. + * + * @param latDir + * @param latDeg + * @param latMin + * @param latSec + * @param latSecFrac + * @param lonDir + * @param lonDeg + * @param lonMin + * @param lonSec + * @param lonSecFrac + */ + public Geopoint(final String latDir, final String latDeg, final String latMin, final String latSec, final String latSecFrac, + final String lonDir, final String lonDeg, final String lonMin, final String lonSec, final String lonSecFrac) { + latitude = (Double.parseDouble(latDeg) + Double.parseDouble(latMin) / 60 + Double.parseDouble(latSec + "." + addZeros(Integer.parseInt(latSecFrac), 3)) / 3600) * + (getLatSign(latDir)); + longitude = (Double.parseDouble(lonDeg) + Double.parseDouble(lonMin) / 60 + Double.parseDouble(lonSec + "." + addZeros(Integer.parseInt(lonSecFrac), 3)) / 3600) * + (getLonSign(lonDir)); + } + + /** * Get latitude in degree. * * @return latitude @@ -259,54 +310,6 @@ public final class Geopoint implements ICoordinates, Parcelable { return format(GeopointFormatter.Format.LAT_LON_DECMINUTE); } - /** - * Converts this geopoint to value type Direction. - * - * @return Direction - */ - public Direction asDirection() { - if (direction == null) { // because geopoint is immutable we can "cache" the result - direction = new Direction(latitude, longitude); - } - return direction; - } - - /** - * Converts this geopoint to value type DDD. - * - * @return - */ - public DDD asDDD() { - if (ddd == null) { - ddd = new DDD(latitude, longitude); - } - return ddd; - } - - /** - * Converts this geopoint to value type DMM. - * - * @return - */ - public DMM asDMM() { - if (dmm == null) { - dmm = new DMM(latitude, longitude); - } - return dmm; - } - - /** - * Converts this geopoint to value type DMS. - * - * @return - */ - public DMS asDMS() { - if (dms == null) { - dms = new DMS(latitude, longitude); - } - return dms; - } - abstract public static class GeopointException extends RuntimeException { @@ -387,4 +390,212 @@ public final class Geopoint implements ICoordinates, Parcelable { } }; + /** + * Get latitude character (N or S). + * + * @return + */ + public char getLatDir() { + return latitude >= 0 ? 'N' : 'S'; + } + + /** + * Get longitude chararcter (E or W). + * + * @return + */ + public char getLonDir() { + return longitude >= 0 ? 'E' : 'W'; + } + + /** + * Get the integral non-negative latitude degrees. + * + * @return + */ + public int getLatDeg() { + return getDeg(latitude); + } + + /** + * Get the integral non-negative longitude degrees. + * + * @return + */ + public int getLonDeg() { + return getDeg(longitude); + } + + private static int getDeg(final double deg) { + return (int) Math.abs(deg); + } + + /** + * Get the fractional part of the latitude degrees scaled up by 10^5. + * + * @return + */ + public int getLatDegFrac() { + return getDegFrac(latitude); + } + + /** + * Get the fractional part of the longitude degrees scaled up by 10^5. + * + * @return + */ + public int getLonDegFrac() { + return getDegFrac(longitude); + } + + private static int getDegFrac(final double deg) { + return (int) (Math.round(Math.abs(deg) * 100000) % 100000); + } + + /** + * Get the integral latitude minutes. + * + * @return + */ + public int getLatMin() { + return getMin(latitude); + } + + /** + * Get the integral longitude minutes. + * + * @return + */ + public int getLonMin() { + return getMin(longitude); + } + + private static int getMin(final double deg) { + return ((int) Math.abs(deg * 60)) % 60; + } + + /** + * Get the fractional part of the latitude minutes scaled up by 1000. + * + * @return + */ + public int getLatMinFrac() { + return getMinFrac(latitude); + } + + /** + * Get the fractional part of the longitude minutes scaled up by 1000. + * + * @return + */ + public int getLonMinFrac() { + return getMinFrac(longitude); + } + + private static int getMinFrac(final double deg) { + return (int) (Math.round(Math.abs(deg) * 60000) % 1000); + } + + /** + * Get the latitude minutes. + * + * @return + */ + public double getLatMinRaw() { + return getMinRaw(latitude); + } + + /** + * Get the longitude minutes. + * + * @return + */ + public double getLonMinRaw() { + return getMinRaw(longitude); + } + + private static double getMinRaw(final double deg) { + return (Math.abs(deg) * 60) % 60; + } + + /** + * Get the integral part of the latitude seconds. + * + * @return + */ + public int getLatSec() { + return getSec(latitude); + } + + /** + * Get the integral part of the longitude seconds. + * + * @return + */ + public int getLonSec() { + return getSec(longitude); + } + + private static int getSec(final double deg) { + return ((int) Math.abs(deg * 3600)) % 60; + } + + /** + * Get the fractional part of the latitude seconds scaled up by 1000. + * + * @return + */ + + public int getLatSecFrac() { + return getSecFrac(latitude); + } + + /** + * Get the fractional part of the longitude seconds scaled up by 1000. + * + * @return + */ + + public int getLonSecFrac() { + return getSecFrac(longitude); + } + + private static int getSecFrac(final double deg) { + return (int) (Math.round(Math.abs(deg) * 3600000) % 1000); + } + + /** + * Get the latitude seconds. + * + * @return + */ + public double getLatSecRaw() { + return getSecRaw(latitude); + } + + /** + * Get the longitude seconds. + * + * @return + */ + public double getLonSecRaw() { + return getSecRaw(longitude); + } + + private static double getSecRaw(final double deg) { + return (Math.abs(deg) * 3600) % 3600; + } + + private static String addZeros(final int value, final int len) { + return StringUtils.leftPad(Integer.toString(value), len, '0'); + } + + private static int getLonSign(final String lonDir) { + return "W".equalsIgnoreCase(lonDir) ? -1 : 1; + } + + private static int getLatSign(final String latDir) { + return "S".equalsIgnoreCase(latDir) ? -1 : 1; + } + } diff --git a/main/src/cgeo/geocaching/geopoint/GeopointFormatter.java b/main/src/cgeo/geocaching/geopoint/GeopointFormatter.java index 43f83a3..0b3df05 100644 --- a/main/src/cgeo/geocaching/geopoint/GeopointFormatter.java +++ b/main/src/cgeo/geocaching/geopoint/GeopointFormatter.java @@ -1,8 +1,5 @@ package cgeo.geocaching.geopoint; -import cgeo.geocaching.geopoint.direction.DMM; -import cgeo.geocaching.geopoint.direction.DMS; - import java.util.Locale; /** @@ -62,9 +59,6 @@ public class GeopointFormatter final double latSigned = gp.getLatitude(); final double lonSigned = gp.getLongitude(); - DMM dmm = gp.asDMM(); - DMS dms = gp.asDMS(); - switch (format) { case LAT_LON_DECDEGREE: return String.format("%.6f %.6f", latSigned, lonSigned); @@ -74,33 +68,34 @@ public class GeopointFormatter case LAT_LON_DECMINUTE: return String.format("%c %02d° %06.3f · %c %03d° %06.3f", - dmm.latDir, dmm.latDeg, dmm.latMinRaw, dmm.lonDir, dmm.lonDeg, dmm.lonMinRaw); + gp.getLatDir(), gp.getLatDeg(), gp.getLatMinRaw(), gp.getLonDir(), gp.getLonDeg(), gp.getLonMinRaw()); case LAT_LON_DECMINUTE_RAW: return String.format((Locale) null, "%c %02d° %06.3f %c %03d° %06.3f", - dmm.latDir, dmm.latDeg, dmm.latMinRaw, dmm.lonDir, dmm.lonDeg, dmm.lonMinRaw); + gp.getLatDir(), gp.getLatDeg(), gp.getLatMinRaw(), gp.getLonDir(), gp.getLonDeg(), gp.getLonMinRaw()); case LAT_LON_DECSECOND: return String.format("%c %02d° %02d' %06.3f\" · %c %03d° %02d' %06.3f\"", - dms.latDir, dms.latDeg, dms.latMin, dms.latSecRaw, dms.lonDir, dms.lonDeg, dms.lonMin, dms.lonSecRaw); + gp.getLatDir(), gp.getLatDeg(), gp.getLatMin(), gp.getLatSecRaw(), + gp.getLonDir(), gp.getLonDeg(), gp.getLonMin(), gp.getLonSecRaw()); case LAT_DECDEGREE_RAW: return String.format((Locale) null, "%.6f", latSigned); case LAT_DECMINUTE: - return String.format("%c %02d° %06.3f", dmm.latDir, dmm.latDeg, dmm.latMinRaw); + return String.format("%c %02d° %06.3f", gp.getLatDir(), gp.getLatDeg(), gp.getLatMinRaw()); case LAT_DECMINUTE_RAW: - return String.format("%c %02d %06.3f", dmm.latDir, dmm.latDeg, dmm.latMinRaw); + return String.format("%c %02d %06.3f", gp.getLatDir(), gp.getLatDeg(), gp.getLatMinRaw()); case LON_DECDEGREE_RAW: return String.format((Locale) null, "%.6f", lonSigned); case LON_DECMINUTE: - return String.format("%c %03d° %06.3f", dmm.lonDir, dmm.lonDeg, dmm.lonMinRaw); + return String.format("%c %03d° %06.3f", gp.getLonDir(), gp.getLonDeg(), gp.getLonMinRaw()); case LON_DECMINUTE_RAW: - return String.format("%c %03d %06.3f", dmm.lonDir, dmm.lonDeg, dmm.lonMinRaw); + return String.format("%c %03d %06.3f", gp.getLonDir(), gp.getLonDeg(), gp.getLonMinRaw()); } // Keep the compiler happy even though it cannot happen diff --git a/main/src/cgeo/geocaching/geopoint/direction/DDD.java b/main/src/cgeo/geocaching/geopoint/direction/DDD.java deleted file mode 100644 index 939d6ca..0000000 --- a/main/src/cgeo/geocaching/geopoint/direction/DDD.java +++ /dev/null @@ -1,47 +0,0 @@ -package cgeo.geocaching.geopoint.direction; - -import cgeo.geocaching.geopoint.Geopoint; - -import java.math.BigDecimal; -import java.math.RoundingMode; - -/** - * Value type for the DDD.DDDDD format. - */ -public final class DDD extends Direction { - - /** latitude degree value */ - public final int latDeg; - /** fractional part of the latitude degree value */ - public final int latDegFrac; - - public final int lonDeg; - public final int lonDegFrac; - - public DDD(final double latSigned, final double lonSigned) { - super(latSigned, lonSigned); - BigDecimal bdLat = BigDecimal.valueOf(latSigned).abs(); - latDeg = bdLat.intValue(); - BigDecimal bdLatFrac = bdLat.subtract(BigDecimal.valueOf(latDeg)).multiply(BD_ONEHOUNDREDTHOUSAND); - latDegFrac = bdLatFrac.setScale(0, RoundingMode.HALF_UP).intValue(); - - BigDecimal bdlon = BigDecimal.valueOf(lonSigned).abs(); - lonDeg = bdlon.intValue(); - BigDecimal bdLonFrac = bdlon.subtract(BigDecimal.valueOf(lonDeg)).multiply(BD_ONEHOUNDREDTHOUSAND); - lonDegFrac = bdLonFrac.setScale(0, RoundingMode.HALF_UP).intValue(); - } - - public static Geopoint createGeopoint(final String latDir, final String latDeg, final String latDegFrac, - final String lonDir, final String lonDeg, final String lonDegFrac) { - double lat = 0.0d; - double lon = 0.0d; - try { - lat = Double.parseDouble(latDeg + "." + addZeros(Integer.parseInt(latDegFrac), 5)); - lon = Double.parseDouble(lonDeg + "." + addZeros(Integer.parseInt(lonDegFrac), 5)); - } catch (NumberFormatException e) { - } - lat *= "S".equalsIgnoreCase(latDir) ? -1 : 1; - lon *= "W".equalsIgnoreCase(lonDir) ? -1 : 1; - return new Geopoint(lat, lon); - } -}
\ No newline at end of file diff --git a/main/src/cgeo/geocaching/geopoint/direction/DMM.java b/main/src/cgeo/geocaching/geopoint/direction/DMM.java deleted file mode 100644 index 7426f28..0000000 --- a/main/src/cgeo/geocaching/geopoint/direction/DMM.java +++ /dev/null @@ -1,56 +0,0 @@ -package cgeo.geocaching.geopoint.direction; - -import cgeo.geocaching.geopoint.Geopoint; - -import java.math.BigDecimal; -import java.math.RoundingMode; - -public final class DMM extends Direction { - - public final int latDeg; - public final double latMinRaw; - public final int latMin; - public final int latMinFrac; - - public final int lonDeg; - public final double lonMinRaw; - public final int lonMin; - public final int lonMinFrac; - - public DMM(final double latSigned, final double lonSigned) { - super(latSigned, lonSigned); - BigDecimal bdLat = BigDecimal.valueOf(latSigned).abs(); - latDeg = bdLat.intValue(); - BigDecimal bdLatMin = bdLat.subtract(BigDecimal.valueOf(latDeg)).multiply(BD_SIXTY); - // Rounding here ... - bdLatMin = bdLatMin.setScale(3, RoundingMode.HALF_UP); - latMinRaw = bdLatMin.doubleValue(); - latMin = bdLatMin.intValue(); - BigDecimal bdLatMinFrac = bdLatMin.subtract(BigDecimal.valueOf(latMin)).multiply(BD_THOUSAND); - latMinFrac = bdLatMinFrac.setScale(0, RoundingMode.HALF_UP).intValue(); - - BigDecimal bdlon = BigDecimal.valueOf(lonSigned).abs(); - lonDeg = bdlon.intValue(); - BigDecimal bdLonMin = bdlon.subtract(BigDecimal.valueOf(lonDeg)).multiply(BD_SIXTY); - // Rounding here ... - bdLonMin = bdLonMin.setScale(3, RoundingMode.HALF_UP); - lonMinRaw = bdLonMin.doubleValue(); - lonMin = bdLonMin.intValue(); - BigDecimal bdLonMinFrac = bdLonMin.subtract(BigDecimal.valueOf(lonMin)).multiply(BD_THOUSAND); - lonMinFrac = bdLonMinFrac.setScale(0, RoundingMode.HALF_UP).intValue(); - } - - public static Geopoint createGeopoint(final String latDir, final String latDeg, final String latMin, final String latMinFrac, - final String lonDir, final String lonDeg, final String lonMin, final String lonMinFrac) { - double lat = 0.0d; - double lon = 0.0d; - try { - lat = Double.parseDouble(latDeg) + Double.parseDouble(latMin + "." + addZeros(Integer.parseInt(latMinFrac), 3)) / D60; - lon = Double.parseDouble(lonDeg) + Double.parseDouble(lonMin + "." + addZeros(Integer.parseInt(lonMinFrac), 3)) / D60; - } catch (NumberFormatException e) { - } - lat *= "S".equalsIgnoreCase(latDir) ? -1 : 1; - lon *= "W".equalsIgnoreCase(lonDir) ? -1 : 1; - return new Geopoint(lat, lon); - } -}
\ No newline at end of file diff --git a/main/src/cgeo/geocaching/geopoint/direction/DMS.java b/main/src/cgeo/geocaching/geopoint/direction/DMS.java deleted file mode 100644 index 34a9a3d..0000000 --- a/main/src/cgeo/geocaching/geopoint/direction/DMS.java +++ /dev/null @@ -1,62 +0,0 @@ -package cgeo.geocaching.geopoint.direction; - -import cgeo.geocaching.geopoint.Geopoint; - -import java.math.BigDecimal; -import java.math.RoundingMode; - -public final class DMS extends Direction { - - public final int latDeg; - public final int latMin; - public final double latSecRaw; - public final int latSec; - public final int latSecFrac; - - public final int lonDeg; - public final int lonMin; - public final double lonSecRaw; - public final int lonSec; - public final int lonSecFrac; - - public DMS(final double latSigned, final double lonSigned) { - super(latSigned, lonSigned); - BigDecimal bdLat = BigDecimal.valueOf(latSigned).abs(); - latDeg = bdLat.intValue(); - BigDecimal bdLatMin = bdLat.subtract(BigDecimal.valueOf(latDeg)).multiply(BD_SIXTY); - latMin = bdLatMin.intValue(); - BigDecimal bdLatSec = bdLatMin.subtract(BigDecimal.valueOf(latMin)).multiply(BD_SIXTY); - // Rounding here ... - bdLatSec = bdLatSec.setScale(3, RoundingMode.HALF_UP); - latSecRaw = bdLatSec.doubleValue(); - latSec = bdLatSec.intValue(); - BigDecimal bdLatSecFrac = bdLatSec.subtract(BigDecimal.valueOf(latSec)).multiply(BD_THOUSAND); - latSecFrac = bdLatSecFrac.setScale(0, RoundingMode.HALF_UP).intValue(); - - BigDecimal bdlon = BigDecimal.valueOf(lonSigned).abs(); - lonDeg = bdlon.intValue(); - BigDecimal bdLonMin = bdlon.subtract(BigDecimal.valueOf(lonDeg)).multiply(BD_SIXTY); - lonMin = bdLonMin.intValue(); - BigDecimal bdLonSec = bdLonMin.subtract(BigDecimal.valueOf(lonMin)).multiply(BD_SIXTY); - // Rounding here ... - bdLonSec = bdLonSec.setScale(3, RoundingMode.HALF_UP); - lonSecRaw = bdLonSec.doubleValue(); - lonSec = bdLonSec.intValue(); - BigDecimal bdLonSecFrac = bdLonSec.subtract(BigDecimal.valueOf(lonSec)).multiply(BD_THOUSAND); - lonSecFrac = bdLonSecFrac.setScale(0, RoundingMode.HALF_UP).intValue(); - } - - public static Geopoint createGeopoint(final String latDir, final String latDeg, final String latMin, final String latSec, final String latSecFrac, - final String lonDir, final String lonDeg, final String lonMin, final String lonSec, final String lonSecFrac) { - double lat = 0.0d; - double lon = 0.0d; - try { - lat = Double.parseDouble(latDeg) + Double.parseDouble(latMin) / D60 + Double.parseDouble(latSec + "." + addZeros(Integer.parseInt(latSecFrac), 3)) / D3600; - lon = Double.parseDouble(lonDeg) + Double.parseDouble(lonMin) / D60 + Double.parseDouble(lonSec + "." + addZeros(Integer.parseInt(lonSecFrac), 3)) / D3600; - } catch (NumberFormatException e) { - } - lat *= "S".equalsIgnoreCase(latDir) ? -1 : 1; - lon *= "W".equalsIgnoreCase(lonDir) ? -1 : 1; - return new Geopoint(lat, lon); - } -}
\ No newline at end of file diff --git a/main/src/cgeo/geocaching/geopoint/direction/Direction.java b/main/src/cgeo/geocaching/geopoint/direction/Direction.java deleted file mode 100644 index ad91516..0000000 --- a/main/src/cgeo/geocaching/geopoint/direction/Direction.java +++ /dev/null @@ -1,29 +0,0 @@ -package cgeo.geocaching.geopoint.direction; - -import org.apache.commons.lang3.StringUtils; - -import java.math.BigDecimal; - -public class Direction { - /* Constant values needed for calculation */ - static final double D60 = 60.0d; - private static final double D1000 = 1000.0d; - static final double D3600 = 3600.0d; - static final BigDecimal BD_SIXTY = BigDecimal.valueOf(D60); - static final BigDecimal BD_THOUSAND = BigDecimal.valueOf(D1000); - static final BigDecimal BD_ONEHOUNDREDTHOUSAND = BigDecimal.valueOf(100000.0d); - - /** latitude direction, 'N' or 'S' */ - public final char latDir; - /** longitude direction, 'E' or 'W' */ - public final char lonDir; - - public Direction(final double latSigned, final double lonSigned) { - latDir = latSigned < 0 ? 'S' : 'N'; - lonDir = lonSigned < 0 ? 'W' : 'E'; - } - - protected static String addZeros(final int value, final int len) { - return StringUtils.leftPad(Integer.toString(value), len, '0'); - } -}
\ No newline at end of file |
