Browse Source

首頁路况 排序

develop
王兴琳 4 months ago
parent
commit
eb46b186dd
  1. 99
      zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java

99
zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java

@ -1157,23 +1157,59 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
double startPileNo = jsonObject.getDouble("start_pile_no");
String stakeMark = formatNumber(startPileNo);
jsonObject.put("stakeMark", stakeMark);//
//as_one_road
//是否为同一条路,1-是,0-否
if (asOneRoad == UniversalEnum.ZERO.getNumber()) {
String link_lng_lats = jsonObject.getString("link_lng_lats");
System.out.println("排序前:"+link_lng_lats);
String string = jsonObject.getString("list_link_info");
JSONArray jsonlist = JSON.parseArray(string);
JSONObject o = (JSONObject) jsonlist.get(UniversalEnum.ZERO.getNumber());
String string1 = o.getString("firstPoint");
String[] split = string1.split(",");
double firstLon = Double.parseDouble(split[UniversalEnum.ZERO.getNumber()]);
double firstLat = Double.parseDouble(split[UniversalEnum.ONE.getNumber()]);
String string2 = o.getString("lastPoint");
String lngLats = o.getString("lngLats");
String[] lngLatsString = lngLats.split(";");
double s = Double.parseDouble(lngLatsString[UniversalEnum.ZERO.getNumber()].split(",")[UniversalEnum.ZERO.getNumber()]);
double s2 = Double.parseDouble(lngLatsString[UniversalEnum.ZERO.getNumber()].split(",")[UniversalEnum.ONE.getNumber()]);
double s3 = Double.parseDouble(lngLatsString[lngLatsString.length - UniversalEnum.ONE.getNumber()].split(",")[UniversalEnum.ZERO.getNumber()]);
double s4 = Double.parseDouble(lngLatsString[lngLatsString.length - UniversalEnum.ONE.getNumber()].split(",")[UniversalEnum.ONE.getNumber()]);
List<Location> locations = new ArrayList<>();
for (String s1 : lngLatsString) {
String[] s2 = s1.split(",");
locations.add(new Location(Double.parseDouble(s2[1]), Double.parseDouble(s2[0]))); // 示例地点1
}
double fixedLat = firstLat; // Los Angeles的纬度
System.out.println("Lat Angeles的纬度"+fixedLat);
double fixedLon = firstLon; // Los Angeles的经度
System.out.println("Lon Angeles的纬度"+fixedLon);
// 根据距离固定点的远近排序
// Collections.sort(locations, Comparator.comparingDouble(location -> haversineDistance(fixedLat, fixedLon, location.latitude, location.longitude)));
// 按照经度升序排序坐标数组
Collections.sort(locations, Comparator.comparingDouble(location ->location.getLongitude()));
// 将排序后的坐标重新格式化为原始的字符串格式
StringBuilder sortedLngLatsBuilder = new StringBuilder();
// 输出排序后的地点及其距离
for (int i = 0; i < locations.size(); i++) {
Location loc = locations.get(i);
if (i > 0) {
sortedLngLatsBuilder.append(";");
}
sortedLngLatsBuilder.append(loc.longitude)
.append(",")
.append(loc.latitude);
// System.out.println("Location " + (i + 1) + ": (" + loc.latitude + ", " + loc.longitude + ") - Distance from Los Angeles: " + haversineDistance(fixedLat, fixedLon, loc.latitude, loc.longitude) + " km");
}
String sortedLngLats = sortedLngLatsBuilder.toString();
jsonObject.put("link_lng_lats",sortedLngLats);
System.out.println("排序后的坐标字符串:" + sortedLngLats);
//as_one_road
//是否为同一条路,1-是,0-否
if (asOneRoad == UniversalEnum.ZERO.getNumber()) {
double s = locations.get(UniversalEnum.ZERO.getNumber()).longitude;
double s2 =locations.get(UniversalEnum.ZERO.getNumber()).latitude ;
double s3 = locations.get(locations.size()-UniversalEnum.ONE.getNumber()).longitude;
double s4 = locations.get(locations.size()-UniversalEnum.ONE.getNumber()).latitude;
double distanceM = calculateDistance(s2, s, s4, s3);
// System.out.printf("最大距离为: %.2f 米%n", distanceM);
jsonObject.put("max_jam_dist", distanceM);//最大拥堵距离
@ -1181,9 +1217,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
String endmark = formatNumber(endMake);
jsonObject.put("endMark", endmark);//结束桩号
String[] split = string1.split(",");
double firstLon = Double.parseDouble(split[UniversalEnum.ZERO.getNumber()]);
double firstLat = Double.parseDouble(split[UniversalEnum.ONE.getNumber()]);
String[] split2 = string2.split(",");
double lastLon = Double.parseDouble(split2[UniversalEnum.ZERO.getNumber()]);
double lastLat = Double.parseDouble(split2[UniversalEnum.ONE.getNumber()]);
@ -1353,5 +1387,46 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
return distance;
}
static class Location {
double latitude;
double longitude;
// 构造函数
public Location(double latitude, double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
// Getter 和 Setter(可选)
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
}
// 使用哈弗辛公式计算两点之间的距离(单位:千米)
static double haversineDistance(double lat1, double lon1, double lat2, double lon2) {
final int R = 6371; // 地球半径,单位千米
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
lat1 = Math.toRadians(lat1);
lat2 = Math.toRadians(lat2);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c;
}
}

Loading…
Cancel
Save