有一个路径规划功能,同时规划多个节点,多个节点一般用作巡检。节点之间通过路径规划算法算出连接节点,并把各规划节点和连接节点统一成一条规划路径。现要只给规划节点进行排序,按我们之前的规划节点进行排序(使用路径规划算法之后)。规划节点顺序不通,同一条路径要一直累计序号,同一个规划节点可以有多个序号,但不能重复。花了整整一下午时间加晚上到8,9点才写出来,特此记录。
String lastPoint = "";
StringBuilder sb = new StringBuilder();
//未去重路线的巡检点位置,用于记录
List<Integer> indexRecord = new ArrayList<>();
//route为路径规划算法算出的所以有序节点,用逗号隔开,需要实现去重,两个连续节点重复的
//此处只有多个节点的编号
int i = 0;
for (String point : route.split(",")) {
if (lastPoint == "") {
lastPoint = point;
sb.append(point + ",");
i++;
indexRecord.add(i);
} else {
String nextPointCodes[] = map.get(lastPoint).split(",");
//节点的相邻节点转换,逗号分隔转数组转List集合
Set<String> setPoint = new HashSet<>();
if (nextPointCodes != null && nextPointCodes.length > 0) {
for (String nextPointCode : nextPointCodes) {
setPoint.add(nextPointCode);
}
}
//如果两个巡检点直接相连,就不需要使用算法计算,否则使用算法计算
if (setPoint.contains(point)) {
lastPoint = point;
sb.append(point + ",");
i++;
indexRecord.add(i);
} else {
List<List<String>> list = new ArrayList<>();
int[] array = new int[]{0};
int index = 0;
//路径规划算法
countRoute(lastPoint, point, map, list, index, array);
//从规划路径中找最优路径
String bestRoute = findBestRoute(lastPoint, point, list, locationMap);
sb.append(bestRoute + ",");
//记录规划节点的位置
i = i + bestRoute.split(",").length;
indexRecord.add(i);
//取前面路径中最后一个位置点作为起点
String num[] = bestRoute.split(",");
lastPoint = num[num.length - 1];
}
}
}
//去粗重复数量
int repeatTotalNum = 0;//中间有几个不是规划节点的个数
//去重路线的巡检点位置
List<Integer> repeatIndexRecord = new ArrayList<>();
//去重
String lastCode = "";
i = 0; //去重后的巡检点下标累加,表示第几个巡检点
String codes[] = sb.toString().split(",");
StringBuilder sbb = new StringBuilder();
for (int a = 1; a <= codes.length; a++) {
String code = codes[a - 1];
if(StringUtils.isBlank(code)){
continue;
}
if (StringUtils.isNotBlank(code) && lastCode == "") {
lastCode = code;
sbb.append(code + ",");
i++;
repeatIndexRecord.add(i);
} else {
//去重,上一个点不等于当前点,就去掉
if (StringUtils.isNotBlank(code) && !lastCode.contains(code)) {
if (a >= indexRecord.get(i)) {
i++;
//有相差
if (repeatTotalNum != 0) {
logger.info("d=={}", (indexRecord.get(i-1) - repeatTotalNum));
repeatIndexRecord.add(indexRecord.get(i - 1) - repeatTotalNum);
} else if (repeatTotalNum == 0 && repeatTotalNum == 0) {
logger.info("b=={}", i);
repeatIndexRecord.add(i);
}
}
lastCode = code;
sbb.append(code + ",");
} else {
//中间节点重复个数累计
repeatTotalNum++;
}
}
}
logger.info("jsonObjects={}", sb.toString());
logger.info("jsonObjectss={}", sbb.toString());
Map<String, String> indexMap = new HashMap<>();
//设置巡检区域序号,用于前端显示巡检顺序
logger.info("indexRecord={}", indexRecord.toString());
logger.info("repeatIndexRecord={}", repeatIndexRecord.toString());
int idx = 1;
//一个节点多个序号处理
for (Integer o : repeatIndexRecord) {
if (!indexMap.containsKey(jsonObjects.get(o - 1).getId() + "")) {
indexMap.put(jsonObjects.get(o - 1).getId() + "", idx + "");
idx++;
} else {
indexMap.put(jsonObjects.get(o - 1).getId() + "", indexMap.get(jsonObjects.get(o - 1).getId() + "") + "," + idx);
idx++;
}
}