修改获取最近路段接口

master
游胜振 1 year ago
parent 0c88fcad74
commit 48c6bc0f7e
  1. 62
      hiatmp-hidden-danger-server/src/main/java/com/hisense/hiatmp/server_api/controller/HighDangerController.java
  2. 5
      hiatmp-hidden-danger-server/src/main/java/com/hisense/hiatmp/server_api/mapper/HighDangerMapper.java
  3. 44
      hiatmp-hidden-danger-server/src/main/java/com/hisense/hiatmp/server_api/utils/CoordinateUtils.java
  4. 21
      hiatmp-hidden-danger-server/src/main/resources/sql-mapper/HighDangerMapper.xml
  5. 4
      hiatmp-model/src/main/java/com/hisense/hiatmp/model/common/CrossingDTO.java

@ -8,6 +8,7 @@ import com.hisense.hiatmp.server_api.mapper.OperatorMapper;
import com.hisense.hiatmp.server_api.model.BisRoadVO;
import com.hisense.hiatmp.server_api.service.HighDangerService;
import com.hisense.hiatmp.model.dmr.Point;
import com.hisense.hiatmp.server_api.utils.CoordinateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
@ -155,21 +157,26 @@ public class HighDangerController {
}
// 获取全部道路
@GetMapping("/getAllSection")
/*@GetMapping("/getAllSection")
public ServerResponse<?> getAllRoad(){
List<SectionVO> allSection = highDangerMapper.getAllSection();
return ServerResponse.ok(allSection);
}
}*/
// 获取最近道路
@PostMapping("/getNearRoadInfo")
public ServerResponse<?> getNearRoad(@RequestBody Point point,
@RequestParam(defaultValue = "1") Long pageNum,
@RequestParam(defaultValue = "10") Long pageSize){
//获取三公里内的坐标
double[] rectangle = CoordinateUtils.getRectangleCoordinates(point.getY(),point.getX(),3);
// 获取全部路段数据
List<SectionVO> allRoadInfo = highDangerMapper.getAllSection();
BigDecimal d = new BigDecimal(point.getX()).setScale(1,BigDecimal.ROUND_DOWN);
SectionVO sectionVO = new SectionVO();
sectionVO.setPosition(String.valueOf(d));
List<SectionVO> allRoadInfo = highDangerMapper.getAllSection(sectionVO);
HashMap<String, Map<Double, String>> combinedMap = new HashMap<>();
HashMap<String, Map<Double, String>> pointMapSection = new HashMap<>();
HashMap<String, Map<Double, String>> pointMapCrossing = new HashMap<>();
@ -179,50 +186,60 @@ public class HighDangerController {
for(SectionVO dto : allRoadInfo){
Map<Double,String> map = new HashMap<>();
// 临时存储最短距离
Double minDistance = null;
//Double minDistance = null;
if(dto.getPosition() == null){
continue;
}
// 将单条路段的坐标点依次取出进行对比
String[] split = dto.getPosition().split(",");
for(int i = 0; i < split.length; i += 2){
float x = Float.parseFloat(split[i]);
float y = Float.parseFloat(split[i + 1]);
float x = Float.parseFloat(split[0]);
float y = Float.parseFloat(split[1]);
if (!(x >= rectangle[0] && x <= rectangle[2] && y >= rectangle[1] && y <= rectangle[3])){
continue;
}
//for(int i = 0; i < split.length; i += 2){
//float x = Float.parseFloat(split[i]);
//float y = Float.parseFloat(split[i + 1]);
Dpoint.setX(x);
Dpoint.setY(y);
// 计算两点间的距离
Double pointDistance = highDangerService.getPointDistance(Dpoint, point);
// 最短距离为空,初次存入第一个值
if(minDistance == null){
/*if(minDistance == null){
minDistance = pointDistance;
continue;
}else{
if(pointDistance < minDistance){
minDistance = pointDistance;
}
}
}
map.put(minDistance, dto.getPosition());
}*/
//}
map.put(pointDistance, dto.getPosition());
// 将路段和最短路线
pointMapSection.put(dto.getSectionName(), map);
minDistance = null;
//minDistance = null;
}
// 获取所有路口信息
List<CrossingDTO> allCrossing = highDangerMapper.getAllCrossing();
CrossingDTO crossingDTO = new CrossingDTO();
crossingDTO.setLonMin(rectangle[0]);
crossingDTO.setLatMin(rectangle[1]);
crossingDTO.setLonMax(rectangle[2]);
crossingDTO.setLatMax(rectangle[3]);
List<CrossingDTO> allCrossing = highDangerMapper.getAllCrossing(crossingDTO);
// 遍历集合,将所有坐标点与当前坐标比较
for(CrossingDTO cross : allCrossing){
Map<Double,String> map = new HashMap<>();
float x = cross.getLongitude();
float y = cross.getLatitude();
/*if (!(x >= rectangle[0] && x <= rectangle[2] && y >= rectangle[1] && y <= rectangle[3])){
continue;
}*/
Dpoint.setX(x);
Dpoint.setY(y);
// 计算两点间的距离
Double pointDistance = highDangerService.getPointDistance(Dpoint, point);
map.put(pointDistance, String.valueOf(cross.getLongitude() + "," + cross.getLatitude()));
map.put(pointDistance, cross.getLongitude() + "," + cross.getLatitude());
pointMapCrossing.put(cross.getCrossingName(), map);
}
@ -235,11 +252,14 @@ public class HighDangerController {
// .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
// 将路段根据计算的值进行排序
LinkedHashMap<String, Map<Double, String>> collect = highDangerService.sortMapByValue(combinedMap).entrySet().stream()
.skip((pageNum - 1) * pageSize)
.limit(pageSize)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
LinkedHashMap<String, Map<Double, String>> collect = new LinkedHashMap<>();
if (null != combinedMap) {
collect = highDangerService.sortMapByValue(combinedMap).entrySet().stream()
.skip((pageNum - 1) * pageSize)
.limit(pageSize)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}
return ServerResponse.ok(collect);
}

@ -6,6 +6,7 @@ import com.hisense.hiatmp.model.common.*;
import com.hisense.hiatmp.server_api.model.BisRoadVO;
import org.springframework.stereotype.Repository;
import java.math.BigDecimal;
import java.util.List;
@Repository
@ -24,9 +25,9 @@ public interface HighDangerMapper extends BaseMapper<HighDangerBase>{
List<HighDangerBase> getHigDangerSearch(String search);
List<SectionVO> getAllSection();
List<SectionVO> getAllSection(SectionVO sectionVO);
List<CrossingDTO> getAllCrossing();
List<CrossingDTO> getAllCrossing(CrossingDTO crossingDTO);
HighDangerRoad getRoadInfo(String businessId);

@ -0,0 +1,44 @@
package com.hisense.hiatmp.server_api.utils;
import java.math.BigDecimal;
public class CoordinateUtils {
// 地球半径(km)
private static final double EARTH_RADIUS = 6371.0;
/**
* 获取指定距离范围内的矩形坐标
* @param lat 纬度
* @param lon 经度
* @param distance 距离(公里)
* @return 矩形坐标范围(最小经度, 最小纬度, 最大经度, 最大纬度)
*/
public static double[] getRectangleCoordinates(double lat, double lon, double distance) {
double latRad = Math.toRadians(lat);
double lonRad = Math.toRadians(lon);
// 纬度范围计算
double latMin = latRad - Math.asin(Math.sin(distance / (EARTH_RADIUS * Math.cos(latRad))) / Math.cos(distance / EARTH_RADIUS));
double latMax = latRad + Math.asin(Math.sin(distance / (EARTH_RADIUS * Math.cos(latRad))) / Math.cos(distance / EARTH_RADIUS));
// 经度范围计算
double lonMin = lonRad - Math.atan2(Math.sin(distance / EARTH_RADIUS) * Math.cos(latRad), Math.cos(distance / EARTH_RADIUS) - Math.sin(latRad) * Math.sin(latMin));
double lonMax = lonRad + Math.atan2(Math.sin(distance / EARTH_RADIUS) * Math.cos(latRad), Math.cos(distance / EARTH_RADIUS) - Math.sin(latRad) * Math.sin(latMax));
return new double[]{
Math.toDegrees(lonMin),
Math.toDegrees(latMin),
Math.toDegrees(lonMax),
Math.toDegrees(latMax)
};
}
public static void main(String[] args) {
/*double[] rectangle = getRectangleCoordinates(39.910922, 116.401735, 3); // 北京天安门坐标,3公里范围
System.out.println("最小经度: " + rectangle[0]);
System.out.println("最小纬度: " + rectangle[1]);
System.out.println("最大经度: " + rectangle[2]);
System.out.println("最大纬度: " + rectangle[3]);*/
}
}

@ -166,7 +166,7 @@
WHERE hdb.name like #{search}
</select>
<select id="getAllSection" resultType="com.hisense.hiatmp.model.common.SectionVO">
<select id="getAllSection" parameterType="com.hisense.hiatmp.model.common.SectionVO" resultType="com.hisense.hiatmp.model.common.SectionVO">
SELECT
section_code,
section_name,
@ -176,12 +176,29 @@
area_code,
position
FROM bis_section
where 1 = 1
<if test="position != null and position != ''">
AND position like concat(concat('%',#{position}),'%')
</if>
</select>
<select id="getAllCrossing" resultType="com.hisense.hiatmp.model.common.CrossingDTO">
<select id="getAllCrossing" parameterType="com.hisense.hiatmp.model.common.CrossingDTO" resultType="com.hisense.hiatmp.model.common.CrossingDTO">
SELECT
*
FROM bis_crossing
where 1 = 1
<if test="lonMin != null">
AND longitude &gt;= #{lonMin}
</if>
<if test="lonMax != null">
AND longitude &lt;= #{lonMax}
</if>
<if test="latMin != null">
AND latitude &gt;= #{latMin}
</if>
<if test="latMax != null">
AND latitude &lt;= #{latMax}
</if>
</select>

@ -14,6 +14,10 @@ public class CrossingDTO implements Serializable {
private String area;
private Float longitude;
private Float latitude;
private Double lonMin;
private Double latMin;
private Double lonMax;
private Double latMax;
// private String description;
}
Loading…
Cancel
Save