Java Distance Calculate distance_Sphere(double rlon1, double rlat1, double rlon2, double rlat2)

Here you can find the source of distance_Sphere(double rlon1, double rlat1, double rlon2, double rlat2)

Description

Calculates the distance traveled along a sphere (great circle distance)

License

BSD License

Parameter

Parameter Description
rlon1 - The longitude of origin
rlat1 - The latitude of origin
rlon2 - The destination longitude
rlat2 - The destination latitude.

Return

- Distance traveled in meters.

Declaration


public static double distance_Sphere(double rlon1, double rlat1, double rlon2, double rlat2) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2014 Geoscience Australia (www.ga.gov.au)
 * @author - Johnathan Kool (Geoscience Australia)
 * /*from  ww w .j a  v  a2 s .  c  om*/
 * Licensed under the BSD-3 License
 * 
 * http://opensource.org/licenses/BSD-3-Clause
 *  
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *  
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the copyright holder nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software 
 *     without specific prior written permission.
 *  
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 ******************************************************************************/

public class Main {
    private final static double R_EARTH = 6371009;

    /**
     * Calculates the distance traveled along a sphere (great circle distance)
     * 
     * @param rlon1 -
     *            The longitude of origin
     * @param rlat1 -
     *            The latitude of origin
     * @param rlon2 -
     *            The destination longitude
     * @param rlat2 -
     *            The destination latitude.
     * @return - Distance traveled in meters.
     */

    public static double distance_Sphere(double rlon1, double rlat1, double rlon2, double rlat2) {

        double rln1, rlt1, rln2, rlt2;
        double dist;

        rln1 = Math.toRadians(rlon1);
        rlt1 = Math.toRadians(rlat1);
        rln2 = Math.toRadians(rlon2);
        rlt2 = Math.toRadians(rlat2);

        double d_lambda = Math.abs(rln1 - rln2);

        // Simple great circle distance

        // dist = Math.acos(Math.cos(rlt1) * Math.cos(rlt2) * Math.cos(rln2 -
        // rln1)
        // + Math.sin(rlt1) * Math.sin(rlt2));

        // More complex great circle distance formula to reduce error due to
        // rounding - from Wikipedia http://en.wikipedia.org/wiki/Great-circle_distance

        double n1 = Math.pow(Math.cos(rlt2) * Math.sin(d_lambda), 2);
        double n2 = Math
                .pow(Math.cos(rlt1) * Math.sin(rlt2) - (Math.sin(rlt1) * Math.cos(rlt2) * Math.cos(d_lambda)), 2);
        double numerator = Math.sqrt(n1 + n2);
        double denominator = Math.sin(rlt1) * Math.sin(rlt2) + Math.cos(rlt1) * Math.cos(rlt2) * Math.cos(d_lambda);
        dist = Math.atan2(numerator, denominator);

        return 2 * Math.PI * R_EARTH * Math.toDegrees(dist) / 360;
    }
}

Related

  1. distance2d(double x1, double y1, double x2, double y2)
  2. distance2D(double x1, double z1, double x2, double z2)
  3. distance2points3D(double[] p1, double[] p2)
  4. distance3d(double x1, double y1, double z1, double x2, double y2, double z2)
  5. distance3Sq(double x1, double y1, double z1, double x2, double y2, double z2)
  6. distance_to_endpoint(int x1, int y1, int x2, int y2, int x, int y)
  7. distance_to_endpoint(int x1, int y1, int x2, int y2, int x, int y)
  8. distance_to_line(double x1, double y1, double x2, double y2, double x3, double y3)
  9. distanceAbs(final int a, final int b)