PostGIS实现路径等间距插值方法

1 简介

在业务上要实现一条路径等间距插值点的位置信息,便于后端给前端实时推送。

2 技术路线

利用PostGIS函数ST_LineInterpolatePoint实现在LineString上指定位置插入点对象,并返回插入点。

  • 语法
geometry  ST_LineInterpolatePoint(geometry  aLinestring , float8  aFraction);
  • 参数
    aLinestring : 目标LineString对象;
    aFraction:插入点位于该线段的百分比,是一个从0到1的浮点值。

3 实现方法

  • Repository
    @Query(value = "SELECT ST_AsText(ST_LineInterpolatePoint(geom, ?1)) FROM (SELECT ?2 \\:\\: geometry as geom) As insert_point ;",
            nativeQuery = true)
    String findDataByDistance(float percent, String route);
  • service
public class RouteService {
    protected Logger logger =  LoggerFactory.getLogger(this.getClass());

    private static final DataConvertTools cv = new DataConvertTools();

    @Autowired
    private TblTestRepository tblTestRepository;

    public List getPointByDistance(double length, int distance, String routePath) {
        List pointList = new ArrayList<>();
        for(int dis = 0;dis < length;dis++){
            float percent;
            if(length-dis >= distance){
                Double doubleValue = dis/length;
                percent = doubleValue.floatValue();
                dis+=distance;
            }else{
                percent = 1f;
            }
            String point = tblTestRepository.findDataByDistance(percent,routePath);
            pointList.add(point);
        }
        return pointList;
    }
}
  • controller
@CrossOrigin
@RestController
@RequestMapping("/route")
@Api(value = "获取路径等距离点Controller", tags = "获取路径等距离点接口")
public class RouteController extends BaseController{

    @Autowired
    private RouteService routeService;

    @RequestMapping(value = "/getPointByDistance", method = RequestMethod.POST)
    @ApiOperation(value = "根据间距获取路径插值点",notes = "routePath类似于:LINESTRING(0 0,2 2)")
    public List queryByPeriod(@RequestParam(value="length") double length, @RequestParam(value="distance") int distance, @RequestParam(value="routePath") String routePath) {
        return routeService.getPointByDistance(length,distance,routePath);
    }
}
  • 接口测试结果


    参数输入

    返回结果

你可能感兴趣的:(PostGIS实现路径等间距插值方法)