From 99241ce321ec2f4cff30bf53801d716c578605ed Mon Sep 17 00:00:00 2001 From: Leandro Luque Date: Mon, 8 Mar 2021 18:13:39 -0300 Subject: [PATCH] fix: fix issue with datum conversion --- .classpath | 15 +-- .settings/org.eclipse.jdt.core.prefs | 6 +- .../CoordinatesConverter.java | 123 ++++++++++-------- .../CoordinatesConverterTest.java | 28 +++- 4 files changed, 98 insertions(+), 74 deletions(-) diff --git a/.classpath b/.classpath index e45c080..6d0e29d 100644 --- a/.classpath +++ b/.classpath @@ -6,11 +6,6 @@ - - - - - @@ -18,13 +13,7 @@ - - - - - - - + @@ -42,6 +31,7 @@ + @@ -51,6 +41,5 @@ - diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index ac8e750..1b6e1ef 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,9 +1,9 @@ eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 -org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore org.eclipse.jdt.core.compiler.processAnnotations=disabled org.eclipse.jdt.core.compiler.release=disabled -org.eclipse.jdt.core.compiler.source=1.5 +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/src/main/java/br/com/skywalker/coordinateconverter/CoordinatesConverter.java b/src/main/java/br/com/skywalker/coordinateconverter/CoordinatesConverter.java index f1ec42a..221f532 100644 --- a/src/main/java/br/com/skywalker/coordinateconverter/CoordinatesConverter.java +++ b/src/main/java/br/com/skywalker/coordinateconverter/CoordinatesConverter.java @@ -21,22 +21,22 @@ public static DecimalDegreesCoordinates toDecimalDegree(DegreesMinutesSecondsCoo return toDatum(new DecimalDegreesCoordinates(fromCoordinates), toDatum); } - public static DecimalDegreesCoordinates toDecimalDegree(Coordinates fromCoordinates, Datum toDatum) { - if (fromCoordinates instanceof UTMCoordinates) { - return toDecimalDegree((UTMCoordinates) fromCoordinates, toDatum); - } else if (fromCoordinates instanceof DegreesMinutesSecondsCoordinates) { - return toDecimalDegree((DegreesMinutesSecondsCoordinates) fromCoordinates, toDatum); - } else if (fromCoordinates instanceof DecimalDegreesCoordinates) { - if (fromCoordinates.getDatum().isEqual(toDatum)) { - return (DecimalDegreesCoordinates) fromCoordinates; - } - - return toDatum((DecimalDegreesCoordinates) fromCoordinates, toDatum); - } - - throw new IllegalArgumentException("The passed coordinate is an unknown implementation of Coordinates: [" + - fromCoordinates.getClass().getName() + "]"); - } + public static DecimalDegreesCoordinates toDecimalDegree(Coordinates fromCoordinates, Datum toDatum) { + if (fromCoordinates instanceof UTMCoordinates) { + return toDecimalDegree((UTMCoordinates) fromCoordinates, toDatum); + } else if (fromCoordinates instanceof DegreesMinutesSecondsCoordinates) { + return toDecimalDegree((DegreesMinutesSecondsCoordinates) fromCoordinates, toDatum); + } else if (fromCoordinates instanceof DecimalDegreesCoordinates) { + if (fromCoordinates.getDatum().isEqual(toDatum)) { + return (DecimalDegreesCoordinates) fromCoordinates; + } + + return toDatum((DecimalDegreesCoordinates) fromCoordinates, toDatum); + } + + throw new IllegalArgumentException("The passed coordinate is an unknown implementation of Coordinates: [" + + fromCoordinates.getClass().getName() + "]"); + } public static DecimalDegreesCoordinates toDD(UTMCoordinates fromCoordinates) { return toDecimalDegree(fromCoordinates); @@ -54,7 +54,7 @@ public static DecimalDegreesCoordinates toDD(DegreesMinutesSecondsCoordinates fr return toDecimalDegree(fromCoordinates, toDatum); } - // ************************************************************************ + // ************************************************************************ public static UTMCoordinates toUTM(DecimalDegreesCoordinates fromCoordinates) { return new UTMCoordinates(fromCoordinates); @@ -72,23 +72,23 @@ public static UTMCoordinates toUTM(DegreesMinutesSecondsCoordinates fromCoordina return toDatum(new UTMCoordinates(new DecimalDegreesCoordinates(fromCoordinates)), toDatum); } - public static UTMCoordinates toUTM(Coordinates fromCoordinates, Datum toDatum) { - if (fromCoordinates instanceof DegreesMinutesSecondsCoordinates) { - return toUTM((DegreesMinutesSecondsCoordinates) fromCoordinates, toDatum); - } else if (fromCoordinates instanceof DecimalDegreesCoordinates) { - return toUTM((DecimalDegreesCoordinates) fromCoordinates, toDatum); - } else if (fromCoordinates instanceof UTMCoordinates) { - if (fromCoordinates.getDatum().isEqual(toDatum)) { - return (UTMCoordinates) fromCoordinates; - } - return toDatum((UTMCoordinates) fromCoordinates, toDatum); - } + public static UTMCoordinates toUTM(Coordinates fromCoordinates, Datum toDatum) { + if (fromCoordinates instanceof DegreesMinutesSecondsCoordinates) { + return toUTM((DegreesMinutesSecondsCoordinates) fromCoordinates, toDatum); + } else if (fromCoordinates instanceof DecimalDegreesCoordinates) { + return toUTM((DecimalDegreesCoordinates) fromCoordinates, toDatum); + } else if (fromCoordinates instanceof UTMCoordinates) { + if (fromCoordinates.getDatum().isEqual(toDatum)) { + return (UTMCoordinates) fromCoordinates; + } + return toDatum((UTMCoordinates) fromCoordinates, toDatum); + } - throw new IllegalArgumentException("The passed coordinate is an unknown implementation of Coordinates: [" + - fromCoordinates.getClass().getName() + "]"); - } + throw new IllegalArgumentException("The passed coordinate is an unknown implementation of Coordinates: [" + + fromCoordinates.getClass().getName() + "]"); + } - // ************************************************************************ + // ************************************************************************ public static DegreesMinutesSecondsCoordinates toDegreesMinutesSeconds(UTMCoordinates fromCoordinates) { return new DegreesMinutesSecondsCoordinates(new DecimalDegreesCoordinates(fromCoordinates)); @@ -108,21 +108,21 @@ public static DegreesMinutesSecondsCoordinates toDegreesMinutesSeconds(DecimalDe return toDatum(new DegreesMinutesSecondsCoordinates(fromCoordinates), toDatum); } - public static DegreesMinutesSecondsCoordinates toDegreesMinutesSeconds(Coordinates fromCoordinates, Datum toDatum) { - if (fromCoordinates instanceof UTMCoordinates) { - return toDegreesMinutesSeconds((UTMCoordinates) fromCoordinates, toDatum); - } else if (fromCoordinates instanceof DecimalDegreesCoordinates) { - return toDegreesMinutesSeconds((DecimalDegreesCoordinates) fromCoordinates, toDatum); - } else if (fromCoordinates instanceof DegreesMinutesSecondsCoordinates) { - if (fromCoordinates.getDatum().isEqual(toDatum)) { - return (DegreesMinutesSecondsCoordinates) fromCoordinates; - } - return toDatum((DegreesMinutesSecondsCoordinates) fromCoordinates, toDatum); - } + public static DegreesMinutesSecondsCoordinates toDegreesMinutesSeconds(Coordinates fromCoordinates, Datum toDatum) { + if (fromCoordinates instanceof UTMCoordinates) { + return toDegreesMinutesSeconds((UTMCoordinates) fromCoordinates, toDatum); + } else if (fromCoordinates instanceof DecimalDegreesCoordinates) { + return toDegreesMinutesSeconds((DecimalDegreesCoordinates) fromCoordinates, toDatum); + } else if (fromCoordinates instanceof DegreesMinutesSecondsCoordinates) { + if (fromCoordinates.getDatum().isEqual(toDatum)) { + return (DegreesMinutesSecondsCoordinates) fromCoordinates; + } + return toDatum((DegreesMinutesSecondsCoordinates) fromCoordinates, toDatum); + } - throw new IllegalArgumentException("The passed coordinate is an unknown implementation of Coordinates: [" + - fromCoordinates.getClass().getName() + "]"); - } + throw new IllegalArgumentException("The passed coordinate is an unknown implementation of Coordinates: [" + + fromCoordinates.getClass().getName() + "]"); + } public static DegreesMinutesSecondsCoordinates toDMS(UTMCoordinates fromCoordinates) { return toDegreesMinutesSeconds(fromCoordinates); @@ -143,29 +143,38 @@ public static DegreesMinutesSecondsCoordinates toDMS(DecimalDegreesCoordinates f // ************************************************************************ public static UTMCoordinates toDatum(UTMCoordinates coordinates, Datum datum) { + if (coordinates.getDatum() == datum) { + return coordinates; + } return new UTMCoordinates(new DecimalDegreesCoordinates(coordinates).convertDatum(datum)); } public static DecimalDegreesCoordinates toDatum(DecimalDegreesCoordinates coordinates, Datum datum) { + if (coordinates.getDatum() == datum) { + return coordinates; + } return coordinates.convertDatum(datum); } public static DegreesMinutesSecondsCoordinates toDatum(DegreesMinutesSecondsCoordinates coordinates, Datum datum) { + if (coordinates.getDatum() == datum) { + return coordinates; + } return new DegreesMinutesSecondsCoordinates(new DecimalDegreesCoordinates(coordinates).convertDatum(datum)); } - public static Coordinates toDatum(Coordinates coordinates, Datum datum) { - if (coordinates instanceof UTMCoordinates) { - return toDatum((UTMCoordinates) coordinates, datum); - } else if (coordinates instanceof DecimalDegreesCoordinates) { - return toDatum((DecimalDegreesCoordinates) coordinates, datum); - } else if (coordinates instanceof DegreesMinutesSecondsCoordinates) { - return toDatum((DegreesMinutesSecondsCoordinates) coordinates, datum); - } + public static Coordinates toDatum(Coordinates coordinates, Datum datum) { + if (coordinates instanceof UTMCoordinates) { + return toDatum((UTMCoordinates) coordinates, datum); + } else if (coordinates instanceof DecimalDegreesCoordinates) { + return toDatum((DecimalDegreesCoordinates) coordinates, datum); + } else if (coordinates instanceof DegreesMinutesSecondsCoordinates) { + return toDatum((DegreesMinutesSecondsCoordinates) coordinates, datum); + } - throw new IllegalArgumentException("The passed coordinate is an unknown implementation of Coordinates: [" + - coordinates.getClass().getName() + "]"); - } + throw new IllegalArgumentException("The passed coordinate is an unknown implementation of Coordinates: [" + + coordinates.getClass().getName() + "]"); + } // ************************************************************************ diff --git a/src/test/java/br/com/skywalker/coordinateconverter/CoordinatesConverterTest.java b/src/test/java/br/com/skywalker/coordinateconverter/CoordinatesConverterTest.java index f97c6ca..51c2123 100644 --- a/src/test/java/br/com/skywalker/coordinateconverter/CoordinatesConverterTest.java +++ b/src/test/java/br/com/skywalker/coordinateconverter/CoordinatesConverterTest.java @@ -11,7 +11,25 @@ public void testDDToDMS() { DecimalDegreesCoordinates coordinates = new DecimalDegreesCoordinates(-46.1795700000, -23.5151100000, Datums.SAD69); assertEquals("46° 10' 46.4520\" S 23° 30' 54.3960\" W", - CoordinatesConverter.toDMS(coordinates).toString()); + CoordinatesConverter.toDMS(coordinates).toString().replace(",", ".")); + } + + @Test + public void testDDToDMS1() { + DecimalDegreesCoordinates coordinates = new DecimalDegreesCoordinates(-7, -37, Datums.SAD69); + DegreesMinutesSecondsCoordinates degreesMinutesSecondsCoordinates = CoordinatesConverter.toDMS(coordinates); + assertEquals("7° 0' 0.0000\" S 37° 0' 0.0000\" W", + degreesMinutesSecondsCoordinates.toString().replace(",", ".")); + System.out.println(CoordinatesConverter.toDD(degreesMinutesSecondsCoordinates).toString()); + } + + @Test + public void testDMSToDD() { + DegreesMinutesSecondsCoordinates degreesMinutesSecondsCoordinates = new DegreesMinutesSecondsCoordinates( + Location.SOUTH, 7, 0, 0, Location.WEST, 37, 0, 0, Datums.WGS84); + System.out.println(CoordinatesConverter.toDD(degreesMinutesSecondsCoordinates).toString()); + assertEquals("-7.0000000000 -37.0000000000", + CoordinatesConverter.toDD(degreesMinutesSecondsCoordinates).toString().replace(",", ".")); } @Test @@ -49,5 +67,13 @@ public void testDDToUTMSIRGAS2000() { assertEquals(305861.6300, utmCoordinates.getX(), 0.01); assertEquals(4882853.4451, utmCoordinates.getY(), 0.01); } + + + @Test + public void testUTMToDMS() { + UTMCoordinates utmCoordinates = CoordinatesConverter.toUTM(new DecimalDegreesCoordinates(-7, -37, Datums.WGS84)); + DegreesMinutesSecondsCoordinates degreesMinutesSecondsCoordinates = CoordinatesConverter.toDegreesMinutesSeconds(utmCoordinates); + System.out.println(degreesMinutesSecondsCoordinates.toString()); + } }