正如您所指出的,您的方法将在以下情况下失败,其中 S 是起点,E 是终点,X 是您正在测试的点:
确定一个点是否位于 LINESTRING 上的其他两个点之间 http://img10.imageshack.us/img10/4937/gmap.png
使用该方法,点 X 将错误地导致位于点 S 和点 E 之间,因为它通过了算法的测试 1 和测试 2:即。点 X 在线串上,X 到 S 和 X 到 E 的距离都小于 S 到 E 的距离。
一种可能的解决方案
您可以将线串路径“分解”成单独的线段,每个线段只有两个点,这样:
LINESTRING(-122.360 47.656, -122.343 47.656, -122.310 47.690, -122.310 47.670)
会分解成:
LINESTRING(-122.360 47.656, -122.343 47.656)
LINESTRING(-122.343 47.656, -122.310 47.690)
LINESTRING(-122.310 47.690, -122.310 47.670)
然后,您将能够遍历上述每个线段并使用 测试该点是否位于这些线段之一上STIntersects
。当一个点通过此测试时,您将能够确定它是否在起点和终点之内。
如果可能的话,我建议将您的起点/终点存储为线串路径上某个点的索引,而不是原始地理点。首先,这将使解决这个问题变得更容易,但除此之外,您将消除数据的重复,这也保证了您不能有一个不属于线串的起点/终点。这样做的缺点是您不能在线段的中间设置起点/终点,但它们必须位于路径的拐角处。现在您必须确定此限制在您的应用程序中是否可以接受。
如果你选择上面的表示,我们可以用下面的递归函数来解决这个问题,其中@path
表示道路的线串是表示道路上@start_point
的@end_end
两个点的索引@path
(第一个索引是1),并且@test_point
是地理点进行测试。测试点可以位于谎言上的任何地方。
CREATE FUNCTION [dbo].[func_PointBetween](@path geography,
@start_point int,
@end_point int,
@test_point geography)
RETURNS tinyint
AS
BEGIN
DECLARE @result tinyint = 0;
DECLARE @num_points int = @path.STNumPoints();
DECLARE @line_segment geography;
IF (@start_point < @end_point) AND (@end_point < @num_points)
BEGIN
/* Generate the line segment from the current start point
to the following point (@start_point + 1). */
SET @line_segment = geography::STLineFromText('LINESTRING(' +
CAST(@path.STPointN(@start_point).Long AS varchar(32))+ ' ' +
CAST(@path.STPointN(@start_point).Lat AS varchar(32)) + ',' +
CAST(@path.STPointN(@start_point + 1).Long AS varchar(32))+ ' ' +
CAST(@path.STPointN(@start_point + 1).Lat AS varchar(32)) + ')',
4326);
/* Add a buffer of 25m to @test_point. This is optional, but
recommended, otherwise it will be very difficult to get a
point exactly on the line. The buffer value may be tweaked
as necessary for your application. */
IF @test_point.STBuffer(25).STIntersects(@line_segment) = 1
BEGIN
/* The test point is on one of the line segments between
@start_point and @end_point. Return 1 and stop the
recursion. */
SET @result = 1;
END
ELSE
BEGIN
/* The test point is not between the @start_point and
@start_point + 1. Increment @start_point by 1 and
continue recursively. */
SET @result = [dbo].[func_PointBetween](@path,
@start_point + 1,
@end_point,
@test_point);
END
END
ELSE
BEGIN
/* There are no further points. The test point is not between the
@start_point and @end_point. Return 0 and stop the recursion. */
SET @result = 0;
END
RETURN @result;
END
为了测试上述功能,我定义了上面地图中显示的 6 点线串。然后我们将定义两个测试点:@test_point_a
,它正好位于第三个和第四个点之间,以及@test_point_b
,它位于路径之外。
DECLARE @road geography;
DECLARE @test_point_a geography;
DECLARE @test_point_b geography;
SET @road = geography::STGeomFromText('LINESTRING(-122.360 47.656,
-122.343 47.656,
-122.310 47.690,
-122.310 47.670,
-122.300 47.670,
-122.290 47.660)',
4326);
/* This point lies between point 3 and point 4 */
SET @test_point_a = geography::STGeomFromText('POINT(-122.310 47.680)', 4326);
/* This point lies outside the path */
SET @test_point_b = geography::STGeomFromText('POINT(-122.310 47.700)', 4326);
/* This returns 1, because the test point is between start and end */
SELECT dbo.func_PointBetween(@road, 2, 5, @test_point_a);
/* This returns 0 because the test point is not between start and end */
SELECT dbo.func_PointBetween(@road, 4, 5, @test_point_a);
/* This returns 0 because the test point lies outside the path */
SELECT dbo.func_PointBetween(@road, 1, 6, @test_point_b);