diff --git a/src/main/java/br/com/skywalker/coordinateconverter/CoordinatesConverter.java b/src/main/java/br/com/skywalker/coordinateconverter/CoordinatesConverter.java index 822c8ce..f1ec42a 100644 --- a/src/main/java/br/com/skywalker/coordinateconverter/CoordinatesConverter.java +++ b/src/main/java/br/com/skywalker/coordinateconverter/CoordinatesConverter.java @@ -21,6 +21,23 @@ 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 toDD(UTMCoordinates fromCoordinates) { return toDecimalDegree(fromCoordinates); } @@ -37,6 +54,8 @@ public static DecimalDegreesCoordinates toDD(DegreesMinutesSecondsCoordinates fr return toDecimalDegree(fromCoordinates, toDatum); } + // ************************************************************************ + public static UTMCoordinates toUTM(DecimalDegreesCoordinates fromCoordinates) { return new UTMCoordinates(fromCoordinates); } @@ -53,6 +72,24 @@ 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); + } + + 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)); } @@ -71,6 +108,22 @@ 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); + } + + throw new IllegalArgumentException("The passed coordinate is an unknown implementation of Coordinates: [" + + fromCoordinates.getClass().getName() + "]"); + } + public static DegreesMinutesSecondsCoordinates toDMS(UTMCoordinates fromCoordinates) { return toDegreesMinutesSeconds(fromCoordinates); } @@ -101,6 +154,19 @@ public static DegreesMinutesSecondsCoordinates toDatum(DegreesMinutesSecondsCoor 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); + } + + throw new IllegalArgumentException("The passed coordinate is an unknown implementation of Coordinates: [" + + coordinates.getClass().getName() + "]"); + } + // ************************************************************************ public static UTMCoordinates toHemisphere(UTMCoordinates coordinates, Hemisphere hemisphere) {