Newer
Older
adjustgpx-core / src / osm / jp / gpx / Complementation.java
@haya4 haya4 on 18 Aug 2019 3 KB Java11 - AdjustTime2
  1. package osm.jp.gpx;
  2.  
  3. import java.text.ParseException;
  4.  
  5. public class Complementation {
  6. public static final Double R = (6378137D + 6356752.314D)/2D; // 6367444.657m
  7. //public static final double dLat = 0.00453D; // 1km距離を表す緯度(差分)
  8. //public static final double dLon = 0.005588D; // 1km距離を表す経度(差分)
  9. public TagTrkpt imaTag = null;
  10. public TagTrkpt maeTag = null;
  11. public static boolean param_GpxOutputSpeed = false;
  12. public static boolean param_GpxOverwriteMagvar = false;
  13. /**
  14. * @param imaE
  15. * @param maeE
  16. * @code{
  17. * <trkpt lat="34.976635" lon="138.466228">
  18. * <ele>267.291</ele>
  19. * <magvar>359</magvar>
  20. * <speed></speed>
  21. * <time>2016-07-02T08:25:18Z</time>
  22. * </trkpt>
  23. * }
  24. *
  25. *
  26. * @throws ParseException
  27. */
  28. public Complementation(TagTrkpt imaE, TagTrkpt maeE) throws ParseException {
  29. this.imaTag = new TagTrkpt(imaE.trkpt);
  30. if (maeE != null) {
  31. this.maeTag = new TagTrkpt(maeE.trkpt);
  32. }
  33. }
  34. /**
  35. * 緯度・経度と時間差から速度(km/h)を求める
  36. *
  37. */
  38. public void complementationSpeed() {
  39. if (imaTag.speedStr != null) {
  40. try {
  41. Double.parseDouble(imaTag.speedStr);
  42. }
  43. catch (NumberFormatException e) {
  44. // 数字以外なら<speed>エレメントを削除する
  45. imaTag.removeElement("speed");
  46. imaTag.speedStr = null;
  47. }
  48. }
  49. if (imaTag.speedStr == null) {
  50. double d = GeoDistance.calcDistHubeny(imaTag.lat, imaTag.lon, maeTag.lat, maeTag.lon);
  51. String str = Double.toString((d * 3600) / (imaTag.time.getTime() - maeTag.time.getTime()));
  52. int iDot = str.indexOf('.');
  53. if (iDot > 0) {
  54. str = str.substring(0, iDot+2);
  55. }
  56. imaTag.appendElement("speed", str);
  57. imaTag.speedStr = str;
  58. }
  59. }
  60.  
  61. /**
  62. * 経度(longitude)と経度から進行方向を求める
  63. * @throws ParseException
  64. */
  65. public void complementationMagvar() throws ParseException {
  66. if (imaTag.magvarStr != null) {
  67. try {
  68. Double.parseDouble(imaTag.magvarStr);
  69. }
  70. catch (NumberFormatException e) {
  71. // 数字以外なら<magvar>エレメントを削除する
  72. imaTag.removeElement("magvar");
  73. imaTag.magvarStr = null;
  74. }
  75. }
  76. if (imaTag.magvarStr == null) {
  77. Double r = Math.cos(Math.toRadians((imaTag.lat + maeTag.lat) / 2)) * R;
  78. Double x = Math.toRadians(imaTag.lon - maeTag.lon) * r;
  79. Double y = Math.toRadians(imaTag.lat - maeTag.lat) * R;
  80. double rad = Math.toDegrees(Math.atan2(y, x));
  81. if (y >= 0) {
  82. if (x >= 0) {
  83. rad = 0 - (rad - 90);
  84. }
  85. else {
  86. rad = 360 - (rad - 90);
  87. }
  88. }
  89. else {
  90. if (x >= 0) {
  91. rad = 90 - rad;
  92. }
  93. else {
  94. rad = 90 - rad;
  95. }
  96. }
  97.  
  98. String str = Double.toString(rad);
  99. int iDot = str.indexOf('.');
  100. if (iDot > 0) {
  101. str = str.substring(0, iDot);
  102. }
  103. imaTag.appendElement("magvar", str);
  104. imaTag.magvarStr = str;
  105. }
  106. }
  107. }