diff --git a/zc-business/src/main/java/com/zc/business/enums/LocationEnum.java b/zc-business/src/main/java/com/zc/business/enums/LocationEnum.java new file mode 100644 index 00000000..acb3d04a --- /dev/null +++ b/zc-business/src/main/java/com/zc/business/enums/LocationEnum.java @@ -0,0 +1,55 @@ +package com.zc.business.enums; + +import java.util.Optional; + +/** + * + */ +public enum LocationEnum { + CHANGQING_UNIVERSITY_CITY(1, "长清大学城"), + CHANGQING(2, "长清"), + XIAOLI(3, "孝里"), + PINGYIN_NORTH(4, "平阴北"), + PINGYIN(5, "平阴"), + PINGYIN_SOUTH(6, "平阴南"), + DONGPING(7, "东平"), + LIANGSHAN_EAST(8, "梁山东"), + LIANGSHAN(9, "梁山"), + JIAXIANG_WEST(10, "嘉祥西"); + + private final int order; + private final String name; + + LocationEnum(int order, String name) { + this.order = order; + this.name = name; + } + + public int getOrder() { + return order; + } + + public String getName() { + return name; + } + + // 通过序号查找枚举值,未找到时返回null + public static String findByOrder(int order) { + for (LocationEnum location : LocationEnum.values()) { + if (location.getOrder() == order) { + return location.getName(); + } + } + return " "; // 未找到匹配的枚举值 + } + + // 查找序号,通过地点名称 + public static int findOrderByName(String name) { + for (LocationEnum location : LocationEnum.values()) { + if (location.getName().equals(name)) { + return location.getOrder(); + } + } + return -1; // 未找到匹配的名称 + } +} \ No newline at end of file diff --git a/zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java b/zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java index e6ec4a39..a645c260 100644 --- a/zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java +++ b/zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java @@ -1,4 +1,5 @@ package com.zc.business.service.impl; + import cn.hutool.core.bean.BeanUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; @@ -12,6 +13,7 @@ import com.zc.business.domain.DcCongestionSection; import com.zc.business.domain.DcFacility; import com.zc.business.domain.DcRoadSection; import com.zc.business.domain.DcRoadSectionCongestion; +import com.zc.business.enums.LocationEnum; import com.zc.business.enums.StakeMarkRange; import com.zc.business.enums.UniversalEnum; import com.zc.business.service.IDcFacilityService; @@ -39,6 +41,7 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; @Service public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsService { @@ -194,24 +197,24 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi Map headers = new HashMap<>(); headers.put("Authorization", getAccessToken()); try { - Response response // 请求响应 - = okHttp - .headers(headers) - .url(baseUrl + UniversalEnum.GETS_THE_VEHICLE_TRAFFIC_URI_IN_TRANSIT.getValue()) // 请求地址 - .data(requestParams) // 请求参数 - .post(); // 请求方法 - - ResponseBody body = response.body(); - if (body != null) { - String jsonString = body.string(); - if (JSON.isValidArray(jsonString)) { - return JSON.parseArray(jsonString); - } else { - return new JSONArray(); + Response response // 请求响应 + = okHttp + .headers(headers) + .url(baseUrl + UniversalEnum.GETS_THE_VEHICLE_TRAFFIC_URI_IN_TRANSIT.getValue()) // 请求地址 + .data(requestParams) // 请求参数 + .post(); // 请求方法 + + ResponseBody body = response.body(); + if (body != null) { + String jsonString = body.string(); + if (JSON.isValidArray(jsonString)) { + return JSON.parseArray(jsonString); + } else { + return new JSONArray(); + } } - } - return new JSONArray(); + return new JSONArray(); } catch (IOException e) { // 处理异常 e.printStackTrace(); @@ -234,73 +237,73 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi Map headers = new HashMap<>(); headers.put("Authorization", getAccessToken()); try { - Response response // 请求响应 - = okHttp - .headers(headers) - .url(baseUrl + UniversalEnum.AVERAGE_SPEED_URI_OF_GANTRY_IN_TRANSIT.getValue()) // 请求地址 - .data(requestParams) // 请求参数 - .post(); // 请求方法 + Response response // 请求响应 + = okHttp + .headers(headers) + .url(baseUrl + UniversalEnum.AVERAGE_SPEED_URI_OF_GANTRY_IN_TRANSIT.getValue()) // 请求地址 + .data(requestParams) // 请求参数 + .post(); // 请求方法 - ResponseBody body = response.body(); + ResponseBody body = response.body(); - JSONArray jsonArray = null; + JSONArray jsonArray = null; - if (body != null) { - String jsonString = body.string(); - if (JSON.isValidArray(jsonString)) { - jsonArray = JSON.parseArray(jsonString); + if (body != null) { + String jsonString = body.string(); + if (JSON.isValidArray(jsonString)) { + jsonArray = JSON.parseArray(jsonString); + } } - } - Map map = new HashMap<>(); + Map map = new HashMap<>(); - if (jsonArray != null) { - jsonArray.forEach(jsonObject -> { - if (jsonObject instanceof JSONObject) { - String gantryId = ((JSONObject) jsonObject).getString("gantry_id"); - map.put(gantryId, ((JSONObject) jsonObject).getInteger("avg_speed")); - } - }); - } + if (jsonArray != null) { + jsonArray.forEach(jsonObject -> { + if (jsonObject instanceof JSONObject) { + String gantryId = ((JSONObject) jsonObject).getString("gantry_id"); + map.put(gantryId, ((JSONObject) jsonObject).getInteger("avg_speed")); + } + }); + } - if (map.isEmpty()) { - return new JSONArray(); - } + if (map.isEmpty()) { + return new JSONArray(); + } - LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.in(DcFacility::getFacilityCode, map.keySet()); + queryWrapper.in(DcFacility::getFacilityCode, map.keySet()); - List facilities = facilityService.list(queryWrapper); + List facilities = facilityService.list(queryWrapper); - if (facilities == null || facilities.isEmpty()) { - return new JSONArray(); - } + if (facilities == null || facilities.isEmpty()) { + return new JSONArray(); + } - List roadSections = roadSectionService.selectDcRoadSectionListAll(null); + List roadSections = roadSectionService.selectDcRoadSectionListAll(null); - JSONArray jsonArray1 = new JSONArray(); + JSONArray jsonArray1 = new JSONArray(); - roadSections.forEach(item -> { - Integer startStakeMark = StakeMarkUtils.stakeMarkToInt(item.getStartStakeMark()); - Integer endStakeMark = StakeMarkUtils.stakeMarkToInt(item.getEndStakeMark()); + roadSections.forEach(item -> { + Integer startStakeMark = StakeMarkUtils.stakeMarkToInt(item.getStartStakeMark()); + Integer endStakeMark = StakeMarkUtils.stakeMarkToInt(item.getEndStakeMark()); - for (DcFacility facility : facilities) { - Integer stakeMark = StakeMarkUtils.stakeMarkToInt(facility.getStakeMark()); + for (DcFacility facility : facilities) { + Integer stakeMark = StakeMarkUtils.stakeMarkToInt(facility.getStakeMark()); - if (stakeMark >= startStakeMark && stakeMark <= endStakeMark) { - JSONObject jsonObject = new JSONObject(); - jsonObject.put("sectionId", item.getId()); - jsonObject.put("sectionName", item.getSectionName()); - jsonObject.put("avgSpeed", map.get(facility.getFacilityCode())); - jsonObject.put("facilityCode", facility.getFacilityCode()); - jsonArray1.add(jsonObject); - break; + if (stakeMark >= startStakeMark && stakeMark <= endStakeMark) { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("sectionId", item.getId()); + jsonObject.put("sectionName", item.getSectionName()); + jsonObject.put("avgSpeed", map.get(facility.getFacilityCode())); + jsonObject.put("facilityCode", facility.getFacilityCode()); + jsonArray1.add(jsonObject); + break; + } } - } - }); + }); - return jsonArray1; + return jsonArray1; } catch (IOException e) { // 处理异常 e.printStackTrace(); @@ -322,23 +325,23 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi Map headers = new HashMap<>(); headers.put("Authorization", getAccessToken()); try { - Response response // 请求响应 - = okHttp - .headers(headers) - .url(baseUrl + UniversalEnum.GETS_THE_URI_OF_THE_CURRENT_CONGESTION_EVENT.getValue()) // 请求地址 - .data(requestParams) // 请求参数 - .post(); // 请求方法 - - ResponseBody body = response.body(); - if (body != null) { - String jsonString = body.string(); - if (JSON.isValidArray(jsonString)) { - return JSON.parseArray(jsonString); - } else { - return new JSONArray(); + Response response // 请求响应 + = okHttp + .headers(headers) + .url(baseUrl + UniversalEnum.GETS_THE_URI_OF_THE_CURRENT_CONGESTION_EVENT.getValue()) // 请求地址 + .data(requestParams) // 请求参数 + .post(); // 请求方法 + + ResponseBody body = response.body(); + if (body != null) { + String jsonString = body.string(); + if (JSON.isValidArray(jsonString)) { + return JSON.parseArray(jsonString); + } else { + return new JSONArray(); + } } - } - return new JSONArray(); + return new JSONArray(); } catch (IOException e) { // 处理异常 e.printStackTrace(); @@ -577,24 +580,24 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi Map headers = new HashMap<>(); headers.put("Authorization", getAccessToken()); try { - Response response // 请求响应 - = okHttp - .headers(headers) - .url(baseUrl + UniversalEnum.GETS_THE_LANE_OCCUPANCY_URI.getValue()) // 请求地址 - .data(requestParams) // 请求参数 - .post(); // 请求方法 - - ResponseBody body = response.body(); - if (body != null) { - String jsonString = body.string(); - if (JSON.isValidArray(jsonString)) { - return JSON.parseArray(jsonString); - } else { - return new JSONArray(); + Response response // 请求响应 + = okHttp + .headers(headers) + .url(baseUrl + UniversalEnum.GETS_THE_LANE_OCCUPANCY_URI.getValue()) // 请求地址 + .data(requestParams) // 请求参数 + .post(); // 请求方法 + + ResponseBody body = response.body(); + if (body != null) { + String jsonString = body.string(); + if (JSON.isValidArray(jsonString)) { + return JSON.parseArray(jsonString); + } else { + return new JSONArray(); + } } - } - return new JSONArray(); + return new JSONArray(); } catch (IOException e) { // 处理异常 e.printStackTrace(); @@ -628,25 +631,25 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi Map headers = new HashMap<>(); headers.put("Authorization", getAccessToken()); try { - Response response // 请求响应 - = okHttp - .headers(headers) - .url(baseUrl + UniversalEnum.OBTAIN_THE_SHELF_INDICATOR_DATA.getValue()) // 请求地址 - .data(requestParams) // 请求参数 - .post(); // 请求方法 + Response response // 请求响应 + = okHttp + .headers(headers) + .url(baseUrl + UniversalEnum.OBTAIN_THE_SHELF_INDICATOR_DATA.getValue()) // 请求地址 + .data(requestParams) // 请求参数 + .post(); // 请求方法 - ResponseBody body = response.body(); - if (body != null) { - String jsonString = body.string(); - if (JSON.isValidArray(jsonString)) { - return JSON.parseArray(jsonString); - } else { - return new JSONArray(); + ResponseBody body = response.body(); + if (body != null) { + String jsonString = body.string(); + if (JSON.isValidArray(jsonString)) { + return JSON.parseArray(jsonString); + } else { + return new JSONArray(); + } } - } - return new JSONArray(); + return new JSONArray(); } catch (IOException e) { // 处理异常 e.printStackTrace(); @@ -790,33 +793,42 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi ResponseBody body = response.body(); if (body != null) { String jsonString = body.string(); - if (JSON.isValidArray(jsonString)) { - JSONArray jsonArray = JSON.parseArray(jsonString); - Map sumByName = new LinkedHashMap<>(); - List> list = new ArrayList(); - for (Object item : jsonArray) { // 这里做了微调,直接遍历jsonArray的Object - JSONObject jsonObject = (JSONObject) item; + if (!JSON.isValidArray(jsonString)) return Collections.emptyList(); - // 获取当前时间 - LocalTime now = LocalTime.now(); - // 获取当前小时数(24小时制) - int currentHour = now.getHour(); - if (jsonObject.getInteger("data_hour") == currentHour) { - String name = jsonObject.getString("ts_name"); // 更安全的取值方式 - int totalFlow = jsonObject.getInteger("total_flow"); // 专门针对Integer类型 + JSONArray jsonArray = JSON.parseArray(jsonString); - sumByName.put(name, totalFlow); - } - } - // 正确创建新的映射对象并添加到list中 - for (Map.Entry entry : sumByName.entrySet()) { - Map singleResult = new LinkedHashMap<>(); // 每次循环都创建一个新的映射 - singleResult.put("name", entry.getKey()); - singleResult.put("value", entry.getValue().toString()); - list.add(singleResult); - } - return list; - } +// 从JSON数据创建原始数据映射,构造为String类型值 + Map> originalDataMap = jsonArray.stream() + .filter(item -> ((JSONObject) item).getInteger("data_hour") == LocalTime.now().getHour()) + .collect(Collectors.toMap( + item -> ((JSONObject) item).getString("ts_name"), + item -> { + JSONObject jsonObject = (JSONObject) item; + return new HashMap() {{ + put("value", String.valueOf(jsonObject.getInteger("total_flow"))); + put("orderId", jsonObject.getString("order_id")); + }}; + }, + (oldVal, newVal) -> oldVal, // 保留旧值以解决键冲突 + LinkedHashMap::new // 保持插入顺序 + )); + +// 直接基于原始数据映射和LocationEnum构建最终结果列表 + List> resultList = Arrays.stream(LocationEnum.values()) + .map(location -> { + Map defaultData = new HashMap() {{ + put("value", "0"); + put("orderId", String.valueOf(LocationEnum.findOrderByName(location.getName()))); + }}; + return new HashMap() {{ + put("name", location.getName()); + put("value", originalDataMap.getOrDefault(location.getName(), defaultData).get("value")); + put("orderId", originalDataMap.getOrDefault(location.getName(), defaultData).get("orderId")); + }}; + }) + .collect(Collectors.toList()); + + return resultList; } return new ArrayList(); } catch (IOException e) { @@ -848,24 +860,24 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi Map headers = new HashMap<>(); headers.put("Authorization", getAccessToken()); try { - Response response // 请求响应 - = okHttp - .headers(headers) - .url(baseUrl + UniversalEnum.THE_ENTRANCE_OF_EACH_TOLL_STATION_IS_DIVIDED_INTO_VEHICLE_FLOW.getValue()) // 请求地址 - .data(requestParams) // 请求参数 - .post(); // 请求方法 - - ResponseBody body = response.body(); - if (body != null) { - String jsonString = body.string(); - if (JSON.isValidArray(jsonString)) { - return JSON.parseArray(jsonString); - }else { - return new JSONArray(); + Response response // 请求响应 + = okHttp + .headers(headers) + .url(baseUrl + UniversalEnum.THE_ENTRANCE_OF_EACH_TOLL_STATION_IS_DIVIDED_INTO_VEHICLE_FLOW.getValue()) // 请求地址 + .data(requestParams) // 请求参数 + .post(); // 请求方法 + + ResponseBody body = response.body(); + if (body != null) { + String jsonString = body.string(); + if (JSON.isValidArray(jsonString)) { + return JSON.parseArray(jsonString); + } else { + return new JSONArray(); + } } - } - return new JSONArray(); + return new JSONArray(); } catch (IOException e) { // 处理异常 e.printStackTrace(); @@ -995,7 +1007,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi } else { return new JSONArray(); } - }else { + } else { return new JSONArray(); } } catch (IOException e) { @@ -1059,36 +1071,36 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi Map headers = new HashMap<>(); headers.put("Authorization", getAccessToken()); try { - Response response // 请求响应 - = okHttp - .headers(headers) - .url(baseUrl + UniversalEnum.THE_WHOLE_SECTION_TWO_WAY_REAL_TIME_TRAFFIC_FLOW_URL.getValue()) // 请求地址 - .data(requestParams) // 请求参数 - .post(); // 请求方法 - - ResponseBody body = response.body(); - if (body != null) { - String jsonString = body.string(); - if (JSON.isValidArray(jsonString)) { - JSONArray jsonArray = JSON.parseArray(jsonString); - for (Object item : jsonArray) { - JSONObject jsonObject = (JSONObject) item; - if (jsonObject.getString("gantry_id").equals(nearestFacility.getFacilityCode())) { - Map map = new HashMap<>(); - int totalFlow = jsonObject.getInteger("total_flow"); - int data_hour = jsonObject.getInteger("data_hour"); - map.put("hour", data_hour); - map.put("totalFlow", totalFlow); - mapList.add(map); + Response response // 请求响应 + = okHttp + .headers(headers) + .url(baseUrl + UniversalEnum.THE_WHOLE_SECTION_TWO_WAY_REAL_TIME_TRAFFIC_FLOW_URL.getValue()) // 请求地址 + .data(requestParams) // 请求参数 + .post(); // 请求方法 + + ResponseBody body = response.body(); + if (body != null) { + String jsonString = body.string(); + if (JSON.isValidArray(jsonString)) { + JSONArray jsonArray = JSON.parseArray(jsonString); + for (Object item : jsonArray) { + JSONObject jsonObject = (JSONObject) item; + if (jsonObject.getString("gantry_id").equals(nearestFacility.getFacilityCode())) { + Map map = new HashMap<>(); + int totalFlow = jsonObject.getInteger("total_flow"); + int data_hour = jsonObject.getInteger("data_hour"); + map.put("hour", data_hour); + map.put("totalFlow", totalFlow); + mapList.add(map); + } + } } - } - } // 输出结果 - return mapList; - } + return mapList; + } - return null; + return null; } catch (IOException e) { // 处理异常 e.printStackTrace(); @@ -1129,67 +1141,67 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestParamsJson)) .build(); Response response = okHttpClient.newCall(request).execute(); - ResponseBody body = response.body(); - if (body != null) { - String jsonString = body.string(); - if (JSON.isValidArray(jsonString)) { - JSONArray jsonArray = JSON.parseArray(jsonString); - for (Object object : jsonArray) { - JSONObject jsonObject = (JSONObject) object; - Integer asOneRoad = jsonObject.getInteger("as_one_road"); - if (jsonObject.getDouble("end_pile_no") != null) { - double endPileNo = jsonObject.getDouble("end_pile_no"); - String endMark = formatNumber(endPileNo); - jsonObject.put("endMark", endMark);//结束桩号 - } - 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 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 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()]); - double distanceM = calculateDistance(s2, s, s4, s3); - // System.out.printf("最大距离为: %.2f 米%n", distanceM); - jsonObject.put("max_jam_dist", distanceM);//最大拥堵距离 - double endMake = startPileNo + Double.parseDouble(String.format("%.3f", distanceM)); - 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()]); - //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 * UniversalEnum.THOUSAND.getNumber(); // 将距离转换为米 - //System.out.printf("两点之间的距离为: %.2f 米%n", distanceMeters); - jsonObject.put("jam_dist", distanceMeters);//当前拥堵距离 + ResponseBody body = response.body(); + if (body != null) { + String jsonString = body.string(); + if (JSON.isValidArray(jsonString)) { + JSONArray jsonArray = JSON.parseArray(jsonString); + for (Object object : jsonArray) { + JSONObject jsonObject = (JSONObject) object; + Integer asOneRoad = jsonObject.getInteger("as_one_road"); + if (jsonObject.getDouble("end_pile_no") != null) { + double endPileNo = jsonObject.getDouble("end_pile_no"); + String endMark = formatNumber(endPileNo); + jsonObject.put("endMark", endMark);//结束桩号 + } + 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 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 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()]); + double distanceM = calculateDistance(s2, s, s4, s3); + // System.out.printf("最大距离为: %.2f 米%n", distanceM); + jsonObject.put("max_jam_dist", distanceM);//最大拥堵距离 + double endMake = startPileNo + Double.parseDouble(String.format("%.3f", distanceM)); + 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()]); + //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 * UniversalEnum.THOUSAND.getNumber(); // 将距离转换为米 + //System.out.printf("两点之间的距离为: %.2f 米%n", distanceMeters); + jsonObject.put("jam_dist", distanceMeters);//当前拥堵距离 + } } - } - return jsonArray; + return jsonArray; + } } - } - return new JSONArray(); + return new JSONArray(); } catch (IOException e) { // 处理异常 e.printStackTrace(); @@ -1236,8 +1248,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi } return totalFlow1 + totalFlow2; - } - catch (IOException e) { + } catch (IOException e) { // 处理异常 e.printStackTrace(); return UniversalEnum.ZERO.getNumber(); @@ -1270,12 +1281,12 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi Gson gson = new Gson(); String requestParamsJson = gson.toJson(requestParams); - Request request = new Request.Builder() - .url(baseUrl + UniversalEnum.EACH_TOLL_STATION_ENTRANCE_BY_TYPE_OF_HOURLY_TRAFFIC_FLOW.getValue()) - .headers(Headers.of(headers)) - .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestParamsJson)) - .build(); - Response response = okHttpClient.newCall(request).execute(); + Request request = new Request.Builder() + .url(baseUrl + UniversalEnum.EACH_TOLL_STATION_ENTRANCE_BY_TYPE_OF_HOURLY_TRAFFIC_FLOW.getValue()) + .headers(Headers.of(headers)) + .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestParamsJson)) + .build(); + Response response = okHttpClient.newCall(request).execute(); return response; }