Browse Source

拥堵路段 距离计算

develop
王兴琳 5 months ago
parent
commit
55aaf21347
  1. 157
      ruoyi-ui/src/views/text/index.vue
  2. 67
      zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java

157
ruoyi-ui/src/views/text/index.vue

@ -0,0 +1,157 @@
<!--<template>-->
<!-- <div>-->
<!-- WebSocket Test-->
<!-- </div>-->
<!--</template>-->
<!--<script>-->
<!--import {mapState} from "vuex";-->
<!--const synth = window.speechSynthesis // -->
<!--const msg = new SpeechSynthesisUtterance()-->
<!--export default {-->
<!-- name: 'WebSocketTest',-->
<!-- data() {-->
<!-- return {-->
<!-- ws: null, // WebSocket-->
<!-- name:'', // WebSocket-->
<!-- // WebSocket-->
<!-- socketConfig: {-->
<!-- password: 'zc&ws123',-->
<!-- // tokenSN: this.token, // -->
<!-- heartRate: 1000, // -->
<!-- },-->
<!-- };-->
<!-- },-->
<!-- computed: {-->
<!-- ...mapState({-->
<!-- token: state => state.user.token,-->
<!-- websocket: state => state.user.websocket-->
<!-- })-->
<!-- },-->
<!-- created() {-->
<!-- this.initWebSocket();-->
<!-- },-->
<!-- beforeDestroy() {-->
<!-- if (this.ws) {-->
<!-- this.ws.close();-->
<!-- }-->
<!-- },-->
<!-- methods: {-->
<!-- // -->
<!-- handleSpeak(text) {-->
<!-- msg.text = text // -->
<!-- msg.lang = 'zh-CN' // 使:-->
<!-- msg.volume = 1 // 1-->
<!-- msg.rate = 1 // 1-->
<!-- msg.pitch = 1 // 1-->
<!-- synth.speak(msg) // -->
<!-- },-->
<!-- // -->
<!-- handleStop(e) {-->
<!-- msg.text = e-->
<!-- msg.lang = 'zh-CN'-->
<!-- synth.cancel(msg) // -->
<!-- },-->
<!-- open2(message){-->
<!-- this.$notify({-->
<!-- title: '提示',-->
<!-- message: message,-->
<!-- duration: 0-->
<!-- });-->
<!-- },-->
<!-- initWebSocket() {-->
<!-- const { password, heartRate ,tokenSN} = this.socketConfig;-->
<!-- const url = 'ws://' + location.hostname + ':7789/ws';-->
<!-- this.ws = new WebSocket(url);-->
<!-- this.ws.onmessage = (message) => {-->
<!-- console.log('WebSocket 收到的数据', message.data);-->
<!-- message = JSON.parse(message.data);-->
<!-- const method = message.method;-->
<!-- if (method !== 'event') {-->
<!-- return;-->
<!-- }-->
<!-- const params = message.params;-->
<!-- const subEvent = params.subEvent;-->
<!-- const content = params.content;-->
<!-- switch (subEvent) {-->
<!-- case '1':-->
<!-- console.log('WebSocket 视频AI:', content);-->
<!-- this.handleSpeak(content) // -->
<!-- break;-->
<!-- case '0':-->
<!-- console.log('WebSocket 手动录入:', content);-->
<!-- console.log('WebSocket 手动录入:', content.event);-->
<!-- console.log('WebSocket 手动录入:', content.content);-->
<!-- this.open2(content.content);-->
<!-- this.handleSpeak(content) // -->
<!-- break;-->
<!-- default:-->
<!-- }-->
<!-- };-->
<!-- // WebSocket-->
<!-- this.ws.onopen = this.onOpen;-->
<!-- this.ws.onerror = this.onError;-->
<!-- this.ws.onclose = this.onClose;-->
<!-- },-->
<!-- onOpen(event) {-->
<!-- console.log('WebSocket opened:', event);-->
<!-- },-->
<!-- onError(event) {-->
<!-- console.error('WebSocket error:', event);-->
<!-- },-->
<!-- onClose(event) {-->
<!-- console.log('WebSocket closed:', event);-->
<!-- },-->
<!-- },-->
<!--};-->
<!--</script>-->
<template>
<div>
<input v-model="inputValue" @input="formatInput" type="text">
<p>格式化后的值: {{ formattedValue }}</p>
</div>
</template>
<script>
export default {
data() {
return {
inputValue: '', //
formattedValue: '' //
};
},
watch: {
inputValue(newValue) {
this.formattedValue = this.formatToThreeDigits(newValue);
},
},
methods: {
formatInput(e) {
//
const value = e.target.value.replace(/[^0-9]/g, '');
if (value.length > 3) {
this.inputValue = value.slice(0, 3); //
} else {
this.inputValue = value;
}
},
formatToThreeDigits(value) {
return value.padStart(3, '0'); // 0
},
},
};
</script>

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

@ -43,6 +43,9 @@ import java.util.regex.Pattern;
@Service @Service
public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsService { public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsService {
// 地球平均半径,单位为公里
private static final double EARTH_RADIUS = 6371.0;
// 日志记录器 // 日志记录器
protected final Logger logger = LoggerFactory.getLogger(this.getClass()); protected final Logger logger = LoggerFactory.getLogger(this.getClass());
@ -928,8 +931,37 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
Integer asOneRoad = jsonObject.getInteger("as_one_road"); Integer asOneRoad = jsonObject.getInteger("as_one_road");
if (asOneRoad == UniversalEnum.ONE.getNumber()){ 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; return jsonObject;
}else { }else {
@ -942,7 +974,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
} }
public DcFacility findNearestFacility(List<DcFacility> dcFacilityList, String stakeMarkCode) { public DcFacility findNearestFacility(List<DcFacility> dcFacilityList, String stakeMarkCode) {
// 将目标桩号转换为整数,以便比较 // 将目标桩号转换为整数
int targetCodeAsInt = Integer.parseInt(extract(stakeMarkCode)); int targetCodeAsInt = Integer.parseInt(extract(stakeMarkCode));
DcFacility nearestFacility = null; DcFacility nearestFacility = null;
@ -950,11 +982,9 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
for (DcFacility facility : dcFacilityList) { for (DcFacility facility : dcFacilityList) {
// 假设每个DcFacility对象中有一个方法或直接字段可以获取处理后的桩号字符串 // 假设每个DcFacility对象中有一个方法或直接字段可以获取处理后的桩号字符串
String stakeMark = facility.getStakeMark(); // 请替换为实际的getter方法名 String stakeMark = facility.getStakeMark();
// 将当前设施的桩号转换为整数 // 将当前设施的桩号转换为整数
int currentCodeAsInt = Integer.parseInt(extract(stakeMark)); int currentCodeAsInt = Integer.parseInt(extract(stakeMark));
// 计算与目标桩号的距离(差值的绝对值) // 计算与目标桩号的距离(差值的绝对值)
int distance = Math.abs(targetCodeAsInt - currentCodeAsInt); int distance = Math.abs(targetCodeAsInt - currentCodeAsInt);
@ -967,5 +997,30 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
return nearestFacility; // 返回最近的桩号信息 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