Java Distance Calculate distance(double latDegrees1, double lonDegrees1, double latDegrees2, double lonDegrees2)

Here you can find the source of distance(double latDegrees1, double lonDegrees1, double latDegrees2, double lonDegrees2)

Description

distance

License

Open Source License

Declaration

public static double distance(double latDegrees1, double lonDegrees1, double latDegrees2, double lonDegrees2) 

Method Source Code

//package com.java2s;

public class Main {
    public static final double c_wgs84_a = 6378137.0;
    public static final double c_wgs84_e2 = 0.00669437999013;

    public static double distance(double latDegrees1, double lonDegrees1, double latDegrees2, double lonDegrees2) {
        double[] offsets = WGS84displacement(latDegrees1, lonDegrees1, 0, latDegrees2, lonDegrees2, 0);
        return Math.hypot(offsets[0], offsets[1]);
    }/*from   ww  w.  jav  a2 s. c om*/

    /**
     * Copied from Dune
     * 
     * @param loc1
     * @param loc2
     * @return
     */
    public static double[] WGS84displacement(double latDegrees1, double lonDegrees1, double depth1,
            double latDegrees2, double lonDegrees2, double depth2) {

        double cs1[];
        double cs2[];

        cs1 = toECEF(latDegrees1, lonDegrees1, depth1);
        cs2 = toECEF(latDegrees2, lonDegrees2, depth2);

        double ox = cs2[0] - cs1[0];
        double oy = cs2[1] - cs1[1];
        double oz = cs2[2] - cs1[2];
        double[] lld1 = { latDegrees1, lonDegrees1, depth1 };

        double slat = Math.sin(Math.toRadians(lld1[0]));
        double clat = Math.cos(Math.toRadians(lld1[0]));
        double slon = Math.sin(Math.toRadians(lld1[1]));
        double clon = Math.cos(Math.toRadians(lld1[1]));

        double[] ret = new double[3];

        ret[0] = -slat * clon * ox - slat * slon * oy + clat * oz; // North
        ret[1] = -slon * ox + clon * oy; // East
        ret[2] = depth1 - depth2;

        return ret;
    }

    /**
     * Copied from Dune
     * 
     * @param loc
     * @return
     */
    private static double[] toECEF(double latDegrees, double lonDegrees, double depth) {

        double lld[] = { latDegrees, lonDegrees, depth };

        lld[0] = Math.toRadians(lld[0]);
        lld[1] = Math.toRadians(lld[1]);

        double cos_lat = Math.cos(lld[0]);
        double sin_lat = Math.sin(lld[0]);
        double cos_lon = Math.cos(lld[1]);
        double sin_lon = Math.sin(lld[1]);
        double rn = c_wgs84_a / Math.sqrt(1.0 - c_wgs84_e2 * sin_lat * sin_lat);
        double[] ned = new double[3];
        ned[0] = (rn - lld[2]) * cos_lat * cos_lon;
        ned[1] = (rn - lld[2]) * cos_lat * sin_lon;
        ned[2] = (((1.0 - c_wgs84_e2) * rn) - lld[2]) * sin_lat;

        return ned;
    }
}

Related

  1. distance(double lat1, double lon1, double lat2, double lon2)
  2. distance(double lat1, double lon1, double lat2, double lon2)
  3. distance(double lat1, double lon1, double lat2, double lon2)
  4. distance(double lat1, double lon1, double lat2, double lon2)
  5. distance(double latA, double logA, double latB, double logB)
  6. distance(double latitude1, double longitude1, double latitude2, double longitude2)
  7. distance(double lon1, double lat1, double lon2, double lat2)
  8. distance(double u, double v, double pu, double pv)
  9. distance(double w1, double h1, double w2, double h2, int type)