车辆伴随分析算法实现 - 基于过车记录的伴随车辆识别
车辆伴随分析算法实现 - 基于过车记录的伴随车辆识别
该算法根据车辆过车记录,判断车辆之间的伴随关系,并返回伴随车辆信息,包括伴随车辆的车型、车牌号、伴随时间以及经过的设备信息。
算法规则:
- A车与B车非同一辆车才进行计算。
- A车与B车的过车记录都经过了卡口1,卡口2,卡口3,卡口4。且A车与B车经过每个相同卡口的'gcsj'间隔<=X。 那么'bscs'为4次,如果'bscs'>=Y。即A,B为伴随车。
数据结构:
- AnalysisJudgmentVehiceFollowVO: 包含车辆信息,包括车辆识别码('sbid')、车辆名称('sbmc')、车辆类型('hpzl')、车牌号('hphm')、过车时间('gcsj'),均为字符串类型。
- AnalysisJudgmentFollowCarVO: 包含伴随车辆信息,包括车型('hpzl')、车牌号('hphm')、伴随车辆车型('bscHpzl')、伴随车辆车牌号('bscHphm')、伴随时间('bssj'),以及连续通过的相同设备('sbid')集合。
算法实现代码:
public List<AnalysisJudgmentFollowCarVO> getFollowCarList(List<AnalysisJudgmentVehiceFollowVO> carList, int x, int y) {
List<AnalysisJudgmentFollowCarVO> followCarList = new ArrayList<>();
Map<String, List<AnalysisJudgmentVehiceFollowVO>> carMap = new HashMap<>();
for (AnalysisJudgmentVehiceFollowVO car : carList) {
String key = car.getHpzl() + car.getHphm();
if (!carMap.containsKey(key)) {
carMap.put(key, new ArrayList<>());
}
carMap.get(key).add(car);
}
for (Map.Entry<String, List<AnalysisJudgmentVehiceFollowVO>> entry : carMap.entrySet()) {
List<AnalysisJudgmentVehiceFollowVO> carRecords = entry.getValue();
if (carRecords.size() <= 1) {
continue;
}
for (int i = 0; i < carRecords.size(); i++) {
AnalysisJudgmentVehiceFollowVO carA = carRecords.get(i);
for (int j = i + 1; j < carRecords.size(); j++) {
AnalysisJudgmentVehiceFollowVO carB = carRecords.get(j);
if (carA.getSbid().equals(carB.getSbid())) {
continue;
}
List<String> sameSbidList = new ArrayList<>();
int bscs = 0;
int startIndexA = 0;
int startIndexB = 0;
while (startIndexA < carA.getSbidList().size() && startIndexB < carB.getSbidList().size()) {
String sbidA = carA.getSbidList().get(startIndexA);
String sbidB = carB.getSbidList().get(startIndexB);
int compareResult = sbidA.compareTo(sbidB);
if (compareResult == 0) {
sameSbidList.add(sbidA);
startIndexA++;
startIndexB++;
} else if (compareResult < 0) {
startIndexA++;
} else {
startIndexB++;
}
}
if (sameSbidList.size() < 4) {
continue;
}
int lastSameSbidIndex = sameSbidList.size() - 1;
String lastSameSbid = sameSbidList.get(lastSameSbidIndex);
String firstSameSbid = sameSbidList.get(0);
String lastGcsjA = carA.getGcsjBySbid(lastSameSbid);
String firstGcsjA = carA.getGcsjBySbid(firstSameSbid);
String lastGcsjB = carB.getGcsjBySbid(lastSameSbid);
String firstGcsjB = carB.getGcsjBySbid(firstSameSbid);
long timeDiffA = getTimeDiff(firstGcsjA, lastGcsjA);
long timeDiffB = getTimeDiff(firstGcsjB, lastGcsjB);
if (timeDiffA <= x && timeDiffB <= x) {
for (String sbid : sameSbidList) {
bscs++;
}
if (bscs >= y) {
AnalysisJudgmentFollowCarVO followCar = new AnalysisJudgmentFollowCarVO();
followCar.setHpzl(carA.getHpzl());
followCar.setHphm(carA.getHphm());
followCar.setBscHpzl(carB.getHpzl());
followCar.setBscHphm(carB.getHphm());
followCar.setBssj(String.valueOf(timeDiffB));
followCar.setSbidList(sameSbidList);
followCarList.add(followCar);
}
}
}
}
}
return followCarList;
}
private long getTimeDiff(String startTimeStr, String endTimeStr) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date startTime = sdf.parse(startTimeStr);
Date endTime = sdf.parse(endTimeStr);
return (endTime.getTime() - startTime.getTime()) / (1000 * 60);
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
算法流程:
- 将所有车辆信息存入一个Map中,key为车辆类型+车牌号,value为该车辆的所有过车记录。
- 遍历Map,对于每个车辆,获取其所有过车记录。
- 比较该车辆与其他车辆的过车记录,判断是否符合伴随车辆的条件。
- 若符合条件,则将伴随车辆信息存储到新的集合中。
- 返回新的集合。
应用场景:
该算法可用于交通监控、车辆轨迹分析、安全预警等场景。
原文地址: https://www.cveoy.top/t/topic/n1pW 著作权归作者所有。请勿转载和采集!