Java BigDecimal Create getDecimalCoords(Integer degrees, Integer minutes, Integer seconds)

Here you can find the source of getDecimalCoords(Integer degrees, Integer minutes, Integer seconds)

Description

Convert DMS format coordinate into decimal degrees, where W and S are indicated by negative degrees.

License

Apache License

Parameter

Parameter Description
degrees a parameter
minutes a parameter
seconds a parameter

Declaration

public static Double getDecimalCoords(Integer degrees, Integer minutes, Integer seconds)
        throws NumberFormatException 

Method Source Code

//package com.java2s;
/**//  w w  w .  j a  v a2  s .  c  o m
 * Copyright 2010 Peter Brewer and Daniel Murphy
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 *   
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.math.BigDecimal;

public class Main {
    /**
     * Convert DMS format coordinate into decimal degrees, where W and S are
     * indicated by negative degrees.
     * 
     * @param degrees
     * @param minutes
     * @param seconds
     * @return
     */
    public static Double getDecimalCoords(Integer degrees, Integer minutes, Integer seconds)
            throws NumberFormatException {
        Double deg = null;
        Double min = null;
        Double sec = null;

        if (degrees != null) {
            deg = Double.valueOf(degrees);
        }

        if (minutes != null) {
            min = Double.valueOf(minutes);
        }

        if (seconds != null) {
            sec = Double.valueOf(minutes);
        }

        return getDecimalCoords(deg, min, sec);
    }

    /**
     * Convert DMS format coordinate into decimal degrees, where W and S are
     * indicated by negative degrees.
     * 
     * @param degrees
     * @param minutes
     * @param seconds
     * @return
     */
    public static Double getDecimalCoords(Double degrees, Double minutes, Double seconds)
            throws NumberFormatException {
        Double coords = 0.0;
        Integer significantFigures = 0;

        if (degrees != null) {
            if (degrees <= 180.0 && degrees >= -180.0) {
                coords = degrees;
            } else {
                throw new NumberFormatException("Degrees out of bounds");
            }
        } else {
            throw new NumberFormatException("Degrees cannot be null in a coordinate");
        }

        if (minutes != null) {
            significantFigures = 4;
            if (minutes >= 0.0 && minutes < 60.0) {
                coords = coords + Double.valueOf(minutes) / 60.0;
            } else {
                throw new NumberFormatException("Minutes out of bounds");
            }
        }

        if (seconds != null) {
            significantFigures = 6;
            if (seconds >= 0.0 && seconds < 60.0) {
                Double secpart = ((Double.valueOf(seconds) / 60.0) / 60.0);
                coords = coords + secpart;
            } else {
                throw new NumberFormatException("Seconds out of bounds");
            }
        }

        BigDecimal bd = new BigDecimal(coords);
        bd = bd.setScale(significantFigures, BigDecimal.ROUND_CEILING);
        return bd.doubleValue();
    }

    /**
     * Convert DMS with NSEW sign into decimal coordinates
     * 
     * @param sign
     * @param degrees
     * @param minutes
     * @param seconds
     * @return
     */
    public static Double getDecimalCoords(String sign, Integer degrees, Integer minutes, Integer seconds)
            throws NumberFormatException {
        Double coords = getDecimalCoords(degrees, minutes, seconds);

        if (sign.equalsIgnoreCase("S") || sign.equalsIgnoreCase("W")) {
            coords = 0 - coords;
            return coords;
        } else if (sign.equalsIgnoreCase("N") || sign.equalsIgnoreCase("E")) {
            return coords;
        }

        throw new NumberFormatException(
                "Coordinate direction must be one of N,S,E or W, but direction was '" + sign + "'");

    }

    /**
     * Convert DMS with NSEW sign into decimal coordinates
     * 
     * @param sign
     * @param degrees
     * @param minutes
     * @param seconds
     * @return
     */
    public static Double getDecimalCoords(String sign, Double degrees, Double minutes, Double seconds)
            throws NumberFormatException {
        Double coords = getDecimalCoords(degrees, minutes, seconds);

        sign = sign.trim();

        if (sign.equalsIgnoreCase("S") || sign.equalsIgnoreCase("W")) {
            coords = 0 - coords;
            return coords;
        } else if (sign.equalsIgnoreCase("N") || sign.equalsIgnoreCase("E")) {
            return coords;
        }

        throw new NumberFormatException(
                "Coordinate direction must be one of N,S,E or W, but direction was '" + sign + "'");

    }
}

Related

  1. getBigDecimalValue(Object o)
  2. getDecimal(double a)
  3. getDecimal(Object[] data, int ordinal)
  4. getDecimal(String clusive, int[] currentDecimal)
  5. getDecimal(String number, BigDecimal def)
  6. getDecimalValue(String value, Object franctionDigits)
  7. getDecimalValues(String[] values, String dataType)
  8. nullToBigDecimalZero(final Object obj)
  9. nullToZero(BigDecimal decimal)