Moving a Point along a Path in SQL Server 2008

前端 未结 3 650
刺人心
刺人心 2020-12-17 04:25

I have a geography field stored in my database, holding a linestring path.

I want to move a point n meters along this linestring, and return the destin

3条回答
  •  时光说笑
    2020-12-17 05:18

    I used Daniel's answer from above, but I had to fix the "func_MoveAlongPath" signature to

    CREATE FUNCTION [dbo].[func_MoveAlongPath](@path geography, 
                                           @distance **float**, 
                                           @index int = 1)
    

    The int would returned wrong results, because it would round the values in the recursive calls. I then converted it into an iterative version, since the recursive one couldn't handle larger distances in the sample data I had:

    CREATE FUNCTION [dbo].[func_MoveAlongPathIter](@path geography, 
                                                   @distance float)   
    RETURNS geography
    AS
    BEGIN
        DECLARE @index          int = 1;
        DECLARE @result         geography = null;
        DECLARE @num_points     int = @path.STNumPoints();
        DECLARE @dist_to_next   float;
        DECLARE @comul_distance float = 0;
    
        WHILE (@index < @num_points - 1) AND (@comul_distance < @distance)
        BEGIN
            SET @dist_to_next = @path.STPointN(@index).STDistance(@path.STPointN(@index + 1));
            SET @comul_distance += @dist_to_next;
            SET @index += 1;
        END
    
        SET @result = [dbo].[func_MoveTowardsPoint](@path.STPointN(@index - 1),
                                                            @path.STPointN(@index),
                                                            @distance - (@comul_distance - @dist_to_next));
        RETURN @result;
    END
    

提交回复
热议问题