代码提交
This commit is contained in:
@ -1,6 +1,5 @@
|
||||
package com.yj.earth.business.controller;
|
||||
|
||||
import com.yj.earth.annotation.CheckAuth;
|
||||
import com.yj.earth.auth.AuthInfo;
|
||||
import com.yj.earth.auth.AuthValidator;
|
||||
import com.yj.earth.common.util.ApiResponse;
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
package com.yj.earth.business.controller;
|
||||
|
||||
import com.yj.earth.common.util.GdalUtil;
|
||||
import com.yj.earth.common.util.GdalJsonConverter;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
@ -29,7 +29,7 @@ public class GdalController {
|
||||
public void importDataStreamGzip(@Parameter(description = "矢量文件路径", required = true) @RequestParam("path") String path, HttpServletResponse response) throws IOException {
|
||||
|
||||
// 解析矢量文件、得到JSON数据
|
||||
String jsonData = GdalUtil.readVectorToSpecifiedGeoJson(path);
|
||||
String jsonData = GdalJsonConverter.convertToJson(path);
|
||||
|
||||
// 设置响应头
|
||||
String filename = URLEncoder.encode("data.gz", StandardCharsets.UTF_8);
|
||||
|
||||
@ -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());
|
||||
}
|
||||
}
|
||||
// 解析结果
|
||||
|
||||
@ -0,0 +1,89 @@
|
||||
package com.yj.earth.business.controller;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.yj.earth.business.domain.PbfInfo;
|
||||
import com.yj.earth.business.service.PbfInfoService;
|
||||
import com.yj.earth.common.util.ApiResponse;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
@Tag(name = "地图文件管理")
|
||||
@RestController
|
||||
@RequestMapping("/pbfInfo")
|
||||
public class PbfInfoController {
|
||||
|
||||
@Resource
|
||||
private PbfInfoService pbfInfoService;
|
||||
@Resource
|
||||
private GraphHopperController graphHopperController;
|
||||
|
||||
@Operation(summary = "添加地图文件")
|
||||
@PostMapping("/add")
|
||||
public ApiResponse add(@Parameter(description = "地图文件路径") @RequestParam(required = true) String path) {
|
||||
PbfInfo pbfInfo = new PbfInfo();
|
||||
pbfInfo.setPath(path);
|
||||
pbfInfo.setName(FileUtil.mainName(path));
|
||||
pbfInfo.setIsEnable(0);
|
||||
pbfInfoService.save(pbfInfo);
|
||||
return ApiResponse.success(null);
|
||||
}
|
||||
|
||||
@Operation(summary = "删除地图文件")
|
||||
@PostMapping("/delete")
|
||||
public ApiResponse delete(@Parameter(description = "地图文件ID") @RequestParam(required = true) String id) {
|
||||
pbfInfoService.removeById(id);
|
||||
return ApiResponse.success(null);
|
||||
}
|
||||
|
||||
@Operation(summary = "启用地图文件")
|
||||
@PostMapping("/enable")
|
||||
public ApiResponse enable(@Parameter(description = "地图文件ID") @RequestParam(required = true) String id) {
|
||||
// 根据文件ID查询数据
|
||||
PbfInfo pbfInfo = pbfInfoService.getById(id);
|
||||
pbfInfo.setIsEnable(1);
|
||||
pbfInfoService.updateById(pbfInfo);
|
||||
// 调用地图服务
|
||||
graphHopperController.loadMap(pbfInfo.getPath());
|
||||
// 除这条数据以外的全部为禁用
|
||||
List<PbfInfo> list = pbfInfoService.list();
|
||||
for (PbfInfo info : list) {
|
||||
if (!info.getId().equals(id)) {
|
||||
info.setIsEnable(0);
|
||||
pbfInfoService.updateById(info);
|
||||
}
|
||||
}
|
||||
return ApiResponse.success(null);
|
||||
}
|
||||
|
||||
@Operation(summary = "禁用地图文件")
|
||||
@PostMapping("/disable")
|
||||
public ApiResponse disable(@Parameter(description = "地图文件ID") @RequestParam(required = true) String id) {
|
||||
PbfInfo pbfInfo = new PbfInfo();
|
||||
pbfInfo.setId(id);
|
||||
pbfInfo.setIsEnable(0);
|
||||
pbfInfoService.updateById(pbfInfo);
|
||||
graphHopperController.clearMap();
|
||||
return ApiResponse.success(null);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取所有地图文件")
|
||||
@PostMapping("/list")
|
||||
public ApiResponse list() {
|
||||
LambdaQueryWrapper<PbfInfo> queryWrapper = new QueryWrapper<PbfInfo>().lambda();
|
||||
// 把启用的排在最前面
|
||||
queryWrapper.orderByDesc(PbfInfo::getIsEnable);
|
||||
queryWrapper.orderByAsc(PbfInfo::getCreatedAt);
|
||||
return ApiResponse.success(pbfInfoService.list(queryWrapper));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user