在SQL Server 2008中沿路径移动一个点

4

我在数据库中存储了一个地理字段,它保存了一个linestring路径。

我想要沿着这条linestring路径将一个点向前移动n米,并返回目的地。

例如,我想要从linestring路径的起点开始,沿着路径向前500米的目的地点。

以下是一个示例--你需要使用什么函数?或者还有其他方法吗?

DECLARE @g geography;
SET @g = geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656, -122.310 47.690)', 4326);
SELECT @g.YourFunctionHere(100).ToString();
3个回答

14

这个有点棘手,但肯定是可以做到的。

让我们从计算一个点到另一个点的方位角开始。给定起始点、方位角和距离,下面的函数将返回目标点:

CREATE FUNCTION [dbo].[func_MoveTowardsPoint](@start_point geography,
                                              @end_point   geography,  
                                              @distance    int)  /* Meters */   
RETURNS geography
AS
BEGIN
    DECLARE @ang_dist float = @distance / 6371000.0;  /* Earth's radius */
    DECLARE @bearing  decimal(18,15);
    DECLARE @lat_1    decimal(18,15) = Radians(@start_point.Lat);
    DECLARE @lon_1    decimal(18,15) = Radians(@start_point.Long);
    DECLARE @lat_2    decimal(18,15) = Radians(@end_point.Lat);
    DECLARE @lon_diff decimal(18,15) = Radians(@end_point.Long - @start_point.Long);
    DECLARE @new_lat  decimal(18,15);
    DECLARE @new_lon  decimal(18,15);
    DECLARE @result   geography;

    /* First calculate the bearing */

    SET @bearing = ATN2(sin(@lon_diff) * cos(@lat_2),
                        (cos(@lat_1) * sin(@lat_2)) - 
                        (sin(@lat_1) * cos(@lat_2) * 
                        cos(@lon_diff)));

    /* Then use the bearing and the start point to find the destination */

    SET @new_lat = asin(sin(@lat_1) * cos(@ang_dist) + 
                        cos(@lat_1) * sin(@ang_dist) * cos(@bearing));

    SET @new_lon = @lon_1 + atn2( sin(@bearing) * sin(@ang_dist) * cos(@lat_1), 
                                  cos(@ang_dist) - sin(@lat_1) * sin(@lat_2));

    /* Convert from Radians to Decimal */

    SET @new_lat = Degrees(@new_lat);
    SET @new_lon = Degrees(@new_lon);

    /* Return the geography result */

    SET @result = 
        geography::STPointFromText('POINT(' + CONVERT(varchar(64), @new_lon) + ' ' + 
                                              CONVERT(varchar(64), @new_lat) + ')', 
                                   4326);

    RETURN @result;
END

我了解您需要一个函数,该函数以线串为输入,而不仅仅是起点和终点。该点必须沿着连结线段的路径移动,并且必须继续在路径的“拐角”周围移动。这可能一开始看起来很复杂,但我认为可以按照以下方式处理:

  1. 使用STPointN()迭代遍历每个点,从x=1到x=STNumPoints()
  2. 使用STDistance()找出当前迭代中的点与下一个点之间的距离:@linestring.STPointN(x).STDistance(@linestring.STPointN(x+1))
  3. 如果上述距离大于您的输入距离'n':

    ...则目标点位于该点和下一个点之间。只需将点x作为起点、点x+1作为终点,距离n应用func_MoveTowardsPoint。返回结果并中断迭代。

    否则:

    ...目标点在迭代中下一个点的路径之后。从点x和点x+1之间的距离中减去您的距离'n'。使用修改后的距离继续进行迭代。

您可能已经注意到,我们可以很容易地实现上述递归而不是迭代。

让我们这样做:

CREATE FUNCTION [dbo].[func_MoveAlongPath](@path geography, 
                                           @distance int, 
                                           @index int = 1)   
RETURNS geography
AS
BEGIN
    DECLARE @result       geography = null;
    DECLARE @num_points   int = @path.STNumPoints();
    DECLARE @dist_to_next float;

    IF @index < @num_points
    BEGIN
        /* There is still at least one point further from the point @index
           in the linestring. Find the distance to the next point. */

        SET @dist_to_next = @path.STPointN(@index).STDistance(@path.STPointN(@index + 1));

        IF @distance <= @dist_to_next 
        BEGIN
            /* @dist_to_next is within this point and the next. Return
              the destination point with func_MoveTowardsPoint(). */

            SET @result = [dbo].[func_MoveTowardsPoint](@path.STPointN(@index),
                                                        @path.STPointN(@index + 1),
                                                        @distance);
        END
        ELSE
        BEGIN
            /* The destination is further from the next point. Subtract
               @dist_to_next from @distance and continue recursively. */

            SET @result = [dbo].[func_MoveAlongPath](@path, 
                                                     @distance - @dist_to_next,
                                                     @index + 1);
        END
    END
    ELSE
    BEGIN
        /* There is no further point. Our distance exceeds the length 
           of the linestring. Return the last point of the linestring.
           You may prefer to return NULL instead. */

        SET @result = @path.STPointN(@index);
    END

    RETURN @result;
END

现在有了这个设置,该进行一些测试了。让我们使用在问题中提供的原始线串,并请求在350m、3500m和7000m处的目标点:

DECLARE @g geography;
SET @g = geography::STGeomFromText('LINESTRING(-122.360 47.656, 
                                               -122.343 47.656, 
                                               -122.310 47.690)', 4326);

SELECT [dbo].[func_MoveAlongPath](@g, 350, DEFAULT).ToString();
SELECT [dbo].[func_MoveAlongPath](@g, 3500, DEFAULT).ToString();
SELECT [dbo].[func_MoveAlongPath](@g, 7000, DEFAULT).ToString();

我们的测试返回以下结果:

POINT (-122.3553270591861 47.6560002502638)
POINT (-122.32676470116748 47.672728464582583)
POINT (-122.31 47.69)

请注意,我们请求的最后一个距离(7000米)超过了线串的长度,因此我们被返回了最后一个点。在这种情况下,如果您愿意,可以轻松修改该函数以返回NULL。


请注意,在计算 @new_lon 时,我认为您想要在其中使用 sin(@new_lat) 而不是 sin(@lat_2) - Bloopy

4

在 CodePlex 上的 SQL Spatial Tools 库中还有 LocateAlongGeog 函数。详情请见此链接


1

我使用了以上Daniel的答案,但是我必须修正"func_MoveAlongPath"的签名为

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

使用int会返回错误的结果,因为它会在递归调用中舍入值。因此,我将其转换为迭代版本,因为递归版本无法处理我的示例数据中的较大距离:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接