LocationUtils.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package cn.com.taiji.utils;
  2. import cn.com.taiji.entity.Location;
  3. import cn.hutool.core.date.DateUnit;
  4. import cn.hutool.core.date.DateUtil;
  5. import java.math.BigDecimal;
  6. import java.util.ArrayList;
  7. import java.util.Date;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. /**
  11. * @author chenfangchao
  12. * @title: LocationUtils
  13. * @projectName es-track-analysis
  14. * @description: TODO
  15. * @date 2023/2/7 5:40 PM
  16. */
  17. public class LocationUtils {
  18. public static List<Location> generateLogLat(String startTime, String endTime, Double startLog, Double startLat, Double endLog, Double endLat) {
  19. List<Location> list = new ArrayList<>();
  20. try {
  21. final int seconds = 1000;
  22. //经纬度差
  23. Double difflog = Math.abs(startLog - endLog);
  24. Double difflat = Math.abs(startLat - endLat);
  25. //相差秒数
  26. long diffSeconds = diffSeconds(startTime, endTime);
  27. //平均经纬度差
  28. Double avgLog = difflog / diffSeconds;
  29. Double avgLat = difflat / diffSeconds;
  30. //开始时间时间戳
  31. long startTimeStamp = dateToStamp(startTime);
  32. long endTimeStamp = dateToStamp(endTime);
  33. Double resLog = startLog;
  34. Double resLat = startLat;
  35. long startTimeStampNew = startTimeStamp + seconds;
  36. //判断时间大小关系
  37. if (startTimeStampNew < endTimeStamp) {
  38. for (long i = startTimeStamp + seconds; i < endTimeStamp; i += seconds) {
  39. resLog = avgLog + resLog;
  40. resLat = avgLat + resLat;
  41. double bdLog = doubleDown(resLog);
  42. double bdLat = doubleDown(resLat);
  43. Location location = new Location(bdLog,bdLat);
  44. list.add(location);
  45. }
  46. } else {
  47. for (long i = endTimeStamp + seconds; i < startTimeStampNew; i += seconds) {
  48. resLog = avgLog + resLog;
  49. resLat = avgLat + resLat;
  50. double bdLog = doubleDown(resLog);
  51. double bdLat = doubleDown(resLat);
  52. Location location = new Location(bdLog,bdLat);
  53. list.add(location);
  54. }
  55. }
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. }
  59. return list;
  60. }
  61. public static double doubleDown(Double data) {
  62. try {
  63. BigDecimal bd = new BigDecimal(data);
  64. double res = bd.setScale(7, BigDecimal.ROUND_DOWN).doubleValue();
  65. return res;
  66. } catch (Exception e) {
  67. e.printStackTrace();
  68. }
  69. return 0;
  70. }
  71. public static long dateToStamp(String time) {
  72. Date date = DateUtil.parse(time);
  73. long ts = date.getTime();
  74. return ts;
  75. }
  76. public static long diffSeconds(String startTime,String endTime){
  77. Date startDate= DateUtil.parse(startTime);
  78. Date endDate=DateUtil.parse(endTime);
  79. long second=DateUtil.between(startDate,endDate, DateUnit.SECOND);
  80. return second;
  81. }
  82. }