代码提交

This commit is contained in:
2025-10-09 11:03:15 +08:00
parent 65048fbe89
commit 3e9cac5e32
22 changed files with 1195 additions and 269 deletions

View File

@ -59,7 +59,7 @@ public class GraphHopperController {
@Operation(summary = "加载地图数据")
@PostMapping("/loadMap")
@CheckAuth
public ApiResponse webLoadMap(@Parameter(description = "文件路径") @RequestParam String path) {
public ApiResponse loadMap(@Parameter(description = "文件路径") @RequestParam String path) {
File osmFile = new File(path);
if (!osmFile.exists()) {
return ApiResponse.failure("地图文件不存在: " + path);
@ -106,6 +106,44 @@ public class GraphHopperController {
return ApiResponse.success(null);
}
@Operation(summary = "清除地图服务")
@PostMapping("/clearMap")
@CheckAuth
public ApiResponse clearMap() {
// 防止并发操作(与加载操作互斥)
if (isLoading.get()) {
return ApiResponse.failure("地图正在加载中、无法清除、请稍后再试");
}
// 标记正在处理清除操作
isLoading.set(true);
try {
// 1. 关闭当前GraphHopper实例
if (currentHopper != null) {
log.info("开始关闭当前地图服务实例");
currentHopper.close();
currentHopper = null;
log.info("地图服务实例已关闭");
}
// 2. 删除地图数据目录
log.info("开始删除地图数据目录");
deleteOldGraphDir();
log.info("地图数据目录已删除");
// 3. 重置状态变量
isLoaded.set(false);
return ApiResponse.success("地图服务已成功清除");
} catch (Exception e) {
log.error("清除地图服务失败", e);
return ApiResponse.failure("清除地图服务失败: " + e.getMessage());
} finally {
// 释放操作锁
isLoading.set(false);
}
}
@Operation(summary = "路径规划")
@PostMapping("/route")
@CheckAuth
@ -126,7 +164,7 @@ public class GraphHopperController {
}
ghPoints.add(new GHPoint(request.getEndLat(), request.getEndLng())); // 终点
// 构建请求仅指定Profile、无setWeighting
// 构建请求
String targetProfile = request.getProfile() != null ? request.getProfile() : "car";
GHRequest ghRequest = new GHRequest(ghPoints)
.setProfile(targetProfile);
@ -139,12 +177,14 @@ public class GraphHopperController {
// 检查是否有超出范围的错误
boolean hasOutOfBoundsError = response.getErrors().stream()
.anyMatch(e -> e instanceof com.graphhopper.util.exceptions.PointOutOfBoundsException);
boolean hasPointNotFoundError = response.getErrors().stream()
.anyMatch(e -> e instanceof com.graphhopper.util.exceptions.PointNotFoundException);
if (hasOutOfBoundsError) {
// 返回超出范围的特定格式响应
return ApiResponse.failure("路径超出地图范围");
} else if (hasPointNotFoundError) {
return ApiResponse.failure("未超地图范围但找不到路径点");
} else {
return ApiResponse.failure("路径计算失败: " + response.getErrors().toString());
return ApiResponse.failure("路径计算异常: " + response.getErrors().get(0).getMessage());
}
}
// 解析结果