Browse Source

拥堵路段 距离计算

develop
王兴琳 5 months ago
parent
commit
a4ed41d14b
  1. 67
      zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java

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

@ -43,6 +43,9 @@ import java.util.regex.Pattern;
@Service
public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsService {
// 地球平均半径,单位为公里
private static final double EARTH_RADIUS = 6371.0;
// 日志记录器
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
@ -928,8 +931,37 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
Integer asOneRoad = jsonObject.getInteger("as_one_road");
if (asOneRoad == UniversalEnum.ONE.getNumber()){
String string = jsonObject.getString("road_name");
System.out.println(string);
String string = jsonObject.getString("list_link_info");
JSONArray jsonlist = JSON.parseArray(string);
JSONObject o = (JSONObject) jsonlist.get(0);
String string1 = o.getString("firstPoint");
String string2 = o.getString("lastPoint");
String lngLats = o.getString("lngLats");
String[] lngLatsString = lngLats.split(";");
double s = Double.parseDouble(lngLatsString[0].split(",")[0]);
double s2 = Double.parseDouble(lngLatsString[0].split(",")[1]);
double s3 = Double.parseDouble(lngLatsString[lngLatsString.length-1].split(",")[0]);
double s4 = Double.parseDouble(lngLatsString[lngLatsString.length-1].split(",")[1]);
double distanceM = calculateDistance(s2, s, s4, s3)* 1000;
// System.out.printf("最大距离为: %.2f 米%n", distanceM);
jsonObject.put("max_jam_dist",distanceM);//最大拥堵距离
String[] split = string1.split(",");
double firstLon = Double.parseDouble(split[0]);
double firstLat = Double.parseDouble(split[1]);
String[] split2 = string2.split(",");
double lastLon = Double.parseDouble(split2[0]);
double lastLat = Double.parseDouble(split2[1]);
//double firstLat = 35.38524527319016;
// double firstLon = 118.39808642864227;
//double lastLat = 35.386351346969604;
// double lastLon = 118.4038907289505;
double distanceKm = calculateDistance(firstLat, firstLon, lastLat, lastLon);
double distanceMeters = distanceKm * 1000; // 将距离转换为米
//System.out.printf("两点之间的距离为: %.2f 米%n", distanceMeters);
jsonObject.put("jam_dist",distanceMeters);//当前拥堵距离
return jsonObject;
}else {
@ -942,7 +974,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
}
public DcFacility findNearestFacility(List<DcFacility> dcFacilityList, String stakeMarkCode) {
// 将目标桩号转换为整数,以便比较
// 将目标桩号转换为整数
int targetCodeAsInt = Integer.parseInt(extract(stakeMarkCode));
DcFacility nearestFacility = null;
@ -950,11 +982,9 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
for (DcFacility facility : dcFacilityList) {
// 假设每个DcFacility对象中有一个方法或直接字段可以获取处理后的桩号字符串
String stakeMark = facility.getStakeMark(); // 请替换为实际的getter方法名
String stakeMark = facility.getStakeMark();
// 将当前设施的桩号转换为整数
int currentCodeAsInt = Integer.parseInt(extract(stakeMark));
// 计算与目标桩号的距离(差值的绝对值)
int distance = Math.abs(targetCodeAsInt - currentCodeAsInt);
@ -967,5 +997,30 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
return nearestFacility; // 返回最近的桩号信息
}
//经纬度计算距离
public static double calculateDistance(double startLat, double startLon, double endLat, double endLon) {
// 将角度转换为弧度
double startLatRad = Math.toRadians(startLat);
double startLonRad = Math.toRadians(startLon);
double endLatRad = Math.toRadians(endLat);
double endLonRad = Math.toRadians(endLon);
// 经纬度之差
double dLat = endLatRad - startLatRad;
double dLon = endLonRad - startLonRad;
// Haversine公式
double a = Math.pow(Math.sin(dLat / 2), 2) +
Math.cos(startLatRad) * Math.cos(endLatRad) *
Math.pow(Math.sin(dLon / 2), 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
// 计算两点间的距离
double distance = EARTH_RADIUS * c;
return distance;
}
}

Loading…
Cancel
Save