Newer
Older
restamp / src / main / java / osm / surveyor / util / GeoDistance.java
@haya4 haya4 on 26 Jan 2020 1 KB Restamp
  1. package osm.surveyor.util;
  2.  
  3. /**
  4. * The MIT License (MIT)
  5. * Copyright(C) 2007-2012 やまだらけ
  6. * http://yamadarake.jp/trdi/report000001.html
  7. * 「Cords.java」を改変
  8. * 2016-10-03
  9. *
  10. * @author やまだらけ yama_darake@yahoo.co.jp
  11. *
  12. */
  13. public class GeoDistance {
  14.  
  15. public static final double GRS80_A = 6378137.000; // 赤道半径(m)
  16. public static final double GRS80_E2 = 0.00669438002301188;
  17. public static final double GRS80_MNUM = 6335439.32708317; //
  18.  
  19. public static final double WGS84_A = 6378137.000;
  20. public static final double WGS84_E2 = 0.00669437999019758;
  21. public static final double WGS84_MNUM = 6335439.32729246;
  22.  
  23. /**
  24. * 角度(180度)をラジアン(2π)に変換する
  25. * @param deg
  26. * @return
  27. */
  28. public static double deg2rad(double deg){
  29. return deg * Math.PI / 180.0;
  30. }
  31.  
  32. /**
  33. * 距離(m)を返す
  34. * @param lat1
  35. * @param lng1
  36. * @param lat2
  37. * @param lng2
  38. * @return
  39. */
  40. public static double calcDistHubeny(double lat1, double lng1,
  41. double lat2, double lng2){
  42. double my = deg2rad((lat1 + lat2) / 2.0); // 平均緯度
  43. double dy = deg2rad(lat1 - lat2); // 2点間の緯度
  44. double dx = deg2rad(lng1 - lng2); // 2点間の経度
  45.  
  46. double sin = Math.sin(my);
  47. double w = Math.sqrt(1.0 - GRS80_E2 * sin * sin);
  48. double m = GRS80_MNUM / (w * w * w);
  49. double n = GRS80_A / w;
  50. double dym = dy * m;
  51. double dxncos = dx * n * Math.cos(my);
  52.  
  53. return Math.sqrt(dym * dym + dxncos * dxncos);
  54. }
  55.  
  56.  
  57. public static void main(String[] args){
  58. System.out.println("Coords Test Program");
  59. double lat1, lng1, lat2, lng2;
  60.  
  61. lat1 = Double.parseDouble(args[0]);
  62. lng1 = Double.parseDouble(args[1]);
  63. lat2 = Double.parseDouble(args[2]);
  64. lng2 = Double.parseDouble(args[3]);
  65.  
  66. double d = calcDistHubeny(lat1, lng1, lat2, lng2);
  67.  
  68. System.out.println("Distance = " + d + " m");
  69. }
  70. }