资源相关
This commit is contained in:
@ -8,6 +8,7 @@ import com.graphhopper.GraphHopper;
|
||||
import com.graphhopper.ResponsePath;
|
||||
import com.graphhopper.config.Profile;
|
||||
import com.graphhopper.util.shapes.GHPoint;
|
||||
import com.yj.earth.annotation.CheckAuth;
|
||||
import com.yj.earth.business.domain.FileInfo;
|
||||
import com.yj.earth.business.service.FileInfoService;
|
||||
import com.yj.earth.common.config.GraphHopperProperties;
|
||||
@ -51,6 +52,7 @@ public class GraphHopperController {
|
||||
private final AtomicBoolean isLoaded = new AtomicBoolean(false);
|
||||
|
||||
@Operation(summary = "获取地图列表")
|
||||
@CheckAuth
|
||||
@GetMapping("/list")
|
||||
public ApiResponse list() {
|
||||
LambdaQueryWrapper<FileInfo> queryWrapper = new LambdaQueryWrapper<>();
|
||||
@ -60,6 +62,7 @@ public class GraphHopperController {
|
||||
|
||||
@Operation(summary = "加载地图数据")
|
||||
@PostMapping("/loadMap")
|
||||
@CheckAuth
|
||||
public ApiResponse loadMap(@Parameter(description = "文件ID") @RequestParam String fileId) {
|
||||
// 参数校验
|
||||
if (fileId == null) {
|
||||
@ -118,6 +121,7 @@ public class GraphHopperController {
|
||||
|
||||
@Operation(summary = "路径规划")
|
||||
@PostMapping("/route")
|
||||
@CheckAuth
|
||||
public ApiResponse calculateRoute(@RequestBody RouteRequest request) {
|
||||
// 校验地图是否加载完成 + 实例是否可用
|
||||
if (!isLoaded.get() || currentHopper == null) {
|
||||
@ -145,26 +149,36 @@ public class GraphHopperController {
|
||||
|
||||
// 处理错误
|
||||
if (response.hasErrors()) {
|
||||
return ApiResponse.failure("路径计算失败: " + response.getErrors().toString());
|
||||
}
|
||||
// 检查是否有超出范围的错误
|
||||
boolean hasOutOfBoundsError = response.getErrors().stream()
|
||||
.anyMatch(e -> e instanceof com.graphhopper.util.exceptions.PointOutOfBoundsException);
|
||||
|
||||
if (hasOutOfBoundsError) {
|
||||
// 返回超出范围的特定格式响应
|
||||
return ApiResponse.failure("路径超出地图范围");
|
||||
} else {
|
||||
return ApiResponse.failure("路径计算失败: " + response.getErrors().toString());
|
||||
}
|
||||
}
|
||||
// 解析结果
|
||||
ResponsePath bestPath = response.getBest();
|
||||
List<Point> pathPoints = new ArrayList<>();
|
||||
bestPath.getPoints().forEach(ghPoint ->
|
||||
pathPoints.add(new Point(ghPoint.getLat(), ghPoint.getLon()))
|
||||
);
|
||||
|
||||
// 封装返回
|
||||
RouteResponse routeResponse = new RouteResponse(bestPath.getDistance() / 1000, (double) (bestPath.getTime() / 60000), pathPoints);
|
||||
return ApiResponse.success(routeResponse);
|
||||
|
||||
} catch (com.graphhopper.util.exceptions.PointOutOfBoundsException e) {
|
||||
// 捕获单点超出范围的异常
|
||||
return ApiResponse.failure("路径超出地图范围");
|
||||
} catch (Exception e) {
|
||||
return ApiResponse.failure("路径计算异常: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "获取交通方式")
|
||||
@CheckAuth
|
||||
@PostMapping("/profiles")
|
||||
public ApiResponse profiles() {
|
||||
return ApiResponse.success(graphHopperProperties.getProfiles());
|
||||
|
||||
Reference in New Issue
Block a user